import React, { ReactNode } from "react";
import classnames from "classnames";
import PATHS from "router/paths";
import { IDropdownOption } from "interfaces/dropdownOption";
import { ILabelSummary } from "interfaces/label";
// @ts-ignore
import Dropdown from "components/forms/fields/Dropdown";
import Radio from "components/forms/fields/Radio";
import DataError from "components/DataError";
import Spinner from "components/Spinner";
import Checkbox from "components/forms/fields/Checkbox";
import CustomLink from "components/CustomLink";
const baseClass = "target-label-selector";
interface ITargetChooserProps {
selectedTarget: string;
onSelect: (val: string) => void;
disableOptions?: boolean;
title: string | null;
subTitle?: string;
}
const TargetChooser = ({
selectedTarget,
onSelect,
disableOptions = false,
title,
subTitle,
}: ITargetChooserProps) => {
return (
{title &&
{title}
}
{subTitle &&
{subTitle}
}
);
};
interface ILabelChooserProps {
isError: boolean;
isLoading: boolean;
labels: ILabelSummary[];
selectedLabels: Record;
selectedCustomTarget?: string;
customTargetOptions?: IDropdownOption[];
customHelpText?: ReactNode;
dropdownHelpText?: ReactNode;
onSelectCustomTarget?: (val: string) => void;
onSelectLabel: ({ name, value }: { name: string; value: boolean }) => void;
disableOptions: boolean;
}
const LabelChooser = ({
isError,
isLoading,
labels,
customHelpText,
dropdownHelpText,
selectedLabels,
selectedCustomTarget,
customTargetOptions = [],
onSelectCustomTarget,
onSelectLabel,
disableOptions,
}: ILabelChooserProps) => {
const getHelpText = (value?: string) => {
if (dropdownHelpText) return dropdownHelpText;
return customTargetOptions.find((option) => option.value === value)
?.helpText;
};
if (isLoading) {
return ;
}
if (isError) {
return ;
}
// Not using here as we want to include short string only
if (!labels.length) {
return (
to target
specific hosts.
);
}
return (
{!!customTargetOptions.length && (
)}
{customTargetOptions.length
? getHelpText(selectedCustomTarget)
: customHelpText}
{labels.map((label) => {
return (
{label.name}
);
})}
);
};
interface IDropdownTargetLabelSelectorProps {
selectedTargetType: string;
selectedCustomTarget?: string;
customTargetOptions?: IDropdownOption[];
selectedLabels: Record;
labels: ILabelSummary[];
customHelpText?: ReactNode;
/** set this prop to show a help text. If it is included then it will override
* the selected options defined `helpText`
*/
dropdownHelpText?: ReactNode;
isLoadingLabels?: boolean;
isErrorLabels?: boolean;
className?: string;
onSelectTargetType: (val: string) => void;
onSelectCustomTarget?: (val: string) => void;
onSelectLabel: ({ name, value }: { name: string; value: boolean }) => void;
disableOptions?: boolean;
title?: string;
suppressTitle?: boolean;
subTitle?: string;
}
/**
* DropdownTargetLabelSelector lets the user target "All hosts" or a "Custom"
* set of hosts, scoped by a single label mode chosen from a dropdown (e.g.
* include any / include all / exclude any). Used by reports, software, and the
* not-yet-migrated policy forms. For the tabbed include + exclude experience,
* use the sibling TargetLabelSelector.
*/
const DropdownTargetLabelSelector = ({
selectedTargetType,
selectedCustomTarget,
customTargetOptions = [],
selectedLabels,
dropdownHelpText,
customHelpText,
className,
labels,
isLoadingLabels = false,
isErrorLabels = false,
onSelectTargetType,
onSelectCustomTarget,
onSelectLabel,
disableOptions = false,
title = "Target",
subTitle,
suppressTitle = false,
}: IDropdownTargetLabelSelectorProps) => {
const classNames = classnames(baseClass, className, "form");
return (
{selectedTargetType === "Custom" && (
)}
);
};
export default DropdownTargetLabelSelector;