mirror of
https://github.com/p-stream/p-stream.git
synced 2026-04-05 06:59:44 +00:00
- No saves happen until backend settings are loaded and applied - Automatic syncers wait for settings to be loaded before syncing
31 lines
879 B
TypeScript
31 lines
879 B
TypeScript
import { useRef } from "react";
|
|
import { useAsync, useInterval } from "react-use";
|
|
|
|
import { useAuth } from "@/hooks/auth/useAuth";
|
|
import { useAuthStore } from "@/stores/auth";
|
|
|
|
const AUTH_CHECK_INTERVAL = 12 * 60 * 60 * 1000;
|
|
|
|
export function useAuthRestore() {
|
|
const { account } = useAuthStore();
|
|
const { restore } = useAuth();
|
|
const setSettingsLoading = useAuthStore((s) => s.setSettingsLoading);
|
|
const hasRestored = useRef(false);
|
|
|
|
useInterval(() => {
|
|
if (account) restore(account);
|
|
}, AUTH_CHECK_INTERVAL);
|
|
|
|
const result = useAsync(async () => {
|
|
if (hasRestored.current || !account) return;
|
|
setSettingsLoading(true);
|
|
try {
|
|
await restore(account);
|
|
} finally {
|
|
setSettingsLoading(false);
|
|
hasRestored.current = true;
|
|
}
|
|
}, []); // no deps because we don't want to it ever rerun after the first time
|
|
|
|
return result;
|
|
}
|