1
0
mirror of https://github.com/elk-zone/elk synced 2024-12-12 05:38:04 +09:00
elk/composables/featureFlags.ts

58 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-11-29 07:57:27 +09:00
import type { Account } from 'masto'
import { STORAGE_KEY_FEATURE_FLAGS } from '~/constants'
export interface FeatureFlags {
experimentalVirtualScroll: boolean
}
export type FeatureFlagsMap = Record<string, FeatureFlags>
export const allFeatureFlags = useLocalStorage<FeatureFlagsMap>(STORAGE_KEY_FEATURE_FLAGS, {}, { deep: true })
export function getDefaultFeatureFlags(): FeatureFlags {
return {
experimentalVirtualScroll: false,
}
}
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 function useFeatureFlags() {
const featureFlags = currentUserFeatureFlags.value
return featureFlags
}
export function toggleFeatureFlag(key: keyof FeatureFlags) {
const featureFlags = currentUserFeatureFlags.value
if (featureFlags[key])
featureFlags[key] = !featureFlags[key]
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]
}