mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
- When used in non compact mode (eg KubeObjectMeta's age) the seconds can be displayed but previously between 10m and 180m of age, the display would only update every minute Signed-off-by: Sebastian Malton <sebastian@malton.name> Signed-off-by: Sebastian Malton <sebastian@malton.name>
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
import { observer } from "mobx-react";
|
|
import React from "react";
|
|
import { formatDuration } from "../../utils";
|
|
import { reactiveNow } from "../../../common/utils/reactive-now/reactive-now";
|
|
|
|
export interface ReactiveDurationProps {
|
|
timestamp: string | undefined;
|
|
|
|
/**
|
|
* Whether the display string should prefer length over precision
|
|
* @default true
|
|
*/
|
|
compact?: boolean;
|
|
}
|
|
|
|
const everySecond = 1000;
|
|
const everyMinute = 60 * 1000;
|
|
|
|
/**
|
|
* This function computes a resonable update interval, matching `formatDuration`'s rules on when to display seconds
|
|
*/
|
|
function computeUpdateInterval(creationTimestampEpoch: number, compact: boolean): number {
|
|
const seconds = Math.floor((Date.now() - creationTimestampEpoch) / 1000);
|
|
const minutes = Math.floor(seconds / 60);
|
|
|
|
if (minutes < 10) {
|
|
return everySecond;
|
|
}
|
|
|
|
if (compact) {
|
|
return everyMinute;
|
|
}
|
|
|
|
if (minutes < (60 * 3)) {
|
|
return everySecond;
|
|
}
|
|
|
|
return everyMinute;
|
|
}
|
|
|
|
export const ReactiveDuration = observer(({ timestamp, compact = true }: ReactiveDurationProps) => {
|
|
if (!timestamp) {
|
|
return <>{"<unknown>"}</>;
|
|
}
|
|
|
|
const timestampSeconds = new Date(timestamp).getTime();
|
|
|
|
return (
|
|
<>
|
|
{formatDuration(reactiveNow(computeUpdateInterval(timestampSeconds, compact)) - timestampSeconds, compact)}
|
|
</>
|
|
);
|
|
});
|