mirror of
https://gitee.com/spark-store-project/spark-store
synced 2026-06-22 14:13:49 +08:00
fix(sync): isolate installed sync state
This commit is contained in:
@@ -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));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user