fleet/frontend/pages/hosts/components/DiskSpaceIndicator/DiskSpaceIndicator.tsx
jacobshandling 994256bcaa
Refactor DiskSpaceIndicator to use ProgressBar (#33198)
## Precursor for #31671 

- Add width option to `ProgressBar`
- Refactor `DiskSpaceIndicator`
  - Use `ProgressBar` with new `width` option
  - Replace raw react tooltip with `TooltipWrapper`
  - Clean up confusing styles
- Update tests, ensure consistent style with previous implementation on
hosts table, hosts details page, my device page

<img width="1020" height="546" alt="Screenshot 2025-09-18 at 4 49 28 PM"
src="https://github.com/user-attachments/assets/a0c958d0-8b2b-466c-b169-a91dc8fb984c"
/>
<img width="1020" height="546" alt="Screenshot 2025-09-18 at 4 49 35 PM"
src="https://github.com/user-attachments/assets/f60f1e0a-573d-438b-9ded-ec45825599c1"
/>


- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-09-19 12:54:43 -07:00

86 lines
2.4 KiB
TypeScript

import React from "react";
import { PlacesType } from "react-tooltip-5";
import { COLORS } from "styles/var/colors";
import ProgressBar from "components/ProgressBar";
import TooltipWrapper from "components/TooltipWrapper";
const baseClass = "disk-space-indicator";
interface IDiskSpaceIndicatorProps {
gigsDiskSpaceAvailable: number | "---";
percentDiskSpaceAvailable: number;
platform: string;
inTableCell?: boolean;
tooltipPosition?: PlacesType;
}
const DiskSpaceIndicator = ({
gigsDiskSpaceAvailable,
percentDiskSpaceAvailable,
platform,
inTableCell = false,
tooltipPosition = "top",
}: IDiskSpaceIndicatorProps): JSX.Element => {
if (gigsDiskSpaceAvailable === 0 || gigsDiskSpaceAvailable === "---") {
return <span className={`${baseClass}__empty`}>No data available</span>;
}
const getDiskSpaceIndicatorColor = (): string => {
// return space-dependent indicator colors for mac and windows hosts, green for linux
if (platform === "darwin" || platform === "windows") {
if (gigsDiskSpaceAvailable < 16) {
return COLORS["ui-error"];
} else if (gigsDiskSpaceAvailable < 32) {
return COLORS["ui-warning"];
}
}
return COLORS["status-success"];
};
const diskSpaceTooltipText = ((): string | undefined => {
if (platform === "darwin" || platform === "windows") {
if (gigsDiskSpaceAvailable < 16) {
return "Not enough disk space available to install most small operating systems updates.";
} else if (gigsDiskSpaceAvailable < 32) {
return "Not enough disk space available to install most large operating systems updates.";
}
return "Enough disk space available to install most operating systems updates.";
}
return undefined;
})();
const renderBar = () => (
<ProgressBar
sections={[
{
color: getDiskSpaceIndicatorColor(),
portion: percentDiskSpaceAvailable / 100,
},
]}
width="small"
/>
);
return (
<span className={baseClass}>
{diskSpaceTooltipText ? (
<TooltipWrapper
position={tooltipPosition}
tipOffset={10}
showArrow
underline={false}
tipContent={diskSpaceTooltipText}
>
{renderBar()}
</TooltipWrapper>
) : (
renderBar()
)}
{gigsDiskSpaceAvailable} GB{!inTableCell && " available"}
</span>
);
};
export default DiskSpaceIndicator;