"use client"; import type { FocusEventHandler } from "react"; import { Avatar, CheckIcon, CloseButton, Combobox, Group, Input, Pill, PillsInput, Stack, Text, useCombobox, } from "@mantine/core"; import type { IntegrationKind } from "@homarr/definitions"; import { getIconUrl } from "@homarr/definitions"; import { useI18n } from "@homarr/translation/client"; import classes from "./widget-integration-select.module.css"; interface WidgetIntegrationSelectProps { label: string; onChange: (value: string[]) => void; value?: string[]; error?: string; onFocus?: FocusEventHandler; onBlur?: FocusEventHandler; data: IntegrationSelectOption[]; } export const WidgetIntegrationSelect = ({ data, onChange, value: valueProp, ...props }: WidgetIntegrationSelectProps) => { const t = useI18n(); const multiSelectValues = valueProp ?? []; const combobox = useCombobox({ onDropdownClose: () => combobox.resetSelectedOption(), onDropdownOpen: () => combobox.updateSelectedOptionIndex("active"), }); const handleValueSelect = (selectedValue: string) => onChange( multiSelectValues.includes(selectedValue) ? multiSelectValues.filter((value) => value !== selectedValue) : [...multiSelectValues, selectedValue], ); const handleValueRemove = (valueToRemove: string) => onChange(multiSelectValues.filter((value) => value !== valueToRemove)); const values = multiSelectValues.map((item) => { const option = data.find((integration) => integration.id === item); if (!option) { return null; } return handleValueRemove(item)} />; }); const options = data.map((item) => { return ( {multiSelectValues.includes(item.id) ? : null} {item.name} {item.url} ); }); return ( combobox.toggleDropdown()} {...props}> {values.length > 0 ? values : {t("common.multiSelect.placeholder")}} combobox.closeDropdown()} onKeyDown={(event) => { if (event.key !== "Backspace") return; event.preventDefault(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion handleValueRemove(multiSelectValues[multiSelectValues.length - 1]!); }} /> {options} ); }; export interface IntegrationSelectOption { id: string; name: string; url: string; kind: IntegrationKind; } interface IntegrationPillProps { option: IntegrationSelectOption; onRemove: () => void; } const IntegrationPill = ({ option, onRemove }: IntegrationPillProps) => ( {option.name} );