diff --git a/app/components/home.tsx b/app/components/home.tsx index 465ad0f1e..54cb1ea05 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -28,7 +28,9 @@ import { useAppConfig } from "../store/config"; import { AuthPage } from "./auth"; import { getClientConfig } from "../config/client"; import { type ClientApi, getClientApi } from "../client/api"; -import { useAccessStore } from "../store"; +import { useSyncStore } from "../store/sync"; +import { showToast } from "./ui-lib"; +import Locale from "@/app/locales"; export function Loading(props: { noLogo?: boolean }) { return ( @@ -224,12 +226,32 @@ export function Home() { useSwitchTheme(); useLoadData(); useHtmlLang(); + const syncStore = useSyncStore(); useEffect(() => { console.log("[Config] got config from build time", getClientConfig()); - useAccessStore.getState().fetch(); }, []); + useEffect(() => { + let running = true; + + setTimeout(async () => { + if (running && syncStore.cloudSync()) { + try { + await syncStore.sync(); + showToast(Locale.Settings.Sync.Success); + } catch (e: unknown) { + showToast(Locale.Settings.Sync.Fail); + console.error("[Sync]", e); + } + } + }); + + return () => { + running = false; + }; + }, [syncStore]); + if (!useHasHydrated()) { return ; } diff --git a/app/store/sync.ts b/app/store/sync.ts index 8477c1e4b..bbde212c0 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -27,18 +27,22 @@ const DEFAULT_SYNC_STATE = { useProxy: true, proxyUrl: ApiPath.Cors as string, - webdav: { + [ProviderType.WebDAV]: { endpoint: "", username: "", password: "", }, - upstash: { + [ProviderType.UpStash]: { endpoint: "", username: STORAGE_KEY, apiKey: "", }, + autoSync: { + onStart: true, + }, + lastSyncTime: 0, lastProvider: "", }; @@ -46,7 +50,7 @@ const DEFAULT_SYNC_STATE = { export const useSyncStore = createPersistStore( DEFAULT_SYNC_STATE, (set, get) => ({ - cloudSync() { + cloudSync(): boolean { const config = get()[get().provider]; return Object.values(config).every((c) => c.toString().length > 0); },