Skip to content

Commit d74ed80

Browse files
Add secondary disk and network stats to system cards
1 parent 4d8cd81 commit d74ed80

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

src/components/home/SystemStats.tsx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { FieldPath } from 'juststore'
2-
import { Clock, Cpu, HardDrive, type LucideIcon, MemoryStick } from 'lucide-react'
2+
import { Clock, Cpu, HardDrive, type LucideIcon, MemoryStick, Wifi } from 'lucide-react'
33
import { Suspense } from 'react'
44
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
5-
import { useIsMobile } from '@/hooks/use-mobile'
65
import { formatDuration } from '@/lib/format'
76
import SystemStatsProvider from './SystemStatsProvider'
87
import SystemStatValue from './SystemStatValue'
@@ -33,8 +32,10 @@ function SystemStatsMobile() {
3332
}
3433

3534
function SystemStatsDesktop() {
35+
const hasSecondaryDisk = Boolean(store.systemInfo.secondaryPartitionUsageDesc.use())
36+
3637
return (
37-
<div className="hidden sm:grid grid-cols-2 gap-2 px-1 sm:grid-cols-4 sm:gap-4">
38+
<div className="hidden sm:grid grid-cols-2 gap-2 px-1 sm:grid-cols-5 sm:gap-4">
3839
{statsProps.map(stat => (
3940
<Card key={stat.key} size="sm" className="h-full">
4041
<CardHeader>
@@ -46,11 +47,28 @@ function SystemStatsDesktop() {
4647
</div>
4748
</CardHeader>
4849
<CardContent className="space-y-1.5 sm:space-y-2">
49-
<SystemStatValue
50-
valueKey={stat.key}
51-
descriptionKey={stat.descriptionKey}
52-
type={stat.type}
53-
/>
50+
{stat.key === 'rootPartitionUsage' ? (
51+
<>
52+
<SystemStatValue
53+
valueKey={stat.key}
54+
descriptionKey={stat.descriptionKey}
55+
type={stat.type}
56+
/>
57+
{hasSecondaryDisk && (
58+
<SystemStatValue
59+
valueKey="secondaryPartitionUsage"
60+
descriptionKey="secondaryPartitionUsageDesc"
61+
type="progress"
62+
/>
63+
)}
64+
</>
65+
) : (
66+
<SystemStatValue
67+
valueKey={stat.key}
68+
descriptionKey={stat.descriptionKey}
69+
type={stat.type}
70+
/>
71+
)}
5472
</CardContent>
5573
</Card>
5674
))}
@@ -62,7 +80,7 @@ type StatProp = {
6280
label: string
6381
mobileLabel: string
6482
icon: LucideIcon
65-
mobileValueMode?: 'percent' | 'description' | 'duration'
83+
mobileValueMode?: 'percent' | 'description' | 'duration' | 'text'
6684
type: 'text' | 'progress' | 'duration'
6785
color: string
6886
key: FieldPath<Store['systemInfo']>
@@ -77,6 +95,9 @@ function MobileStatRow({ stat }: { stat: StatProp }) {
7795
if (stat.mobileValueMode === 'duration') {
7896
return formatDuration(Number(value), { unit: 's' })
7997
}
98+
if (stat.mobileValueMode === 'text') {
99+
return String(value)
100+
}
80101
return `${value}%`
81102
})()
82103

@@ -130,4 +151,13 @@ const statsProps: StatProp[] = [
130151
key: 'rootPartitionUsage',
131152
descriptionKey: 'rootPartitionUsageDesc',
132153
},
154+
{
155+
label: 'Network',
156+
mobileLabel: 'Net',
157+
icon: Wifi,
158+
mobileValueMode: 'text',
159+
type: 'text',
160+
color: 'bg-chart-4',
161+
key: 'networkSpeedSummary',
162+
},
133163
] as const

src/components/home/SystemStatsProvider.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ export default function SystemStatsProvider() {
1515
cpuAverage: Math.round(data.cpu_average * 100) / 100,
1616
rootPartitionUsage: Math.round(getDiskUsage(data.disks, '/') ?? 0 * 100),
1717
rootPartitionUsageDesc: getDiskUsageDesc(data.disks, '/'),
18+
secondaryPartitionUsage: Math.round(getSecondaryDiskUsage(data.disks, '/') ?? 0 * 100),
19+
secondaryPartitionUsageDesc: getSecondaryDiskUsageDesc(data.disks, '/'),
1820
memoryUsage: Math.round(data.memory.used_percent * 100) / 100,
1921
memoryUsageDesc: getMemoryUsageDesc(data.memory),
22+
networkSpeedSummary: getNetworkSpeedSummary(data),
2023
}),
2124
})
2225

@@ -44,6 +47,30 @@ function getDiskUsageDesc(disks: Record<string, DiskUsageStat>, path: string) {
4447
return `${formatBytes(disk.used, { precision: 1 })} / ${formatBytes(disk.total, { precision: 1 })}`
4548
}
4649

50+
function getSecondaryDisk(disks: Record<string, DiskUsageStat>, path: string) {
51+
const allDisks = Object.values(disks)
52+
const primaryDisk = getDisk(disks, path)
53+
return allDisks.find(d => d.path !== primaryDisk?.path)
54+
}
55+
56+
function getSecondaryDiskUsage(disks: Record<string, DiskUsageStat>, path: string) {
57+
return getSecondaryDisk(disks, path)?.used_percent
58+
}
59+
60+
function getSecondaryDiskUsageDesc(disks: Record<string, DiskUsageStat>, path: string) {
61+
const disk = getSecondaryDisk(disks, path)
62+
if (!disk) {
63+
return ''
64+
}
65+
return `${disk.path}: ${formatBytes(disk.used, { precision: 1 })} / ${formatBytes(disk.total, { precision: 1 })}`
66+
}
67+
4768
function getMemoryUsageDesc(memory: MemVirtualMemoryStat) {
4869
return `${formatBytes(memory.used, { precision: 1 })} / ${formatBytes(memory.total, { precision: 1 })}`
4970
}
71+
72+
function getNetworkSpeedSummary(data: SystemInfo) {
73+
const uploadSpeed = data.network?.upload_speed ?? 0
74+
const downloadSpeed = data.network?.download_speed ?? 0
75+
return `↑ ${formatBytes(uploadSpeed, { precision: 0, unit: '/s' })}${formatBytes(downloadSpeed, { precision: 0, unit: '/s' })}`
76+
}

src/components/home/store.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ type SystemInfoSimple = {
4040
cpuAverage: number
4141
rootPartitionUsage: number
4242
rootPartitionUsageDesc: string
43+
secondaryPartitionUsage: number
44+
secondaryPartitionUsageDesc: string
4345
memoryUsage: number
4446
memoryUsageDesc: string
47+
networkSpeedSummary: string
4548
}
4649

4750
export const store = createStore<Store>('homepage', {
@@ -50,8 +53,11 @@ export const store = createStore<Store>('homepage', {
5053
cpuAverage: 0,
5154
rootPartitionUsage: 0,
5255
rootPartitionUsageDesc: '',
56+
secondaryPartitionUsage: 0,
57+
secondaryPartitionUsageDesc: '',
5358
memoryUsage: 0,
5459
memoryUsageDesc: '',
60+
networkSpeedSummary: '—',
5561
},
5662
homepageCategories: [],
5763
searchQuery: '',

0 commit comments

Comments
 (0)