Open History to see the last commit
1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import type { RunId, ScopedThreadRef } from "@t3tools/contracts";
3import { create } from "zustand";
4import { createJSONStorage, persist } from "zustand/middleware";
5
6import { resolveStorage } from "./lib/storage";
7
8export type DiffPanelSelection =
9 | { kind: "branch"; baseRef: string | null }
10 | { kind: "unstaged" }
11 | { kind: "turn"; turnId: RunId; filePath: string | null; revealRequestId: number };
12
13export type DiffRenderMode = "stacked" | "split";
14
15const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
16const DEFAULT_WORKING_TREE_SELECTION: DiffPanelSelection = { kind: "unstaged" };
17
18interface DiffPanelStoreState {
19 byThreadKey: Record<string, DiffPanelSelection>;
20 branchBaseRefByThreadKey: Record<string, string | null>;
21 diffRenderMode: DiffRenderMode;
22 setDiffRenderMode: (mode: DiffRenderMode) => void;
23 selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
24 selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
25 selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void;
26 reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
27 removeThread: (ref: ScopedThreadRef) => void;
28}
29
30function normalizeBaseRef(baseRef: string | null): string | null {
31 const normalized = baseRef?.trim();
32 return normalized ? normalized : null;
33}
34
35export const useDiffPanelStore = create<DiffPanelStoreState>()(
36 persist(
37 (set) => ({
38 byThreadKey: {},
39 branchBaseRefByThreadKey: {},
40 diffRenderMode: "stacked",
41 setDiffRenderMode: (diffRenderMode) => set({ diffRenderMode }),
42 selectGitScope: (ref, scope) =>
43 set((state) => {
44 const threadKey = scopedThreadKey(ref);
45 const previous = state.byThreadKey[threadKey];
46 const previousBaseRef =
47 previous?.kind === "branch"
48 ? previous.baseRef
49 : (state.branchBaseRefByThreadKey[threadKey] ?? null);
50 return {
51 byThreadKey: {
52 ...state.byThreadKey,
53 [threadKey]:
54 scope === "branch"
55 ? { kind: "branch", baseRef: previousBaseRef }
56 : { kind: "unstaged" },
57 },
58 branchBaseRefByThreadKey:
59 previous?.kind === "branch"
60 ? { ...state.branchBaseRefByThreadKey, [threadKey]: previous.baseRef }
61 : state.branchBaseRefByThreadKey,
62 };
63 }),
64 selectBranchBaseRef: (ref, baseRef) =>
65 set((state) => {
66 const threadKey = scopedThreadKey(ref);
67 const normalizedBaseRef = normalizeBaseRef(baseRef);
68 return {
69 byThreadKey: {
70 ...state.byThreadKey,
71 [threadKey]: { kind: "branch", baseRef: normalizedBaseRef },
72 },
73 branchBaseRefByThreadKey: {
74 ...state.branchBaseRefByThreadKey,
75 [threadKey]: normalizedBaseRef,
76 },
77 };
78 }),
79 selectTurn: (ref, turnId, filePath) =>
80 set((state) => {
81 const threadKey = scopedThreadKey(ref);
82 const previous = state.byThreadKey[threadKey];
83 return {
84 byThreadKey: {
85 ...state.byThreadKey,
86 [threadKey]: {
87 kind: "turn",
88 turnId,
89 filePath: filePath?.trim() || null,
90 revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1,
91 },
92 },
93 };
94 }),
95 reconcileTurnSelection: (ref, availableTurnIds) =>
96 set((state) => {
97 const threadKey = scopedThreadKey(ref);
98 const previous = state.byThreadKey[threadKey];
99 const latestTurnId = availableTurnIds[0];
100 if (
101 previous?.kind !== "turn" ||
102 latestTurnId === undefined ||
103 availableTurnIds.includes(previous.turnId)
104 ) {
105 return state;
106 }
107 return {
108 byThreadKey: {
109 ...state.byThreadKey,
110 [threadKey]: { ...previous, turnId: latestTurnId },
111 },
112 };
113 }),
114 removeThread: (ref) =>
115 set((state) => {
116 const threadKey = scopedThreadKey(ref);
117 if (!(threadKey in state.byThreadKey) && !(threadKey in state.branchBaseRefByThreadKey)) {
118 return state;
119 }
120 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
121 const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } =
122 state.branchBaseRefByThreadKey;
123 return { byThreadKey, branchBaseRefByThreadKey };
124 }),
125 }),
126 {
127 name: "t3code:diff-panel-state:v1",
128 version: 2,
129 storage: createJSONStorage(() =>
130 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
131 ),
132 partialize: (state) => ({
133 byThreadKey: state.byThreadKey,
134 branchBaseRefByThreadKey: state.branchBaseRefByThreadKey,
135 diffRenderMode: state.diffRenderMode,
136 }),
137 },
138 ),
139);
140
141export function selectThreadDiffPanelSelection(
142 byThreadKey: Record<string, DiffPanelSelection>,
143 ref: ScopedThreadRef | null | undefined,
144 hasWorkingTreeChanges = false,
145): DiffPanelSelection {
146 if (!ref) return DEFAULT_SELECTION;
147 return (
148 byThreadKey[scopedThreadKey(ref)] ??
149 (hasWorkingTreeChanges ? DEFAULT_WORKING_TREE_SELECTION : DEFAULT_SELECTION)
150 );
151}