refactor: user-specific storage
This commit is contained in:
parent
75f49461de
commit
2bee673a14
3 changed files with 47 additions and 57 deletions
|
@ -1,4 +1,3 @@
|
|||
import type { Account } from 'masto'
|
||||
import { STORAGE_KEY_FEATURE_FLAGS } from '~/constants'
|
||||
|
||||
export interface FeatureFlags {
|
||||
|
@ -7,8 +6,6 @@ export interface FeatureFlags {
|
|||
}
|
||||
export type FeatureFlagsMap = Record<string, FeatureFlags>
|
||||
|
||||
export const allFeatureFlags = useLocalStorage<FeatureFlagsMap>(STORAGE_KEY_FEATURE_FLAGS, {}, { deep: true })
|
||||
|
||||
export function getDefaultFeatureFlags(): FeatureFlags {
|
||||
return {
|
||||
experimentalVirtualScroll: false,
|
||||
|
@ -16,17 +13,7 @@ export function getDefaultFeatureFlags(): FeatureFlags {
|
|||
}
|
||||
}
|
||||
|
||||
export const currentUserFeatureFlags = computed(() => {
|
||||
if (!currentUser.value?.account.id)
|
||||
return {} as FeatureFlags
|
||||
|
||||
const id = `${currentUser.value.account.acct}@${currentUser.value.server}`
|
||||
|
||||
if (!allFeatureFlags.value[id])
|
||||
allFeatureFlags.value[id] = getDefaultFeatureFlags()
|
||||
|
||||
return allFeatureFlags.value[id] as FeatureFlags
|
||||
})
|
||||
export const currentUserFeatureFlags = useUserLocalStorage(STORAGE_KEY_FEATURE_FLAGS, getDefaultFeatureFlags)
|
||||
|
||||
export function useFeatureFlags() {
|
||||
const featureFlags = currentUserFeatureFlags.value
|
||||
|
@ -42,18 +29,3 @@ export function toggleFeatureFlag(key: keyof FeatureFlags) {
|
|||
else
|
||||
featureFlags[key] = true
|
||||
}
|
||||
|
||||
export function clearUserFeatureFlags(account?: Account) {
|
||||
if (!account)
|
||||
account = currentUser.value?.account
|
||||
|
||||
if (!account)
|
||||
return
|
||||
|
||||
const id = `${account.acct}@${currentUser.value?.server}`
|
||||
if (!allFeatureFlags.value[id])
|
||||
return
|
||||
|
||||
delete allFeatureFlags.value[id]
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue