Files
t3code/apps/web/src/diffPanelStore.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

140 lines
5.0 KiB
TypeScript

import { scopedThreadKey } from "@t3tools/client-runtime/environment";
import type { RunId, ScopedThreadRef } from "@t3tools/contracts";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { resolveStorage } from "./lib/storage";
export type DiffPanelSelection =
| { kind: "branch"; baseRef: string | null }
| { kind: "unstaged" }
| { kind: "turn"; turnId: RunId; filePath: string | null; revealRequestId: number };
const DEFAULT_SELECTION: DiffPanelSelection = { kind: "unstaged" };
interface DiffPanelStoreState {
byThreadKey: Record<string, DiffPanelSelection>;
branchBaseRefByThreadKey: Record<string, string | null>;
selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void;
reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
removeThread: (ref: ScopedThreadRef) => void;
}
function normalizeBaseRef(baseRef: string | null): string | null {
const normalized = baseRef?.trim();
return normalized ? normalized : null;
}
export const useDiffPanelStore = create<DiffPanelStoreState>()(
persist(
(set) => ({
byThreadKey: {},
branchBaseRefByThreadKey: {},
selectGitScope: (ref, scope) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const previous = state.byThreadKey[threadKey];
const previousBaseRef =
previous?.kind === "branch"
? previous.baseRef
: (state.branchBaseRefByThreadKey[threadKey] ?? null);
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]:
scope === "branch"
? { kind: "branch", baseRef: previousBaseRef }
: { kind: "unstaged" },
},
branchBaseRefByThreadKey:
previous?.kind === "branch"
? { ...state.branchBaseRefByThreadKey, [threadKey]: previous.baseRef }
: state.branchBaseRefByThreadKey,
};
}),
selectBranchBaseRef: (ref, baseRef) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const normalizedBaseRef = normalizeBaseRef(baseRef);
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]: { kind: "branch", baseRef: normalizedBaseRef },
},
branchBaseRefByThreadKey: {
...state.branchBaseRefByThreadKey,
[threadKey]: normalizedBaseRef,
},
};
}),
selectTurn: (ref, turnId, filePath) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const previous = state.byThreadKey[threadKey];
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]: {
kind: "turn",
turnId,
filePath: filePath?.trim() || null,
revealRequestId: previous?.kind === "turn" ? previous.revealRequestId + 1 : 1,
},
},
};
}),
reconcileTurnSelection: (ref, availableTurnIds) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const previous = state.byThreadKey[threadKey];
const latestTurnId = availableTurnIds[0];
if (
previous?.kind !== "turn" ||
latestTurnId === undefined ||
availableTurnIds.includes(previous.turnId)
) {
return state;
}
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]: { ...previous, turnId: latestTurnId },
},
};
}),
removeThread: (ref) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
if (!(threadKey in state.byThreadKey) && !(threadKey in state.branchBaseRefByThreadKey)) {
return state;
}
const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
const { [threadKey]: _removedBaseRef, ...branchBaseRefByThreadKey } =
state.branchBaseRefByThreadKey;
return { byThreadKey, branchBaseRefByThreadKey };
}),
}),
{
name: "t3code:diff-panel-state:v1",
version: 2,
storage: createJSONStorage(() =>
resolveStorage(typeof window !== "undefined" ? window.localStorage : undefined),
),
partialize: (state) => ({
byThreadKey: state.byThreadKey,
branchBaseRefByThreadKey: state.branchBaseRefByThreadKey,
}),
},
),
);
export function selectThreadDiffPanelSelection(
byThreadKey: Record<string, DiffPanelSelection>,
ref: ScopedThreadRef | null | undefined,
): DiffPanelSelection {
if (!ref) return DEFAULT_SELECTION;
return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION;
}