2024-02-28 09:48:11 +00:00
|
|
|
import { Fragment, useRef, useState } from "react";
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { ChevronDown } from "lucide-react";
|
2024-05-08 17:31:20 +00:00
|
|
|
// headless ui
|
2024-03-19 14:38:35 +00:00
|
|
|
import { Combobox } from "@headlessui/react";
|
2024-05-08 17:31:20 +00:00
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { cn } from "@/helpers/common.helper";
|
2024-05-08 17:31:20 +00:00
|
|
|
// hooks
|
2024-03-19 14:38:35 +00:00
|
|
|
import { useMember } from "@/hooks/store";
|
|
|
|
import { useDropdownKeyDown } from "@/hooks/use-dropdown-key-down";
|
|
|
|
import useOutsideClickDetector from "@/hooks/use-outside-click-detector";
|
2024-02-28 09:48:11 +00:00
|
|
|
// components
|
|
|
|
import { DropdownButton } from "../buttons";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { BUTTON_VARIANTS_WITH_TEXT } from "../constants";
|
|
|
|
import { ButtonAvatars } from "./avatar";
|
2024-05-08 17:31:20 +00:00
|
|
|
// constants
|
2024-03-06 13:09:14 +00:00
|
|
|
import { MemberOptions } from "./member-options";
|
2024-05-08 17:31:20 +00:00
|
|
|
// types
|
2024-02-28 09:48:11 +00:00
|
|
|
import { MemberDropdownProps } from "./types";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
projectId?: string;
|
|
|
|
onClose?: () => void;
|
|
|
|
} & MemberDropdownProps;
|
|
|
|
|
|
|
|
export const MemberDropdown: React.FC<Props> = observer((props) => {
|
|
|
|
const {
|
|
|
|
button,
|
|
|
|
buttonClassName,
|
|
|
|
buttonContainerClassName,
|
|
|
|
buttonVariant,
|
|
|
|
className = "",
|
|
|
|
disabled = false,
|
|
|
|
dropdownArrow = false,
|
|
|
|
dropdownArrowClassName = "",
|
|
|
|
hideIcon = false,
|
|
|
|
multiple,
|
|
|
|
onChange,
|
|
|
|
onClose,
|
|
|
|
placeholder = "Members",
|
2024-03-21 15:29:08 +00:00
|
|
|
tooltipContent,
|
2024-02-28 09:48:11 +00:00
|
|
|
placement,
|
|
|
|
projectId,
|
|
|
|
showTooltip = false,
|
|
|
|
tabIndex,
|
|
|
|
value,
|
|
|
|
} = props;
|
|
|
|
// states
|
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
// refs
|
|
|
|
const dropdownRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
// popper-js refs
|
|
|
|
const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(null);
|
|
|
|
|
|
|
|
const { getUserDetails } = useMember();
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2024-02-28 09:48:11 +00:00
|
|
|
const comboboxProps: any = {
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
disabled,
|
|
|
|
};
|
|
|
|
if (multiple) comboboxProps.multiple = true;
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
if (!isOpen) return;
|
|
|
|
setIsOpen(false);
|
|
|
|
onClose && onClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleDropdown = () => {
|
|
|
|
setIsOpen((prevIsOpen) => !prevIsOpen);
|
2024-02-29 07:53:37 +00:00
|
|
|
if (isOpen) onClose && onClose();
|
2024-02-28 09:48:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const dropdownOnChange = (val: string & string[]) => {
|
|
|
|
onChange(val);
|
|
|
|
if (!multiple) handleClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown = useDropdownKeyDown(toggleDropdown, handleClose);
|
|
|
|
|
|
|
|
const handleOnClick = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
toggleDropdown();
|
|
|
|
};
|
|
|
|
|
|
|
|
useOutsideClickDetector(dropdownRef, handleClose);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Combobox
|
|
|
|
as="div"
|
|
|
|
ref={dropdownRef}
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
className={cn("h-full", className)}
|
|
|
|
onChange={dropdownOnChange}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
{...comboboxProps}
|
|
|
|
>
|
|
|
|
<Combobox.Button as={Fragment}>
|
|
|
|
{button ? (
|
|
|
|
<button
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
|
|
|
className={cn("clickable block h-full w-full outline-none", buttonContainerClassName)}
|
|
|
|
onClick={handleOnClick}
|
|
|
|
>
|
|
|
|
{button}
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
|
|
|
className={cn(
|
|
|
|
"clickable block h-full max-w-full outline-none",
|
|
|
|
{
|
|
|
|
"cursor-not-allowed text-custom-text-200": disabled,
|
|
|
|
"cursor-pointer": !disabled,
|
|
|
|
},
|
|
|
|
buttonContainerClassName
|
|
|
|
)}
|
|
|
|
onClick={handleOnClick}
|
|
|
|
>
|
|
|
|
<DropdownButton
|
|
|
|
className={buttonClassName}
|
|
|
|
isActive={isOpen}
|
|
|
|
tooltipHeading={placeholder}
|
2024-03-21 15:29:08 +00:00
|
|
|
tooltipContent={tooltipContent ?? `${value?.length ?? 0} assignee${value?.length !== 1 ? "s" : ""}`}
|
2024-02-28 09:48:11 +00:00
|
|
|
showTooltip={showTooltip}
|
|
|
|
variant={buttonVariant}
|
|
|
|
>
|
|
|
|
{!hideIcon && <ButtonAvatars showTooltip={showTooltip} userIds={value} />}
|
|
|
|
{BUTTON_VARIANTS_WITH_TEXT.includes(buttonVariant) && (
|
|
|
|
<span className="flex-grow truncate text-xs leading-5">
|
|
|
|
{Array.isArray(value) && value.length > 0
|
|
|
|
? value.length === 1
|
|
|
|
? getUserDetails(value[0])?.display_name
|
|
|
|
: ""
|
|
|
|
: placeholder}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{dropdownArrow && (
|
|
|
|
<ChevronDown className={cn("h-2.5 w-2.5 flex-shrink-0", dropdownArrowClassName)} aria-hidden="true" />
|
|
|
|
)}
|
|
|
|
</DropdownButton>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Combobox.Button>
|
|
|
|
{isOpen && (
|
|
|
|
<MemberOptions
|
|
|
|
isOpen={isOpen}
|
|
|
|
projectId={projectId}
|
|
|
|
placement={placement}
|
|
|
|
referenceElement={referenceElement}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
});
|