mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
- Remove nested git repository from bugulma/frontend/.git - Add all frontend files to main repository tracking - Convert from separate frontend/backend repos to unified monorepo - Preserve all frontend code and development history as tracked files - Eliminate nested repository complexity for simpler development workflow This creates a proper monorepo structure with frontend and backend coexisting in the same repository for easier development and deployment.
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
/**
|
|
* Hook for persisting state in localStorage with SSR safety
|
|
*/
|
|
export function useLocalStorage<T>(
|
|
key: string,
|
|
initialValue: T
|
|
): [T, (value: T | ((prevValue: T) => T)) => void] {
|
|
const [storedValue, setStoredValue] = useState<T>(() => {
|
|
if (typeof window === 'undefined') {
|
|
return initialValue;
|
|
}
|
|
|
|
try {
|
|
const item = window.localStorage.getItem(key);
|
|
return item ? JSON.parse(item) : initialValue;
|
|
} catch (error) {
|
|
console.warn(`Error reading localStorage key "${key}":`, error);
|
|
return initialValue;
|
|
}
|
|
});
|
|
|
|
const setValue = useCallback((value: T | ((prevValue: T) => T)) => {
|
|
try {
|
|
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
setStoredValue(valueToStore);
|
|
|
|
if (typeof window !== 'undefined') {
|
|
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
|
}
|
|
} catch (error) {
|
|
console.warn(`Error setting localStorage key "${key}":`, error);
|
|
}
|
|
}, [key, storedValue]);
|
|
|
|
return [storedValue, setValue];
|
|
}
|