import { Fragment, useCallback, useState } from 'react' import { Progress } from '@janhq/joi' import { useClickOutside } from '@janhq/joi' import { useAtom, useAtomValue } from 'jotai' import { MonitorIcon, XIcon, ChevronDown, ChevronUp, FolderOpenIcon, } from 'lucide-react' import { twMerge } from 'tailwind-merge' import useGetSystemResources from '@/hooks/useGetSystemResources' import { usePath } from '@/hooks/usePath' import { toGibibytes } from '@/utils/converter' import { utilizedMemory } from '@/utils/memory' import TableActiveModel from './TableActiveModel' import { showSystemMonitorPanelAtom } from '@/helpers/atoms/App.atom' import { reduceTransparentAtom } from '@/helpers/atoms/Setting.atom' import { cpuUsageAtom, gpusAtom, ramUtilitizedAtom, totalRamAtom, usedRamAtom, } from '@/helpers/atoms/SystemBar.atom' const SystemMonitor = () => { const totalRam = useAtomValue(totalRamAtom) const usedRam = useAtomValue(usedRamAtom) const cpuUsage = useAtomValue(cpuUsageAtom) const gpus = useAtomValue(gpusAtom) const { onRevealInFinder } = usePath() const [showFullScreen, setShowFullScreen] = useState(false) const ramUtilitized = useAtomValue(ramUtilitizedAtom) const [showSystemMonitorPanel, setShowSystemMonitorPanel] = useAtom( showSystemMonitorPanelAtom ) const [control, setControl] = useState(null) const [elementExpand, setElementExpand] = useState( null ) const reduceTransparent = useAtomValue(reduceTransparentAtom) const { watch, stopWatching } = useGetSystemResources() useClickOutside( () => { toggleShowSystemMonitorPanel(false) setShowFullScreen(false) }, null, [control, elementExpand] ) const toggleShowSystemMonitorPanel = useCallback( (isShow: boolean) => { setShowSystemMonitorPanel(isShow) if (isShow) { watch() } else { stopWatching() } }, [setShowSystemMonitorPanel, stopWatching, watch] ) return (
{ toggleShowSystemMonitorPanel(!showSystemMonitorPanel) setShowFullScreen(false) }} > System Monitor
{showSystemMonitorPanel && (
Running Models
onRevealInFinder('Logs')} > App Log
{showFullScreen ? ( setShowFullScreen(!showFullScreen)} /> ) : ( setShowFullScreen(!showFullScreen)} /> )} { toggleShowSystemMonitorPanel(false) setShowFullScreen(false) }} />
CPU
{cpuUsage}%
Memory
{toGibibytes(usedRam, { hideUnit: true })}/ {toGibibytes(totalRam, { hideUnit: true })} GB
{ramUtilitized}%
{gpus.length > 0 && (
{gpus.map((gpu, index) => { const gpuUtilization = utilizedMemory( gpu.memoryFree, gpu.memoryTotal ) return (
{gpu.name}
{gpu.memoryTotal - gpu.memoryFree}/ {gpu.memoryTotal} MB
{gpuUtilization}%
) })}
)}
)}
) } export default SystemMonitor