import { Fragment, ReactNode, useState } from "react";
import { Combobox } from "@headlessui/react";
import { usePopper } from "react-popper";
import { Placement } from "@popperjs/core";
import { Check, ChevronDown, Search } from "lucide-react";
// icons
import { PriorityIcon } from "@plane/ui";
// helpers
import { cn } from "helpers/common.helper";
// types
import { TIssuePriorities } from "@plane/types";
import { TButtonVariants } from "./types";
// constants
import { ISSUE_PRIORITIES } from "constants/issue";
type Props = {
button?: ReactNode;
buttonClassName?: string;
buttonContainerClassName?: string;
buttonVariant: TButtonVariants;
className?: string;
disabled?: boolean;
dropdownArrow?: boolean;
highlightUrgent?: boolean;
onChange: (val: TIssuePriorities) => void;
placement?: Placement;
value: TIssuePriorities;
};
type ButtonProps = {
className?: string;
dropdownArrow: boolean;
hideText?: boolean;
highlightUrgent: boolean;
priority: TIssuePriorities;
};
const BorderButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, highlightUrgent, priority } = props;
const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority);
const priorityClasses = {
urgent: "bg-red-500/20 text-red-950 border-red-500",
high: "bg-orange-500/20 text-orange-950 border-orange-500",
medium: "bg-yellow-500/20 text-yellow-950 border-yellow-500",
low: "bg-custom-primary-100/20 text-custom-primary-950 border-custom-primary-100",
none: "bg-custom-background-80 border-custom-border-300",
};
return (
{!hideText &&
{priorityDetails?.title}}
{dropdownArrow &&
}
);
};
const BackgroundButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, highlightUrgent, priority } = props;
const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority);
const priorityClasses = {
urgent: "bg-red-500/20 text-red-950",
high: "bg-orange-500/20 text-orange-950",
medium: "bg-yellow-500/20 text-yellow-950",
low: "bg-blue-500/20 text-blue-950",
none: "bg-custom-background-80",
};
return (
{!hideText &&
{priorityDetails?.title}}
{dropdownArrow &&
}
);
};
const TransparentButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, highlightUrgent, priority } = props;
const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority);
const priorityClasses = {
urgent: "text-red-950",
high: "text-orange-950",
medium: "text-yellow-950",
low: "text-blue-950",
none: "",
};
return (
{!hideText &&
{priorityDetails?.title}}
{dropdownArrow &&
}
);
};
export const PriorityDropdown: React.FC = (props) => {
const {
button,
buttonClassName,
buttonContainerClassName,
buttonVariant,
className = "",
disabled = false,
dropdownArrow = false,
highlightUrgent = true,
onChange,
placement,
value,
} = props;
// states
const [query, setQuery] = useState("");
// popper-js refs
const [referenceElement, setReferenceElement] = useState(null);
const [popperElement, setPopperElement] = useState(null);
// popper-js init
const { styles, attributes } = usePopper(referenceElement, popperElement, {
placement: placement ?? "bottom-start",
modifiers: [
{
name: "preventOverflow",
options: {
padding: 12,
},
},
],
});
const options = ISSUE_PRIORITIES.map((priority) => {
const priorityClasses = {
urgent: "bg-red-500/20 text-red-950 border-red-500",
high: "bg-orange-500/20 text-orange-950 border-orange-500",
medium: "bg-yellow-500/20 text-yellow-950 border-yellow-500",
low: "bg-custom-primary-100/20 text-custom-primary-950 border-custom-primary-100",
none: "bg-custom-background-80 border-custom-border-300",
};
return {
value: priority.key,
query: priority.key,
content: (
),
};
});
const filteredOptions =
query === "" ? options : options.filter((o) => o.query.toLowerCase().includes(query.toLowerCase()));
return (
{button ? (
) : (
)}
setQuery(e.target.value)}
placeholder="Search"
displayValue={(assigned: any) => assigned?.name}
/>
{filteredOptions.length > 0 ? (
filteredOptions.map((option) => (
`w-full truncate flex items-center justify-between gap-2 rounded px-1 py-1.5 cursor-pointer select-none ${
active ? "bg-custom-background-80" : ""
} ${selected ? "text-custom-text-100" : "text-custom-text-200"}`
}
>
{({ selected }) => (
<>
{option.content}
{selected && }
>
)}
))
) : (
No matching results
)}
);
};