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