fleet/frontend/components/TeamsDropdown/TeamsDropdown.tsx
Jacob Shandling cb58849d95
Fleet Premium to Sandbox (#11372)
## Addresses #9371 
### Adds a suite of UI logic for premium features in the Sandbox
environment

For reviewer: please review the work for the below 3 substasks, which
are the only remaining subtasks encompassed by this PR that have not yet
passed review individually:
  - #10822 (9)
  - #10823 (10)
  - #10824 (11)

## Checklist for submitter
- [x] Changes file added for user-visible changes in `changes/`
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
Co-authored-by: Martin Angers <martin.n.angers@gmail.com>
2023-04-27 08:53:30 -07:00

137 lines
3.4 KiB
TypeScript

import React, { useMemo } from "react";
import classnames from "classnames";
import {
APP_CONTEXT_ALL_TEAMS_SUMMARY,
ITeamSummary,
APP_CONTEX_NO_TEAM_SUMMARY,
} from "interfaces/team";
// @ts-ignore
import Dropdown from "components/forms/fields/Dropdown";
import ReactTooltip from "react-tooltip";
import { uniqueId } from "lodash";
const generateDropdownOptions = (
teams: ITeamSummary[] | undefined,
includeAll: boolean,
includeNoTeams?: boolean
) => {
if (!teams) {
return [];
}
const options = teams.map((team) => ({
disabled: false,
label: team.name,
value: team.id,
}));
const filtered = options.filter(
(o) =>
!(
(o.label === APP_CONTEX_NO_TEAM_SUMMARY.name && !includeNoTeams) ||
(o.label === APP_CONTEXT_ALL_TEAMS_SUMMARY.name && !includeAll)
)
);
return filtered;
};
interface ITeamsDropdownProps {
currentUserTeams: ITeamSummary[];
selectedTeamId?: number;
includeAll?: boolean; // Include the "All Teams" option;
includeNoTeams?: boolean;
isDisabled?: boolean;
isSandboxMode?: boolean;
onChange: (newSelectedValue: number) => void;
onOpen?: () => void;
onClose?: () => void;
}
const baseClass = "component__team-dropdown";
const TeamsDropdown = ({
currentUserTeams,
selectedTeamId,
includeAll = true,
includeNoTeams = false,
isDisabled = false,
isSandboxMode = false,
onChange,
onOpen,
onClose,
}: ITeamsDropdownProps): JSX.Element => {
const teamOptions = useMemo(
() => generateDropdownOptions(currentUserTeams, includeAll, includeNoTeams),
[currentUserTeams, includeAll, includeNoTeams]
);
const selectedValue = teamOptions.find(
(option) => selectedTeamId === option.value
)
? selectedTeamId
: teamOptions[0]?.value;
const dropdownWrapperClasses = classnames(`${baseClass}-wrapper`, {
disabled: isDisabled || undefined,
});
const renderDropdown = () => {
if (isSandboxMode) {
const tooltipId = uniqueId();
return (
<>
<span data-tip data-for={tooltipId}>
<Dropdown
value={selectedValue}
placeholder="All teams"
options={teamOptions}
className={baseClass}
searchable={false}
disabled
/>
</span>
<ReactTooltip
type="light"
effect="solid"
id={tooltipId}
clickable
delayHide={200}
arrowColor="transparent"
overridePosition={(pos: { left: number; top: number }) => {
return {
left: pos.left - 150,
top: pos.top + 78,
};
}}
>
{`Teams allow you to segment hosts into specific groups of endpoints. This feature is included in Fleet Premium.`}
<br />
<a href="https://calendly.com/fleetdm/demo">
Contact us to learn more.
</a>
</ReactTooltip>
</>
);
}
if (teamOptions.length) {
return (
<Dropdown
value={selectedValue}
placeholder="All teams"
className={baseClass}
options={teamOptions}
searchable={false}
disabled={isDisabled}
onChange={onChange}
onOpen={onOpen}
onClose={onClose}
/>
);
}
};
return <div className={dropdownWrapperClasses}>{renderDropdown()}</div>;
};
export default TeamsDropdown;