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

diffPanelStore.ts

231 lines8.7 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: "commit"; sha: string; baseRef: string | null }
21 | { kind: "turn"; turnId: RunId; filePath: string | null; revealRequestId: number };
22​
23const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
24​
25interface DiffPanelStoreState {
26 byThreadKey: Record<string, DiffPanelSelection>;
27 branchBaseRefByThreadKey: Record<string, string | null>;
28 /** A link that selected a commit before opening the closed pane; opening keeps that selection. */
29 keepSelectionOnOpenThreadKey: string | null;
30 selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
31 selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
32 selectToolCall: (ref: ScopedThreadRef, target: ToolCallDiffTarget) => void;
33 selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void;
34 selectCommit: (ref: ScopedThreadRef, sha: string, keepOnOpen?: boolean) => void;
35 consumeKeepSelectionOnOpen: (ref: ScopedThreadRef) => boolean;
36 reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
37 removeThread: (ref: ScopedThreadRef) => void;
39​
40function normalizeBaseRef(baseRef: string | null): string | null {
41 const normalized = baseRef?.trim();
42 return normalized ? normalized : null;
44​
45function branchBaseRef(
46 state: Pick<DiffPanelStoreState, "byThreadKey" | "branchBaseRefByThreadKey">,
47 threadKey: string,
48): string | null {
49 const previous = state.byThreadKey[threadKey];
50 return previous?.kind === "branch" || previous?.kind === "commit"
51 ? previous.baseRef
52 : (state.branchBaseRefByThreadKey[threadKey] ?? null);
54​
55export const useDiffPanelStore = create<DiffPanelStoreState>()(
56 persist(
57 (set, get) => ({
58 byThreadKey: {},
59 branchBaseRefByThreadKey: {},
60 keepSelectionOnOpenThreadKey: null,
61 selectGitScope: (ref, scope) =>
62 set((state) => {
63 const threadKey = scopedThreadKey(ref);
64 const previous = state.byThreadKey[threadKey];
65 const previousBaseRef = branchBaseRef(state, threadKey);
66 return {
67 byThreadKey: {
68 ...state.byThreadKey,
69 [threadKey]:
70 scope === "branch"
71 ? { kind: "branch", baseRef: previousBaseRef }
72 : { kind: "unstaged" },
73 },
74 branchBaseRefByThreadKey:
75 previous?.kind === "branch" || previous?.kind === "commit"
76 ? { ...state.branchBaseRefByThreadKey, [threadKey]: previous.baseRef }
77 : state.branchBaseRefByThreadKey,
78 };
79 }),
80 selectBranchBaseRef: (ref, baseRef) =>
81 set((state) => {
82 const threadKey = scopedThreadKey(ref);
83 const normalizedBaseRef = normalizeBaseRef(baseRef);
84 return {
85 byThreadKey: {
86 ...state.byThreadKey,
87 [threadKey]: { kind: "branch", baseRef: normalizedBaseRef },
88 },
89 branchBaseRefByThreadKey: {
90 ...state.branchBaseRefByThreadKey,
91 [threadKey]: normalizedBaseRef,
92 },
93 };
94 }),
95 selectToolCall: (ref, target) =>
96 set((state) => ({
97 byThreadKey: {
98 ...state.byThreadKey,
99 [scopedThreadKey(ref)]: { kind: "tool-call", ...target },
100 },
101 })),
102 selectTurn: (ref, turnId, filePath) =>
103 set((state) => {
104 const threadKey = scopedThreadKey(ref);
105 const previous = state.byThreadKey[threadKey];
106 return {
107 byThreadKey: {
108 ...state.byThreadKey,
109 [threadKey]: {
110 kind: "turn",
111 turnId,
112 filePath: filePath?.trim() || null,
113 revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1,
114 },
115 },
116 };
117 }),
118 selectCommit: (ref, sha, keepOnOpen = false) =>
119 set((state) => {
120 const threadKey = scopedThreadKey(ref);
121 const baseRef = branchBaseRef(state, threadKey);
122 return {
123 byThreadKey: {
124 ...state.byThreadKey,
125 [threadKey]: { kind: "commit", sha, baseRef },
126 },
127 branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [threadKey]: baseRef },
128 keepSelectionOnOpenThreadKey: keepOnOpen
129 ? threadKey
130 : state.keepSelectionOnOpenThreadKey,
131 };
132 }),
133 consumeKeepSelectionOnOpen: (ref) => {
134 if (get().keepSelectionOnOpenThreadKey !== scopedThreadKey(ref)) return false;
135 set({ keepSelectionOnOpenThreadKey: null });
136 return true;
137 },
138 reconcileTurnSelection: (ref, availableTurnIds) =>
139 set((state) => {
140 const threadKey = scopedThreadKey(ref);
141 const previous = state.byThreadKey[threadKey];
142 const latestTurnId = availableTurnIds[0];
143 if (
144 previous?.kind !== "turn" ||
145 latestTurnId === undefined ||
146 availableTurnIds.includes(previous.turnId)
147 ) {
148 return state;
149 }
150 return {
151 byThreadKey: {
152 ...state.byThreadKey,
153 [threadKey]: { ...previous, turnId: latestTurnId },
154 },
155 };
156 }),
157 removeThread: (ref) =>
158 set((state) => {
159 const threadKey = scopedThreadKey(ref);
160 if (!(threadKey in state.byThreadKey) && !(threadKey in state.branchBaseRefByThreadKey)) {
161 return state;
162 }
163 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
164 const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } =
165 state.branchBaseRefByThreadKey;
166 return { byThreadKey, branchBaseRefByThreadKey };
167 }),
168 }),
169 {
170 name: "t3code:diff-panel-state:v1",
171 version: 2,
172 storage: createJSONStorage(() =>
173 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
174 ),
175 partialize: (state) => ({
176 byThreadKey: state.byThreadKey,
177 branchBaseRefByThreadKey: state.branchBaseRefByThreadKey,
178 }),
179 },
180 ),
181);
182​
183interface DiffPanelSessionStoreState {
184 collapsedFileKeysByScopeKey: Record<string, ReadonlySet<string>>;
185 updateCollapsedFileKeys: (
186 scopeKey: string,
187 defaults: ReadonlySet<string>,
188 update: (current: ReadonlySet<string>) => ReadonlySet<string>,
189 ) => void;
191​
192// Session-only so folds survive right-panel switches without changing reload defaults.
193export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((set) => ({
194 collapsedFileKeysByScopeKey: {},
195 updateCollapsedFileKeys: (scopeKey, defaults, update) =>
196 set((state) => {
197 const current = state.collapsedFileKeysByScopeKey[scopeKey] ?? defaults;
198 const next = update(current);
199 if (next === current) return state;
200 return {
201 collapsedFileKeysByScopeKey: { ...state.collapsedFileKeysByScopeKey, [scopeKey]: next },
202 };
203 }),
204}));
205​
206export function selectThreadDiffPanelSelection(
207 byThreadKey: Record<string, DiffPanelSelection>,
208 ref: ScopedThreadRef | null | undefined,
209): DiffPanelSelection {
210 if (!ref) return DEFAULT_SELECTION;
211 return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION;
213​
214export function selectThreadBranchBaseRef(
215 state: Pick<DiffPanelStoreState, "byThreadKey" | "branchBaseRefByThreadKey">,
216 ref: ScopedThreadRef | null | undefined,
217): string | null {
218 return ref ? branchBaseRef(state, scopedThreadKey(ref)) : null;
220​
221export function resolveToolCallDiffTarget(
222 target: ToolCallDiffTarget,
223 items: ReadonlyArray<ThreadStructureVisibleTurnItem>,
224): ToolCallDiffTarget {
225 const row = items.find(
226 (row) => row.sourceThreadId === target.threadId && row.sourceItemId === target.itemId,
227 );
228 if (row?.item.type !== "file_change") return target;
229 const revision = turnItemDetailRevision(row.item);
230 return revision === target.revision ? target : { ...target, revision };