The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { withoutPromptLinkParams } from "@t3tools/shared/promptLinks"; |
| 3 | import { parseThreadLinkPrompt } from "@t3tools/shared/promptLinksCopy"; |
| 4 | import { useLocation, useNavigate } from "@tanstack/react-router"; |
| 5 | import { msg } from "gt-react"; |
| 6 | import { useEffect } from "react"; |
| 7 | |
| 8 | import { m } from "../../i18n/messages"; |
| 9 | import { isThreadRoutePathname } from "../../threadRoutes"; |
| 10 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 11 | import { placeLinkedPrompt } from "./linkedPrompt"; |
| 12 | |
| 13 | const handledLinkKeys = new Set<string>(); |
| 14 | |
| 15 | export function ThreadLinkPrompt({ threadRef }: { readonly threadRef: ScopedThreadRef }) { |
| 16 | const navigate = useNavigate(); |
| 17 | const location = useLocation({ |
| 18 | select: (current) => ({ |
| 19 | key: current.state.__TSR_key ?? current.href, |
| 20 | pathname: current.pathname, |
| 21 | search: current.searchStr, |
| 22 | }), |
| 23 | structuralSharing: true, |
| 24 | }); |
| 25 | |
| 26 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- places a ?prompt link into the composer once per location and strips it from the URL via the router |
| 27 | useEffect(() => { |
| 28 | // A pending navigation shows the next location while the params still name this thread. |
| 29 | if (!isThreadRoutePathname(location.pathname, threadRef)) return; |
| 30 | if (!new URLSearchParams(location.search).has("prompt")) return; |
| 31 | if (!handledLinkKeys.has(location.key)) { |
| 32 | handledLinkKeys.add(location.key); |
| 33 | const parsed = parseThreadLinkPrompt(location.search, m); |
| 34 | if (!parsed.ok) { |
| 35 | toastManager.add( |
| 36 | stackedThreadToast({ |
| 37 | type: "error", |
| 38 | title: m(msg("Could not open the linked prompt")), |
| 39 | description: parsed.error, |
| 40 | }), |
| 41 | ); |
| 42 | } else if (parsed.value !== null) { |
| 43 | placeLinkedPrompt(threadRef, parsed.value); |
| 44 | } |
| 45 | } |
| 46 | void navigate({ |
| 47 | href: `${location.pathname}${withoutPromptLinkParams(location.search)}`, |
| 48 | replace: true, |
| 49 | }); |
| 50 | }, [location, navigate, threadRef]); |
| 51 | |
| 52 | return null; |
| 53 | } |