import { Fragment, ReactNode, useEffect, useState } from "react";
import { observer } from "mobx-react-lite";
import { Combobox } from "@headlessui/react";
import { usePopper } from "react-popper";
import { Placement } from "@popperjs/core";
import { Check, ChevronDown, Search } from "lucide-react";
// hooks
import { useApplication, useProjectState } from "hooks/store";
// icons
import { StateGroupIcon } from "@plane/ui";
// helpers
import { cn } from "helpers/common.helper";
// types
import { IState } from "@plane/types";
import { TButtonVariants } from "./types";
type Props = {
button?: ReactNode;
buttonClassName?: string;
buttonContainerClassName?: string;
buttonVariant: TButtonVariants;
className?: string;
disabled?: boolean;
dropdownArrow?: boolean;
onChange: (val: string) => void;
placement?: Placement;
projectId: string;
value: string;
};
type ButtonProps = {
className?: string;
dropdownArrow: boolean;
hideText?: boolean;
state: IState | undefined;
};
const BorderButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, state } = props;
return (
{!hideText && {state?.name ?? "State"}}
{dropdownArrow && }
);
};
const BackgroundButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, state } = props;
return (
{!hideText && {state?.name ?? "State"}}
{dropdownArrow && }
);
};
const TransparentButton = (props: ButtonProps) => {
const { className, dropdownArrow, hideText = false, state } = props;
return (
{!hideText && {state?.name ?? "State"}}
{dropdownArrow && }
);
};
export const StateDropdown: React.FC = observer((props) => {
const {
button,
buttonClassName,
buttonContainerClassName,
buttonVariant,
className = "",
disabled = false,
dropdownArrow = false,
onChange,
placement,
projectId,
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,
},
},
],
});
// store hooks
const {
router: { workspaceSlug },
} = useApplication();
const { fetchProjectStates, getProjectStates, getStateById } = useProjectState();
const statesList = getProjectStates(projectId);
const options = statesList?.map((state) => ({
value: state.id,
query: `${state?.name}`,
content: (
{state?.name}
),
}));
const filteredOptions =
query === "" ? options : options?.filter((o) => o.query.toLowerCase().includes(query.toLowerCase()));
// fetch states of the project if not already present in the store
useEffect(() => {
if (!workspaceSlug) return;
if (!statesList) fetchProjectStates(workspaceSlug, projectId);
}, [fetchProjectStates, projectId, statesList, workspaceSlug]);
const selectedState = getStateById(value);
return (
{button ? (
) : (
)}
setQuery(e.target.value)}
placeholder="Search"
displayValue={(assigned: any) => assigned?.name}
/>
{filteredOptions ? (
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 matches found
)
) : (
Loading...
)}
);
});