mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 23:47:06 +00:00
feat(ui): added dynamic secret and lease api hook
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { Dispatch, SetStateAction, useEffect, useState } from "react";
|
||||
|
||||
type Props<T extends unknown> = {
|
||||
initialState: T;
|
||||
delay?: number;
|
||||
};
|
||||
|
||||
// this hook is used when you need to reset the state to previous one after a particular time
|
||||
// usecase#1: To make copy to copied and back to copy in clipboard operation
|
||||
export const useTimedReset = <T extends string | number | boolean>({
|
||||
delay = 2000,
|
||||
initialState
|
||||
}: Props<T>): [T, boolean, Dispatch<SetStateAction<T>>] => {
|
||||
const [state, setState] = useState<T>(initialState);
|
||||
useEffect(() => {
|
||||
let timer: NodeJS.Timeout;
|
||||
if (state !== initialState) {
|
||||
timer = setTimeout(() => setState(initialState), delay);
|
||||
}
|
||||
return () => clearTimeout(timer);
|
||||
}, [state]);
|
||||
|
||||
// state, isChaning, setState
|
||||
return [state, state !== initialState, setState];
|
||||
};
|
||||
Reference in New Issue
Block a user