From 1cccaa2e804a875697e7ffe6e329a33a45c65daa Mon Sep 17 00:00:00 2001 From: JiangYingjin Date: Sun, 2 Mar 2025 12:25:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=8C=E6=AD=A5=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/settings.tsx | 15 ++++++++++ app/locales/cn.ts | 1 + app/locales/en.ts | 1 + app/locales/fr.ts | 1 + app/locales/it.ts | 1 + app/locales/pt.ts | 1 + app/store/sync.ts | 20 ++++++++----- app/utils/sync.ts | 56 ++++++++++++++++++------------------- 8 files changed, 61 insertions(+), 35 deletions(-) diff --git a/app/components/settings.tsx b/app/components/settings.tsx index a74ff17b1..6c193702c 100644 --- a/app/components/settings.tsx +++ b/app/components/settings.tsx @@ -528,6 +528,21 @@ function SyncItems() { setShowSyncConfigModal(true); }} /> + {couldSync && ( + } + text={Locale.UI.Overwrite} + onClick={async () => { + try { + await syncStore.overwrite(); + showToast(Locale.Settings.Sync.Success); + } catch (e) { + showToast(Locale.Settings.Sync.Fail); + console.error("[Sync]", e); + } + }} + /> + )} {couldSync && ( } diff --git a/app/locales/cn.ts b/app/locales/cn.ts index 47be019a8..4d75b529d 100644 --- a/app/locales/cn.ts +++ b/app/locales/cn.ts @@ -757,6 +757,7 @@ const cn = { Export: "导出", Import: "导入", Sync: "同步", + Overwrite: "覆盖", Config: "配置", }, Exporter: { diff --git a/app/locales/en.ts b/app/locales/en.ts index fddb6f091..3ae1eb208 100644 --- a/app/locales/en.ts +++ b/app/locales/en.ts @@ -762,6 +762,7 @@ const en: LocaleType = { Edit: "Edit", Export: "Export", Import: "Import", + Overwrite: "Overwrite", Sync: "Sync", Config: "Config", }, diff --git a/app/locales/fr.ts b/app/locales/fr.ts index d25c60eb6..5157bedca 100644 --- a/app/locales/fr.ts +++ b/app/locales/fr.ts @@ -589,6 +589,7 @@ const fr: PartialLocaleType = { Edit: "Modifier", Export: "Exporter", Import: "Importer", + Overwrite: "Remplacer", Sync: "Synchroniser", Config: "Configurer", }, diff --git a/app/locales/it.ts b/app/locales/it.ts index 59bc1eb15..8ea29129e 100644 --- a/app/locales/it.ts +++ b/app/locales/it.ts @@ -590,6 +590,7 @@ const it: PartialLocaleType = { Edit: "Modifica", Export: "Esporta", Import: "Importa", + Overwrite: "Sostituisci", Sync: "Sincronizza", Config: "Configura", }, diff --git a/app/locales/pt.ts b/app/locales/pt.ts index 152f50228..29afa149a 100644 --- a/app/locales/pt.ts +++ b/app/locales/pt.ts @@ -505,6 +505,7 @@ const pt: PartialLocaleType = { Edit: "Editar", Export: "Exportar", Import: "Importar", + Overwrite: "Substituir", Sync: "Sincronizar", Config: "Configurar", }, diff --git a/app/store/sync.ts b/app/store/sync.ts index 8477c1e4b..105e3e83e 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -28,7 +28,7 @@ const DEFAULT_SYNC_STATE = { proxyUrl: ApiPath.Cors as string, webdav: { - endpoint: "", + endpoint: "https://dav.jyj.cx", username: "", password: "", }, @@ -88,7 +88,7 @@ export const useSyncStore = createPersistStore( return client; }, - async sync() { + async sync(overwrite = false) { const localState = getLocalAppState(); const provider = get().provider; const config = get()[provider]; @@ -103,11 +103,13 @@ export const useSyncStore = createPersistStore( ); return; } else { - const parsedRemoteState = JSON.parse( - await client.get(config.username), - ) as AppState; - mergeAppState(localState, parsedRemoteState); - setLocalAppState(localState); + if (!overwrite) { + const parsedRemoteState = JSON.parse( + await client.get(config.username), + ) as AppState; + mergeAppState(localState, parsedRemoteState); + setLocalAppState(localState); + } } } catch (e) { console.log("[Sync] failed to get remote state", e); @@ -119,6 +121,10 @@ export const useSyncStore = createPersistStore( this.markSyncTime(); }, + async overwrite() { + await this.sync(true); + }, + async check() { const client = this.getClient(); return await client.check(); diff --git a/app/utils/sync.ts b/app/utils/sync.ts index 1acfc1289..eb119b3a3 100644 --- a/app/utils/sync.ts +++ b/app/utils/sync.ts @@ -1,11 +1,11 @@ import { ChatSession, - useAccessStore, - useAppConfig, + // useAccessStore, + // useAppConfig, useChatStore, } from "../store"; -import { useMaskStore } from "../store/mask"; -import { usePromptStore } from "../store/prompt"; +// import { useMaskStore } from "../store/mask"; +// import { usePromptStore } from "../store/prompt"; import { StoreKey } from "../constant"; import { merge } from "./merge"; @@ -32,18 +32,18 @@ export type GetStoreState = T extends { getState: () => infer U } const LocalStateSetters = { [StoreKey.Chat]: useChatStore.setState, - [StoreKey.Access]: useAccessStore.setState, - [StoreKey.Config]: useAppConfig.setState, - [StoreKey.Mask]: useMaskStore.setState, - [StoreKey.Prompt]: usePromptStore.setState, + // [StoreKey.Access]: useAccessStore.setState, + // [StoreKey.Config]: useAppConfig.setState, + // [StoreKey.Mask]: useMaskStore.setState, + // [StoreKey.Prompt]: usePromptStore.setState, } as const; const LocalStateGetters = { [StoreKey.Chat]: () => getNonFunctionFileds(useChatStore.getState()), - [StoreKey.Access]: () => getNonFunctionFileds(useAccessStore.getState()), - [StoreKey.Config]: () => getNonFunctionFileds(useAppConfig.getState()), - [StoreKey.Mask]: () => getNonFunctionFileds(useMaskStore.getState()), - [StoreKey.Prompt]: () => getNonFunctionFileds(usePromptStore.getState()), + // [StoreKey.Access]: () => getNonFunctionFileds(useAccessStore.getState()), + // [StoreKey.Config]: () => getNonFunctionFileds(useAppConfig.getState()), + // [StoreKey.Mask]: () => getNonFunctionFileds(useMaskStore.getState()), + // [StoreKey.Prompt]: () => getNonFunctionFileds(usePromptStore.getState()), } as const; export type AppState = { @@ -100,22 +100,22 @@ const MergeStates: StateMerger = { return localState; }, - [StoreKey.Prompt]: (localState, remoteState) => { - localState.prompts = { - ...remoteState.prompts, - ...localState.prompts, - }; - return localState; - }, - [StoreKey.Mask]: (localState, remoteState) => { - localState.masks = { - ...remoteState.masks, - ...localState.masks, - }; - return localState; - }, - [StoreKey.Config]: mergeWithUpdate, - [StoreKey.Access]: mergeWithUpdate, + // [StoreKey.Prompt]: (localState, remoteState) => { + // localState.prompts = { + // ...remoteState.prompts, + // ...localState.prompts, + // }; + // return localState; + // }, + // [StoreKey.Mask]: (localState, remoteState) => { + // localState.masks = { + // ...remoteState.masks, + // ...localState.masks, + // }; + // return localState; + // }, + // [StoreKey.Config]: mergeWithUpdate, + // [StoreKey.Access]: mergeWithUpdate, }; export function getLocalAppState() {