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 { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts"; |
| 3 | import { useState } from "react"; |
| 4 | |
| 5 | function toThreadRef( |
| 6 | environmentId: EnvironmentId | null, |
| 7 | threadId: ThreadId | null, |
| 8 | ): ScopedThreadRef | null { |
| 9 | return environmentId !== null && threadId !== null |
| 10 | ? scopeThreadRef(environmentId, threadId) |
| 11 | : null; |
| 12 | } |
| 13 | |
| 14 | /** Key by ids: streaming shell identity changes must not retrigger effects. */ |
| 15 | export function useActiveThreadRef( |
| 16 | thread: { readonly environmentId: EnvironmentId; readonly id: ThreadId } | null | undefined, |
| 17 | ): ScopedThreadRef | null { |
| 18 | const environmentId = thread?.environmentId ?? null; |
| 19 | const threadId = thread?.id ?? null; |
| 20 | const [ref, setRef] = useState(() => toThreadRef(environmentId, threadId)); |
| 21 | if ((ref?.environmentId ?? null) !== environmentId || (ref?.threadId ?? null) !== threadId) { |
| 22 | const next = toThreadRef(environmentId, threadId); |
| 23 | setRef(next); |
| 24 | return next; |
| 25 | } |
| 26 | return ref; |
| 27 | } |