pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
at main 26 lines 733 B view raw
1import { useRef } from "react"; 2import { useAsync, useInterval } from "react-use"; 3 4import { useAuth } from "@/hooks/auth/useAuth"; 5import { useAuthStore } from "@/stores/auth"; 6 7const AUTH_CHECK_INTERVAL = 12 * 60 * 60 * 1000; 8 9export function useAuthRestore() { 10 const { account } = useAuthStore(); 11 const { restore } = useAuth(); 12 const hasRestored = useRef(false); 13 14 useInterval(() => { 15 if (account) restore(account); 16 }, AUTH_CHECK_INTERVAL); 17 18 const result = useAsync(async () => { 19 if (hasRestored.current || !account) return; 20 await restore(account).finally(() => { 21 hasRestored.current = true; 22 }); 23 }, []); // no deps because we don't want to it ever rerun after the first time 24 25 return result; 26}