mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
eb06ef8049
* simplify TeamsDropdown component and update ManageHostPage to keep policy filter across team change * fix TeamDropdown for users not on global team
92 lines
2.0 KiB
TypeScript
92 lines
2.0 KiB
TypeScript
import React, { useContext, useMemo } from "react";
|
|
import classnames from "classnames";
|
|
import { ITeamSummary } from "interfaces/team";
|
|
|
|
// @ts-ignore
|
|
import Dropdown from "components/forms/fields/Dropdown";
|
|
import { AppContext } from "context/app";
|
|
|
|
const generateDropdownOptions = (
|
|
teams: ITeamSummary[] | undefined,
|
|
includeAll: boolean
|
|
) => {
|
|
if (!teams) {
|
|
return [];
|
|
}
|
|
|
|
const options = teams.map((team) => ({
|
|
disabled: false,
|
|
label: team.name,
|
|
value: team.id,
|
|
}));
|
|
|
|
if (includeAll) {
|
|
options.unshift({
|
|
disabled: false,
|
|
label: "All teams",
|
|
value: 0,
|
|
});
|
|
}
|
|
|
|
return options;
|
|
};
|
|
interface ITeamsDropdownProps {
|
|
currentUserTeams: ITeamSummary[];
|
|
selectedTeamId?: number;
|
|
includeAll?: boolean; // Include the "All Teams" option;
|
|
isDisabled?: boolean;
|
|
onChange: (newSelectedValue: number) => void;
|
|
onOpen?: () => void;
|
|
onClose?: () => void;
|
|
}
|
|
|
|
const baseClass = "component__team-dropdown";
|
|
|
|
const TeamsDropdown = ({
|
|
currentUserTeams,
|
|
selectedTeamId,
|
|
includeAll = true,
|
|
isDisabled,
|
|
onChange,
|
|
onOpen,
|
|
onClose,
|
|
}: ITeamsDropdownProps): JSX.Element => {
|
|
const { isOnGlobalTeam = false } = useContext(AppContext);
|
|
|
|
const teamOptions = useMemo(
|
|
() =>
|
|
generateDropdownOptions(currentUserTeams, includeAll && isOnGlobalTeam),
|
|
[currentUserTeams, includeAll, isOnGlobalTeam]
|
|
);
|
|
|
|
const selectedValue = teamOptions.find(
|
|
(option) => selectedTeamId === option.value
|
|
)
|
|
? selectedTeamId
|
|
: teamOptions[0]?.value;
|
|
|
|
const dropdownWrapperClasses = classnames(`${baseClass}-wrapper`, {
|
|
disabled: isDisabled || undefined,
|
|
});
|
|
|
|
return (
|
|
<div className={dropdownWrapperClasses}>
|
|
{teamOptions.length && (
|
|
<Dropdown
|
|
value={selectedValue}
|
|
placeholder="All teams"
|
|
className={baseClass}
|
|
options={teamOptions}
|
|
searchable={false}
|
|
disabled={isDisabled || false}
|
|
onChange={onChange}
|
|
onOpen={onOpen}
|
|
onClose={onClose}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default TeamsDropdown;
|