Skip to content

Commit 9aa6e8a

Browse files
committed
fix: format time
1 parent d6010e1 commit 9aa6e8a

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

apps/web/src/features/about/components/InfoBlock.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
export const InfoBlock: React.FC<{ items: { [key: string]: string }; label: string }> = ({ items, label }) => {
1+
import { TimeValue } from './TimeValue';
2+
3+
export type InfoBlockProps = {
4+
items: {
5+
[key: string]: string;
6+
};
7+
label: string;
8+
};
9+
10+
export const InfoBlock = ({ items, label }: InfoBlockProps) => {
211
return (
312
<div>
413
<h5 className="mb-1 font-semibold">{label}</h5>
514
<ul className="text-muted-foreground grid gap-0.5">
615
{Object.entries(items).map(([key, value]) => (
716
<li key={key}>
817
<span>{key}: </span>
9-
<span>{value}</span>
18+
{value.startsWith('Uptime=') ? <TimeValue value={parseInt(value.slice(7))} /> : <span>{value}</span>}
1019
</li>
1120
))}
1221
</ul>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useCallback, useRef, useState } from 'react';
2+
3+
import { parseDuration } from '@douglasneuroinformatics/libjs';
4+
import { useInterval } from '@douglasneuroinformatics/libui/hooks';
5+
6+
export type TimeValueProps = {
7+
value: number;
8+
};
9+
10+
export const TimeValue = ({ value }: TimeValueProps) => {
11+
const format = useCallback((uptime: number) => {
12+
let { days, hours, minutes, seconds } = parseDuration(uptime * 1000);
13+
hours += days * 24;
14+
return [hours, minutes, seconds].map((value) => (value < 10 ? '0' + value : value)).join(':');
15+
}, []);
16+
const valueRef = useRef<number>(value);
17+
18+
const [state, setState] = useState(format(value));
19+
20+
useInterval(() => {
21+
valueRef.current = valueRef.current + 1;
22+
setState(format(valueRef.current + 1));
23+
}, 1000);
24+
25+
return <span>{state}</span>;
26+
};

apps/web/src/features/about/pages/AboutPage.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { parseDuration } from '@douglasneuroinformatics/libjs';
21
import { Card, Heading } from '@douglasneuroinformatics/libui/components';
32
import { useTranslation } from '@douglasneuroinformatics/libui/hooks';
43
import type { ReleaseInfo } from '@opendatacapture/schemas/setup';
@@ -64,12 +63,6 @@ export const AboutPage = () => {
6463

6564
const { isGatewayEnabled } = setupStateQuery.data;
6665

67-
const translateUptime = (uptime: number) => {
68-
let { days, hours, minutes, seconds } = parseDuration(uptime * 1000);
69-
hours += days * 24;
70-
return [hours, minutes, seconds].map((value) => (value < 10 ? '0' + value : value)).join(':');
71-
};
72-
7366
const translateReleaseInfo = (release: ReleaseInfo) => {
7467
const translatedReleaseInfo = {
7568
[t(translations.buildType)]: t(translations.buildTypes[release.type]),
@@ -92,7 +85,7 @@ export const AboutPage = () => {
9285
gatewayInfo[t(translations.status)] = gatewayHealthData.status.toString();
9386
if (gatewayHealthData.ok) {
9487
Object.assign(gatewayInfo, translateReleaseInfo(gatewayHealthData.release), {
95-
[t(translations.uptime)]: translateUptime(gatewayHealthData.uptime)
88+
[t(translations.uptime)]: `Uptime=${gatewayHealthData.uptime}`
9689
});
9790
}
9891
return gatewayInfo;
@@ -127,7 +120,7 @@ export const AboutPage = () => {
127120
<InfoBlock
128121
items={{
129122
...translateReleaseInfo(setupStateQuery.data.release),
130-
[t(translations.uptime)]: translateUptime(setupStateQuery.data.uptime)
123+
[t(translations.uptime)]: `Uptime=${setupStateQuery.data.uptime}`
131124
}}
132125
label={t({
133126
en: 'Core API',

0 commit comments

Comments
 (0)