-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 [code health] Refactor useCardSettings to remove redundant effect and eslint-disable
#376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,36 +10,19 @@ import { | |
| import type { CardLayout, CardBlockId } from "@/lib/types"; | ||
|
|
||
| export function useCardSettings(mounted: boolean) { | ||
| const [isHydrated, setIsHydrated] = useState(false); | ||
| const [layout, setLayout] = useState<CardLayout>(() => loadCardSettings().layout); | ||
| const [displayOptions, setDisplayOptions] = useState<CardDisplayOptions>( | ||
| () => loadCardSettings().options, | ||
| ); | ||
|
|
||
| // Initialize state from storage on mount | ||
| useEffect(() => { | ||
| if (!mounted || isHydrated) { | ||
| return; | ||
| } | ||
|
|
||
| const { layout: storedLayout, options: storedOptions } = loadCardSettings(); | ||
|
|
||
| // eslint-disable-next-line react-hooks/set-state-in-effect | ||
| setLayout((prev) => JSON.stringify(prev) !== JSON.stringify(storedLayout) ? storedLayout : prev); | ||
| // eslint-disable-next-line react-hooks/set-state-in-effect | ||
| setDisplayOptions((prev) => JSON.stringify(prev) !== JSON.stringify(storedOptions) ? storedOptions : prev); | ||
|
|
||
| setIsHydrated(true); | ||
| }, [mounted, isHydrated]); | ||
|
|
||
| // Persist changes to storage | ||
| useEffect(() => { | ||
| if (!mounted || !isHydrated) { | ||
| if (!mounted) { | ||
| return; | ||
| } | ||
|
|
||
| saveCardSettings(layout, displayOptions); | ||
| }, [layout, displayOptions, mounted, isHydrated]); | ||
| }, [layout, displayOptions, mounted]); | ||
|
Comment on lines
13
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨 Critical Bug: User settings will be wiped out on every page load (SSR/Hydration issue)While removing the Why this happens:
Solution:To prevent this, we must keep the const [layout, setLayout] = useState<CardLayout>(() => loadCardSettings().layout);
const [displayOptions, setDisplayOptions] = useState<CardDisplayOptions>(
() => loadCardSettings().options,
);
const [isHydrated, setIsHydrated] = useState(false);
// Initialize state from storage on mount
useEffect(() => {
if (!mounted || isHydrated) {
return;
}
const { layout: storedLayout, options: storedOptions } = loadCardSettings();
setLayout(storedLayout);
setDisplayOptions(storedOptions);
setIsHydrated(true);
}, [mounted, isHydrated]);
// Persist changes to storage
useEffect(() => {
if (!mounted || !isHydrated) {
return;
}
saveCardSettings(layout, displayOptions);
}, [layout, displayOptions, mounted, isHydrated]); |
||
|
|
||
| const toggleMainBlockVisibility = useCallback((blockId: CardBlockId) => { | ||
| setLayout((prev) => toggleBlockVisibility(prev, blockId)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loadCardSettings()が2つのuseState遅延初期化で個別に呼ばれており、localStorage への読み取りが2回発生します。1回の呼び出しに統一することで、I/Oを減らしつつコードの意図も明確になります。Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!