fix: relax persistence type contraints

This commit is contained in:
rektdeckard 2025-05-22 19:01:01 -06:00
parent 2ccb2e53b9
commit 97d9cb1013
2 changed files with 5 additions and 12 deletions

View File

@ -52,7 +52,7 @@ function deserializeSettingsState(state: SerializedSettingsState): SettingsState
const SettingsStateContext = createContext<SettingsStateContextType>({
state: initialState,
set: () => {},
set: () => { },
});
const SettingsStateProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
@ -63,8 +63,6 @@ const SettingsStateProvider: React.FC<{ children: React.ReactNode }> = ({ childr
const deserializedState = useMemo(() => deserializeSettingsState(state), [state]);
console.info({ deserializedState });
const setSettingsState = useCallback(
(dispatch: SetStateAction<SettingsState>) => {
if (typeof dispatch === 'function') {

View File

@ -2,12 +2,7 @@
import { Dispatch, SetStateAction, useState } from 'react';
type JsonPrimitive = string | number | boolean | null;
type JsonArray = JsonValue[];
type JsonObject = { [key: string]: JsonValue };
type JsonValue = JsonPrimitive | JsonArray | JsonObject;
function saveToLocalStorage<T extends JsonValue>(key: string, value: T): void {
function saveToLocalStorage<T extends object>(key: string, value: T): void {
if (typeof localStorage === 'undefined') {
console.error('Local storage is not available.');
return;
@ -25,7 +20,7 @@ function saveToLocalStorage<T extends JsonValue>(key: string, value: T): void {
}
}
function loadFromLocalStorage<T extends JsonValue>(key: string): T | undefined {
function loadFromLocalStorage<T extends object>(key: string): T | undefined {
if (typeof localStorage === 'undefined') {
console.error('Local storage is not available.');
return undefined;
@ -44,7 +39,7 @@ function loadFromLocalStorage<T extends JsonValue>(key: string): T | undefined {
}
}
export function createLocalStorageInterface<T extends JsonValue>(
export function createLocalStorageInterface<T extends object>(
key: string,
): { load: () => T | undefined; save: (value: T) => void } {
return {
@ -53,7 +48,7 @@ export function createLocalStorageInterface<T extends JsonValue>(
};
}
export function usePersistToLocalStorage<T extends JsonValue>(
export function usePersistToLocalStorage<T extends object>(
key: string,
initialValue: T,
): [T, Dispatch<SetStateAction<T>>] {