okami.mom/strata

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

Niko1221rc1 merge ds-coder (d6a5a7837)4fb068a8c

main
1.6 KiB31 linesraw
1import { useEffect } from 'react';
2import { api } from '../lib/client';
3import { useStore } from '../lib/store';
4import { PcFitCard, type PcLive } from './PcFitCard';
5
6const tenth = (n: number | null | undefined) => typeof n === 'number' && Number.isFinite(n) ? Math.round(n * 10) / 10 : null;
7
8/**
9 * "This computer" with the host's own readings: while the Models page is open the host reads the card every few seconds (the same ask the
10 * Monitor makes), and the free graphics memory and RAM come from those readings, not from a guess. It subscribes to the three numbers only,
11 * so the Models page itself does not redraw on every reading.
12 */
13export function PcComputer(props: Omit<React.ComponentProps<typeof PcFitCard>, 'live'> & { vramAllGb?: number }) {
14  const vramUsed = useStore(s => tenth(s.monitor?.hardware?.vramUsedGb));
15  const ramUsed = useStore(s => tenth(s.monitor?.hardware?.ramUsedGb));
16  const diskRead = useStore(s => tenth(s.monitor?.hardware?.diskReadGbs));
17  useEffect(() => {
18    const ask = () => { if (typeof document === 'undefined' || document.visibilityState !== 'hidden') void api.get('/api/monitor').catch(() => undefined); };
19    ask();
20    const timer = setInterval(ask, 10000);
21    return () => clearInterval(timer);
22  }, []);
23  const vramTotal = props.vramAllGb ?? props.vramGb;
24  const live: PcLive = {
25    vramTotalGb: vramTotal,
26    freeVramGb: vramUsed !== null && vramTotal > 0 ? Math.max(0, Math.round((vramTotal - vramUsed) * 10) / 10) : null,
27    freeRamGb: ramUsed !== null && props.ramGb ? Math.max(0, Math.round((props.ramGb - ramUsed) * 10) / 10) : null,
28    diskReadGbs: diskRead,
29  };
30  return <PcFitCard {...props} live={live}/>;
31}