feat: toggleable feature flags (#209)
This commit is contained in:
parent
f08777f629
commit
69d009d02a
10 changed files with 111 additions and 12 deletions
57
composables/featureFlags.ts
Normal file
57
composables/featureFlags.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
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]
|
||||
}
|
||||
|
|
@ -73,6 +73,7 @@ export async function signout() {
|
|||
// Clear stale data
|
||||
delete servers.value[_currentUserId]
|
||||
clearUserDrafts()
|
||||
clearUserFeatureFlags()
|
||||
|
||||
// Remove the current user from the users
|
||||
users.value.splice(index, 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue