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

diffPanelStore.ts

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