import { VSCodeButton, VSCodeTextField } from "@vscode/webview-ui-toolkit/react" import { memo, useEffect, useRef, useState } from "react" import { ApiConfigMeta } from "../../../../src/shared/ExtensionMessage" import { Dropdown } from "vscrui" import type { DropdownOption } from "vscrui" interface ApiConfigManagerProps { currentApiConfigName?: string listApiConfigMeta?: ApiConfigMeta[] onSelectConfig: (configName: string) => void onDeleteConfig: (configName: string) => void onRenameConfig: (oldName: string, newName: string) => void onUpsertConfig: (configName: string) => void } const ApiConfigManager = ({ currentApiConfigName = "", listApiConfigMeta = [], onSelectConfig, onDeleteConfig, onRenameConfig, onUpsertConfig, }: ApiConfigManagerProps) => { const [editState, setEditState] = useState<"new" | "rename" | null>(null) const [inputValue, setInputValue] = useState("") const inputRef = useRef() // Focus input when entering edit mode useEffect(() => { if (editState) { setTimeout(() => inputRef.current?.focus(), 0) } }, [editState]) // Reset edit state when current profile changes useEffect(() => { setEditState(null) setInputValue("") }, [currentApiConfigName]) const handleAdd = () => { const newConfigName = currentApiConfigName + " (copy)" onUpsertConfig(newConfigName) } const handleStartRename = () => { setEditState("rename") setInputValue(currentApiConfigName || "") } const handleCancel = () => { setEditState(null) setInputValue("") } const handleSave = () => { const trimmedValue = inputValue.trim() if (!trimmedValue) return if (editState === "new") { onUpsertConfig(trimmedValue) } else if (editState === "rename" && currentApiConfigName) { if (currentApiConfigName === trimmedValue) { setEditState(null) setInputValue("") return } onRenameConfig(currentApiConfigName, trimmedValue) } setEditState(null) setInputValue("") } const handleDelete = () => { if (!currentApiConfigName || !listApiConfigMeta || listApiConfigMeta.length <= 1) return // Let the extension handle both deletion and selection onDeleteConfig(currentApiConfigName) } const isOnlyProfile = listApiConfigMeta?.length === 1 return (
{editState ? (
setInputValue(e.target.value)} placeholder={editState === "new" ? "Enter profile name" : "Enter new name"} style={{ flexGrow: 1 }} onKeyDown={(e: any) => { if (e.key === "Enter" && inputValue.trim()) { handleSave() } else if (e.key === "Escape") { handleCancel() } }} />
) : ( <>
{ onSelectConfig((value as DropdownOption).value) }} style={{ minWidth: 130, zIndex: 1002, }} role="combobox" options={listApiConfigMeta.map((config) => ({ value: config.name, label: config.name, }))} /> {currentApiConfigName && ( <> )}

Save different API configurations to quickly switch between providers and settings

)}
) } export default memo(ApiConfigManager)