fleet/frontend/components/HumanTimeDiffWithDateTip/HumanTimeDiffWithDateTip.tsx
Scott Gress e985d20b1d
UI for scheduling batch scripts (#31885)
# Details

This PR merges the feature branch for the scheduled scripts UI into
main. This includes the following previously-approved PRs:

* https://github.com/fleetdm/fleet/pull/31750
* https://github.com/fleetdm/fleet/pull/31604
* https://github.com/fleetdm/fleet/pull/31797


# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

## Testing

- [X] Added/updated automated tests
- [X] Where appropriate, [automated tests simulate multiple hosts and
test for host
isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing)
(updates to one hosts's records do not affect another)

- [X] QA'd all new/changed functionality manually

---------

Co-authored-by: jacobshandling <61553566+jacobshandling@users.noreply.github.com>
Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-08-14 10:10:45 -05:00

74 lines
2.1 KiB
TypeScript

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;
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
/>
);
};