| 1 | import { buildNewThreadLink, type LinkedPrompt } from "@t3tools/shared/promptLinks"; |
| 2 | import { Alert } from "react-native"; |
| 3 | |
| 4 | import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic"; |
| 5 | import { |
| 6 | getComposerDraftSnapshot, |
| 7 | setComposerDraftText, |
| 8 | waitForComposerDraftsLoaded, |
| 9 | } from "../../state/use-composer-drafts"; |
| 10 | import { linkedThreadDraftAction } from "./prompt-link-model"; |
| 11 | |
| 12 | /** Shows the one notice a prompt link owes the user, built with `promptLinkNotice`. */ |
| 13 | export function showPromptLinkNotice(notice: string): void { |
| 14 | Alert.alert("Prompt from a link", notice); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Puts a linked prompt into an existing thread's composer draft. An empty composer is filled; |
| 19 | * typed text is replaced only when the user says so. The notice rides in the same alert, so a |
| 20 | * link never stacks two dialogs. |
| 21 | */ |
| 22 | export async function applyLinkedPromptToThreadDraft( |
| 23 | draftKey: string, |
| 24 | prompt: LinkedPrompt, |
| 25 | notice: string, |
| 26 | ): Promise<void> { |
| 27 | try { |
| 28 | await waitForComposerDraftsLoaded(); |
| 29 | } catch { |
| 30 | Alert.alert("Could not open link", "Drafts could not be loaded, so the prompt was not added."); |
| 31 | return; |
| 32 | } |
| 33 | const action = linkedThreadDraftAction(getComposerDraftSnapshot(draftKey).text, prompt.text); |
| 34 | if (action !== "ask") { |
| 35 | if (action === "fill") setComposerDraftText(draftKey, prompt.text); |
| 36 | showPromptLinkNotice(notice); |
| 37 | return; |
| 38 | } |
| 39 | Alert.alert("Linked prompt ready. Replace draft?", notice, [ |
| 40 | { text: "Keep mine", style: "cancel" }, |
| 41 | { text: "Replace", onPress: () => setComposerDraftText(draftKey, prompt.text) }, |
| 42 | ]); |
| 43 | } |
| 44 | |
| 45 | /** Copies a shareable link that opens a new task with this prompt. Used by sent user messages. */ |
| 46 | export async function copyPromptLink(prompt: string): Promise<void> { |
| 47 | if (!(await tryCopyTextWithHaptic(buildNewThreadLink({ prompt }), { target: "prompt link" }))) { |
| 48 | Alert.alert("Could not copy", "Try again."); |
| 49 | } |
| 50 | } |