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() { export function getHeaders() {
const accessStore = useAccessStore.getState(); const accessStore = useAccessStore.getState();
console.log("===== accessStore", accessStore);
const chatStore = useChatStore.getState(); const chatStore = useChatStore.getState();
const headers: Record<string, string> = { const headers: Record<string, string> = {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -247,7 +248,7 @@ export function getHeaders() {
ACCESS_CODE_PREFIX + accessStore.accessCode, ACCESS_CODE_PREFIX + accessStore.accessCode,
); );
} }
console.log("API accessStore.accessCode", accessStore.accessCode);
return headers; return headers;
} }

View File

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

View File

@ -2,6 +2,7 @@ import { getClientConfig } from "../config/client";
import { Updater } from "../typing"; import { Updater } from "../typing";
import { ApiPath, STORAGE_KEY, StoreKey } from "../constant"; import { ApiPath, STORAGE_KEY, StoreKey } from "../constant";
import { createPersistStore } from "../utils/store"; import { createPersistStore } from "../utils/store";
import { useAccessStore } from "../store";
import { import {
AppState, AppState,
getLocalAppState, getLocalAppState,
@ -91,28 +92,29 @@ export const useSyncStore = createPersistStore(
}, },
async sync() { async sync() {
const accessStore = useAccessStore.getState();
const localState = getLocalAppState(); const localState = getLocalAppState();
const provider = get().provider; const provider = get().provider;
const config = get()[provider]; const config = get()[provider];
const client = this.getClient(); const client = this.getClient();
console.log("===== accessStore", accessStore);
try { try {
const remoteState = await client.get(config.username); const remoteState = await client.get(config.username);
if (!remoteState || remoteState === "") { if (!remoteState || remoteState === "") {
await client.set(config.username, JSON.stringify(localState)); await client.set(config.username, JSON.stringify(localState));
console.log("[Sync] Remote state is empty, using local state instead."); console.log(
return "[Sync] Remote state is empty, using local state instead.",
);
return;
} else { } else {
const parsedRemoteState = JSON.parse( let parsedRemoteState = JSON.parse(await client.get(config.username));
await client.get(config.username), parsedRemoteState["access-control"] = accessStore;
) as AppState; console.log("parsedRemoteState------", parsedRemoteState);
mergeAppState(localState, parsedRemoteState); mergeAppState(localState, parsedRemoteState);
setLocalAppState(localState); setLocalAppState(localState);
} }
} catch (e) { } catch (e) {
console.log("[Sync] failed to get remote state", e); console.log("[Sync] failed to get remote state", e);
throw e; throw e;
@ -120,8 +122,6 @@ export const useSyncStore = createPersistStore(
await client.set(config.username, JSON.stringify(localState)); await client.set(config.username, JSON.stringify(localState));
this.markSyncTime(); this.markSyncTime();
}, },

View File

@ -119,14 +119,12 @@ const MergeStates: StateMerger = {
}; };
export function getLocalAppState() { export function getLocalAppState() {
const accessStore = useAccessStore();
const appState = Object.fromEntries( const appState = Object.fromEntries(
Object.entries(LocalStateGetters).map(([key, getter]) => { Object.entries(LocalStateGetters).map(([key, getter]) => {
return [key, getter()]; return [key, getter()];
}), }),
) as AppState; ) as AppState;
appState["access-control"].accessCode = accessStore.accessCode
return appState; return appState;
} }
@ -140,10 +138,10 @@ export function mergeAppState(localState: AppState, remoteState: AppState) {
Object.keys(localState).forEach(<T extends keyof AppState>(k: string) => { Object.keys(localState).forEach(<T extends keyof AppState>(k: string) => {
const key = k as T; const key = k as T;
//if(k !== ''){ //if(k !== ''){
console.log('mergeAppState',k) console.log("mergeAppState", k);
const localStoreState = localState[key]; const localStoreState = localState[key];
const remoteStoreState = remoteState[key]; const remoteStoreState = remoteState[key];
MergeStates[key](localStoreState, remoteStoreState); MergeStates[key](localStoreState, remoteStoreState);
//} //}
}); });