mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-10-07 02:22:38 -04:00
* (lint) MOB-1063 enforce trailing commas * autofix trailing commas * manually fix newly introduced maxlen violations * add trailing comma convention to i18n build
37 lines
1.1 KiB
TypeScript
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;
|