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

diffPanelStore.ts

259 lines9.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 | {
22 kind: "turn";
23 turnId: RunId;
24 /** From the thread's folder, so a tracked repository's file carries that repository's folder. */
25 filePath: string | null;
26 revealRequestId: number;
27 /** A turn diff section key to narrow to; absent shows every repository. */
28 repository?: string;
29 };
30​
31const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
32​
33interface DiffPanelStoreState {
34 byThreadKey: Record<string, DiffPanelSelection>;
35 branchBaseRefByThreadKey: Record<string, string | null>;
36 selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
37 selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
38 selectToolCall: (ref: ScopedThreadRef, target: ToolCallDiffTarget) => void;
39 selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void;
40 selectTurnRepository: (ref: ScopedThreadRef, turnId: RunId, repository: string | null) => void;
41 selectCommit: (ref: ScopedThreadRef, sha: string) => void;
42 reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
43 removeThread: (ref: ScopedThreadRef) => void;
45​
46function normalizeBaseRef(baseRef: string | null): string | null {
47 const normalized = baseRef?.trim();
48 return normalized ? normalized : null;
50​
51function branchBaseRef(
52 state: Pick<DiffPanelStoreState, "byThreadKey" | "branchBaseRefByThreadKey">,
53 threadKey: string,
54): string | null {
55 const previous = state.byThreadKey[threadKey];
56 return previous?.kind === "branch" || previous?.kind === "commit"
57 ? previous.baseRef
58 : (state.branchBaseRefByThreadKey[threadKey] ?? null);
60​
61export const useDiffPanelStore = create<DiffPanelStoreState>()(
62 persist(
63 (set) => ({
64 byThreadKey: {},
65 branchBaseRefByThreadKey: {},
66 selectGitScope: (ref, scope) =>
67 set((state) => {
68 const threadKey = scopedThreadKey(ref);
69 const previous = state.byThreadKey[threadKey];
70 const previousBaseRef = branchBaseRef(state, threadKey);
71 return {
72 byThreadKey: {
73 ...state.byThreadKey,
74 [threadKey]:
75 scope === "branch"
76 ? { kind: "branch", baseRef: previousBaseRef }
77 : { kind: "unstaged" },
78 },
79 branchBaseRefByThreadKey:
80 previous?.kind === "branch" || previous?.kind === "commit"
81 ? { ...state.branchBaseRefByThreadKey, [threadKey]: previous.baseRef }
82 : state.branchBaseRefByThreadKey,
83 };
84 }),
85 selectBranchBaseRef: (ref, baseRef) =>
86 set((state) => {
87 const threadKey = scopedThreadKey(ref);
88 const normalizedBaseRef = normalizeBaseRef(baseRef);
89 return {
90 byThreadKey: {
91 ...state.byThreadKey,
92 [threadKey]: { kind: "branch", baseRef: normalizedBaseRef },
93 },
94 branchBaseRefByThreadKey: {
95 ...state.branchBaseRefByThreadKey,
96 [threadKey]: normalizedBaseRef,
97 },
98 };
99 }),
100 selectToolCall: (ref, target) =>
101 set((state) => ({
102 byThreadKey: {
103 ...state.byThreadKey,
104 [scopedThreadKey(ref)]: { kind: "tool-call", ...target },
105 },
106 })),
107 selectTurn: (ref, turnId, filePath) =>
108 set((state) => {
109 const threadKey = scopedThreadKey(ref);
110 const previous = state.byThreadKey[threadKey];
111 // Git reports paths with their edge whitespace; only a blank one means no file.
112 const requestedPath = filePath !== undefined && filePath.trim() !== "" ? filePath : null;
113 return {
114 byThreadKey: {
115 ...state.byThreadKey,
116 [threadKey]: {
117 kind: "turn",
118 turnId,
119 filePath: requestedPath,
120 revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1,
121 // A requested file may sit outside the repository the view was narrowed to.
122 ...(requestedPath === null &&
123 previous?.kind === "turn" &&
124 previous.repository !== undefined
125 ? { repository: previous.repository }
126 : {}),
127 },
128 },
129 };
130 }),
131 selectTurnRepository: (ref, turnId, repository) =>
132 set((state) => {
133 const threadKey = scopedThreadKey(ref);
134 const previous = state.byThreadKey[threadKey];
135 return {
136 byThreadKey: {
137 ...state.byThreadKey,
138 [threadKey]: {
139 kind: "turn",
140 turnId,
141 filePath: null,
142 revealRequestId: previous?.kind === "turn" ? previous.revealRequestId : 0,
143 ...(repository === null ? {} : { repository }),
144 },
145 },
146 };
147 }),
148 selectCommit: (ref, sha) =>
149 set((state) => {
150 const threadKey = scopedThreadKey(ref);
151 const baseRef = branchBaseRef(state, threadKey);
152 return {
153 byThreadKey: {
154 ...state.byThreadKey,
155 [threadKey]: { kind: "commit", sha, baseRef },
156 },
157 branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [threadKey]: baseRef },
158 };
159 }),
160 reconcileTurnSelection: (ref, availableTurnIds) =>
161 set((state) => {
162 const threadKey = scopedThreadKey(ref);
163 const previous = state.byThreadKey[threadKey];
164 const latestTurnId = availableTurnIds[0];
165 if (
166 previous?.kind !== "turn" ||
167 latestTurnId === undefined ||
168 availableTurnIds.includes(previous.turnId)
169 ) {
170 return state;
171 }
172 return {
173 byThreadKey: {
174 ...state.byThreadKey,
175 [threadKey]: { ...previous, turnId: latestTurnId },
176 },
177 };
178 }),
179 removeThread: (ref) =>
180 set((state) => {
181 const threadKey = scopedThreadKey(ref);
182 if (!(threadKey in state.byThreadKey) && !(threadKey in state.branchBaseRefByThreadKey)) {
183 return state;
184 }
185 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
186 const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } =
187 state.branchBaseRefByThreadKey;
188 return { byThreadKey, branchBaseRefByThreadKey };
189 }),
190 }),
191 {
192 name: "t3code:diff-panel-state:v1",
193 version: 2,
194 storage: createJSONStorage(() =>
195 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
196 ),
197 partialize: (state) => ({
198 byThreadKey: state.byThreadKey,
199 branchBaseRefByThreadKey: state.branchBaseRefByThreadKey,
200 }),
201 },
202 ),
203);
204​
205interface DiffPanelSessionStoreState {
206 collapsedFileKeysByScopeKey: Record<string, ReadonlySet<string>>;
207 updateCollapsedFileKeys: (
208 scopeKey: string,
209 defaults: ReadonlySet<string>,
210 update: (current: ReadonlySet<string>) => ReadonlySet<string>,
211 ) => void;
212 // Null follows the diffIgnoreWhitespace setting until the panel toggle is used.
213 ignoreWhitespace: boolean | null;
214 setIgnoreWhitespace: (ignoreWhitespace: boolean) => void;
216​
217// Session-only so folds and the whitespace toggle survive right-panel switches without
218// changing reload defaults.
219export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((set) => ({
220 collapsedFileKeysByScopeKey: {},
221 updateCollapsedFileKeys: (scopeKey, defaults, update) =>
222 set((state) => {
223 const current = state.collapsedFileKeysByScopeKey[scopeKey] ?? defaults;
224 const next = update(current);
225 if (next === current) return state;
226 return {
227 collapsedFileKeysByScopeKey: { ...state.collapsedFileKeysByScopeKey, [scopeKey]: next },
228 };
229 }),
230 ignoreWhitespace: null,
231 setIgnoreWhitespace: (ignoreWhitespace) => set({ ignoreWhitespace }),
232}));
233​
234export function selectThreadDiffPanelSelection(
235 byThreadKey: Record<string, DiffPanelSelection>,
236 ref: ScopedThreadRef | null | undefined,
237): DiffPanelSelection {
238 if (!ref) return DEFAULT_SELECTION;
239 return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION;
241​
242export function selectThreadBranchBaseRef(
243 state: Pick<DiffPanelStoreState, "byThreadKey" | "branchBaseRefByThreadKey">,
244 ref: ScopedThreadRef | null | undefined,
245): string | null {
246 return ref ? branchBaseRef(state, scopedThreadKey(ref)) : null;
248​
249export function resolveToolCallDiffTarget(
250 target: ToolCallDiffTarget,
251 items: ReadonlyArray<ThreadStructureVisibleTurnItem>,
252): ToolCallDiffTarget {
253 const row = items.find(
254 (row) => row.sourceThreadId === target.threadId && row.sourceItemId === target.itemId,
255 );
256 if (row?.item.type !== "file_change") return target;
257 const revision = turnItemDetailRevision(row.item);
258 return revision === target.revision ? target : { ...target, revision };