import { watchImmediate } from "@vueuse/core"; import { type Ref, ref, type UnwrapRef } from "vue"; import { syncRefToLocalStorage } from "./persistentRef"; /** * Local storage composable specific to current user. * @param key * @param initialValue */ export function useUserLocalStorageFromHashId( key: string, initialValue: T, hashedUserId: Ref, ): Ref> { const refToSync = ref(initialValue); let hasSynced = false; watchImmediate( () => hashedUserId.value, () => { if (hashedUserId.value && !hasSynced) { syncRefToLocalStorage(`${key}-${hashedUserId.value}`, refToSync); hasSynced = true; } }, ); return refToSync; }