fix(sync): isolate installed sync state

This commit is contained in:
2026-05-19 02:03:29 +08:00
parent 753f91e837
commit 34551fce7b
6 changed files with 269 additions and 12 deletions
+19 -6
View File
@@ -1,26 +1,39 @@
import { ref, watch } from "vue";
const INSTALLED_SYNC_STORAGE_KEY = "spark-store-installed-sync-enabled";
let activeSyncUserId: number | null = null;
const readSyncEnabled = (): boolean | null => {
const savedValue = localStorage.getItem(INSTALLED_SYNC_STORAGE_KEY);
const syncStorageKey = (userId: number | null): string =>
userId === null
? INSTALLED_SYNC_STORAGE_KEY
: `${INSTALLED_SYNC_STORAGE_KEY}:${userId}`;
const readSyncEnabled = (userId: number | null): boolean | null => {
const savedValue = localStorage.getItem(syncStorageKey(userId));
if (savedValue === "true") return true;
if (savedValue === "false") return false;
return null;
};
export const installedSyncEnabled = ref<boolean | null>(readSyncEnabled());
export const installedSyncEnabled = ref<boolean | null>(
readSyncEnabled(activeSyncUserId),
);
export const loadInstalledSyncPreference = (userId: number | null): void => {
activeSyncUserId = userId;
installedSyncEnabled.value = readSyncEnabled(userId);
};
export const setInstalledSyncEnabled = (enabled: boolean): void => {
installedSyncEnabled.value = enabled;
localStorage.setItem(INSTALLED_SYNC_STORAGE_KEY, String(enabled));
localStorage.setItem(syncStorageKey(activeSyncUserId), String(enabled));
};
watch(installedSyncEnabled, (enabled) => {
if (enabled === null) {
localStorage.removeItem(INSTALLED_SYNC_STORAGE_KEY);
localStorage.removeItem(syncStorageKey(activeSyncUserId));
return;
}
localStorage.setItem(INSTALLED_SYNC_STORAGE_KEY, String(enabled));
localStorage.setItem(syncStorageKey(activeSyncUserId), String(enabled));
});