Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

diffPanelStore.ts

191 lines7.0 KB

1import type { ThreadStructureVisibleTurnItem } from "@t3tools/client-runtime/state/shell";
2import { turnItemDetailRevision } from "@t3tools/client-runtime/work-log/item-detail";
3import { scopedThreadKey } from "@t3tools/client-runtime/environment";
4import type { RunId, ScopedThreadRef, ThreadId, TurnItemId } from "@t3tools/contracts";
5import { create } from "zustand";
6import { createJSONStorage, persist } from "zustand/middleware";
7​
8import { resolveStorage } from "./lib/storage";
9​
10export interface ToolCallDiffTarget {
11 readonly threadId: ThreadId;
12 readonly itemId: TurnItemId;
13 readonly revision: string;
15​
16export type DiffPanelSelection =
17 | ({ kind: "tool-call" } & ToolCallDiffTarget)
18 | { kind: "branch"; baseRef: string | null }
19 | { kind: "unstaged" }
20 | { kind: "turn"; turnId: RunId; filePath: string | null; revealRequestId: number };
21​
22const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
23​
24interface DiffPanelStoreState {
25 byThreadKey: Record<string, DiffPanelSelection>;
26 branchBaseRefByThreadKey: Record<string, string | null>;
27 selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
28 selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
29 selectToolCall: (ref: ScopedThreadRef, target: ToolCallDiffTarget) => void;
30 selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void;
31 reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
32 removeThread: (ref: ScopedThreadRef) => void;
34​
35function normalizeBaseRef(baseRef: string | null): string | null {
36 const normalized = baseRef?.trim();
37 return normalized ? normalized : null;
39​
40export const useDiffPanelStore = create<DiffPanelStoreState>()(
41 persist(
42 (set) => ({
43 byThreadKey: {},
44 branchBaseRefByThreadKey: {},
45 selectGitScope: (ref, scope) =>
46 set((state) => {
47 const threadKey = scopedThreadKey(ref);
48 const previous = state.byThreadKey[threadKey];
49 const previousBaseRef =
50 previous?.kind === "branch"
51 ? previous.baseRef
52 : (state.branchBaseRefByThreadKey[threadKey] ?? null);
53 return {
54 byThreadKey: {
55 ...state.byThreadKey,
56 [threadKey]:
57 scope === "branch"
58 ? { kind: "branch", baseRef: previousBaseRef }
59 : { kind: "unstaged" },
60 },
61 branchBaseRefByThreadKey:
62 previous?.kind === "branch"
63 ? { ...state.branchBaseRefByThreadKey, [threadKey]: previous.baseRef }
64 : state.branchBaseRefByThreadKey,
65 };
66 }),
67 selectBranchBaseRef: (ref, baseRef) =>
68 set((state) => {
69 const threadKey = scopedThreadKey(ref);
70 const normalizedBaseRef = normalizeBaseRef(baseRef);
71 return {
72 byThreadKey: {
73 ...state.byThreadKey,
74 [threadKey]: { kind: "branch", baseRef: normalizedBaseRef },
75 },
76 branchBaseRefByThreadKey: {
77 ...state.branchBaseRefByThreadKey,
78 [threadKey]: normalizedBaseRef,
79 },
80 };
81 }),
82 selectToolCall: (ref, target) =>
83 set((state) => ({
84 byThreadKey: {
85 ...state.byThreadKey,
86 [scopedThreadKey(ref)]: { kind: "tool-call", ...target },
87 },
88 })),
89 selectTurn: (ref, turnId, filePath) =>
90 set((state) => {
91 const threadKey = scopedThreadKey(ref);
92 const previous = state.byThreadKey[threadKey];
93 return {
94 byThreadKey: {
95 ...state.byThreadKey,
96 [threadKey]: {
97 kind: "turn",
98 turnId,
99 filePath: filePath?.trim() || null,
100 revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1,
101 },
102 },
103 };
104 }),
105 reconcileTurnSelection: (ref, availableTurnIds) =>
106 set((state) => {
107 const threadKey = scopedThreadKey(ref);
108 const previous = state.byThreadKey[threadKey];
109 const latestTurnId = availableTurnIds[0];
110 if (
111 previous?.kind !== "turn" ||
112 latestTurnId === undefined ||
113 availableTurnIds.includes(previous.turnId)
114 ) {
115 return state;
116 }
117 return {
118 byThreadKey: {
119 ...state.byThreadKey,
120 [threadKey]: { ...previous, turnId: latestTurnId },
121 },
122 };
123 }),
124 removeThread: (ref) =>
125 set((state) => {
126 const threadKey = scopedThreadKey(ref);
127 if (!(threadKey in state.byThreadKey) && !(threadKey in state.branchBaseRefByThreadKey)) {
128 return state;
129 }
130 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
131 const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } =
132 state.branchBaseRefByThreadKey;
133 return { byThreadKey, branchBaseRefByThreadKey };
134 }),
135 }),
136 {
137 name: "t3code:diff-panel-state:v1",
138 version: 2,
139 storage: createJSONStorage(() =>
140 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
141 ),
142 partialize: (state) => ({
143 byThreadKey: state.byThreadKey,
144 branchBaseRefByThreadKey: state.branchBaseRefByThreadKey,
145 }),
146 },
147 ),
148);
149​
150interface DiffPanelSessionStoreState {
151 collapsedFileKeysByScopeKey: Record<string, ReadonlySet<string>>;
152 updateCollapsedFileKeys: (
153 scopeKey: string,
154 defaults: ReadonlySet<string>,
155 update: (current: ReadonlySet<string>) => ReadonlySet<string>,
156 ) => void;
158​
159// Session-only so folds survive right-panel switches without changing reload defaults.
160export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((set) => ({
161 collapsedFileKeysByScopeKey: {},
162 updateCollapsedFileKeys: (scopeKey, defaults, update) =>
163 set((state) => {
164 const current = state.collapsedFileKeysByScopeKey[scopeKey] ?? defaults;
165 const next = update(current);
166 if (next === current) return state;
167 return {
168 collapsedFileKeysByScopeKey: { ...state.collapsedFileKeysByScopeKey, [scopeKey]: next },
169 };
170 }),
171}));
172​
173export function selectThreadDiffPanelSelection(
174 byThreadKey: Record<string, DiffPanelSelection>,
175 ref: ScopedThreadRef | null | undefined,
176): DiffPanelSelection {
177 if (!ref) return DEFAULT_SELECTION;
178 return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION;
180​
181export function resolveToolCallDiffTarget(
182 target: ToolCallDiffTarget,
183 items: ReadonlyArray<ThreadStructureVisibleTurnItem>,
184): ToolCallDiffTarget {
185 const row = items.find(
186 (row) => row.sourceThreadId === target.threadId && row.sourceItemId === target.itemId,
187 );
188 if (row?.item.type !== "file_change") return target;
189 const revision = turnItemDetailRevision(row.item);
190 return revision === target.revision ? target : { ...target, revision };