/* * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL */ import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Loader2, Search, X } from 'lucide-react'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; import { useObjectList, useObjectLabel, useNoPermissionMessage } from '@/lib/objectOptions'; import type { Schema } from '@/types/schema'; interface ObjectPickerProps { schema: Schema; objectName: string; value: string; onChange: (id: string) => void; onClear?: () => void; placeholder?: string; } export function ObjectPicker({ schema, objectName, value, onChange, onClear, placeholder }: ObjectPickerProps) { const { t } = useTranslation(); const list = useObjectList(objectName, schema); const fromList = list.options.find((o) => o.id === value)?.label; const { label: cheapLabel, loading: labelLoading } = useObjectLabel( objectName, fromList ? null : value || null, schema, ); const display = fromList ?? cheapLabel; const [open, setOpen] = useState(false); const [tooltipOpen, setTooltipOpen] = useState(false); const noPermissionMessage = useNoPermissionMessage(schema, objectName); const handleOpenChange = (next: boolean) => { setOpen(next); if (next) void list.ensureLoaded(); }; return (
{value && ( {labelLoading ? ( ) : ( {display ?? value} )} {onClear && ( )} )} {!value && placeholder && {placeholder}} {noPermissionMessage ? ( {noPermissionMessage} ) : ( {list.loading && (
{t('common.loading', 'Loading...')}
)} {!list.loading && {t('common.noResultsDot', 'No results.')}} {list.options.map((opt) => ( { onChange(opt.id); setOpen(false); }} > {opt.label} ))}
)}
); }