feat(ui): added api hooks and state hooks for dashboard

This commit is contained in:
akhilmhdh
2023-03-19 00:39:18 +05:30
parent 686d3c409d
commit b42c33107e
16 changed files with 588 additions and 26 deletions
+25
View File
@@ -0,0 +1,25 @@
import { useEffect, useState } from 'react';
type TPersisntentStateReturn<T extends unknown> = [T, (val: T) => void];
export const usePersistentState = <T extends unknown>(
initialValue: T,
persistenceKey: string
): TPersisntentStateReturn<T> => {
const [val, setVal] = useState<T>(initialValue);
useEffect(() => {
const temp = localStorage.getItem(persistenceKey);
if (temp) {
const { key } = JSON.parse(temp);
setVal(key);
}
}, []);
const setState = (state: T) => {
localStorage.setItem(persistenceKey, JSON.stringify({ key: state }));
setVal(state);
};
return [val, setState];
};