2024-01-11 12:56:58 +00:00
|
|
|
import { Fragment, useState } from "react";
|
2024-05-08 17:31:20 +00:00
|
|
|
import { observer } from "mobx-react";
|
2024-01-11 12:56:58 +00:00
|
|
|
import { usePopper } from "react-popper";
|
|
|
|
import { Check, Search, Tag } from "lucide-react";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { Combobox } from "@headlessui/react";
|
2024-01-11 12:56:58 +00:00
|
|
|
// hooks
|
2024-04-08 13:41:47 +00:00
|
|
|
import { useLabel } from "@/hooks/store";
|
2024-01-11 12:56:58 +00:00
|
|
|
// components
|
|
|
|
|
|
|
|
export interface IIssueLabelSelect {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
2024-04-08 13:41:47 +00:00
|
|
|
values: string[];
|
2024-01-11 12:56:58 +00:00
|
|
|
onSelect: (_labelIds: string[]) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const IssueLabelSelect: React.FC<IIssueLabelSelect> = observer((props) => {
|
2024-04-08 13:41:47 +00:00
|
|
|
const { workspaceSlug, projectId, issueId, values, onSelect } = props;
|
2024-01-11 12:56:58 +00:00
|
|
|
// store hooks
|
2024-01-22 11:37:32 +00:00
|
|
|
const { fetchProjectLabels, getProjectLabels } = useLabel();
|
2024-01-11 12:56:58 +00:00
|
|
|
// states
|
|
|
|
const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(null);
|
|
|
|
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(null);
|
2024-03-06 13:09:14 +00:00
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
2024-01-11 12:56:58 +00:00
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
|
2024-01-22 11:37:32 +00:00
|
|
|
const projectLabels = getProjectLabels(projectId);
|
2024-01-11 12:56:58 +00:00
|
|
|
|
|
|
|
const fetchLabels = () => {
|
|
|
|
setIsLoading(true);
|
2024-01-22 11:37:32 +00:00
|
|
|
if (!projectLabels && workspaceSlug && projectId)
|
|
|
|
fetchProjectLabels(workspaceSlug, projectId).then(() => setIsLoading(false));
|
2024-01-11 12:56:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const options = (projectLabels ?? []).map((label) => ({
|
|
|
|
value: label.id,
|
|
|
|
query: label.name,
|
|
|
|
content: (
|
|
|
|
<div className="flex items-center justify-start gap-2 overflow-hidden">
|
|
|
|
<span
|
|
|
|
className="h-2.5 w-2.5 flex-shrink-0 rounded-full"
|
|
|
|
style={{
|
|
|
|
backgroundColor: label.color,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<div className="line-clamp-1 inline-block truncate">{label.name}</div>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const filteredOptions =
|
|
|
|
query === "" ? options : options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase()));
|
|
|
|
|
|
|
|
const { styles, attributes } = usePopper(referenceElement, popperElement, {
|
2024-03-20 12:37:11 +00:00
|
|
|
placement: "bottom-end",
|
2024-01-11 12:56:58 +00:00
|
|
|
modifiers: [
|
|
|
|
{
|
|
|
|
name: "preventOverflow",
|
|
|
|
options: {
|
|
|
|
padding: 12,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-04-08 13:41:47 +00:00
|
|
|
const issueLabels = values ?? [];
|
2024-01-11 12:56:58 +00:00
|
|
|
|
|
|
|
const label = (
|
|
|
|
<div
|
2024-03-06 13:09:14 +00:00
|
|
|
className={`relative flex flex-shrink-0 cursor-pointer items-center gap-1 rounded-full border border-custom-border-100 p-0.5 px-2 py-0.5 text-xs text-custom-text-300 transition-all hover:bg-custom-background-90 hover:text-custom-text-200`}
|
2024-01-11 12:56:58 +00:00
|
|
|
>
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Tag className="h-2.5 w-2.5" />
|
|
|
|
</div>
|
|
|
|
<div className="flex-shrink-0">Select Label</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2024-02-26 14:16:00 +00:00
|
|
|
const searchInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
if (query !== "" && e.key === "Escape") {
|
|
|
|
e.stopPropagation();
|
|
|
|
setQuery("");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-08 13:41:47 +00:00
|
|
|
if (!issueId || !values) return <></>;
|
2024-01-11 12:56:58 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Combobox
|
|
|
|
as="div"
|
|
|
|
className={`w-auto max-w-full flex-shrink-0 text-left`}
|
|
|
|
value={issueLabels}
|
|
|
|
onChange={(value) => onSelect(value)}
|
|
|
|
multiple
|
|
|
|
>
|
|
|
|
<Combobox.Button as={Fragment}>
|
|
|
|
<button
|
|
|
|
ref={setReferenceElement}
|
|
|
|
type="button"
|
2024-03-06 13:09:14 +00:00
|
|
|
className="cursor-pointer rounded"
|
2024-01-11 12:56:58 +00:00
|
|
|
onClick={() => !projectLabels && fetchLabels()}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</button>
|
|
|
|
</Combobox.Button>
|
|
|
|
|
|
|
|
<Combobox.Options className="fixed z-10">
|
|
|
|
<div
|
2024-02-21 12:22:35 +00:00
|
|
|
className={`z-10 my-1 w-48 whitespace-nowrap rounded border border-custom-border-300 bg-custom-background-100 py-2.5 text-xs shadow-custom-shadow-rg focus:outline-none`}
|
2024-01-11 12:56:58 +00:00
|
|
|
ref={setPopperElement}
|
|
|
|
style={styles.popper}
|
|
|
|
{...attributes.popper}
|
|
|
|
>
|
2024-02-21 12:22:35 +00:00
|
|
|
<div className="px-2">
|
|
|
|
<div className="flex w-full items-center justify-start rounded border border-custom-border-200 bg-custom-background-90 px-2">
|
|
|
|
<Search className="h-3.5 w-3.5 text-custom-text-300" />
|
|
|
|
<Combobox.Input
|
|
|
|
className="w-full bg-transparent px-2 py-1 text-xs text-custom-text-200 placeholder:text-custom-text-400 focus:outline-none"
|
|
|
|
value={query}
|
|
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
|
|
placeholder="Search"
|
|
|
|
displayValue={(assigned: any) => assigned?.name}
|
2024-02-26 14:16:00 +00:00
|
|
|
onKeyDown={searchInputKeyDown}
|
2024-02-21 12:22:35 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2024-01-11 12:56:58 +00:00
|
|
|
</div>
|
2024-03-06 13:09:14 +00:00
|
|
|
<div className={`vertical-scrollbar scrollbar-sm mt-2 max-h-48 space-y-1 overflow-y-scroll px-2 pr-0`}>
|
2024-01-11 12:56:58 +00:00
|
|
|
{isLoading ? (
|
|
|
|
<p className="text-center text-custom-text-200">Loading...</p>
|
|
|
|
) : filteredOptions.length > 0 ? (
|
|
|
|
filteredOptions.map((option) => (
|
|
|
|
<Combobox.Option
|
|
|
|
key={option.value}
|
|
|
|
value={option.value}
|
|
|
|
className={({ selected }) =>
|
|
|
|
`flex cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5 hover:bg-custom-background-80 ${
|
|
|
|
selected ? "text-custom-text-100" : "text-custom-text-200"
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{({ selected }) => (
|
|
|
|
<>
|
|
|
|
{option.content}
|
|
|
|
{selected && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Check className={`h-3.5 w-3.5`} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Combobox.Option>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<span className="flex items-center gap-2 p-1">
|
|
|
|
<p className="text-left text-custom-text-200 ">No matching results</p>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Combobox.Options>
|
|
|
|
</Combobox>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|