| 1 | import { |
| 2 | CheckIcon, |
| 3 | ChevronUpDownIcon, |
| 4 | CubeIcon, |
| 5 | } from "@heroicons/react/24/outline"; |
| 6 | import { Fragment } from "react"; |
| 7 | import { |
| 8 | Listbox, |
| 9 | ListboxButton, |
| 10 | ListboxOption, |
| 11 | ListboxOptions, |
| 12 | Transition, |
| 13 | } from "../../components/ui"; |
| 14 | import { DisplayInfo } from "../../pages/AddNewModel/configs/models"; |
| 15 | |
| 16 | interface ModelSelectionListboxProps { |
| 17 | /** Model selection listbox for choosing AI providers */ |
| 18 | selectedProvider: DisplayInfo; |
| 19 | setSelectedProvider: (val: DisplayInfo) => void; |
| 20 | topOptions?: DisplayInfo[]; |
| 21 | otherOptions?: DisplayInfo[]; |
| 22 | } |
| 23 | |
| 24 | function ModelSelectionListbox({ |
| 25 | selectedProvider, |
| 26 | setSelectedProvider, |
| 27 | topOptions = [], |
| 28 | otherOptions = [], |
| 29 | }: ModelSelectionListboxProps) { |
| 30 | return ( |
| 31 | <Listbox value={selectedProvider} onChange={setSelectedProvider}> |
| 32 | <div className="relative mb-2 mt-1"> |
| 33 | <ListboxButton className="bg-background border-border text-foreground hover:bg-input relative m-0 grid h-full w-full cursor-pointer grid-cols-[1fr_auto] items-center rounded-lg border border-solid py-2 pl-3 pr-10 text-left focus:outline-none"> |
| 34 | <span className="flex items-center"> |
| 35 | {window.vscMediaUrl && selectedProvider.icon && ( |
| 36 | <img |
| 37 | src={`${window.vscMediaUrl}/logos/${selectedProvider.icon}`} |
| 38 | className="mr-3 h-4 w-4 object-contain object-center" |
| 39 | /> |
| 40 | )} |
| 41 | <span className="text-xs">{selectedProvider.title}</span> |
| 42 | </span> |
| 43 | <span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2"> |
| 44 | <ChevronUpDownIcon |
| 45 | className="text-description-muted h-5 w-5" |
| 46 | aria-hidden="true" |
| 47 | /> |
| 48 | </span> |
| 49 | </ListboxButton> |
| 50 | |
| 51 | <Transition |
| 52 | as={Fragment} |
| 53 | leave="transition ease-in duration-100" |
| 54 | leaveFrom="opacity-100" |
| 55 | leaveTo="opacity-0" |
| 56 | > |
| 57 | <ListboxOptions className="bg-input rounded-default absolute left-0 top-full z-10 mt-1 h-fit w-3/5 overflow-y-auto p-0 focus:outline-none [&]:!max-h-[30vh]"> |
| 58 | {topOptions.length > 0 && ( |
| 59 | <div className="py-1"> |
| 60 | <div className="text-description-muted px-3 py-1 text-xs font-medium uppercase tracking-wider"> |
| 61 | Popular |
| 62 | </div> |
| 63 | {topOptions.map((option, index) => ( |
| 64 | <ListboxOption |
| 65 | key={index} |
| 66 | className={({ selected }: { selected: boolean }) => |
| 67 | ` ${selected ? "bg-list-active" : "bg-input"} hover:bg-list-active hover:text-list-active-foreground relative flex cursor-default cursor-pointer select-none items-center justify-between gap-2 p-1.5 px-3 py-2 pr-4` |
| 68 | } |
| 69 | value={option} |
| 70 | > |
| 71 | {({ selected }) => ( |
| 72 | <> |
| 73 | <div className="flex items-center"> |
| 74 | {option.title === "Autodetect" ? ( |
| 75 | <CubeIcon className="mr-2 h-4 w-4" /> |
| 76 | ) : ( |
| 77 | window.vscMediaUrl && |
| 78 | option.icon && ( |
| 79 | <img |
| 80 | src={`${window.vscMediaUrl}/logos/${option.icon}`} |
| 81 | className="mr-2 h-4 w-4 object-contain object-center" |
| 82 | /> |
| 83 | ) |
| 84 | )} |
| 85 | <span className="text-xs">{option.title}</span> |
| 86 | </div> |
| 87 | {selected && ( |
| 88 | <CheckIcon className="h-3 w-3" aria-hidden="true" /> |
| 89 | )} |
| 90 | </> |
| 91 | )} |
| 92 | </ListboxOption> |
| 93 | ))} |
| 94 | </div> |
| 95 | )} |
| 96 | {topOptions.length > 0 && otherOptions.length > 0 && ( |
| 97 | <div className="bg-border my-1 h-px min-h-px" /> |
| 98 | )} |
| 99 | {otherOptions.length > 0 && ( |
| 100 | <div className="py-1"> |
| 101 | <div className="text-description-muted px-3 py-1 text-xs font-medium uppercase tracking-wider"> |
| 102 | Additional providers |
| 103 | </div> |
| 104 | {otherOptions.map((option, index) => ( |
| 105 | <ListboxOption |
| 106 | key={index} |
| 107 | className={({ selected }: { selected: boolean }) => |
| 108 | ` ${selected ? "bg-list-active" : "bg-input"} hover:bg-list-active hover:text-list-active-foreground relative flex cursor-default cursor-pointer select-none items-center justify-between gap-2 p-1.5 px-3 py-2 pr-4` |
| 109 | } |
| 110 | value={option} |
| 111 | > |
| 112 | {({ selected }) => ( |
| 113 | <> |
| 114 | <div className="flex items-center"> |
| 115 | {option.title === "Autodetect" ? ( |
| 116 | <CubeIcon className="mr-2 h-4 w-4" /> |
| 117 | ) : ( |
| 118 | window.vscMediaUrl && |
| 119 | option.icon && ( |
| 120 | <img |
| 121 | src={`${window.vscMediaUrl}/logos/${option.icon}`} |
| 122 | className="mr-2 h-4 w-4 object-contain object-center" |
| 123 | /> |
| 124 | ) |
| 125 | )} |
| 126 | <span className="text-xs">{option.title}</span> |
| 127 | </div> |
| 128 | |
| 129 | {selected && ( |
| 130 | <CheckIcon className="h-3 w-3" aria-hidden="true" /> |
| 131 | )} |
| 132 | </> |
| 133 | )} |
| 134 | </ListboxOption> |
| 135 | ))} |
| 136 | </div> |
| 137 | )} |
| 138 | </ListboxOptions> |
| 139 | </Transition> |
| 140 | </div> |
| 141 | </Listbox> |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | export default ModelSelectionListbox; |