Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

10.4 KB
Raw Blame
import deepEqual from 'deep-equal';
import { t } from 'i18next';
import { Check, ChevronsUpDown, RefreshCcw, Trash2, X } from 'lucide-react';
import React, { useState, useRef } from 'react';

import { SelectUtilButton } from '@/components/custom/select-util-button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';
import { cn } from '@/lib/utils';

import { Button } from '../ui/button';
import { ScrollArea } from '../ui/scroll-area';

type SelectOption<T> = {
  value: T;
  label: string;
  description?: string;
};

type SearchableSelectProps<T> = {
  options: SelectOption<T>[];
  onChange: (value: T | null) => void;
  value: T | undefined;
  placeholder: string;
  disabled?: boolean;
  loading?: boolean;
  showDeselect?: boolean;
  onRefresh?: () => void;
  showRefresh?: boolean;
  onClose?: () => void;
  variant?: 'field' | 'outline' | 'ghost';
  /**Layout classes only; use variant for the surface */
  triggerClassName?: string;
  /**Widens the popover past the trigger, for compact or inline triggers */
  contentWidth?: string;
  valuesRendering?: (value: unknown) => React.ReactNode;
  openState?: {
    open: boolean;
    setOpen: (open: boolean) => void;
  };
  refreshOnSearch?: (searchValue: string) => void;
  /**Use to show the selected option when search doesn't return the selected option */
  cachedOptions?: {
    value: T;
    label: string;
  }[];
  onOptionDelete?: (value: T) => void;
  id?: string;
  'aria-describedby'?: string;
  'aria-invalid'?: boolean;
};

