fixed:keep local accessCode

This commit is contained in:
cbmland 2024-07-13 20:15:42 +08:00
parent 119f0a94d2
commit c621662aae
4 changed files with 21 additions and 22 deletions

View File

@ -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<string, string> = {
"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;
}

View File

@ -94,7 +94,7 @@ export const useAccessStore = createPersistStore(
(set, get) => ({
enabledAccessControl() {
this.fetch();
return true;
return get().needCode;
},

View File

@ -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();
},

View File

@ -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(<T extends keyof AppState>(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);
//}
});