fleet/frontend/hooks/usePlatformSelector.tsx
Jacob Shandling 53f57c44db
UI - Queries page updates, pt.1 (#12784)
## Addresses #12636 – follow-up work for PR #12713

- Update Platforms column to render the user-selected platforms for a
query if any, otherwise those that are compatible
<img width="686" alt="Screenshot 2023-07-14 at 6 03 06 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/abd1f079-bdfe-45be-b1dd-58eb903672ef">

  - Clean up typing and names around this column 
- Encapsulate logic for query automations column cells into new
QueryAutomationsStatusIndicator component
  - Increase modularity and decrease coupling of StatusIndicator
- Cleanly handle overflowing queries table due to very long query name
<img width="512" alt="Screenshot 2023-07-14 at 6 07 20 PM"
src="https://github.com/fleetdm/fleet/assets/61553566/6e970038-0aac-4f71-b21d-ececfa66b94f">

- Small copy and layout fixes

- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-07-17 14:09:12 -07:00

85 lines
2.4 KiB
TypeScript

import React, { useCallback, useEffect, useState } from "react";
import { forEach } from "lodash";
import {
SelectedPlatformString,
SUPPORTED_PLATFORMS,
} from "interfaces/platform";
import PlatformSelector from "components/PlatformSelector";
export interface IPlatformSelector {
setSelectedPlatforms: (platforms: string[]) => void;
getSelectedPlatforms: () => ("darwin" | "windows" | "linux" | "chrome")[];
isAnyPlatformSelected: boolean;
render: () => JSX.Element;
}
const usePlatformSelector = (
platformContext: SelectedPlatformString | null | undefined,
baseClass = ""
): IPlatformSelector => {
const [checkDarwin, setCheckDarwin] = useState(false);
const [checkWindows, setCheckWindows] = useState(false);
const [checkLinux, setCheckLinux] = useState(false);
const [checkChrome, setCheckChrome] = useState(false);
const checksByPlatform: Record<string, boolean> = {
darwin: checkDarwin,
windows: checkWindows,
linux: checkLinux,
chrome: checkChrome,
};
const settersByPlatform: Record<string, (val: boolean) => void> = {
darwin: setCheckDarwin,
windows: setCheckWindows,
linux: setCheckLinux,
chrome: setCheckChrome,
};
const setSelectedPlatforms = (platformsToCheck: string[]) => {
forEach(settersByPlatform, (setCheck, p) => {
platformsToCheck.includes(p) ? setCheck(true) : setCheck(false);
});
};
const getSelectedPlatforms = useCallback(() => {
return SUPPORTED_PLATFORMS.filter((p) => checksByPlatform[p]);
}, [checksByPlatform]);
const isAnyPlatformSelected = Object.values(checksByPlatform).includes(true);
useEffect(() => {
if (platformContext === "") {
setSelectedPlatforms(["darwin", "windows", "linux", "chrome"]);
}
platformContext && setSelectedPlatforms(platformContext.split(","));
}, [platformContext]);
const render = useCallback(() => {
return (
<PlatformSelector
baseClass={baseClass}
checkDarwin={checkDarwin}
checkWindows={checkWindows}
checkLinux={checkLinux}
checkChrome={checkChrome}
setCheckDarwin={setCheckDarwin}
setCheckWindows={setCheckWindows}
setCheckLinux={setCheckLinux}
setCheckChrome={setCheckChrome}
/>
);
}, [checkDarwin, checkWindows, checkLinux, checkChrome]);
return {
setSelectedPlatforms,
getSelectedPlatforms,
isAnyPlatformSelected,
render,
};
};
export default usePlatformSelector;