okami.mom/strata

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

Niko1221merge f-split-ui4 a7cd2b38e into f-split-int7c4b3d743

main
2.7 KiB37 linesraw
1import { tr } from '../lib/i18n';
2import { useCallback, useEffect, useState } from 'react';
3import { Btn, Icon } from './ui';
4import { answerNotice, tunePresetAction, tuneOff, tuneStatus, type TuneStatus } from '../lib/native-tune';
5import { useNative } from '../lib/native';
6
7/**
8 * Which saved tuning a start will use, in plain words, and a way to switch it off (back to the standard settings) or on again.
9 * Shown on the Models screen so nobody has to guess what runs after an unload or a restart. The tuning is applied by Strata at every
10 * start (see strata_control/presets.py); this only reads it and flips it. Layout is the UI worker's: it is one line of text and one button.
11 */
12export function NativeTuneBadge({ model, resourceProfile, vision }: { model: string; resourceProfile: 'work' | 'performance'; vision: string }) {
13    const native = useNative();
14    const [status, setStatus] = useState<TuneStatus | null>(null), [busy, setBusy] = useState(false), [error, setError] = useState('');
15    const jobs = native?.jobs.filter(j => j.state === 'running').length ?? 0;
16    const read = useCallback(async () => {
17        try { setStatus(await tuneStatus(model, resourceProfile, vision)); setError(''); }
18        catch { setStatus(null); }
19    }, [model, resourceProfile, vision]);
20    useEffect(() => { void read(); }, [read, jobs, native?.instances.length]);
21    async function flip(fn: () => Promise<unknown>) {
22        setBusy(true); setError('');
23        try { await fn(); await read(); }
24        catch (e) { setError(e instanceof Error ? e.message : String(e)); }
25        finally { setBusy(false); }
26    }
27    if (!status || (!status.active && !status.last_active)) return null;
28    const active = status.active;
29    return <p className="native-tune-line" data-tuning-active={!!active} role="status"><Icon name="gauge" size={15}/><span>
30        {active ? <>{tr("Using your tuning '{0}'", [active.name])}{status.state === 'hard' ? <> {'·'} {tr("This PC changed since it was tuned, so these settings are not used until you tune again.")}</> : null}
31            {active.answers && active.answers.class !== 'exact' ? <> {'·'} {tr(answerNotice(active.answers.class, active.answers.default_repeatable))}</> : null}{' '}
32            <Btn size="sm" variant="ghost" disabled={busy} onClick={() => void flip(() => tuneOff(model, resourceProfile, vision))}>{tr("Switch off")}</Btn></>
33            : <>{tr("Your tuning is off. The standard settings are used.")}{' '}
34            <Btn size="sm" variant="ghost" disabled={busy} onClick={() => void flip(() => tunePresetAction(status.last_active!.id, 'apply'))}>{tr("Switch on")}</Btn></>}
35        {error ? <span className="inline-error" role="alert"> {error}</span> : null}
36    </span></p>;
37}