diff --git a/app/client/api.ts b/app/client/api.ts index c0c71480c..a874c9223 100644 --- a/app/client/api.ts +++ b/app/client/api.ts @@ -170,6 +170,7 @@ export class ClientApi { export function getHeaders() { const accessStore = useAccessStore.getState(); + console.log("===== accessStore", accessStore); const chatStore = useChatStore.getState(); const headers: Record = { "Content-Type": "application/json", @@ -247,7 +248,7 @@ export function getHeaders() { ACCESS_CODE_PREFIX + accessStore.accessCode, ); } - + console.log("API accessStore.accessCode", accessStore.accessCode); return headers; } diff --git a/app/store/access.ts b/app/store/access.ts index 26359e55c..29255bc6c 100644 --- a/app/store/access.ts +++ b/app/store/access.ts @@ -94,7 +94,7 @@ export const useAccessStore = createPersistStore( (set, get) => ({ enabledAccessControl() { this.fetch(); - + return true; return get().needCode; }, diff --git a/app/store/sync.ts b/app/store/sync.ts index 25fbe91f2..ff37f2f9f 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -2,6 +2,7 @@ import { getClientConfig } from "../config/client"; import { Updater } from "../typing"; import { ApiPath, STORAGE_KEY, StoreKey } from "../constant"; import { createPersistStore } from "../utils/store"; +import { useAccessStore } from "../store"; import { AppState, getLocalAppState, @@ -56,7 +57,7 @@ export const useSyncStore = createPersistStore( markSyncTime() { set({ lastSyncTime: Date.now(), lastProvider: get().provider }); }, - + export() { const state = getLocalAppState(); const datePart = isApp @@ -89,30 +90,31 @@ export const useSyncStore = createPersistStore( const client = createSyncClient(provider, get()); return client; }, - + async sync() { + const accessStore = useAccessStore.getState(); const localState = getLocalAppState(); const provider = get().provider; const config = get()[provider]; const client = this.getClient(); - - - - + console.log("===== accessStore", accessStore); + try { const remoteState = await client.get(config.username); if (!remoteState || remoteState === "") { await client.set(config.username, JSON.stringify(localState)); - console.log("[Sync] Remote state is empty, using local state instead."); - return + console.log( + "[Sync] Remote state is empty, using local state instead.", + ); + return; } else { - const parsedRemoteState = JSON.parse( - await client.get(config.username), - ) as AppState; + let parsedRemoteState = JSON.parse(await client.get(config.username)); + parsedRemoteState["access-control"] = accessStore; + console.log("parsedRemoteState------", parsedRemoteState); mergeAppState(localState, parsedRemoteState); setLocalAppState(localState); - } + } } catch (e) { console.log("[Sync] failed to get remote state", e); throw e; @@ -120,8 +122,6 @@ export const useSyncStore = createPersistStore( await client.set(config.username, JSON.stringify(localState)); - - this.markSyncTime(); }, diff --git a/app/utils/sync.ts b/app/utils/sync.ts index 62939ff4a..e7cbaad44 100644 --- a/app/utils/sync.ts +++ b/app/utils/sync.ts @@ -119,14 +119,12 @@ const MergeStates: StateMerger = { }; export function getLocalAppState() { - const accessStore = useAccessStore(); const appState = Object.fromEntries( Object.entries(LocalStateGetters).map(([key, getter]) => { return [key, getter()]; }), ) as AppState; - appState["access-control"].accessCode = accessStore.accessCode return appState; } @@ -140,10 +138,10 @@ export function mergeAppState(localState: AppState, remoteState: AppState) { Object.keys(localState).forEach((k: string) => { const key = k as T; //if(k !== ''){ - console.log('mergeAppState',k) - const localStoreState = localState[key]; - const remoteStoreState = remoteState[key]; - MergeStates[key](localStoreState, remoteStoreState); + console.log("mergeAppState", k); + const localStoreState = localState[key]; + const remoteStoreState = remoteState[key]; + MergeStates[key](localStoreState, remoteStoreState); //} });