-
Notifications
You must be signed in to change notification settings - Fork 845
Expand file tree
/
Copy pathHumanTimeDiffWithDateTip.tsx
More file actions
75 lines (68 loc) · 2.2 KB
/
HumanTimeDiffWithDateTip.tsx
File metadata and controls
75 lines (68 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React from "react";
import { uniqueId } from "lodash";
import { humanLastSeen, internationalTimeFormat } from "utilities/helpers";
import { INITIAL_FLEET_DATE } from "utilities/constants";
import ReactTooltip, { Place } from "react-tooltip";
interface IHumanTimeDiffWithDateTip {
timeString: string;
// eslint-disable-next-line react/no-unused-prop-types
cutoffBeforeFleetLaunch?: boolean;
tooltipPosition?: Place;
}
/** Returns "Unavailable" if date is empty string or "Unavailable"
* 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,
tooltipPosition = "top",
}: IHumanTimeDiffWithDateTip): JSX.Element => {
const id = uniqueId();
if (timeString === "Unavailable" || timeString === "") {
return <span>Unavailable</span>;
}
// 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>;
}
try {
return (
<>
<span className="date-tooltip" data-tip data-for={`tooltip-${id}`}>
{humanLastSeen(timeString)}
</span>
<ReactTooltip
className="date-tooltip-text"
place={tooltipPosition}
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>;
}
};
/** Returns a HumanTimeDiffWithDateTip configured to return "Never" in the case
* that the timeString is before the launch date of Fleet */
export const HumanTimeDiffWithFleetLaunchCutoff = ({
timeString,
tooltipPosition = "top",
}: IHumanTimeDiffWithDateTip): JSX.Element => {
return (
<HumanTimeDiffWithDateTip
timeString={timeString}
tooltipPosition={tooltipPosition}
cutoffBeforeFleetLaunch
/>
);
};