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 { useLocale, useMessages } from "gt-react"; |
| 2 | import { useState } from "react"; |
| 3 | import { |
| 4 | computeStableMessagesTimelineRows, |
| 5 | handoffRunsSignature, |
| 6 | type MessagesTimelineRowsInput, |
| 7 | sameRowsInput, |
| 8 | deriveMessagesTimelineRowsWithState, |
| 9 | type StableMessagesTimelineRowsState, |
| 10 | type MessagesTimelineRow, |
| 11 | } from "./MessagesTimeline.logic"; |
| 12 | import { type HandoffTimelineRun } from "./V2LifecycleRow"; |
| 13 | |
| 14 | const EMPTY_TIMELINE_RUNS: ReadonlyArray<HandoffTimelineRun> = []; |
| 15 | |
| 16 | export function useStableHandoffRuns( |
| 17 | runs: ReadonlyArray<HandoffTimelineRun>, |
| 18 | ): ReadonlyArray<HandoffTimelineRun> { |
| 19 | const [state, setState] = useState<{ |
| 20 | readonly source: ReadonlyArray<HandoffTimelineRun> | null; |
| 21 | readonly signature: string; |
| 22 | readonly value: ReadonlyArray<HandoffTimelineRun>; |
| 23 | }>({ source: null, signature: "", value: EMPTY_TIMELINE_RUNS }); |
| 24 | if (state.source === runs) return state.value; |
| 25 | const signature = handoffRunsSignature(runs); |
| 26 | const value = |
| 27 | signature === state.signature |
| 28 | ? state.value |
| 29 | : runs.map((run) => ({ |
| 30 | id: run.id, |
| 31 | ordinal: run.ordinal, |
| 32 | providerInstanceId: run.providerInstanceId, |
| 33 | modelSelection: run.modelSelection, |
| 34 | })); |
| 35 | setState({ source: runs, signature, value }); |
| 36 | return value; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Rows derived from the previous projection of the same list, so a streamed |
| 41 | * text update patches its message rows instead of deriving every row again. |
| 42 | */ |
| 43 | export function useIncrementalTimelineRows( |
| 44 | identity: string, |
| 45 | input: MessagesTimelineRowsInput, |
| 46 | ): MessagesTimelineRow[] { |
| 47 | // Keyed on the locale: without a GTProvider, useMessages() returns a new function every render. |
| 48 | const locale = useLocale(); |
| 49 | const resolve = useMessages(); |
| 50 | const [state, setState] = useState(() => ({ |
| 51 | identity, |
| 52 | input, |
| 53 | locale, |
| 54 | projection: deriveMessagesTimelineRowsWithState(input, null, resolve), |
| 55 | })); |
| 56 | const reusable = state.identity === identity && state.locale === locale; |
| 57 | if (reusable && sameRowsInput(state.input, input)) { |
| 58 | return state.projection.rows; |
| 59 | } |
| 60 | const projection = deriveMessagesTimelineRowsWithState( |
| 61 | input, |
| 62 | reusable ? state.projection : null, |
| 63 | resolve, |
| 64 | ); |
| 65 | setState({ identity, input, locale, projection }); |
| 66 | return projection.rows; |
| 67 | } |
| 68 | |
| 69 | export function useStableRows( |
| 70 | rows: MessagesTimelineRow[], |
| 71 | identity: string, |
| 72 | ): MessagesTimelineRow[] { |
| 73 | const [state, setState] = useState<{ |
| 74 | readonly identity: string; |
| 75 | readonly source: MessagesTimelineRow[] | null; |
| 76 | readonly stable: StableMessagesTimelineRowsState; |
| 77 | }>({ identity, source: null, stable: { byId: new Map(), result: [] } }); |
| 78 | if (state.identity === identity && state.source === rows) return state.stable.result; |
| 79 | const stable = computeStableMessagesTimelineRows( |
| 80 | rows, |
| 81 | state.identity === identity ? state.stable : { byId: new Map(), result: [] }, |
| 82 | ); |
| 83 | setState({ identity, source: rows, stable }); |
| 84 | return stable.result; |
| 85 | } |