|
1 | | -import React, { useEffect, useState } from 'react'; |
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
2 | 2 | import { |
3 | 3 | faClock, |
4 | 4 | faCloudDownloadAlt, |
@@ -31,13 +31,30 @@ const getBackgroundColor = (value: number, max: number | null): string | undefin |
31 | 31 | return undefined; |
32 | 32 | }; |
33 | 33 |
|
| 34 | +const Limit = ({ limit, children }: { limit: string | null; children: React.ReactNode }) => ( |
| 35 | + <> |
| 36 | + {children} |
| 37 | + <span className={'ml-1 text-gray-300 text-[70%] select-none'}>/ {limit || '∞'}</span> |
| 38 | + </> |
| 39 | +); |
| 40 | + |
34 | 41 | const ServerDetailsBlock = ({ className }: { className?: string }) => { |
35 | 42 | const [stats, setStats] = useState<Stats>({ memory: 0, cpu: 0, disk: 0, uptime: 0, tx: 0, rx: 0 }); |
36 | 43 |
|
37 | 44 | const status = ServerContext.useStoreState((state) => state.status.value); |
38 | 45 | const connected = ServerContext.useStoreState((state) => state.socket.connected); |
39 | 46 | const instance = ServerContext.useStoreState((state) => state.socket.instance); |
40 | 47 | const limits = ServerContext.useStoreState((state) => state.server.data!.limits); |
| 48 | + |
| 49 | + const textLimits = useMemo( |
| 50 | + () => ({ |
| 51 | + cpu: limits?.cpu ? `${limits.cpu}%` : null, |
| 52 | + memory: limits?.memory ? bytesToString(mbToBytes(limits.memory)) : null, |
| 53 | + disk: limits?.disk ? bytesToString(mbToBytes(limits.disk)) : null, |
| 54 | + }), |
| 55 | + [limits] |
| 56 | + ); |
| 57 | + |
41 | 58 | const allocation = ServerContext.useStoreState((state) => { |
42 | 59 | const match = state.server.data!.allocations.find((allocation) => allocation.isDefault); |
43 | 60 |
|
@@ -82,56 +99,31 @@ const ServerDetailsBlock = ({ className }: { className?: string }) => { |
82 | 99 | > |
83 | 100 | {stats.uptime > 0 ? <UptimeDuration uptime={stats.uptime / 1000} /> : 'Offline'} |
84 | 101 | </StatBlock> |
85 | | - <StatBlock |
86 | | - icon={faMicrochip} |
87 | | - title={'CPU Load'} |
88 | | - color={getBackgroundColor(stats.cpu, limits.cpu)} |
89 | | - description={ |
90 | | - limits.cpu |
91 | | - ? `This server is allowed to use up to ${limits.cpu}% of the host's available CPU resources.` |
92 | | - : 'No CPU limit has been configured for this server.' |
93 | | - } |
94 | | - > |
95 | | - {status === 'offline' ? <span className={'text-gray-400'}>Offline</span> : `${stats.cpu.toFixed(2)}%`} |
| 102 | + <StatBlock icon={faMicrochip} title={'CPU Load'} color={getBackgroundColor(stats.cpu, limits.cpu)}> |
| 103 | + {status === 'offline' ? ( |
| 104 | + <span className={'text-gray-400'}>Offline</span> |
| 105 | + ) : ( |
| 106 | + <Limit limit={textLimits.cpu}>{stats.cpu.toFixed(2)}%</Limit> |
| 107 | + )} |
96 | 108 | </StatBlock> |
97 | 109 | <StatBlock |
98 | 110 | icon={faMemory} |
99 | 111 | title={'Memory'} |
100 | 112 | color={getBackgroundColor(stats.memory / 1024, limits.memory * 1024)} |
101 | | - description={ |
102 | | - limits.memory |
103 | | - ? `This server is allowed to use up to ${bytesToString(mbToBytes(limits.memory))} of memory.` |
104 | | - : 'No memory limit has been configured for this server.' |
105 | | - } |
106 | 113 | > |
107 | | - {status === 'offline' ? <span className={'text-gray-400'}>Offline</span> : bytesToString(stats.memory)} |
| 114 | + {status === 'offline' ? ( |
| 115 | + <span className={'text-gray-400'}>Offline</span> |
| 116 | + ) : ( |
| 117 | + <Limit limit={textLimits.memory}>{bytesToString(stats.memory)}</Limit> |
| 118 | + )} |
108 | 119 | </StatBlock> |
109 | | - <StatBlock |
110 | | - icon={faHdd} |
111 | | - title={'Disk'} |
112 | | - color={getBackgroundColor(stats.disk / 1024, limits.disk * 1024)} |
113 | | - description={ |
114 | | - limits.disk |
115 | | - ? `This server is allowed to use up to ${bytesToString(mbToBytes(limits.disk))} of disk space.` |
116 | | - : 'No disk space limit has been configured for this server.' |
117 | | - } |
118 | | - > |
119 | | - {bytesToString(stats.disk)} |
| 120 | + <StatBlock icon={faHdd} title={'Disk'} color={getBackgroundColor(stats.disk / 1024, limits.disk * 1024)}> |
| 121 | + <Limit limit={textLimits.disk}>{bytesToString(stats.disk)}</Limit> |
120 | 122 | </StatBlock> |
121 | | - <StatBlock |
122 | | - icon={faCloudDownloadAlt} |
123 | | - title={'Network (Inbound)'} |
124 | | - description={'The total amount of network traffic that your server has received since it was started.'} |
125 | | - > |
| 123 | + <StatBlock icon={faCloudDownloadAlt} title={'Network (Inbound)'}> |
126 | 124 | {status === 'offline' ? <span className={'text-gray-400'}>Offline</span> : bytesToString(stats.rx)} |
127 | 125 | </StatBlock> |
128 | | - <StatBlock |
129 | | - icon={faCloudUploadAlt} |
130 | | - title={'Network (Outbound)'} |
131 | | - description={ |
132 | | - 'The total amount of traffic your server has sent across the internet since it was started.' |
133 | | - } |
134 | | - > |
| 126 | + <StatBlock icon={faCloudUploadAlt} title={'Network (Outbound)'}> |
135 | 127 | {status === 'offline' ? <span className={'text-gray-400'}>Offline</span> : bytesToString(stats.tx)} |
136 | 128 | </StatBlock> |
137 | 129 | </div> |
|
0 commit comments