This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathApiConfigSelector.tsx
More file actions
286 lines (268 loc) · 9.75 KB
/
Copy pathApiConfigSelector.tsx
File metadata and controls
286 lines (268 loc) · 9.75 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { useState, useMemo, useCallback } from "react"
import { Fzf } from "fzf"
import { cn } from "@/lib/utils"
import { useRooPortal } from "@/components/ui/hooks/useRooPortal"
import { Popover, PopoverContent, PopoverTrigger, StandardTooltip } from "@/components/ui"
import { useAppTranslation } from "@/i18n/TranslationContext"
import { vscode } from "@/utils/vscode"
import { Button } from "@/components/ui"
import { IconButton } from "./IconButton"
interface ApiConfigSelectorProps {
value: string
displayName: string
disabled?: boolean
title: string
onChange: (value: string) => void
triggerClassName?: string
listApiConfigMeta: Array<{ id: string; name: string; modelId?: string }>
pinnedApiConfigs?: Record<string, boolean>
togglePinnedApiConfig: (id: string) => void
lockApiConfigAcrossModes: boolean
onToggleLockApiConfig: () => void
}
export const ApiConfigSelector = ({
value,
displayName,
disabled = false,
title,
onChange,
triggerClassName = "",
listApiConfigMeta,
pinnedApiConfigs,
togglePinnedApiConfig,
lockApiConfigAcrossModes,
onToggleLockApiConfig,
}: ApiConfigSelectorProps) => {
const { t } = useAppTranslation()
const [open, setOpen] = useState(false)
const [searchValue, setSearchValue] = useState("")
const portalContainer = useRooPortal("roo-portal")
// Create searchable items for fuzzy search.
const searchableItems = useMemo(
() =>
listApiConfigMeta.map((config) => ({
original: config,
searchStr: config.name,
})),
[listApiConfigMeta],
)
// Create Fzf instance.
const fzfInstance = useMemo(
() => new Fzf(searchableItems, { selector: (item) => item.searchStr }),
[searchableItems],
)
// Filter configs based on search.
const filteredConfigs = useMemo(() => {
if (!searchValue) {
return listApiConfigMeta
}
const matchingItems = fzfInstance.find(searchValue).map((result) => result.item.original)
return matchingItems
}, [listApiConfigMeta, searchValue, fzfInstance])
// Separate pinned and unpinned configs.
const { pinnedConfigs, unpinnedConfigs } = useMemo(() => {
const pinned = filteredConfigs.filter((config) => pinnedApiConfigs?.[config.id])
const unpinned = filteredConfigs.filter((config) => !pinnedApiConfigs?.[config.id])
return { pinnedConfigs: pinned, unpinnedConfigs: unpinned }
}, [filteredConfigs, pinnedApiConfigs])
const handleSelect = useCallback(
(configId: string) => {
onChange(configId)
setOpen(false)
setSearchValue("")
},
[onChange],
)
const handleEditClick = useCallback(() => {
vscode.postMessage({ type: "switchTab", tab: "settings" })
setOpen(false)
}, [])
const renderConfigItem = useCallback(
(config: { id: string; name: string; modelId?: string }, isPinned: boolean) => {
const isCurrentConfig = config.id === value
return (
<div
key={config.id}
onClick={() => handleSelect(config.id)}
className={cn(
"px-3 py-1.5 text-sm cursor-pointer flex items-center group",
"hover:bg-vscode-list-hoverBackground",
isCurrentConfig &&
"bg-vscode-list-activeSelectionBackground text-vscode-list-activeSelectionForeground",
)}>
<div className="flex-1 min-w-0 flex items-center gap-1 overflow-hidden">
<span className="flex-shrink-0">{config.name}</span>
{config.modelId && (
<>
<span
className="text-vscode-descriptionForeground opacity-70 min-w-0 overflow-hidden"
style={{ direction: "rtl", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
{config.modelId}
</span>
</>
)}
</div>
<div className="flex items-center gap-1">
{isCurrentConfig && (
<div className="size-5 p-1 flex items-center justify-center">
<span className="codicon codicon-check text-xs" />
</div>
)}
<StandardTooltip content={isPinned ? t("chat:unpin") : t("chat:pin")}>
<Button
variant="ghost"
size="icon"
tabIndex={-1}
onClick={(e) => {
e.stopPropagation()
togglePinnedApiConfig(config.id)
vscode.postMessage({ type: "toggleApiConfigPin", text: config.id })
}}
className={cn("size-5 flex items-center justify-center", {
"opacity-0 group-hover:opacity-100": !isPinned && !isCurrentConfig,
"bg-accent opacity-100": isPinned,
})}>
<span className="codicon codicon-pin text-xs opacity-50" />
</Button>
</StandardTooltip>
</div>
</div>
)
},
[value, handleSelect, t, togglePinnedApiConfig],
)
return (
<Popover open={open} onOpenChange={setOpen} data-testid="api-config-selector-root">
<StandardTooltip content={title}>
<PopoverTrigger
disabled={disabled}
data-testid="dropdown-trigger"
className={cn(
"min-w-0 inline-flex items-center relative whitespace-nowrap px-1.5 py-1 text-xs",
"bg-transparent border border-[rgba(255,255,255,0.08)] rounded-md text-vscode-foreground",
"transition-all duration-150 focus:outline-none focus-visible:ring-1 focus-visible:ring-vscode-focusBorder focus-visible:ring-inset",
disabled
? "opacity-50 cursor-not-allowed"
: "opacity-90 hover:opacity-100 hover:bg-[rgba(255,255,255,0.03)] hover:border-[rgba(255,255,255,0.15)] cursor-pointer",
triggerClassName,
)}>
<span className="truncate">{displayName}</span>
</PopoverTrigger>
</StandardTooltip>
<PopoverContent
align="start"
sideOffset={4}
container={portalContainer}
className="p-0 overflow-hidden w-[300px]">
<div className="flex flex-col w-full">
{/* Search input or info blurb */}
{listApiConfigMeta.length > 6 ? (
<div className="relative p-2 border-b border-vscode-dropdown-border">
<input
aria-label={t("common:ui.search_placeholder")}
value={searchValue}
onChange={(e) => setSearchValue(e.target.value)}
placeholder={t("common:ui.search_placeholder")}
className="w-full h-8 px-2 py-1 text-xs bg-vscode-input-background text-vscode-input-foreground border border-vscode-input-border rounded focus:outline-0"
autoFocus
/>
{searchValue.length > 0 && (
<div className="absolute right-4 top-0 bottom-0 flex items-center justify-center">
<span
className="codicon codicon-close text-vscode-input-foreground opacity-50 hover:opacity-100 text-xs cursor-pointer"
onClick={() => setSearchValue("")}
/>
</div>
)}
</div>
) : (
<div className="p-3 border-b border-vscode-dropdown-border">
<p className="text-xs text-vscode-descriptionForeground m-0">
{t("prompts:apiConfiguration.select")}
</p>
</div>
)}
{/* Config list - single scroll container */}
{filteredConfigs.length === 0 && searchValue ? (
<div className="py-2 px-3 text-sm text-vscode-foreground/70">{t("common:ui.no_results")}</div>
) : (
<div className="max-h-[300px] overflow-y-auto">
{/* Pinned configs - sticky header */}
{pinnedConfigs.length > 0 && (
<div
className={cn(
"sticky top-0 z-10 bg-vscode-dropdown-background py-1",
unpinnedConfigs.length > 0 && "border-b border-vscode-dropdown-foreground/10",
)}
aria-label="Pinned configurations">
{pinnedConfigs.map((config) => renderConfigItem(config, true))}
</div>
)}
{/* Unpinned configs */}
{unpinnedConfigs.length > 0 && (
<div className="py-1" aria-label="All configurations">
{unpinnedConfigs.map((config) => renderConfigItem(config, false))}
</div>
)}
</div>
)}
{/* Bottom bar with buttons on left and title on right */}
<div className="flex flex-row items-center justify-between px-2 py-2 border-t border-vscode-dropdown-border">
<div className="flex flex-row gap-1">
<IconButton
iconClass="codicon-settings-gear"
title={t("chat:edit")}
onClick={handleEditClick}
tooltip={false}
/>
<StandardTooltip
content={
lockApiConfigAcrossModes
? t("chat:unlockApiConfigAcrossModes")
: t("chat:lockApiConfigAcrossModes")
}>
<Button
aria-label={
lockApiConfigAcrossModes
? t("chat:unlockApiConfigAcrossModes")
: t("chat:lockApiConfigAcrossModes")
}
className={cn(
"inline-flex items-center gap-1 px-1.5 py-1 h-7 rounded-md text-xs",
"bg-transparent border-none cursor-pointer",
"transition-all duration-150",
"hover:bg-[rgba(255,255,255,0.06)]",
"focus:outline-none focus-visible:ring-1 focus-visible:ring-vscode-focusBorder",
lockApiConfigAcrossModes
? "text-vscode-focusBorder opacity-100"
: "text-vscode-descriptionForeground opacity-90 hover:opacity-100",
)}
onClick={onToggleLockApiConfig}>
<span
className={cn(
"codicon",
lockApiConfigAcrossModes ? "codicon-lock" : "codicon-unlock",
)}
style={{ fontSize: 14 }}
/>
<span>{lockApiConfigAcrossModes ? t("chat:locked") : t("chat:unlocked")}</span>
</Button>
</StandardTooltip>
</div>
{/* Info icon and title on the right with matching spacing */}
<div className="flex items-center gap-1 pr-1">
{listApiConfigMeta.length > 6 && (
<StandardTooltip content={t("prompts:apiConfiguration.select")}>
<span className="codicon codicon-info text-xs text-vscode-descriptionForeground opacity-70 hover:opacity-100 cursor-help" />
</StandardTooltip>
)}
<h4 className="m-0 font-medium text-sm text-vscode-descriptionForeground">
{t("prompts:apiConfiguration.title")}
</h4>
</div>
</div>
</div>
</PopoverContent>
</Popover>
)
}