import { createPortal } from 'react-dom' import { useEffect, useState } from 'react' import { useQuery, useQueryClient } from '@tanstack/react-query' import { api } from '../api' /** * Settings → Konduktor: preferences about the app itself rather than about a * library or a feature (those have their own entries in the gear menu). Each * setting is a pref in userprefs.json, written as soon as it is changed. */ /** The prefs this dialog owns, and what each is when never set. */ export const APP_SETTING_DEFAULTS = { expandPlaylistFolders: false, /** Read by the backend when an export runs (`main.run_export`). */ exportRootFolder: false, } type AppSettingKey = keyof typeof APP_SETTING_DEFAULTS /** One app setting, read from the shared ['prefs'] query (the default until it loads). */ export function useAppSetting(key: K): (typeof APP_SETTING_DEFAULTS)[K] { const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }).data const v = prefs?.[key] return typeof v === typeof APP_SETTING_DEFAULTS[key] ? (v as (typeof APP_SETTING_DEFAULTS)[K]) : APP_SETTING_DEFAULTS[key] } export function AppSettings({ onClose }: { onClose: () => void }) { const qc = useQueryClient() const expandFolders = useAppSetting('expandPlaylistFolders') const exportRootFolder = useAppSetting('exportRootFolder') const [error, setError] = useState(null) useEffect(() => { const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose() window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [onClose]) const set = async (patch: Partial) => { setError(null) try { qc.setQueryData(['prefs'], await api.patchPrefs(patch)) } catch (e) { setError((e as Error).message) } } return createPortal(
e.stopPropagation()}>

Konduktor settings

Sidebar

Exports

{error &&
{error}
}
, document.body, ) }