Merge branch 'Donkie:master' into master

This commit is contained in:
Patricio Suarez
2023-07-30 05:54:18 -04:00
committed by GitHub
5 changed files with 255 additions and 9 deletions

View File

@@ -20,7 +20,7 @@ import { ISpool } from "./model";
import {
TableState,
useInitialTableState,
useShowArchive,
useSavedState,
useStoreInitialState,
} from "../../utils/saveload";
import {
@@ -55,7 +55,10 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
const initialState = useInitialTableState("spoolList");
// State for the switch to show archived spools
const [showArchived, setShowArchived] = useShowArchive("spoolList");
const [showArchived, setShowArchived] = useSavedState(
"spoolList-showArchived",
false
);
// Fetch data from the API
const {

View File

@@ -60,17 +60,17 @@ export function useStoreInitialState(tableId: string, state: TableState) {
}, [tableId, state.showColumns]);
}
export function useShowArchive(tableId: string) {
const [showArchive, setShowArchive] = React.useState(() => {
const savedShowArchive = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-showArchive`) : null;
return savedShowArchive ? JSON.parse(savedShowArchive) : false;
export function useSavedState<T>(id: string, defaultValue: T) {
const [state, setState] = React.useState<T>(() => {
const savedState = isLocalStorageAvailable ? localStorage.getItem(`savedStates-${id}`) : null;
return savedState ? JSON.parse(savedState) : defaultValue;
});
React.useEffect(() => {
if (isLocalStorageAvailable) {
localStorage.setItem(`${tableId}-showArchive`, JSON.stringify(showArchive));
localStorage.setItem(`savedStates-${id}`, JSON.stringify(state));
}
}, [tableId, showArchive]);
}, [id, state]);
return [showArchive, setShowArchive] as const;
return [state, setState] as const;
}