Skip to content

Commit 1433784

Browse files
authored
fix: correct lastUpdate calculation & proper format for age timestamps (kyma-project#4630)
1 parent cd65861 commit 1433784

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

public/i18n/en.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,13 @@ common:
427427
value-units:
428428
days: days
429429
days-ago: days ago
430+
days-old: days old
430431
hours-ago: hours ago
432+
hours-old: hours old
431433
minutes-ago: minutes ago
434+
minutes-old: minutes old
432435
seconds-ago: seconds ago
436+
seconds-old: seconds old
433437
'yes': 'yes'
434438
components:
435439
accessible-name:

src/shared/components/CertificateDate/CertificateDate.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useTranslation } from 'react-i18next';
22
import { FormattedDatetime } from 'shared/components/FormattedDate/FormattedDate';
33
import { Tooltip } from 'shared/components/Tooltip/Tooltip';
44
import { Icon } from '@ui5/webcomponents-react';
5-
import useDateNow from 'shared/hooks/dateNow';
5+
import useDateNow from 'shared/hooks/useDateNow';
66

77
export function CertificateDate({ date, lang }) {
88
const { t } = useTranslation();
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useMemo } from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { EMPTY_TEXT_PLACEHOLDER } from 'shared/constants';
4-
import useDateNow from 'shared/hooks/dateNow';
4+
import useDateNow from 'shared/hooks/useDateNow';
55

66
const toSeconds = 1000;
77
const toMinutes = 60;
@@ -12,11 +12,17 @@ const formatResult = (value: number, valueUnit: string) => {
1212
return <>{Math.round(value).toString() + ' ' + valueUnit}</>;
1313
};
1414

15+
type TimeFormat = 'elapsed' | 'age';
16+
17+
interface ReadableElapsedTimeFromNowProps {
18+
timestamp: string;
19+
format?: TimeFormat;
20+
}
21+
1522
export const ReadableElapsedTimeFromNow = ({
1623
timestamp,
17-
}: {
18-
timestamp: string;
19-
}): JSX.Element => {
24+
format = 'elapsed',
25+
}: ReadableElapsedTimeFromNowProps): JSX.Element => {
2026
const { t } = useTranslation();
2127
const now = useDateNow(10_000);
2228

@@ -26,21 +32,23 @@ export const ReadableElapsedTimeFromNow = ({
2632
}
2733

2834
const startDate = new Date(timestamp);
29-
const timeDiff = now - startDate.valueOf();
35+
const timeDiff = Math.max(0, now - startDate.valueOf());
3036

3137
const seconds = timeDiff / toSeconds;
3238
const minutes = seconds / toMinutes;
3339
const hours = minutes / toHours;
3440
const days = hours / toDays;
3541

42+
const suffix = format === 'age' ? 'old' : 'ago';
43+
3644
if (seconds < 60)
37-
return formatResult(seconds, t('common.value-units.seconds-ago'));
45+
return formatResult(seconds, t(`common.value-units.seconds-${suffix}`));
3846
else if (minutes < 60)
39-
return formatResult(minutes, t('common.value-units.minutes-ago'));
47+
return formatResult(minutes, t(`common.value-units.minutes-${suffix}`));
4048
else if (hours < 24)
41-
return formatResult(hours, t('common.value-units.hours-ago'));
42-
else return formatResult(days, t('common.value-units.days-ago'));
43-
}, [timestamp, t, now]);
49+
return formatResult(hours, t(`common.value-units.hours-${suffix}`));
50+
else return formatResult(days, t(`common.value-units.days-${suffix}`));
51+
}, [timestamp, t, now, format]);
4452

4553
return result;
4654
};

src/shared/components/ResourceDetails/ResourceDetails.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,16 @@ function Resource({
332332
</>
333333
);
334334

335-
// https://stackoverflow.com/questions/70330862/how-to-get-the-latest-change-time-of-a-resource-instance-in-k8s
336335
let lastUpdate;
337336
const managedFields = resource.metadata?.managedFields;
338337
if (managedFields && Array.isArray(managedFields)) {
339-
const lastOp = managedFields[managedFields.length - 1];
340-
lastUpdate = lastOp.time;
338+
const latestEntry = managedFields.reduce((latest, current) => {
339+
if (!current.time) return latest;
340+
if (!latest || !latest.time) return current;
341+
return new Date(current.time) > new Date(latest.time) ? current : latest;
342+
}, null);
343+
344+
lastUpdate = latestEntry?.time;
341345
}
342346

343347
const renderUpdateDate = (lastUpdate) => {
@@ -417,6 +421,7 @@ function Resource({
417421
>
418422
<ReadableElapsedTimeFromNow
419423
timestamp={resource.metadata.creationTimestamp}
424+
format="age"
420425
/>
421426
</DynamicPageComponent.Column>
422427
{!hideLastUpdate && (
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export default function useDateNow(refreshInterval: number = 1_000) {
99
};
1010
const interval = setInterval(handler, refreshInterval);
1111
return () => clearInterval(interval);
12-
});
12+
}, [refreshInterval]);
1313
return time;
1414
}

0 commit comments

Comments
 (0)