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

diffPanelStore.ts

218 lines7.7 KB

1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import type { ScopedThreadRef, TurnId } from "@t3tools/contracts";
3import { create } from "zustand";
4import { createJSONStorage, persist } from "zustand/middleware";
5​
6import { resolveStorage } from "./lib/storage";
7​
8export type DiffBaseline = TurnId | typeof THREAD_START_DIFF_BASELINE;
9​
10export type DiffPanelSelection =
11 | { kind: "branch"; baseRef: string | null }
12 | { kind: "unstaged" }
13 | {
14 kind: "turn";
15 turnId: TurnId;
16 filePath: string | null;
17 revealRequestId: number;
18 baselineTurnId?: DiffBaseline | undefined;
19 followLatest?: boolean;
20 };
21​
22const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
23const DEFAULT_WORKING_TREE_SELECTION: DiffPanelSelection = { kind: "unstaged" };
24​
25interface DiffPanelStoreState {
26 byThreadKey: Record<string, DiffPanelSelection>;
27 branchBaseRefByThreadKey: Record<string, string | null>;
28 selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
29 selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
30 selectTurn: (
31 ref: ScopedThreadRef,
32 turnId: TurnId,
33 filePath?: string,
34 comparison?: { baselineTurnId?: DiffBaseline | undefined; followLatest?: boolean },
35 ) => void;
36 pinBaseline: (
37 ref: ScopedThreadRef,
38 baselineTurnId: DiffBaseline | null,
39 latestTurnId: TurnId,
40 ) => void;
41 selectLatestTurn: (ref: ScopedThreadRef, turnId: TurnId) => void;
42 reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<TurnId>) => 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​
51export const useDiffPanelStore = create<DiffPanelStoreState>()(
52 persist(
53 (set, get) => ({
54 byThreadKey: {},
55 branchBaseRefByThreadKey: {},
56 selectGitScope: (ref, scope) =>
57 set((state) => {
58 const threadKey = scopedThreadKey(ref);
59 const previous = state.byThreadKey[threadKey];
60 const previousBaseRef =
61 previous?.kind === "branch"
62 ? previous.baseRef
63 : (state.branchBaseRefByThreadKey[threadKey] ?? null);
64 return {
65 byThreadKey: {
66 ...state.byThreadKey,
67 [threadKey]:
68 scope === "branch"
69 ? { kind: "branch", baseRef: previousBaseRef }
70 : { kind: "unstaged" },
71 },
72 branchBaseRefByThreadKey:
73 previous?.kind === "branch"
74 ? { ...state.branchBaseRefByThreadKey, [threadKey]: previous.baseRef }
75 : state.branchBaseRefByThreadKey,
76 };
77 }),
78 selectBranchBaseRef: (ref, baseRef) =>
79 set((state) => {
80 const threadKey = scopedThreadKey(ref);
81 const normalizedBaseRef = normalizeBaseRef(baseRef);
82 return {
83 byThreadKey: {
84 ...state.byThreadKey,
85 [threadKey]: { kind: "branch", baseRef: normalizedBaseRef },
86 },
87 branchBaseRefByThreadKey: {
88 ...state.branchBaseRefByThreadKey,
89 [threadKey]: normalizedBaseRef,
90 },
91 };
92 }),
93 selectTurn: (ref, turnId, filePath, comparison) =>
94 set((state) => {
95 const threadKey = scopedThreadKey(ref);
96 const previous = state.byThreadKey[threadKey];
97 return {
98 byThreadKey: {
99 ...state.byThreadKey,
100 [threadKey]: {
101 kind: "turn",
102 turnId,
103 ...comparison,
104 filePath: filePath?.trim() || null,
105 revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1,
106 },
107 },
108 };
109 }),
110 pinBaseline: (ref, baselineTurnId, latestTurnId) => {
111 const previous = get().byThreadKey[scopedThreadKey(ref)];
112 const turnId =
113 !baselineTurnId && previous?.kind === "turn" ? previous.turnId : latestTurnId;
114 get().selectTurn(
115 ref,
116 turnId,
117 undefined,
118 baselineTurnId ? { baselineTurnId, followLatest: true } : undefined,
119 );
120 },
121 selectLatestTurn: (ref, turnId) => {
122 const previous = get().byThreadKey[scopedThreadKey(ref)];
123 get().selectTurn(
124 ref,
125 turnId,
126 undefined,
127 previous?.kind === "turn" && previous.baselineTurnId
128 ? { baselineTurnId: previous.baselineTurnId, followLatest: true }
129 : undefined,
130 );
131 },
132 reconcileTurnSelection: (ref, availableTurnIds) =>
133 set((state) => {
134 const threadKey = scopedThreadKey(ref);
135 const previous = state.byThreadKey[threadKey];
136 const latestTurnId = availableTurnIds[0];
137 if (previous?.kind !== "turn") return state;
138 if (latestTurnId === undefined) {
139 return previous.baselineTurnId
140 ? { byThreadKey: { ...state.byThreadKey, [threadKey]: DEFAULT_SELECTION } }
141 : state;
142 }
143 const turnId =
144 previous.followLatest || !availableTurnIds.includes(previous.turnId)
145 ? latestTurnId
146 : previous.turnId;
147 const baselineTurnId =
148 previous.baselineTurnId &&
149 (previous.baselineTurnId === THREAD_START_DIFF_BASELINE ||
150 availableTurnIds.includes(previous.baselineTurnId))
151 ? previous.baselineTurnId
152 : undefined;
153 if (turnId === previous.turnId && baselineTurnId === previous.baselineTurnId)
154 return state;
155 return {
156 byThreadKey: {
157 ...state.byThreadKey,
158 [threadKey]: {
159 ...previous,
160 turnId,
161 baselineTurnId,
162 ...(previous.followLatest ? { followLatest: baselineTurnId !== undefined } : {}),
163 },
164 },
165 };
166 }),
167 removeThread: (ref) =>
168 set((state) => {
169 const threadKey = scopedThreadKey(ref);
170 if (!(threadKey in state.byThreadKey) && !(threadKey in state.branchBaseRefByThreadKey)) {
171 return state;
172 }
173 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
174 const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } =
175 state.branchBaseRefByThreadKey;
176 return { byThreadKey, branchBaseRefByThreadKey };
177 }),
178 }),
179 {
180 name: "t3code:diff-panel-state:v1",
181 version: 1,
182 storage: createJSONStorage(() =>
183 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
184 ),
185 partialize: (state) => ({
186 byThreadKey: state.byThreadKey,
187 branchBaseRefByThreadKey: state.branchBaseRefByThreadKey,
188 }),
189 },
190 ),
191);
192​
193export function selectThreadDiffPanelSelection(
194 byThreadKey: Record<string, DiffPanelSelection>,
195 ref: ScopedThreadRef | null | undefined,
196 hasWorkingTreeChanges = false,
197): DiffPanelSelection {
198 if (!ref) return DEFAULT_SELECTION;
199 return (
200 byThreadKey[scopedThreadKey(ref)] ??
201 (hasWorkingTreeChanges ? DEFAULT_WORKING_TREE_SELECTION : DEFAULT_SELECTION)
202 );
204​
205/** Compare completed snapshots; a pinned turn excludes that turn's own changes. */
206export function getCheckpointDiffRange(
207 toTurnCount: number | null | undefined,
208 baselineTurnCount?: number | null,
209) {
210 if (toTurnCount == null || (baselineTurnCount != null && baselineTurnCount > toTurnCount))
211 return null;
212 return {
213 fromTurnCount: baselineTurnCount ?? Math.max(0, toTurnCount - 1),
214 toTurnCount,
215 };
217​
218export const THREAD_START_DIFF_BASELINE = "thread-start";