All files / web/src/components/settings model-selector.tsx

0% Statements 0/128
100% Branches 1/1
100% Functions 1/1
0% Lines 0/128

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                                                                                                                                                                                                                                                                                                                                                     
'use client';
 
import { Badge } from '@web/components/ui/badge';
import { Button } from '@web/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@web/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@web/components/ui/popover';
import { Skeleton } from '@web/components/ui/skeleton';
import { cn } from '@web/utils/ui/utils';
import { CheckIcon, ChevronsUpDownIcon } from 'lucide-react';
import type { ReactElement } from 'react';
import { useId, useMemo, useState } from 'react';
 
export interface ModelOption {
  id: string;
  modelName: string;
  providerName: string;
  modelType: 'text' | 'embed';
  searchLabel: string;
}
 
export interface ModelSelectorProps {
  /** Label for the setting field */
  label: string;
  /** Description of what this model setting is used for */
  description: string;
  /** Optional recommendation text */
  recommendation?: string;
  /** Currently selected model ID */
  value: string | null;
  /** Callback when model selection changes */
  onChange: (value: string) => void;
  /** Available model options to choose from */
  modelOptions: ModelOption[];
  /** Whether the component is in loading state */
  isLoading: boolean;
  /** Whether this field is required */
  required?: boolean;
}
 
export function ModelSelector({
  label,
  description,
  recommendation,
  value,
  onChange,
  modelOptions,
  isLoading,
  required = true,
}: ModelSelectorProps): ReactElement {
  const [open, setOpen] = useState(false);
 
  // Generate unique IDs for ARIA attributes
  const baseId = useId();
  const listboxId = `${baseId}-listbox`;
  const labelId = `${baseId}-label`;
  const descriptionId = `${baseId}-description`;
 
  const selectedModel = useMemo(
    () => modelOptions.find((m) => m.id === value),
    [modelOptions, value],
  );
 
  const showNotConfiguredWarning =
    required && !value && modelOptions.length > 0;
 
  return (
    <div className="grid gap-4 md:grid-cols-[1fr,300px] items-start py-4 border-b last:border-b-0">
      <div className="space-y-1">
        <h4 id={labelId} className="font-medium">
          {label}
          {required && <span className="text-destructive ml-1">*</span>}
        </h4>
        <p id={descriptionId} className="text-sm text-muted-foreground">
          {description}
        </p>
        {recommendation && (
          <p className="text-sm text-muted-foreground">
            Recommended: {recommendation}
          </p>
        )}
      </div>
      <div className="space-y-2">
        {isLoading ? (
          <Skeleton
            className="h-10 w-full"
            aria-label="Loading model options"
          />
        ) : (
          <Popover open={open} onOpenChange={setOpen}>
            <PopoverTrigger asChild>
              <Button
                variant="outline"
                role="combobox"
                aria-expanded={open}
                aria-haspopup="listbox"
                aria-controls={open ? listboxId : undefined}
                aria-labelledby={labelId}
                aria-describedby={descriptionId}
                aria-required={required}
                aria-invalid={showNotConfiguredWarning}
                className={cn(
                  'w-[400px] justify-between',
                  showNotConfiguredWarning && 'border-destructive',
                )}
              >
                {selectedModel ? (
                  <div className="flex items-center gap-2 truncate">
                    <span className="truncate">{selectedModel.modelName}</span>
                    <Badge variant="outline" className="text-xs shrink-0">
                      {selectedModel.providerName}
                    </Badge>
                  </div>
                ) : (
                  <span className="text-muted-foreground">Select a model</span>
                )}
                <ChevronsUpDownIcon
                  className="ml-2 h-4 w-4 shrink-0 opacity-50"
                  aria-hidden="true"
                />
              </Button>
            </PopoverTrigger>
            <PopoverContent className="w-[400px] p-0" align="start">
              <Command>
                <CommandInput
                  placeholder="Search models..."
                  aria-label={`Search ${label} models`}
                />
                <CommandList
                  id={listboxId}
                  role="listbox"
                  aria-label={`${label} options`}
                >
                  <CommandEmpty>No model found.</CommandEmpty>
                  <CommandGroup>
                    {modelOptions.map((model) => (
                      <CommandItem
                        key={model.id}
                        value={model.searchLabel}
                        onSelect={() => {
                          onChange(model.id);
                          setOpen(false);
                        }}
                        role="option"
                        aria-selected={value === model.id}
                      >
                        <CheckIcon
                          className={cn(
                            'mr-2 h-4 w-4',
                            value === model.id ? 'opacity-100' : 'opacity-0',
                          )}
                          aria-hidden="true"
                        />
                        <div className="flex items-center gap-2">
                          <span>{model.modelName}</span>
                          <Badge variant="outline" className="text-xs">
                            {model.providerName}
                          </Badge>
                        </div>
                      </CommandItem>
                    ))}
                  </CommandGroup>
                </CommandList>
              </Command>
            </PopoverContent>
          </Popover>
        )}
        {showNotConfiguredWarning && (
          <p className="text-sm text-destructive" role="alert">
            This field is required
          </p>
        )}
      </div>
    </div>
  );
}