The best way to control Claude Code, Codex and more from any computer or phone

main
ts 44 lines 1.58 KB
Raw
1 import { useLocale, useMessages } from "gt-react";
2 import { useState } from "react";
3 import {
4 deriveTimelineEntriesFromVisibleTurnItemsWithState,
5 type TimelineEntriesInput,
6 type TimelineEntriesProjection,
7 } from "../session-logic";
8
9 function sameInput(left: TimelineEntriesInput, right: TimelineEntriesInput): boolean {
10 return (
11 left.visibleTurnItems === right.visibleTurnItems &&
12 left.optimisticMessages === right.optimisticMessages &&
13 left.anchoredMessages === right.anchoredMessages &&
14 left.attachmentUrlById === right.attachmentUrlById &&
15 left.attempts === right.attempts &&
16 left.nodes === right.nodes &&
17 left.plans === right.plans
18 );
19 }
20
21 export function useIncrementalTimelineEntries(
22 threadKey: string | null,
23 timelineInput: TimelineEntriesInput,
24 ): TimelineEntriesProjection["entries"] {
25 // Keyed on the locale: without a GTProvider, useMessages() returns a new function every render.
26 const locale = useLocale();
27 const resolve = useMessages();
28 const input = { ...timelineInput, resolve: timelineInput.resolve ?? resolve };
29 const [state, setState] = useState(() => ({
30 threadKey,
31 locale,
32 projection: deriveTimelineEntriesFromVisibleTurnItemsWithState(input),
33 }));
34 const reusable = state.threadKey === threadKey && state.locale === locale;
35 if (reusable && sameInput(state.projection.input, input)) {
36 return state.projection.entries;
37 }
38 const projection = deriveTimelineEntriesFromVisibleTurnItemsWithState(
39 input,
40 reusable ? state.projection : null,
41 );
42 setState({ threadKey, locale, projection });
43 return projection.entries;
44 }