run cloud sync on startup

This commit is contained in:
Wang Guan 2024-10-19 18:58:35 +09:00
parent 06f897f32f
commit 93ad218d68
2 changed files with 31 additions and 5 deletions

View File

@ -28,7 +28,9 @@ import { useAppConfig } from "../store/config";
import { AuthPage } from "./auth"; import { AuthPage } from "./auth";
import { getClientConfig } from "../config/client"; import { getClientConfig } from "../config/client";
import { type ClientApi, getClientApi } from "../client/api"; 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 }) { export function Loading(props: { noLogo?: boolean }) {
return ( return (
@ -224,12 +226,32 @@ export function Home() {
useSwitchTheme(); useSwitchTheme();
useLoadData(); useLoadData();
useHtmlLang(); useHtmlLang();
const syncStore = useSyncStore();
useEffect(() => { useEffect(() => {
console.log("[Config] got config from build time", getClientConfig()); 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()) { if (!useHasHydrated()) {
return <Loading />; return <Loading />;
} }

View File

@ -27,18 +27,22 @@ const DEFAULT_SYNC_STATE = {
useProxy: true, useProxy: true,
proxyUrl: ApiPath.Cors as string, proxyUrl: ApiPath.Cors as string,
webdav: { [ProviderType.WebDAV]: {
endpoint: "", endpoint: "",
username: "", username: "",
password: "", password: "",
}, },
upstash: { [ProviderType.UpStash]: {
endpoint: "", endpoint: "",
username: STORAGE_KEY, username: STORAGE_KEY,
apiKey: "", apiKey: "",
}, },
autoSync: {
onStart: true,
},
lastSyncTime: 0, lastSyncTime: 0,
lastProvider: "", lastProvider: "",
}; };
@ -46,7 +50,7 @@ const DEFAULT_SYNC_STATE = {
export const useSyncStore = createPersistStore( export const useSyncStore = createPersistStore(
DEFAULT_SYNC_STATE, DEFAULT_SYNC_STATE,
(set, get) => ({ (set, get) => ({
cloudSync() { cloudSync(): boolean {
const config = get()[get().provider]; const config = get()[get().provider];
return Object.values(config).every((c) => c.toString().length > 0); return Object.values(config).every((c) => c.toString().length > 0);
}, },