fleet/frontend/components/HumanTimeDiffWithDateTip/HumanTimeDiffWithDateTip.tsx

68 lines
2 KiB
TypeScript
Raw Normal View History

import React from "react";
import { uniqueId } from "lodash";
import { humanLastSeen, internationalTimeFormat } from "utilities/helpers";
Fix certain datetimes to display "Never" when they are prior to Fleet's launch date (#14487) # Checklist for submitter - [x] Added/updated tests - [x] Manual QA for all new/changed functionality > [!NOTE] > For the reviewer, you won't hurt my feelings at all if you have a better implementation in mind and want to close this out! I'll be upfront that I'm much more of a Go dev than a frontend dev. I'm just using this issue as an opportunity to become more familiar with Fleet, since I'm a fan of what y'all are doing and have been looking for a project to spend some spare-time contributing to 😄 > > You should also have edit access to this branch, so feel free to commandeer it as you see fit. # Summary This PR aims to fix a regression identified in #14353 in which certain "zero" datetimes are displayed as "54 years ago" instead of the preferred "Never". The "zero" datetimes are commonly used [as a proxy to indicate](https://github.com/fleetdm/fleet/blob/5cb8051a409df9e82178d9cbe67e7d2370016db2/server/datastore/mysql/hosts.go#L1649) that a date wasn't set, e.g. when a host hasn't had its details fetched yet. We don't want to apply this treatment to _all_ datetimes, since some (like vulnerability data) have valid dates before Fleet was created. To support both use cases, I: * Added an optional boolean, `cutoffBeforeFleetLaunch`, to indicate that dates should be cutoff prior to the hardcoded Fleet launch date. This is set to `false` in `HumanTimeDiffWithDateTip` for backwards-compatibility. * Created `HumanTimeDiffWithFleetLaunchCutoff` which is a drop-in replacement for `HumanTimeDiffWithDateTip` that sets the `cutoffBeforeFleetLaunch` flag to `true`. I then used `HumanTimeDiffWithFleetLaunchCutoff` in the various host related fields I could find. It's possible I missed some, so please double-check me! I'm still getting my bearings on the codebase. Here's a screenshot showing the host table with a host that I had deleted and waited to appear again: <img width="1381" alt="Screenshot 2023-10-11 at 11 27 29 PM" src="https://github.com/fleetdm/fleet/assets/1317288/3cd23879-3233-409f-91a0-8b5e02d09deb"> You may find it helpful to review this commit-by-commit. Closes #14353
2023-10-16 15:43:47 +00:00
import { INITIAL_FLEET_DATE } from "utilities/constants";
import ReactTooltip from "react-tooltip";
interface IHumanTimeDiffWithDateTip {
timeString: string;
Fix certain datetimes to display "Never" when they are prior to Fleet's launch date (#14487) # Checklist for submitter - [x] Added/updated tests - [x] Manual QA for all new/changed functionality > [!NOTE] > For the reviewer, you won't hurt my feelings at all if you have a better implementation in mind and want to close this out! I'll be upfront that I'm much more of a Go dev than a frontend dev. I'm just using this issue as an opportunity to become more familiar with Fleet, since I'm a fan of what y'all are doing and have been looking for a project to spend some spare-time contributing to 😄 > > You should also have edit access to this branch, so feel free to commandeer it as you see fit. # Summary This PR aims to fix a regression identified in #14353 in which certain "zero" datetimes are displayed as "54 years ago" instead of the preferred "Never". The "zero" datetimes are commonly used [as a proxy to indicate](https://github.com/fleetdm/fleet/blob/5cb8051a409df9e82178d9cbe67e7d2370016db2/server/datastore/mysql/hosts.go#L1649) that a date wasn't set, e.g. when a host hasn't had its details fetched yet. We don't want to apply this treatment to _all_ datetimes, since some (like vulnerability data) have valid dates before Fleet was created. To support both use cases, I: * Added an optional boolean, `cutoffBeforeFleetLaunch`, to indicate that dates should be cutoff prior to the hardcoded Fleet launch date. This is set to `false` in `HumanTimeDiffWithDateTip` for backwards-compatibility. * Created `HumanTimeDiffWithFleetLaunchCutoff` which is a drop-in replacement for `HumanTimeDiffWithDateTip` that sets the `cutoffBeforeFleetLaunch` flag to `true`. I then used `HumanTimeDiffWithFleetLaunchCutoff` in the various host related fields I could find. It's possible I missed some, so please double-check me! I'm still getting my bearings on the codebase. Here's a screenshot showing the host table with a host that I had deleted and waited to appear again: <img width="1381" alt="Screenshot 2023-10-11 at 11 27 29 PM" src="https://github.com/fleetdm/fleet/assets/1317288/3cd23879-3233-409f-91a0-8b5e02d09deb"> You may find it helpful to review this commit-by-commit. Closes #14353
2023-10-16 15:43:47 +00:00
cutoffBeforeFleetLaunch?: boolean;
}
/** Returns "Unavailable" if date is empty string or "Unavailable"
Fix certain datetimes to display "Never" when they are prior to Fleet's launch date (#14487) # Checklist for submitter - [x] Added/updated tests - [x] Manual QA for all new/changed functionality > [!NOTE] > For the reviewer, you won't hurt my feelings at all if you have a better implementation in mind and want to close this out! I'll be upfront that I'm much more of a Go dev than a frontend dev. I'm just using this issue as an opportunity to become more familiar with Fleet, since I'm a fan of what y'all are doing and have been looking for a project to spend some spare-time contributing to 😄 > > You should also have edit access to this branch, so feel free to commandeer it as you see fit. # Summary This PR aims to fix a regression identified in #14353 in which certain "zero" datetimes are displayed as "54 years ago" instead of the preferred "Never". The "zero" datetimes are commonly used [as a proxy to indicate](https://github.com/fleetdm/fleet/blob/5cb8051a409df9e82178d9cbe67e7d2370016db2/server/datastore/mysql/hosts.go#L1649) that a date wasn't set, e.g. when a host hasn't had its details fetched yet. We don't want to apply this treatment to _all_ datetimes, since some (like vulnerability data) have valid dates before Fleet was created. To support both use cases, I: * Added an optional boolean, `cutoffBeforeFleetLaunch`, to indicate that dates should be cutoff prior to the hardcoded Fleet launch date. This is set to `false` in `HumanTimeDiffWithDateTip` for backwards-compatibility. * Created `HumanTimeDiffWithFleetLaunchCutoff` which is a drop-in replacement for `HumanTimeDiffWithDateTip` that sets the `cutoffBeforeFleetLaunch` flag to `true`. I then used `HumanTimeDiffWithFleetLaunchCutoff` in the various host related fields I could find. It's possible I missed some, so please double-check me! I'm still getting my bearings on the codebase. Here's a screenshot showing the host table with a host that I had deleted and waited to appear again: <img width="1381" alt="Screenshot 2023-10-11 at 11 27 29 PM" src="https://github.com/fleetdm/fleet/assets/1317288/3cd23879-3233-409f-91a0-8b5e02d09deb"> You may find it helpful to review this commit-by-commit. Closes #14353
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,
}: IHumanTimeDiffWithDateTip): JSX.Element => {
const id = uniqueId();
if (timeString === "Unavailable" || timeString === "") {
return <span>Unavailable</span>;
}
Fix certain datetimes to display "Never" when they are prior to Fleet's launch date (#14487) # Checklist for submitter - [x] Added/updated tests - [x] Manual QA for all new/changed functionality > [!NOTE] > For the reviewer, you won't hurt my feelings at all if you have a better implementation in mind and want to close this out! I'll be upfront that I'm much more of a Go dev than a frontend dev. I'm just using this issue as an opportunity to become more familiar with Fleet, since I'm a fan of what y'all are doing and have been looking for a project to spend some spare-time contributing to 😄 > > You should also have edit access to this branch, so feel free to commandeer it as you see fit. # Summary This PR aims to fix a regression identified in #14353 in which certain "zero" datetimes are displayed as "54 years ago" instead of the preferred "Never". The "zero" datetimes are commonly used [as a proxy to indicate](https://github.com/fleetdm/fleet/blob/5cb8051a409df9e82178d9cbe67e7d2370016db2/server/datastore/mysql/hosts.go#L1649) that a date wasn't set, e.g. when a host hasn't had its details fetched yet. We don't want to apply this treatment to _all_ datetimes, since some (like vulnerability data) have valid dates before Fleet was created. To support both use cases, I: * Added an optional boolean, `cutoffBeforeFleetLaunch`, to indicate that dates should be cutoff prior to the hardcoded Fleet launch date. This is set to `false` in `HumanTimeDiffWithDateTip` for backwards-compatibility. * Created `HumanTimeDiffWithFleetLaunchCutoff` which is a drop-in replacement for `HumanTimeDiffWithDateTip` that sets the `cutoffBeforeFleetLaunch` flag to `true`. I then used `HumanTimeDiffWithFleetLaunchCutoff` in the various host related fields I could find. It's possible I missed some, so please double-check me! I'm still getting my bearings on the codebase. Here's a screenshot showing the host table with a host that I had deleted and waited to appear again: <img width="1381" alt="Screenshot 2023-10-11 at 11 27 29 PM" src="https://github.com/fleetdm/fleet/assets/1317288/3cd23879-3233-409f-91a0-8b5e02d09deb"> You may find it helpful to review this commit-by-commit. Closes #14353
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>;
}
try {
return (
<>
<span className="date-tooltip" data-tip data-for={`tooltip-${id}`}>
{humanLastSeen(timeString)}
</span>
<ReactTooltip
className="date-tooltip-text"
place="top"
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>;
}
};
Fix certain datetimes to display "Never" when they are prior to Fleet's launch date (#14487) # Checklist for submitter - [x] Added/updated tests - [x] Manual QA for all new/changed functionality > [!NOTE] > For the reviewer, you won't hurt my feelings at all if you have a better implementation in mind and want to close this out! I'll be upfront that I'm much more of a Go dev than a frontend dev. I'm just using this issue as an opportunity to become more familiar with Fleet, since I'm a fan of what y'all are doing and have been looking for a project to spend some spare-time contributing to 😄 > > You should also have edit access to this branch, so feel free to commandeer it as you see fit. # Summary This PR aims to fix a regression identified in #14353 in which certain "zero" datetimes are displayed as "54 years ago" instead of the preferred "Never". The "zero" datetimes are commonly used [as a proxy to indicate](https://github.com/fleetdm/fleet/blob/5cb8051a409df9e82178d9cbe67e7d2370016db2/server/datastore/mysql/hosts.go#L1649) that a date wasn't set, e.g. when a host hasn't had its details fetched yet. We don't want to apply this treatment to _all_ datetimes, since some (like vulnerability data) have valid dates before Fleet was created. To support both use cases, I: * Added an optional boolean, `cutoffBeforeFleetLaunch`, to indicate that dates should be cutoff prior to the hardcoded Fleet launch date. This is set to `false` in `HumanTimeDiffWithDateTip` for backwards-compatibility. * Created `HumanTimeDiffWithFleetLaunchCutoff` which is a drop-in replacement for `HumanTimeDiffWithDateTip` that sets the `cutoffBeforeFleetLaunch` flag to `true`. I then used `HumanTimeDiffWithFleetLaunchCutoff` in the various host related fields I could find. It's possible I missed some, so please double-check me! I'm still getting my bearings on the codebase. Here's a screenshot showing the host table with a host that I had deleted and waited to appear again: <img width="1381" alt="Screenshot 2023-10-11 at 11 27 29 PM" src="https://github.com/fleetdm/fleet/assets/1317288/3cd23879-3233-409f-91a0-8b5e02d09deb"> You may find it helpful to review this commit-by-commit. Closes #14353
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,
}: IHumanTimeDiffWithDateTip): JSX.Element => {
return (
<HumanTimeDiffWithDateTip timeString={timeString} cutoffBeforeFleetLaunch />
);
};