mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-16 17:30:06 +00:00
feat(ui): added api hooks and state hooks for dashboard
This commit is contained in:
@@ -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];
|
||||
};
|
||||
Reference in New Issue
Block a user