mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Rework useLocalStorage to use React Query, fix starring projects updates sidebar immediately
This commit is contained in:
6 files changed
+78
-44
No files matched your search
@@ -1,7 +1,8 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
/**
|
||||
* Generic hook for managing localStorage state
|
||||
* Generic hook for managing localStorage state using React Query
|
||||
*/
|
||||
export function useLocalStorage<T>(
|
||||
key: string,
|
||||
@@ -10,11 +11,13 @@ export function useLocalStorage<T>(
|
||||
serialize: (value: T) => string;
|
||||
deserialize: (value: string) => T;
|
||||
}
|
||||
) {
|
||||
): readonly [T, (value: T | ((prev: T) => T)) => void] {
|
||||
const serialize = serializer?.serialize || JSON.stringify;
|
||||
const deserialize = serializer?.deserialize || JSON.parse;
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [state, setState] = useState<T>(() => {
|
||||
const queryFn = () => {
|
||||
try {
|
||||
const item = localStorage.getItem(key);
|
||||
return item ? deserialize(item) : defaultValue;
|
||||
@@ -22,13 +25,22 @@ export function useLocalStorage<T>(
|
||||
console.warn(`Failed to load ${key} from localStorage:`, error);
|
||||
return defaultValue;
|
||||
}
|
||||
};
|
||||
|
||||
const { data: state } = useQuery({
|
||||
queryKey: ['localStorage', key],
|
||||
queryFn,
|
||||
initialData: queryFn,
|
||||
staleTime: Infinity, // localStorage data doesn't become stale
|
||||
gcTime: Infinity, // Keep in cache indefinitely
|
||||
});
|
||||
|
||||
const setValue = (value: T | ((prev: T) => T)) => {
|
||||
try {
|
||||
const valueToStore = value instanceof Function ? value(state) : value;
|
||||
setState(valueToStore);
|
||||
const currentValue = state ?? defaultValue;
|
||||
const valueToStore = value instanceof Function ? value(currentValue) : value;
|
||||
localStorage.setItem(key, serialize(valueToStore));
|
||||
queryClient.setQueryData(['localStorage', key], valueToStore);
|
||||
} catch (error) {
|
||||
console.warn(`Failed to save ${key} to localStorage:`, error);
|
||||
}
|
||||
@@ -39,7 +51,8 @@ export function useLocalStorage<T>(
|
||||
const handleStorageChange = (e: StorageEvent) => {
|
||||
if (e.key === key && e.newValue !== null) {
|
||||
try {
|
||||
setState(deserialize(e.newValue));
|
||||
const newValue = deserialize(e.newValue);
|
||||
queryClient.setQueryData(['localStorage', key], newValue);
|
||||
} catch (error) {
|
||||
console.warn(`Failed to sync ${key} from localStorage:`, error);
|
||||
}
|
||||
@@ -48,7 +61,7 @@ export function useLocalStorage<T>(
|
||||
|
||||
window.addEventListener('storage', handleStorageChange);
|
||||
return () => window.removeEventListener('storage', handleStorageChange);
|
||||
}, [key, deserialize]);
|
||||
}, [key, deserialize, queryClient]);
|
||||
|
||||
return [state, setValue] as const;
|
||||
}
|
||||
Reference in new issue
Block a user