import { useEffect, useState } from 'react'; import { Plural, Trans } from '@lingui/react/macro'; import { WebhookTriggerEvents } from '@prisma/client'; import { Check, ChevronsUpDown } from 'lucide-react'; import { toFriendlyWebhookEventName } from '@documenso/lib/universal/webhook/to-friendly-webhook-event-name'; import { cn } from '@documenso/ui/lib/utils'; import { Button } from '@documenso/ui/primitives/button'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, } from '@documenso/ui/primitives/command'; import { Popover, PopoverContent, PopoverTrigger } from '@documenso/ui/primitives/popover'; import { truncateTitle } from '~/utils/truncate-title'; type WebhookMultiSelectComboboxProps = { listValues: string[]; onChange: (_values: string[]) => void; }; export const WebhookMultiSelectCombobox = ({ listValues, onChange, }: WebhookMultiSelectComboboxProps) => { const [isOpen, setIsOpen] = useState(false); const [selectedValues, setSelectedValues] = useState([]); const triggerEvents = Object.values(WebhookTriggerEvents); useEffect(() => { setSelectedValues(listValues); }, [listValues]); const allEvents = [...new Set([...triggerEvents, ...selectedValues])]; const handleSelect = (currentValue: string) => { let newSelectedValues; if (selectedValues.includes(currentValue)) { newSelectedValues = selectedValues.filter((value) => value !== currentValue); } else { newSelectedValues = [...selectedValues, currentValue]; } setSelectedValues(newSelectedValues); onChange(newSelectedValues); setIsOpen(false); }; return ( toFriendlyWebhookEventName(v)).join(', '), 15, )} /> No value found. {allEvents.map((value: string, i: number) => ( handleSelect(value)}> {toFriendlyWebhookEventName(value)} ))} ); };