okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
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 { 10desktop :boolean ; 11tray :boolean ; 12port :number ; 13defaultPort :number ; 14pinnedPort :number | null ; 15minimizeToTray :boolean ; 16portNotice :{ wanted :number ; used :number } | null ; 17} 18 19export function useShellState () :[ ShellState | null , ( patch :{ port ?:number | null ; tray ?:boolean }) => Promise < string | null >] { 20const [ shell , setShell ] = useState < ShellState | null >( null ); 21useEffect (() => { 22let alive = true ; 23api . get < ShellState >( '/api/shell' ). then ( s => { if ( alive ) setShell ( s ); }). catch (() => undefined ); 24return () => { alive = false ; }; 25}, []); 26const save = async ( patch :{ port ?:number | null ; tray ?:boolean }) => { 27try { setShell ( await api . post < ShellState >( '/api/shell/prefs' , patch )); return null ; } 28catch ( error ) { return error instanceof Error ?error . message :'The choice was not saved.' ; } 29}; 30return [ shell , save ]; 31} 32 33/** The sound for finished work (#3586): off until switched on, with a button to hear it. */ 34export function DoneSoundSetting () { 35const on = useStore ( s => s . doneSound ); 36return < 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 () { 44const on = useStore ( s => s . approvalNotify ); 45return < button className = "st-toggle" role = "switch" aria-checked = { on } aria-label = { tr ( "Notify me when Strata needs an answer" )} 46onClick = {() => { 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 > }) { 51return < 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 > }) { 56const [ text , setText ] = useState ( shell . pinnedPort ?String ( shell . pinnedPort ) :'' ), [ problem , setProblem ] = useState ( '' ); 57useEffect (() => { setText ( shell . pinnedPort ?String ( shell . pinnedPort ) :'' ); }, [ shell . pinnedPort ]); 58const commit = async () => { 59const trimmed = text . trim (); 60if ( trimmed === ( shell . pinnedPort ?String ( shell . pinnedPort ) :'' )) { setProblem ( '' ); return ; } 61if ( trimmed === '' ) { setProblem (( await save ({ port :null })) ?? '' ); return ; } 62const value = Number ( trimmed ); 63if ( ! Number . isInteger ( value ) || value < 1024 || value > 65535 ) { setProblem ( tr ( "Choose a port between 1024 and 65535, or leave it empty." )); return ; } 64setProblem (( await save ({ port :value })) ?? '' ); 65}; 66return < 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 } 69onChange = { 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}