Skip to content

Commit 4338645

Browse files
perf(web): panel presence keeps its retained value in state
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a3e07df commit 4338645

2 files changed

Lines changed: 11 additions & 17 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ export const REACT_COMPILED_FILES = [
3434
"apps/web/src/state/sourceControlActions.ts",
3535
"apps/web/src/components/settings/ProjectFaviconPickerDialog.tsx",
3636
"apps/web/src/components/settings/ThemeEditorPanel.tsx",
37+
"apps/web/src/panelAnimations.ts",
3738
] as const;

‎apps/web/src/panelAnimations.ts‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContext, useEffect, useRef, useState } from "react";
1+
import { createContext, useContext, useEffect, useState } from "react";
22
import { type PanelAnimationDurationMs } from "@t3tools/contracts/settings";
33

44
import { useMediaQuery } from "./hooks/useMediaQuery";
@@ -45,30 +45,23 @@ export function usePanelPresence<T>(
4545
durationMs: PanelAnimationDurationMs,
4646
): { present: boolean; value: T | null } {
4747
const [present, setPresent] = useState(open);
48-
const retainedRef = useRef<{ scopeKey: string | null; value: T | null } | null>(
48+
const [retained, setRetained] = useState<{ scopeKey: string | null; value: T | null } | null>(
4949
open ? { scopeKey, value } : null,
5050
);
51+
if (open && (retained?.scopeKey !== scopeKey || retained.value !== value)) {
52+
setRetained({ scopeKey, value });
53+
}
5154

52-
useEffect(() => {
53-
if (open) retainedRef.current = { scopeKey, value };
54-
}, [open, scopeKey, value]);
55+
if (open && !present) setPresent(true);
56+
if (!open && !animated && present) setPresent(false);
5557

5658
useEffect(() => {
57-
if (open) {
58-
setPresent(true);
59-
return;
60-
}
61-
if (!animated) {
62-
setPresent(false);
63-
return;
64-
}
65-
59+
if (open || !animated) return;
6660
const timeout = window.setTimeout(() => setPresent(false), durationMs);
6761
return () => window.clearTimeout(timeout);
6862
}, [animated, durationMs, open]);
6963

70-
const retainedValue =
71-
retainedRef.current?.scopeKey === scopeKey ? retainedRef.current.value : null;
72-
const visible = open || (animated && present && retainedRef.current?.scopeKey === scopeKey);
64+
const retainedValue = retained?.scopeKey === scopeKey ? retained.value : null;
65+
const visible = open || (animated && present && retained?.scopeKey === scopeKey);
7366
return { present: visible, value: open ? value : visible ? retainedValue : null };
7467
}

0 commit comments

Comments
 (0)