okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4fb068a8c
main
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 }) { 14const vramUsed = useStore ( s => tenth ( s . monitor ?. hardware ?. vramUsedGb )); 15const ramUsed = useStore ( s => tenth ( s . monitor ?. hardware ?. ramUsedGb )); 16const diskRead = useStore ( s => tenth ( s . monitor ?. hardware ?. diskReadGbs )); 17useEffect (() => { 18const ask = () => { if ( typeof document === 'undefined' || document . visibilityState !== 'hidden' ) void api . get ( '/api/monitor' ). catch (() => undefined ); }; 19ask (); 20const timer = setInterval ( ask , 10000 ); 21return () => clearInterval ( timer ); 22}, []); 23const vramTotal = props . vramAllGb ?? props . vramGb ; 24const live :PcLive = { 25vramTotalGb :vramTotal , 26freeVramGb :vramUsed !== null && vramTotal > 0 ?Math . max ( 0 , Math . round (( vramTotal - vramUsed ) * 10 ) / 10 ) :null , 27freeRamGb :ramUsed !== null && props . ramGb ?Math . max ( 0 , Math . round (( props . ramGb - ramUsed ) * 10 ) / 10 ) :null , 28diskReadGbs :diskRead , 29}; 30return < PcFitCard { ...props } live = { live }/>; 31}