Files
prowler/hooks/useLocalStorage.ts
T
Pablo Lara 48f633889a Providers page table (#20)
* fix: add suppressHydrationWarning to resolve console errors

* chore: add server-only library

* WIP: Mock API for providers and start rendering data

* chore: relocate utils folder to proper directory

* chore: install shadcn for tables, adding sttings page

* refactor: improve sidebar display behavior

* chore: add fake data to the dataProviders

* chore: remove the old table and rename ProviderInfo component

* refactor: improve sidebar display behavior adding a custom hook

* feat: the Providers table is rendering real data

* chore: set the default valuef or isCollapse to false

* chore: Added a helper function getProviderAttributes for cleaner access to provider attributes
2024-07-30 00:04:54 -05:00

47 lines
1.1 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
export const useLocalStorage = (
key: string,
initialValue: string | boolean,
): [
string | boolean,
(
value: string | boolean | ((val: string | boolean) => string | boolean),
) => void,
] => {
const [state, setState] = useState<string | boolean>(initialValue);
useEffect(() => {
try {
const value = window.localStorage.getItem(key);
if (value) {
setState(JSON.parse(value));
}
} catch (error) {
console.log(error);
}
}, [key]);
const setValue = (
value: string | boolean | ((val: string | boolean) => string | boolean),
) => {
try {
// If the passed value is a callback function,
// then call it with the existing state.
const valueToStore =
typeof value === "function"
? (value as (val: string | boolean) => string | boolean)(state)
: value;
window.localStorage.setItem(key, JSON.stringify(valueToStore));
setState(valueToStore);
} catch (error) {
console.log(error);
}
};
return [state, setValue];
};