The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { useNavigate } from "@tanstack/react-router"; |
| 3 | import { useGT, useMessages } from "gt-react"; |
| 4 | import { MessagesSquareIcon } from "lucide-react"; |
| 5 | import { |
| 6 | formatHostedThreadLink, |
| 7 | guessThreadLinkRef, |
| 8 | threadLinkCandidates, |
| 9 | type ThreadLink, |
| 10 | } from "@t3tools/shared/threadLinks"; |
| 11 | import { unknownThreadLinkLabel } from "@t3tools/shared/threadLinksCopy"; |
| 12 | import { resolveLinkedThreadOpen } from "@t3tools/client-runtime/linked-thread"; |
| 13 | |
| 14 | import { configuredHostedAppUrl } from "../../hostedPairing"; |
| 15 | import { useThreadRefs, useThreadShell } from "../../state/entities"; |
| 16 | import { useConnectedEnvironmentIds, useEnvironmentIdentities } from "../../state/environments"; |
| 17 | import { useSideThreadTrigger } from "../../hooks/useSideThreadTrigger"; |
| 18 | import { openLinkedThreadRead } from "../../lib/sideChatPane"; |
| 19 | import { useLinkedEnvironments } from "../../state/peerLinks"; |
| 20 | import { orchestrationEnvironment } from "../../state/orchestration"; |
| 21 | import { useEnvironmentQuery } from "../../state/query"; |
| 22 | import { ContextChip, ContextChipLabel } from "../ContextChip"; |
| 23 | import { UnresolvedChip } from "../contextChipParts"; |
| 24 | import { ThreadContextChip } from "../ThreadContextChip"; |
| 25 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 26 | |
| 27 | /** `label` is the link's written text; an autolink has none. */ |
| 28 | export function MarkdownThreadLink(props: { |
| 29 | readonly environmentId: EnvironmentId; |
| 30 | /** The thread the link is written in; a linked environment's thread can open in its right panel. */ |
| 31 | readonly threadRef?: ScopedThreadRef | undefined; |
| 32 | readonly link: ThreadLink; |
| 33 | readonly label: string | null; |
| 34 | }) { |
| 35 | const gt = useGT(); |
| 36 | const messages = useMessages(); |
| 37 | const navigate = useNavigate(); |
| 38 | const trigger = useSideThreadTrigger(); |
| 39 | const panelRef = props.threadRef ?? null; |
| 40 | const candidates = threadLinkCandidates(props.link, props.environmentId); |
| 41 | const writtenRef = candidates[0]!; |
| 42 | const written = useThreadShell(writtenRef); |
| 43 | const saved = useEnvironmentIdentities().some( |
| 44 | (environment) => environment.environmentId === writtenRef.environmentId, |
| 45 | ); |
| 46 | const connected = useConnectedEnvironmentIds().includes(writtenRef.environmentId); |
| 47 | // A thread on a machine this client is not connected to still reads through the one that links to it. |
| 48 | const linkedLabel = useLinkedEnvironments( |
| 49 | connected || writtenRef.environmentId === props.environmentId ? null : props.environmentId, |
| 50 | ).get(writtenRef.environmentId); |
| 51 | // A shell cached from an earlier connection does not make the thread open there now. |
| 52 | const linked = |
| 53 | resolveLinkedThreadOpen({ |
| 54 | environmentId: writtenRef.environmentId, |
| 55 | viaEnvironmentId: props.environmentId, |
| 56 | own: saved ? { connected } : undefined, |
| 57 | linked: linkedLabel !== undefined, |
| 58 | }) === "linked"; |
| 59 | const reachable = saved && !linked; |
| 60 | const lookup = useEnvironmentQuery( |
| 61 | written === null && reachable |
| 62 | ? orchestrationEnvironment.threadLinkMetadata({ |
| 63 | environmentId: writtenRef.environmentId, |
| 64 | input: { threadIds: candidates.map((ref) => ref.threadId) }, |
| 65 | }) |
| 66 | : null, |
| 67 | ); |
| 68 | const settled = lookup.isSuccess && !lookup.isPending; |
| 69 | const target = |
| 70 | written !== null |
| 71 | ? { ref: writtenRef, metadata: written } |
| 72 | : (candidates.flatMap((ref) => { |
| 73 | const metadata = lookup.data?.[ref.threadId]; |
| 74 | return metadata ? [{ ref, metadata }] : []; |
| 75 | })[0] ?? null); |
| 76 | const missing = target === null && (settled || !reachable); |
| 77 | const guess = missing && props.link.environmentId === null; |
| 78 | const refs = useThreadRefs(guess); |
| 79 | const remoteRef = guess |
| 80 | ? guessThreadLinkRef( |
| 81 | props.link, |
| 82 | refs.filter((ref) => ref.environmentId !== writtenRef.environmentId), |
| 83 | ) |
| 84 | : null; |
| 85 | const remote = useThreadShell(remoteRef); |
| 86 | const resolved = target ?? (remoteRef && remote ? { ref: remoteRef, metadata: remote } : null); |
| 87 | if (linked && linkedLabel !== undefined) { |
| 88 | const knownTitle = props.label ?? written?.title ?? null; |
| 89 | const title = knownTitle ?? gt("Thread on {label}", { label: linkedLabel }); |
| 90 | return ( |
| 91 | <Tooltip> |
| 92 | <TooltipTrigger |
| 93 | render={ |
| 94 | <ContextChip |
| 95 | kind="thread" |
| 96 | render={ |
| 97 | <button |
| 98 | type="button" |
| 99 | {...trigger( |
| 100 | (intent) => |
| 101 | openLinkedThreadRead({ |
| 102 | panelRef, |
| 103 | target: { |
| 104 | viaEnvironmentId: props.environmentId, |
| 105 | ...writtenRef, |
| 106 | label: linkedLabel, |
| 107 | title: knownTitle, |
| 108 | }, |
| 109 | intent, |
| 110 | navigate, |
| 111 | }), |
| 112 | // Outside a thread there is no panel to read it in. |
| 113 | { panel: panelRef !== null }, |
| 114 | )} |
| 115 | /> |
| 116 | } |
| 117 | aria-label={gt("Thread, {title}", { title })} |
| 118 | > |
| 119 | <MessagesSquareIcon /> |
| 120 | <ContextChipLabel>{title}</ContextChipLabel> |
| 121 | </ContextChip> |
| 122 | } |
| 123 | /> |
| 124 | <TooltipPopup side="top"> |
| 125 | {gt("Read the thread on {label}", { label: linkedLabel })} |
| 126 | </TooltipPopup> |
| 127 | </Tooltip> |
| 128 | ); |
| 129 | } |
| 130 | if (missing && resolved === null) { |
| 131 | const unknown = unknownThreadLinkLabel(props.link, props.environmentId, messages); |
| 132 | return <UnresolvedChip label={props.label ?? unknown} tooltip={unknown} />; |
| 133 | } |
| 134 | const ref = resolved?.ref ?? writtenRef; |
| 135 | const title = resolved?.metadata.title.trim() || props.label || gt("Thread"); |
| 136 | return ( |
| 137 | <ThreadContextChip |
| 138 | record={{ ...ref, title }} |
| 139 | projectId={resolved?.metadata.projectId ?? null} |
| 140 | copyMarkdown={formatHostedThreadLink(ref, title, configuredHostedAppUrl())} |
| 141 | /> |
| 142 | ); |
| 143 | } |