| 1 | import { |
| 2 | CheckIcon, |
| 3 | ChevronUpDownIcon, |
| 4 | CubeIcon, |
| 5 | } from "@heroicons/react/24/outline"; |
| 6 | import { Fragment } from "react"; |
| 7 | import styled from "styled-components"; |
| 8 | import { |
| 9 | defaultBorderRadius, |
| 10 | lightGray, |
| 11 | vscBackground, |
| 12 | vscForeground, |
| 13 | vscInputBackground, |
| 14 | vscListActiveBackground, |
| 15 | vscListActiveForeground, |
| 16 | } from ".."; |
| 17 | import { |
| 18 | Listbox, |
| 19 | ListboxButton, |
| 20 | ListboxOption, |
| 21 | ListboxOptions, |
| 22 | Transition, |
| 23 | } from "../../components/ui"; |
| 24 | import { DisplayInfo } from "../../pages/AddNewModel/configs/models"; |
| 25 | export const StyledListbox = styled(Listbox)` |
| 26 | background-color: ${vscBackground}; |
| 27 | `; |
| 28 | |
| 29 | export const StyledListboxButton = styled(ListboxButton)` |
| 30 | cursor: pointer; |
| 31 | background-color: ${vscBackground}; |
| 32 | text-align: left; |
| 33 | |
| 34 | padding-left: 0.75rem; |
| 35 | padding-right: 2.5rem; |
| 36 | padding-top: 0.5rem; |
| 37 | padding-bottom: 0.5rem; |
| 38 | |
| 39 | border-radius: 0.5em; |
| 40 | border: 1px solid ${lightGray}; |
| 41 | |
| 42 | margin: 0; |
| 43 | height: 100%; |
| 44 | width: 100%; |
| 45 | |
| 46 | position: relative; |
| 47 | |
| 48 | display: grid; |
| 49 | grid-template-columns: 1fr auto; |
| 50 | align-items: center; |
| 51 | |
| 52 | color: ${vscForeground}; |
| 53 | |
| 54 | &:focus { |
| 55 | outline: none; |
| 56 | } |
| 57 | |
| 58 | &:hover { |
| 59 | background-color: ${vscInputBackground}; |
| 60 | } |
| 61 | `; |
| 62 | |
| 63 | export const StyledListboxOptions = styled(ListboxOptions)` |
| 64 | background-color: ${vscInputBackground}; |
| 65 | padding: 0; |
| 66 | |
| 67 | position: absolute; |
| 68 | top: 100%; |
| 69 | left: 0; |
| 70 | |
| 71 | margin-top: 0.25rem; |
| 72 | |
| 73 | height: fit-content; |
| 74 | max-height: 15rem; |
| 75 | width: 60%; |
| 76 | |
| 77 | border-radius: ${defaultBorderRadius}; |
| 78 | overflow-y: auto; |
| 79 | z-index: 10; |
| 80 | |
| 81 | &:focus { |
| 82 | outline: none; |
| 83 | } |
| 84 | `; |
| 85 | export const StyledListboxOption = styled(ListboxOption)<{ |
| 86 | selected: boolean; |
| 87 | }>` |
| 88 | background-color: ${({ selected }) => |
| 89 | selected ? vscListActiveBackground : vscInputBackground}; |
| 90 | cursor: pointer; |
| 91 | padding: 6px 8px 6px 12px; |
| 92 | |
| 93 | display: flex; |
| 94 | gap: 8px; |
| 95 | align-items: center; |
| 96 | |
| 97 | &:hover { |
| 98 | background-color: ${vscListActiveBackground}; |
| 99 | color: ${vscListActiveForeground}; |
| 100 | } |
| 101 | `; |
| 102 | |
| 103 | interface ModelSelectionListboxProps { |
| 104 | selectedProvider: DisplayInfo; |
| 105 | setSelectedProvider: (val: DisplayInfo) => void; |
| 106 | topOptions?: DisplayInfo[]; |
| 107 | otherOptions?: DisplayInfo[]; |
| 108 | } |
| 109 | |
| 110 | function ModelSelectionListbox({ |
| 111 | selectedProvider, |
| 112 | setSelectedProvider, |
| 113 | topOptions = [], |
| 114 | otherOptions = [], |
| 115 | }: ModelSelectionListboxProps) { |
| 116 | return ( |
| 117 | <StyledListbox value={selectedProvider} onChange={setSelectedProvider}> |
| 118 | <div className="relative mb-2 mt-1"> |
| 119 | <StyledListboxButton> |
| 120 | <span className="flex items-center"> |
| 121 | {window.vscMediaUrl && selectedProvider.icon && ( |
| 122 | <img |
| 123 | src={`${window.vscMediaUrl}/logos/${selectedProvider.icon}`} |
| 124 | className="mr-3 h-4 w-4 object-contain object-center" |
| 125 | /> |
| 126 | )} |
| 127 | <span className="text-xs">{selectedProvider.title}</span> |
| 128 | </span> |
| 129 | <span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2"> |
| 130 | <ChevronUpDownIcon |
| 131 | className="h-5 w-5 text-gray-400" |
| 132 | aria-hidden="true" |
| 133 | /> |
| 134 | </span> |
| 135 | </StyledListboxButton> |
| 136 | <Transition |
| 137 | as={Fragment} |
| 138 | leave="transition ease-in duration-100" |
| 139 | leaveFrom="opacity-100" |
| 140 | leaveTo="opacity-0" |
| 141 | > |
| 142 | <StyledListboxOptions> |
| 143 | {topOptions.length > 0 && ( |
| 144 | <div className="py-1"> |
| 145 | {topOptions.map((option, index) => ( |
| 146 | <StyledListboxOption |
| 147 | selected={selectedProvider.title === option.title} |
| 148 | key={index} |
| 149 | className="relative cursor-default select-none py-2 pr-4 text-gray-400" |
| 150 | value={option} |
| 151 | > |
| 152 | {({ selected }) => ( |
| 153 | <> |
| 154 | {option.title === "Autodetect" ? ( |
| 155 | <CubeIcon className="mr-2 h-4 w-4 text-gray-400" /> |
| 156 | ) : ( |
| 157 | window.vscMediaUrl && |
| 158 | option.icon && ( |
| 159 | <img |
| 160 | src={`${window.vscMediaUrl}/logos/${option.icon}`} |
| 161 | className="mr-1 h-4 w-4 object-contain object-center" |
| 162 | /> |
| 163 | ) |
| 164 | )} |
| 165 | <span className="text-xs">{option.title}</span> |
| 166 | |
| 167 | {selected && ( |
| 168 | <span className="inset-y-0 ml-auto flex items-center pl-3"> |
| 169 | <CheckIcon className="h-5 w-5" aria-hidden="true" /> |
| 170 | </span> |
| 171 | )} |
| 172 | </> |
| 173 | )} |
| 174 | </StyledListboxOption> |
| 175 | ))} |
| 176 | </div> |
| 177 | )} |
| 178 | |
| 179 | {topOptions.length > 0 && otherOptions.length > 0 && ( |
| 180 | <div className="my-1 h-px bg-zinc-500" /> |
| 181 | )} |
| 182 | |
| 183 | {otherOptions.length > 0 && ( |
| 184 | <div className="py-1"> |
| 185 | {otherOptions.map((option, index) => ( |
| 186 | <StyledListboxOption |
| 187 | selected={selectedProvider.title === option.title} |
| 188 | key={index} |
| 189 | className="relative cursor-default select-none py-2 pr-4 text-gray-400" |
| 190 | value={option} |
| 191 | > |
| 192 | {({ selected }) => ( |
| 193 | <> |
| 194 | {option.title === "Autodetect" ? ( |
| 195 | <CubeIcon className="mr-2 h-4 w-4 text-gray-400" /> |
| 196 | ) : ( |
| 197 | window.vscMediaUrl && |
| 198 | option.icon && ( |
| 199 | <img |
| 200 | src={`${window.vscMediaUrl}/logos/${option.icon}`} |
| 201 | className="mr-1 h-4 w-4 object-contain object-center" |
| 202 | /> |
| 203 | ) |
| 204 | )} |
| 205 | <span className="text-xs">{option.title}</span> |
| 206 | |
| 207 | {selected && ( |
| 208 | <span className="inset-y-0 ml-auto flex items-center pl-3"> |
| 209 | <CheckIcon className="h-5 w-5" aria-hidden="true" /> |
| 210 | </span> |
| 211 | )} |
| 212 | </> |
| 213 | )} |
| 214 | </StyledListboxOption> |
| 215 | ))} |
| 216 | </div> |
| 217 | )} |
| 218 | </StyledListboxOptions> |
| 219 | </Transition> |
| 220 | </div> |
| 221 | </StyledListbox> |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | export default ModelSelectionListbox; |