import { useEffect, useState } from 'react'; import { api } from '../lib/client'; import { tr } from '../lib/i18n'; import { askNotificationPermission, playDoneSound } from '../lib/attention'; import { setApprovalNotify, setDoneSound, useStore } from '../lib/store'; import { Btn } from './ui'; /** What the host says about the desktop shell (GET /api/shell): null in a browser or while it loads. */ export interface ShellState { desktop: boolean; tray: boolean; port: number; defaultPort: number; pinnedPort: number | null; minimizeToTray: boolean; portNotice: { wanted: number; used: number } | null; } export function useShellState(): [ShellState | null, (patch: { port?: number | null; tray?: boolean }) => Promise] { const [shell, setShell] = useState(null); useEffect(() => { let alive = true; api.get('/api/shell').then(s => { if (alive) setShell(s); }).catch(() => undefined); return () => { alive = false; }; }, []); const save = async (patch: { port?: number | null; tray?: boolean }) => { try { setShell(await api.post('/api/shell/prefs', patch)); return null; } catch (error) { return error instanceof Error ? error.message : 'The choice was not saved.'; } }; return [shell, save]; } /** The sound for finished work (#3586): off until switched on, with a button to hear it. */ export function DoneSoundSetting() { const on = useStore(s => s.doneSound); return playDoneSound()}>{tr("Play sound")}