TaskView is a self-hosted project and task management platform focused on clarity, ownership, and control. TaskView is built for teams that want a transparent, self-hosted alternative to SaaS task managers.
TypeScript graphviewkanban-boardnodejsproductivityproject-managementself-hostedtasktask-managementtask-managertodoapp
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useDebounceFn } from '@vueuse/core'
import { UI_SETTINGS_KEY, type UiPreferences, type UiPreferencesItem, type UiSettings } from 'taskview-api'
import { $tvApi } from '@/plugins/axios'
const SAVE_DEBOUNCE_MS = 250
type Sections = Record<string, UiPreferencesItem[]>
export const useUiPreferencesStore = defineStore('uiPreferences', () => {
const prefs = ref<Sections>({})
const settings = ref<UiSettings>({})
const loaded = ref(false)
const loading = ref(false)
const saving = ref(false)
function split(blob: UiPreferences): { sections: Sections; settings: UiSettings } {
const { [UI_SETTINGS_KEY]: settingsEntry, ...sections } = blob
return {
sections: sections as Sections,
settings: (settingsEntry as UiSettings | undefined) ?? {},
}
}
let pending: Promise<void> | null = null
async function load() {
loading.value = true
try {
const result = await $tvApi.uiPreferences.fetch()
const { sections, settings: s } = split(result ?? {})
prefs.value = sections
settings.value = s
loaded.value = true
} finally {
loading.value = false
pending = null
}
}
function fetch(): Promise<void> {
pending ??= load()
return pending
}
async function ensureLoaded() {
if (loaded.value) return
await fetch().catch(() => undefined)
}
let revision = 0
let saveChain: Promise<void> = Promise.resolve()
async function save() {
saving.value = true
const sentRevision = revision
try {
const payload: UiPreferences = { ...prefs.value }
if (Object.keys(settings.value).length > 0) {
payload[UI_SETTINGS_KEY] = settings.value
}
const result = await $tvApi.uiPreferences.update(payload)
if (result && sentRevision === revision) {
const { sections, settings: s } = split(result)
prefs.value = sections
settings.value = s
}
} finally {
saving.value = false
}
}
const flush = useDebounceFn(() => {
saveChain = saveChain.then(save, save)
return saveChain
}, SAVE_DEBOUNCE_MS)
function setSection(section: string, items: UiPreferencesItem[]) {
prefs.value = { ...prefs.value, [section]: items }
revision++
flush()
}
function getSection(section: string): UiPreferencesItem[] {
return prefs.value[section] ?? []
}
function setSetting<K extends keyof UiSettings>(key: K, value: UiSettings[K]) {
settings.value = { ...settings.value, [key]: value }
revision++
flush()
}
return {
prefs,
settings,
loaded,
loading,
saving,
fetch,
ensureLoaded,
setSection,
getSection,
setSetting,
}
})