const useOpenState = (openStateInitializer?: {
  open: boolean;
  setOpen: (open: boolean) => void;
}) => {
  const [isOpen, setIsOpen] = useState(false);
  if (openStateInitializer) {
    return openStateInitializer;
  }
  return {
    open: isOpen,
    setOpen: setIsOpen,
  };
};
export const SearchableSelect = <T,>({
  options,
  onChange,
  value,
  placeholder,
  disabled,
  loading,
  showDeselect,
  onRefresh,
  showRefresh,
  onClose,
  variant = 'field',
  triggerClassName,
  contentWidth,
  valuesRendering,
  openState: openStateInitializer,
  refreshOnSearch,
  cachedOptions = [],
  onOptionDelete,
  id,
  'aria-describedby': ariaDescribedBy,
  'aria-invalid': ariaInvalid,
}: SearchableSelectProps<T>) => {
  const triggerRef = useRef<HTMLButtonElement>(null);
  const [searchTerm, setSearchTerm] = useState('');
  const { open, setOpen } = useOpenState(openStateInitializer);
  const triggerWidth = `${triggerRef.current?.clientWidth ?? 0}px`;
  const selectedOption =
    [...options, ...cachedOptions].find((option) =>
      deepEqual(option.value, value),
    ) ?? undefined;
  const filterOptionsIndices = options
    .map((option, index) => {
      return {
        label: option.label,
        value: option.value,
        index: index,
        description: option.description ?? '',
      };
    })
    .filter((option) => {
      if (refreshOnSearch || searchTerm.length === 0) {
        return true;
      }
      return (
        option.label.toLowerCase().includes(searchTerm.toLowerCase()) ||
        option.description.toLowerCase().includes(searchTerm.toLowerCase())
      );
    })
    .map((option) => option.index);

  const onSelect = (index: string) => {
    const optionIndex =
      Number.isInteger(parseInt(index)) && !Number.isNaN(parseInt(index))
        ? parseInt(index)
        : -1;
    setSearchTerm('');

    if (optionIndex === -1) {
      return;
    }
    const option = options[optionIndex];
    onChange(option.value);
  };
  const handleOpenChange = (nextOpen: boolean) => {
    if (!nextOpen) {
      onClose?.();
    }
    if (refreshOnSearch && searchTerm.length > 0) {
      refreshOnSearch('');
      setSearchTerm('');
    }
    setOpen(nextOpen);
  };
  return (
    <Popover modal={true} open={open} onOpenChange={handleOpenChange}>
      <PopoverTrigger
        asChild
        className={cn({
          'cursor-not-allowed': disabled,
        })}
        onClick={(e) => {
          if (disabled) {
            e.preventDefault();
          }
          e.stopPropagation();
        }}
      >
        <div className="relative">
          <Button
            ref={triggerRef}
            id={id}
            aria-describedby={ariaDescribedBy}
            aria-invalid={ariaInvalid}
            variant={variant}
            disabled={disabled}
            role="combobox"
            loading={loading}
            aria-expanded={open}
            className={cn('w-full justify-between', triggerClassName)}
            onClick={(e) => {
              handleOpenChange(!open);
              e.preventDefault();
            }}
          >
            <span className="flex w-full truncate select-none">
              {selectedOption
                ? valuesRendering
                  ? valuesRendering(selectedOption.value)
                  : selectedOption.label
                : placeholder}
            </span>
            <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
          </Button>
          <div className="right-10 top-2 absolute flex gap-2  z-50 items-center">
            {showDeselect && !disabled && selectedOption && !loading && (
              <SelectUtilButton
                tooltipText={t('Unset')}
                onClick={(e) => {
                  e.stopPropagation();
                  e.preventDefault();
                  onChange(null);
                }}
                Icon={X}
              ></SelectUtilButton>
            )}
            {showRefresh && !loading && (
              <SelectUtilButton
                tooltipText={t('Refresh')}
                onClick={(e) => {
                  e.stopPropagation();
                  e.preventDefault();
                  if (onRefresh) {
                    onRefresh();
                  }
                }}
                Icon={RefreshCcw}
              ></SelectUtilButton>
            )}
          </div>
        </div>
      </PopoverTrigger>
      <PopoverContent
        onPointerDownOutside={(e) => {
          const target = e.detail.originalEvent.target;
          if (target instanceof Node && triggerRef.current?.contains(target)) {
            e.preventDefault();
          }
        }}
        style={{
          maxWidth: contentWidth ?? triggerWidth,
          minWidth: contentWidth ?? triggerWidth,
        }}
        className="min-w-full w-full p-0"
      >
        <Command className="w-full" shouldFilter={false}>
          <CommandInput
            placeholder={t(placeholder)}
            value={searchTerm}
            onValueChange={(e) => {
              setSearchTerm(e);
              if (refreshOnSearch) {
                refreshOnSearch(e);
              }
            }}
          />
          {filterOptionsIndices.length === 0 && (
            <CommandEmpty>{t('No results found.')}</CommandEmpty>
          )}

          <CommandGroup>
            <CommandList>
              <ScrollArea
                className="h-full"
                viewPortClassName={'max-h-[200px]'}
              >
                {filterOptionsIndices &&
                  !loading &&
                  filterOptionsIndices.map((filterIndex) => {
                    const option = options[filterIndex];
                    if (!option) {
                      return null;
                    }
                    return (
                      <CommandItem
                        key={filterIndex}
                        value={String(filterIndex)}
                        onSelect={(currentValue) => {
                          setOpen(false);
                          onSelect(currentValue);
                        }}
                        className={cn(
                          'flex gap-2 flex-col items-start',
                          onOptionDelete && 'group/option',
                        )}
                      >
                        <div className="flex gap-2 items-center justify-between w-full">
                          {option.label === '' ? (
                            <span className="">&nbsp;</span>
                          ) : valuesRendering ? (
                            valuesRendering(option.value)
                          ) : (
                            <span className="truncate">{option.label}</span>
                          )}
                          <div className="relative shrink-0 w-4 h-4">
                            {onOptionDelete && (
                              <button
                                type="button"
                                className={cn(
                                  'absolute inset-0 flex items-center justify-center text-gray-11 hover:text-danger-11',
                                  'opacity-0 group-hover/option:opacity-100',
                                )}
                                onClick={(e) => {
                                  e.stopPropagation();
                                  e.preventDefault();
                                  onOptionDelete(option.value);
                                }}
                              >
                                <Trash2 className="h-3.5 w-3.5" />
                              </button>
                            )}
                            <Check
                              className={cn(
                                'absolute inset-0 w-4 h-4',
                                selectedOption?.value !== option.value
                                  ? 'opacity-0'
                                  : cn(
                                      'opacity-100',
                                      onOptionDelete &&
                                        'group-hover/option:opacity-0',
                                    ),
                              )}
                            />
                          </div>
                        </div>
                        {option.description && (
                          <div className="text-sm text-gray-11">
                            {option.description}
                          </div>
                        )}
                      </CommandItem>
                    );
                  })}
                {loading && (
                  <CommandItem disabled>{t('Loading...')}</CommandItem>
                )}
              </ScrollArea>
            </CommandList>
          </CommandGroup>
        </Command>
      </PopoverContent>
    </Popover>
  );
};

SearchableSelect.displayName = 'SearchableSelect';