The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { isLinkedThreadWorking, linkedThreadFailure } from "@t3tools/client-runtime/linked-thread"; |
| 2 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 3 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 4 | import { EyeIcon, PlugIcon } from "lucide-react"; |
| 5 | import { useState, type ReactNode } from "react"; |
| 6 | |
| 7 | import { useStableCallback } from "../../hooks/useStableCallback"; |
| 8 | import { type LinkedThreadSurface, linkedThreadTitle } from "../../linkedThreadSurface"; |
| 9 | import { useEnvironment } from "../../state/environments"; |
| 10 | import { |
| 11 | linkedThreadItemSource, |
| 12 | useConnectLinkedEnvironment, |
| 13 | useLinkedThreadView, |
| 14 | } from "../../state/linkedThreadView"; |
| 15 | import { Alert, AlertAction, AlertDescription, AlertTitle } from "../ui/alert"; |
| 16 | import { Button } from "../ui/button"; |
| 17 | import { ComposerBannerStack, type ComposerBannerStackItem } from "./ComposerBannerStack"; |
| 18 | import { ComposerSurface } from "./ComposerSurface"; |
| 19 | import { |
| 20 | ReadOnlyComposerBar, |
| 21 | ReadOnlyComposerLead, |
| 22 | ReadOnlyComposerStatus, |
| 23 | } from "./ReadOnlyComposerBar"; |
| 24 | import { Spinner } from "../ui/spinner"; |
| 25 | import { toastManager } from "../ui/toast"; |
| 26 | import type { MessagesTimelineHistoryControls } from "./MessagesTimeline"; |
| 27 | import { ReadOnlyThreadTimeline } from "./ReadOnlyThreadTimeline"; |
| 28 | |
| 29 | /** |
| 30 | * Stands where a composer would: the thread takes no messages from here. `action` is the one way |
| 31 | * forward it offers, and `notice` what came of it. |
| 32 | */ |
| 33 | function LinkedThreadComposer(props: { |
| 34 | /** The linked environment the thread is on. */ |
| 35 | readonly label: string; |
| 36 | /** The environment that reads it. */ |
| 37 | readonly via: string; |
| 38 | readonly working: boolean; |
| 39 | readonly action?: ReactNode; |
| 40 | readonly notice?: ComposerBannerStackItem | null; |
| 41 | }) { |
| 42 | const { label, via } = props; |
| 43 | const gt = useGT(); |
| 44 | return ( |
| 45 | <div className="chat-composer-lane flex shrink-0 flex-col gap-1 pt-1.5 pb-4 sm:pt-2 sm:pb-5"> |
| 46 | {props.notice ? <ComposerBannerStack items={[props.notice]} detached /> : null} |
| 47 | <ComposerSurface.Shell> |
| 48 | <ComposerSurface.Host> |
| 49 | <div className="relative z-10"> |
| 50 | <ReadOnlyComposerBar actions={props.action}> |
| 51 | <ReadOnlyComposerLead> |
| 52 | <EyeIcon aria-hidden className="size-4 shrink-0 text-muted-foreground" /> |
| 53 | <span className="min-w-0 truncate font-medium text-foreground"> |
| 54 | <T>Read-only</T> |
| 55 | </span> |
| 56 | </ReadOnlyComposerLead> |
| 57 | <ReadOnlyComposerStatus role="status"> |
| 58 | {props.working |
| 59 | ? gt("On {label} via {via} · Working", { label, via }) |
| 60 | : gt("On {label} via {via}", { label, via })} |
| 61 | </ReadOnlyComposerStatus> |
| 62 | </ReadOnlyComposerBar> |
| 63 | </div> |
| 64 | </ComposerSurface.Host> |
| 65 | </ComposerSurface.Shell> |
| 66 | </div> |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | const NO_ATTACHMENT_URLS: ReadonlyMap<string, string> = new Map(); |
| 71 | const noAttachmentUrls = () => NO_ATTACHMENT_URLS; |
| 72 | const ignore = () => {}; |
| 73 | |
| 74 | /** |
| 75 | * A linked environment's thread, streamed through the environment this client is connected to |
| 76 | * and drawn by the timeline every thread uses. The client never connects to the linked one, so |
| 77 | * the view only reads: what would need that connection is absent. Key it by its surface id. |
| 78 | */ |
| 79 | export function LinkedThread(props: { |
| 80 | /** The thread whose panel shows it as a tab, or null on the main screen. */ |
| 81 | readonly panelRef: ScopedThreadRef | null; |
| 82 | readonly surface: LinkedThreadSurface; |
| 83 | /** Whether it is showing; the stream runs only while it is. */ |
| 84 | readonly visible: boolean; |
| 85 | /** What sits above the timeline on the main screen; a tab already names the thread. */ |
| 86 | readonly renderHeader?: (title: string) => ReactNode; |
| 87 | }) { |
| 88 | const { surface, panelRef } = props; |
| 89 | const gt = useGT(); |
| 90 | const m = useMessages(); |
| 91 | const via = useEnvironment(surface.viaEnvironmentId)?.label ?? gt("this environment"); |
| 92 | const { view, error, retry } = useLinkedThreadView(panelRef, surface, props.visible); |
| 93 | const [turnItemSource] = useState(() => linkedThreadItemSource(surface)); |
| 94 | const label = surface.label; |
| 95 | const projection = view?.thread.projection ?? null; |
| 96 | const connection = useConnectLinkedEnvironment(panelRef, surface); |
| 97 | const connectState = connection.state; |
| 98 | const connecting = connectState.phase === "connecting"; |
| 99 | const connect = async () => { |
| 100 | const outcome = await connection.connect(); |
| 101 | if (outcome.type === "connected") { |
| 102 | toastManager.add({ |
| 103 | type: "success", |
| 104 | title: gt("Connected to {label}", { label }), |
| 105 | description: gt("This device is paired with it and can use its threads."), |
| 106 | }); |
| 107 | } |
| 108 | }; |
| 109 | const connectNotice: ComposerBannerStackItem | null = |
| 110 | connectState.phase === "connecting" |
| 111 | ? { |
| 112 | id: "connecting", |
| 113 | variant: "info", |
| 114 | icon: <Spinner size="sm" />, |
| 115 | title: gt("Connecting to {label}…", { label }), |
| 116 | } |
| 117 | : connectState.phase === "unreachable" |
| 118 | ? { |
| 119 | id: "unreachable", |
| 120 | variant: "warning", |
| 121 | icon: <PlugIcon />, |
| 122 | title: gt("{label} is not reachable from this device", { label: connectState.label }), |
| 123 | description: gt( |
| 124 | "Join a network it is on, such as its Tailscale network, and try again. You can keep reading here.", |
| 125 | ), |
| 126 | } |
| 127 | : connectState.phase === "manual" |
| 128 | ? { |
| 129 | id: "manual", |
| 130 | variant: "info", |
| 131 | icon: <PlugIcon />, |
| 132 | title: gt("{label} needs a pairing code", { label: connectState.prefill.label }), |
| 133 | description: gt("Create one on it, then add it to this device as an environment."), |
| 134 | actions: ( |
| 135 | <Button |
| 136 | size="xs" |
| 137 | variant="ghost" |
| 138 | onClick={() => connection.addWithCode(connectState.prefill)} |
| 139 | > |
| 140 | <T>Enter code</T> |
| 141 | </Button> |
| 142 | ), |
| 143 | } |
| 144 | : null; |
| 145 | const connectAction = ( |
| 146 | <Button size="sm" variant="outline" disabled={connecting} onClick={() => void connect()}> |
| 147 | <PlugIcon aria-hidden /> |
| 148 | <span className="@max-[320px]/composer-surface:sr-only"> |
| 149 | <T>Connect to interact</T> |
| 150 | </span> |
| 151 | </Button> |
| 152 | ); |
| 153 | const failure = error === null ? null : linkedThreadFailure(error, { via, label }, m); |
| 154 | const retryAction = failure?.retryable ? ( |
| 155 | <AlertAction> |
| 156 | <Button size="sm" variant="outline" onClick={retry}> |
| 157 | <T>Try again</T> |
| 158 | </Button> |
| 159 | </AlertAction> |
| 160 | ) : null; |
| 161 | // A file or visual lives on the linked environment, which this client has no way to ask. |
| 162 | const renderHtmlRender = useStableCallback(() => ( |
| 163 | <p className="chat-content-lane text-xs text-muted-foreground"> |
| 164 | {gt("Open this thread on {label} to see this visual.", { label })} |
| 165 | </p> |
| 166 | )); |
| 167 | const working = projection !== null && isLinkedThreadWorking(projection); |
| 168 | const historyControls: MessagesTimelineHistoryControls | undefined = |
| 169 | view === null || view.thread.historyCursor === null |
| 170 | ? undefined |
| 171 | : { |
| 172 | hasMoreHistory: true, |
| 173 | loading: view.history.loading, |
| 174 | error: view.history.failed ? gt("Could not load earlier turns. Try again.") : null, |
| 175 | onLoadEarlier: view.loadEarlier, |
| 176 | }; |
| 177 | return ( |
| 178 | // `data-chat-canvas` gives the timeline the side gutters it has in a thread's own chat. |
| 179 | <div |
| 180 | data-chat-canvas |
| 181 | className="flex h-full min-h-0 flex-col [--chat-lane-inset-end:0px] [--chat-lane-inset-start:0px] [--chat-timeline-gutter:0px]" |
| 182 | > |
| 183 | {props.renderHeader?.( |
| 184 | linkedThreadTitle( |
| 185 | { title: projection?.thread.title || surface.title }, |
| 186 | gt("Thread on {label}", { label }), |
| 187 | ), |
| 188 | )} |
| 189 | {projection === null ? ( |
| 190 | <> |
| 191 | <div className="chat-composer-lane min-h-0 flex-1 overflow-y-auto py-3"> |
| 192 | <div className="chat-content-lane"> |
| 193 | {failure === null ? ( |
| 194 | <div className="flex items-center gap-2 text-sm text-muted-foreground"> |
| 195 | <Spinner size="sm" /> |
| 196 | <T> |
| 197 | Reading from <Var>{label}</Var>… |
| 198 | </T> |
| 199 | </div> |
| 200 | ) : ( |
| 201 | <Alert variant="error"> |
| 202 | <AlertTitle>{failure.title}</AlertTitle> |
| 203 | <AlertDescription>{failure.detail}</AlertDescription> |
| 204 | {retryAction} |
| 205 | </Alert> |
| 206 | )} |
| 207 | </div> |
| 208 | </div> |
| 209 | <LinkedThreadComposer |
| 210 | label={label} |
| 211 | via={via} |
| 212 | working={false} |
| 213 | notice={connectNotice} |
| 214 | action={connectAction} |
| 215 | /> |
| 216 | </> |
| 217 | ) : ( |
| 218 | <ReadOnlyThreadTimeline |
| 219 | // No ":" so the timeline finds no environment thread behind the key. |
| 220 | threadKey={`linked-${encodeURIComponent(surface.id)}`} |
| 221 | projection={projection} |
| 222 | historyControls={historyControls} |
| 223 | turnItemSource={turnItemSource} |
| 224 | attachmentUrls={noAttachmentUrls} |
| 225 | onImageExpand={ignore} |
| 226 | onFileOpen={ignore} |
| 227 | renderHtmlRender={renderHtmlRender} |
| 228 | > |
| 229 | {() => ( |
| 230 | <> |
| 231 | {failure === null ? null : ( |
| 232 | <div className="chat-composer-lane shrink-0 pb-2"> |
| 233 | <div className="chat-content-lane"> |
| 234 | <Alert variant="warning"> |
| 235 | <AlertTitle>{failure.title}</AlertTitle> |
| 236 | <AlertDescription> |
| 237 | {failure.detail} <T>Showing what was read before.</T> |
| 238 | </AlertDescription> |
| 239 | {retryAction} |
| 240 | </Alert> |
| 241 | </div> |
| 242 | </div> |
| 243 | )} |
| 244 | <LinkedThreadComposer |
| 245 | label={label} |
| 246 | via={via} |
| 247 | working={working} |
| 248 | notice={connectNotice} |
| 249 | action={connectAction} |
| 250 | /> |
| 251 | </> |
| 252 | )} |
| 253 | </ReadOnlyThreadTimeline> |
| 254 | )} |
| 255 | </div> |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | /** A linked environment's thread as a right panel tab. Key it by its surface id. */ |
| 260 | export function LinkedThreadPane(props: { |
| 261 | /** The thread whose panel shows the tab. */ |
| 262 | readonly panelRef: ScopedThreadRef; |
| 263 | readonly surface: LinkedThreadSurface; |
| 264 | /** Whether the tab is showing. */ |
| 265 | readonly visible: boolean; |
| 266 | }) { |
| 267 | return <LinkedThread panelRef={props.panelRef} surface={props.surface} visible={props.visible} />; |
| 268 | } |