2023-01-05 15:08:27 +00:00
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
|
|
import { uniqueId } from "lodash";
|
2023-09-11 13:45:15 +00:00
|
|
|
import { humanLastSeen, internationalTimeFormat } from "utilities/helpers";
|
2023-10-16 15:43:47 +00:00
|
|
|
import { INITIAL_FLEET_DATE } from "utilities/constants";
|
2024-03-11 19:39:33 +00:00
|
|
|
import ReactTooltip, { Place } from "react-tooltip";
|
2023-01-05 15:08:27 +00:00
|
|
|
|
2023-09-11 13:45:15 +00:00
|
|
|
interface IHumanTimeDiffWithDateTip {
|
|
|
|
|
timeString: string;
|
2023-10-16 15:43:47 +00:00
|
|
|
cutoffBeforeFleetLaunch?: boolean;
|
2024-03-11 19:39:33 +00:00
|
|
|
tooltipPosition?: Place;
|
2023-09-11 13:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns "Unavailable" if date is empty string or "Unavailable"
|
2023-10-16 15:43:47 +00:00
|
|
|
* Returns "Invalid date" if date is invalid
|
|
|
|
|
* Returns "Never" if cutoffBeforeFleetLaunch is true and date is before the
|
|
|
|
|
* initial launch of Fleet */
|
|
|
|
|
export const HumanTimeDiffWithDateTip = ({
|
|
|
|
|
timeString,
|
|
|
|
|
cutoffBeforeFleetLaunch = false,
|
2024-03-11 19:39:33 +00:00
|
|
|
tooltipPosition = "top",
|
2023-10-16 15:43:47 +00:00
|
|
|
}: IHumanTimeDiffWithDateTip): JSX.Element => {
|
2023-01-05 15:08:27 +00:00
|
|
|
const id = uniqueId();
|
2023-09-11 13:45:15 +00:00
|
|
|
|
|
|
|
|
if (timeString === "Unavailable" || timeString === "") {
|
|
|
|
|
return <span>Unavailable</span>;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 15:43:47 +00:00
|
|
|
// There are cases where dates are set in Fleet to be the "zero date" which
|
|
|
|
|
// serves as an indicator that a particular date isn't set.
|
|
|
|
|
if (cutoffBeforeFleetLaunch && timeString < INITIAL_FLEET_DATE) {
|
|
|
|
|
return <span>Never</span>;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-11 13:45:15 +00:00
|
|
|
try {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2024-02-23 14:57:18 +00:00
|
|
|
<span className="date-tooltip" data-tip data-for={`tooltip-${id}`}>
|
2023-09-11 13:45:15 +00:00
|
|
|
{humanLastSeen(timeString)}
|
|
|
|
|
</span>
|
|
|
|
|
<ReactTooltip
|
|
|
|
|
className="date-tooltip-text"
|
2024-03-11 19:39:33 +00:00
|
|
|
place={tooltipPosition}
|
2023-09-11 13:45:15 +00:00
|
|
|
type="dark"
|
|
|
|
|
effect="solid"
|
|
|
|
|
id={`tooltip-${id}`}
|
|
|
|
|
backgroundColor="#3e4771"
|
|
|
|
|
>
|
|
|
|
|
{internationalTimeFormat(new Date(timeString))}
|
|
|
|
|
</ReactTooltip>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e instanceof RangeError) {
|
|
|
|
|
return <span>Invalid date</span>;
|
|
|
|
|
}
|
|
|
|
|
return <span>Unavailable</span>;
|
|
|
|
|
}
|
2023-01-05 15:08:27 +00:00
|
|
|
};
|
2023-10-16 15:43:47 +00:00
|
|
|
|
|
|
|
|
/** Returns a HumanTimeDiffWithDateTip configured to return "Never" in the case
|
|
|
|
|
* that the timeString is before the launch date of Fleet */
|
|
|
|
|
export const HumanTimeDiffWithFleetLaunchCutoff = ({
|
|
|
|
|
timeString,
|
2025-08-14 15:10:45 +00:00
|
|
|
tooltipPosition = "top",
|
2023-10-16 15:43:47 +00:00
|
|
|
}: IHumanTimeDiffWithDateTip): JSX.Element => {
|
|
|
|
|
return (
|
2025-08-14 15:10:45 +00:00
|
|
|
<HumanTimeDiffWithDateTip
|
|
|
|
|
timeString={timeString}
|
|
|
|
|
tooltipPosition={tooltipPosition}
|
|
|
|
|
cutoffBeforeFleetLaunch
|
|
|
|
|
/>
|
2023-10-16 15:43:47 +00:00
|
|
|
);
|
|
|
|
|
};
|