okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-app: GitHub app and Terminal items for 0.2.73cabb9e3f

main
4.2 KiB74 linesraw
1import { useEffect, useState } from 'react';
2import { api } from '../lib/client';
3import { tr } from '../lib/i18n';
4import { askNotificationPermission, playDoneSound } from '../lib/attention';
5import { setApprovalNotify, setDoneSound, useStore } from '../lib/store';
6import { Btn } from './ui';
7
8/** What the host says about the desktop shell (GET /api/shell): null in a browser or while it loads. */
9export interface ShellState {
10    desktop: boolean;
11    tray: boolean;
12    port: number;
13    defaultPort: number;
14    pinnedPort: number | null;
15    minimizeToTray: boolean;
16    portNotice: { wanted: number; used: number } | null;
17}
18
19export function useShellState(): [ShellState | null, (patch: { port?: number | null; tray?: boolean }) => Promise<string | null>] {
20    const [shell, setShell] = useState<ShellState | null>(null);
21    useEffect(() => {
22        let alive = true;
23        api.get<ShellState>('/api/shell').then(s => { if (alive) setShell(s); }).catch(() => undefined);
24        return () => { alive = false; };
25    }, []);
26    const save = async (patch: { port?: number | null; tray?: boolean }) => {
27        try { setShell(await api.post<ShellState>('/api/shell/prefs', patch)); return null; }
28        catch (error) { return error instanceof Error ? error.message : 'The choice was not saved.'; }
29    };
30    return [shell, save];
31}
32
33/** The sound for finished work (#3586): off until switched on, with a button to hear it. */
34export function DoneSoundSetting() {
35    const on = useStore(s => s.doneSound);
36    return <span className="row">
37        <Btn size="sm" onClick={() => playDoneSound()}>{tr("Play sound")}</Btn>
38        <button className="st-toggle" role="switch" aria-checked={on} aria-label={tr("Sound when work finishes")} onClick={() => setDoneSound(!on)}/>
39    </span>;
40}
41
42/** A desktop notification when Manual mode waits for an approval (#3477). */
43export function ApprovalNotifySetting() {
44    const on = useStore(s => s.approvalNotify);
45    return <button className="st-toggle" role="switch" aria-checked={on} aria-label={tr("Notify me when Strata needs an answer")}
46        onClick={() => { if (!on) void askNotificationPermission(); setApprovalNotify(!on); }}/>;
47}
48
49/** Keep the app running in the tray when its window is closed (#3477). Desktop app on Windows and macOS only. */
50export function TraySetting({ shell, save }: { shell: ShellState; save: (patch: { tray: boolean }) => Promise<string | null> }) {
51    return <button className="st-toggle" role="switch" aria-checked={shell.minimizeToTray} aria-label={tr("Minimise to tray")} onClick={() => void save({ tray: !shell.minimizeToTray })}/>;
52}
53
54/** The port the app's window and local address use (#3402). Empty lets Strata choose; the choice applies the next time Strata starts. */
55export function PortSetting({ shell, save }: { shell: ShellState; save: (patch: { port: number | null }) => Promise<string | null> }) {
56    const [text, setText] = useState(shell.pinnedPort ? String(shell.pinnedPort) : ''), [problem, setProblem] = useState('');
57    useEffect(() => { setText(shell.pinnedPort ? String(shell.pinnedPort) : ''); }, [shell.pinnedPort]);
58    const commit = async () => {
59        const trimmed = text.trim();
60        if (trimmed === (shell.pinnedPort ? String(shell.pinnedPort) : '')) { setProblem(''); return; }
61        if (trimmed === '') { setProblem((await save({ port: null })) ?? ''); return; }
62        const value = Number(trimmed);
63        if (!Number.isInteger(value) || value < 1024 || value > 65535) { setProblem(tr("Choose a port between 1024 and 65535, or leave it empty.")); return; }
64        setProblem((await save({ port: value })) ?? '');
65    };
66    return <span className="stack">
67        <span className="row">
68            <input className="st-input" inputMode="numeric" size={7} aria-label={tr("Port (empty = automatic)")} placeholder={String(shell.defaultPort)} value={text}
69                onChange={e => setText(e.target.value.replace(/[^0-9]/g, '').slice(0, 5))} onBlur={() => void commit()} onKeyDown={e => { if (e.key === 'Enter') void commit(); }}/>
70        </span>
71        <span className="xs muted">{tr("Using port {0} now.", [shell.port])}</span>
72        {problem ? <span className="xs" role="alert">{problem}</span> : null}
73    </span>;
74}