mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 19:58:08 +01:00
Created lib/hooks/useLocalStorage.ts with SSR-safe boolean persistence. Updated ChatAuthCTA and OnboardingChecklist to use it instead of raw localStorage calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
32 lines
717 B
TypeScript
32 lines
717 B
TypeScript
'use client';
|
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
|
|
/**
|
|
* Hook for persisting boolean state in localStorage.
|
|
* SSR-safe: returns initialValue until hydration completes.
|
|
*/
|
|
export function useLocalStorageFlag(
|
|
key: string,
|
|
initialValue = false,
|
|
): [boolean, (value: boolean) => void] {
|
|
const [value, setValue] = useState(initialValue);
|
|
|
|
useEffect(() => {
|
|
const stored = localStorage.getItem(key);
|
|
if (stored !== null) {
|
|
setValue(stored === 'true');
|
|
}
|
|
}, [key]);
|
|
|
|
const setAndPersist = useCallback(
|
|
(newValue: boolean) => {
|
|
setValue(newValue);
|
|
localStorage.setItem(key, String(newValue));
|
|
},
|
|
[key],
|
|
);
|
|
|
|
return [value, setAndPersist];
|
|
}
|