更新同步模块

This commit is contained in:
JiangYingjin 2025-03-02 12:25:33 +08:00
parent d08af47342
commit 1cccaa2e80
8 changed files with 61 additions and 35 deletions

View File

@ -528,6 +528,21 @@ function SyncItems() {
setShowSyncConfigModal(true);
}}
/>
{couldSync && (
<IconButton
icon={<UploadIcon />}
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 && (
<IconButton
icon={<ResetIcon />}

View File

@ -757,6 +757,7 @@ const cn = {
Export: "导出",
Import: "导入",
Sync: "同步",
Overwrite: "覆盖",
Config: "配置",
},
Exporter: {

View File

@ -762,6 +762,7 @@ const en: LocaleType = {
Edit: "Edit",
Export: "Export",
Import: "Import",
Overwrite: "Overwrite",
Sync: "Sync",
Config: "Config",
},

View File

@ -589,6 +589,7 @@ const fr: PartialLocaleType = {
Edit: "Modifier",
Export: "Exporter",
Import: "Importer",
Overwrite: "Remplacer",
Sync: "Synchroniser",
Config: "Configurer",
},

View File

@ -590,6 +590,7 @@ const it: PartialLocaleType = {
Edit: "Modifica",
Export: "Esporta",
Import: "Importa",
Overwrite: "Sostituisci",
Sync: "Sincronizza",
Config: "Configura",
},

View File

@ -505,6 +505,7 @@ const pt: PartialLocaleType = {
Edit: "Editar",
Export: "Exportar",
Import: "Importar",
Overwrite: "Substituir",
Sync: "Sincronizar",
Config: "Configurar",
},

View File

@ -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,12 +103,14 @@ export const useSyncStore = createPersistStore(
);
return;
} else {
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);
throw 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();

View File

@ -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> = 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<AppState[StoreKey.Config]>,
[StoreKey.Access]: mergeWithUpdate<AppState[StoreKey.Access]>,
// [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<AppState[StoreKey.Config]>,
// [StoreKey.Access]: mergeWithUpdate<AppState[StoreKey.Access]>,
};
export function getLocalAppState() {