Files
iNaturalistReactNative/src/sharedHooks/useStoredLayout.ts
T
Ryan Stelly b78be9243d lint rule & autofix for "trailing comma" (#3299)
* (lint) MOB-1063 enforce trailing commas

* autofix trailing commas

* manually fix newly introduced maxlen violations

* add trailing comma convention to i18n build
2025-12-22 20:17:13 -06:00

37 lines
1.1 KiB
TypeScript

import { useCallback, useEffect, useState } from "react";
import { zustandStorage } from "stores/useStore";
interface LayoutHook {
layout: string | null;
writeLayoutToStorage: ( newValue: string ) => void;
}
const useStoredLayout = ( storageKey: string ): LayoutHook => {
const [layout, setLayout] = useState<string | null>( null );
const writeLayoutToStorage = useCallback( ( newValue: string ) => {
zustandStorage.setItem( storageKey, newValue );
setLayout( newValue );
}, [storageKey] );
useEffect( ( ) => {
const readLayoutFromStorage = async ( ) => {
// Casting is necessary because zustandStorage.getItem returns string | number | null
const storedLayout = zustandStorage.getItem( storageKey ) as string | null;
const defaultLayout = storageKey === "exploreObservationsLayout"
? "map"
: "grid";
setLayout( storedLayout || defaultLayout );
};
readLayoutFromStorage( );
}, [writeLayoutToStorage, storageKey] );
return {
layout,
writeLayoutToStorage,
};
};
export default useStoredLayout;