Skip to content

Commit c74749b

Browse files
authored
fix: Stabilize git diff sidebar status while switching workspaces (#83)
1 parent f90dbbb commit c74749b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/features/git/hooks/useGitDiffs.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function useGitDiffs(
2222
const [state, setState] = useState<GitDiffState>(emptyState);
2323
const requestIdRef = useRef(0);
2424
const workspaceIdRef = useRef<string | null>(activeWorkspace?.id ?? null);
25+
const cachedDiffsRef = useRef<Map<string, GitFileDiff[]>>(new Map());
2526

2627
const fileKey = useMemo(
2728
() =>
@@ -53,6 +54,7 @@ export function useGitDiffs(
5354
return;
5455
}
5556
setState({ diffs, isLoading: false, error: null });
57+
cachedDiffsRef.current.set(workspaceId, diffs);
5658
} catch (error) {
5759
console.error("Failed to load git diffs", error);
5860
if (
@@ -74,7 +76,16 @@ export function useGitDiffs(
7476
if (workspaceIdRef.current !== workspaceId) {
7577
workspaceIdRef.current = workspaceId;
7678
requestIdRef.current += 1;
77-
setState(emptyState);
79+
if (!workspaceId) {
80+
setState(emptyState);
81+
return;
82+
}
83+
const cached = cachedDiffsRef.current.get(workspaceId);
84+
setState({
85+
diffs: cached ?? [],
86+
isLoading: false,
87+
error: null,
88+
});
7889
}
7990
}, [activeWorkspace?.id]);
8091

src/features/git/hooks/useGitStatus.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function useGitStatus(activeWorkspace: WorkspaceInfo | null) {
2424
const [status, setStatus] = useState<GitStatusState>(emptyStatus);
2525
const requestIdRef = useRef(0);
2626
const workspaceIdRef = useRef<string | null>(activeWorkspace?.id ?? null);
27+
const cachedStatusRef = useRef<Map<string, GitStatusState>>(new Map());
2728
const workspaceId = activeWorkspace?.id ?? null;
2829

2930
const refresh = useCallback(() => {
@@ -41,7 +42,9 @@ export function useGitStatus(activeWorkspace: WorkspaceInfo | null) {
4142
) {
4243
return;
4344
}
44-
setStatus({ ...data, error: null });
45+
const nextStatus = { ...data, error: null };
46+
setStatus(nextStatus);
47+
cachedStatusRef.current.set(workspaceId, nextStatus);
4548
})
4649
.catch((err) => {
4750
console.error("Failed to load git status", err);
@@ -51,19 +54,26 @@ export function useGitStatus(activeWorkspace: WorkspaceInfo | null) {
5154
) {
5255
return;
5356
}
54-
setStatus({
57+
const nextStatus = {
5558
...emptyStatus,
5659
branchName: "unknown",
5760
error: err instanceof Error ? err.message : String(err),
58-
});
61+
};
62+
setStatus(nextStatus);
63+
cachedStatusRef.current.set(workspaceId, nextStatus);
5964
});
6065
}, [workspaceId]);
6166

6267
useEffect(() => {
6368
if (workspaceIdRef.current !== workspaceId) {
6469
workspaceIdRef.current = workspaceId;
6570
requestIdRef.current += 1;
66-
setStatus(emptyStatus);
71+
if (!workspaceId) {
72+
setStatus(emptyStatus);
73+
return;
74+
}
75+
const cached = cachedStatusRef.current.get(workspaceId);
76+
setStatus(cached ?? emptyStatus);
6777
}
6878
}, [workspaceId]);
6979

0 commit comments

Comments
 (0)