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

diffPanelStore.ts

189 lines7.2 KB

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​
13const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
14​
15interface DiffPanelStoreState {
16 byThreadKey: Record<string, DiffPanelSelection>;
17 branchBaseRefByThreadKey: Record<string, string | null>;
18 /** The repository Uncommitted and Changes show in a multi-repo workspace. */
19 repositoryByThreadKey: Record<string, string>;
20 selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
21 selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
22 selectRepository: (ref: ScopedThreadRef, relativePath: string) => void;
23 selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void;
24 reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
25 removeThread: (ref: ScopedThreadRef) => void;
27​
28function normalizeBaseRef(baseRef: string | null): string | null {
29 const normalized = baseRef?.trim();
30 return normalized ? normalized : null;
32​
33export const useDiffPanelStore = create<DiffPanelStoreState>()(
34 persist(
35 (set) => ({
36 byThreadKey: {},
37 branchBaseRefByThreadKey: {},
38 repositoryByThreadKey: {},
39 selectGitScope: (ref, scope) =>
40 set((state) => {
41 const threadKey = scopedThreadKey(ref);
42 const previous = state.byThreadKey[threadKey];
43 const previousBaseRef =
44 previous?.kind === "branch"
45 ? previous.baseRef
46 : (state.branchBaseRefByThreadKey[threadKey] ?? null);
47 return {
48 byThreadKey: {
49 ...state.byThreadKey,
50 [threadKey]:
51 scope === "branch"
52 ? { kind: "branch", baseRef: previousBaseRef }
53 : { kind: "unstaged" },
54 },
55 branchBaseRefByThreadKey:
56 previous?.kind === "branch"
57 ? { ...state.branchBaseRefByThreadKey, [threadKey]: previous.baseRef }
58 : state.branchBaseRefByThreadKey,
59 };
60 }),
61 selectBranchBaseRef: (ref, baseRef) =>
62 set((state) => {
63 const threadKey = scopedThreadKey(ref);
64 const normalizedBaseRef = normalizeBaseRef(baseRef);
65 return {
66 byThreadKey: {
67 ...state.byThreadKey,
68 [threadKey]: { kind: "branch", baseRef: normalizedBaseRef },
69 },
70 branchBaseRefByThreadKey: {
71 ...state.branchBaseRefByThreadKey,
72 [threadKey]: normalizedBaseRef,
73 },
74 };
75 }),
76 // A base branch picked in one repository may not exist in another, so switching
77 // repositories goes back to the automatic base.
78 selectRepository: (ref, relativePath) =>
79 set((state) => {
80 const threadKey = scopedThreadKey(ref);
81 if (state.repositoryByThreadKey[threadKey] === relativePath) return state;
82 const previous = state.byThreadKey[threadKey];
83 return {
84 repositoryByThreadKey: { ...state.repositoryByThreadKey, [threadKey]: relativePath },
85 byThreadKey:
86 previous?.kind === "branch"
87 ? { ...state.byThreadKey, [threadKey]: { kind: "branch", baseRef: null } }
88 : state.byThreadKey,
89 branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [threadKey]: null },
90 };
91 }),
92 selectTurn: (ref, turnId, filePath) =>
93 set((state) => {
94 const threadKey = scopedThreadKey(ref);
95 const previous = state.byThreadKey[threadKey];
96 return {
97 byThreadKey: {
98 ...state.byThreadKey,
99 [threadKey]: {
100 kind: "turn",
101 turnId,
102 filePath: filePath?.trim() || null,
103 revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1,
104 },
105 },
106 };
107 }),
108 reconcileTurnSelection: (ref, availableTurnIds) =>
109 set((state) => {
110 const threadKey = scopedThreadKey(ref);
111 const previous = state.byThreadKey[threadKey];
112 const latestTurnId = availableTurnIds[0];
113 if (
114 previous?.kind !== "turn" ||
115 latestTurnId === undefined ||
116 availableTurnIds.includes(previous.turnId)
117 ) {
118 return state;
119 }
120 return {
121 byThreadKey: {
122 ...state.byThreadKey,
123 [threadKey]: { ...previous, turnId: latestTurnId },
124 },
125 };
126 }),
127 removeThread: (ref) =>
128 set((state) => {
129 const threadKey = scopedThreadKey(ref);
130 if (
131 !(threadKey in state.byThreadKey) &&
132 !(threadKey in state.branchBaseRefByThreadKey) &&
133 !(threadKey in state.repositoryByThreadKey)
134 ) {
135 return state;
136 }
137 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
138 const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } =
139 state.branchBaseRefByThreadKey;
140 const { [threadKey]: _removedRepository, ...repositoryByThreadKey } =
141 state.repositoryByThreadKey;
142 return { byThreadKey, branchBaseRefByThreadKey, repositoryByThreadKey };
143 }),
144 }),
145 {
146 name: "t3code:diff-panel-state:v1",
147 version: 2,
148 storage: createJSONStorage(() =>
149 resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
150 ),
151 partialize: (state) => ({
152 byThreadKey: state.byThreadKey,
153 branchBaseRefByThreadKey: state.branchBaseRefByThreadKey,
154 repositoryByThreadKey: state.repositoryByThreadKey,
155 }),
156 },
157 ),
158);
159​
160interface DiffPanelSessionStoreState {
161 collapsedFileKeysByScopeKey: Record<string, ReadonlySet<string>>;
162 updateCollapsedFileKeys: (
163 scopeKey: string,
164 defaults: ReadonlySet<string>,
165 update: (current: ReadonlySet<string>) => ReadonlySet<string>,
166 ) => void;
168​
169// Session-only so folds survive right-panel switches without changing reload defaults.
170export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((set) => ({
171 collapsedFileKeysByScopeKey: {},
172 updateCollapsedFileKeys: (scopeKey, defaults, update) =>
173 set((state) => {
174 const current = state.collapsedFileKeysByScopeKey[scopeKey] ?? defaults;
175 const next = update(current);
176 if (next === current) return state;
177 return {
178 collapsedFileKeysByScopeKey: { ...state.collapsedFileKeysByScopeKey, [scopeKey]: next },
179 };
180 }),
181}));
182​
183export function selectThreadDiffPanelSelection(
184 byThreadKey: Record<string, DiffPanelSelection>,
185 ref: ScopedThreadRef | null | undefined,
186): DiffPanelSelection {
187 if (!ref) return DEFAULT_SELECTION;
188 return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION;