import { useEffect, useId, useMemo, useRef } from "react"; import { Plus, Search } from "lucide-react"; import { useTranslation } from "react-i18next"; import { ProviderIcon } from "@/components/settings/shared/ProviderIcon"; import { TruncatedTextTooltip } from "@/components/ui/tooltip"; import { SearchInput } from "@/components/ui/input"; import { ComboboxOption, useComboboxNavigation } from "@/components/ui/combobox"; import { useMediaQuery } from "@/hooks/useMediaQuery"; import type { SettingsPayload } from "@/lib/types"; const PROVIDER_GROUPS = [ { id: "oauth", labelKey: "settings.providers.providerGroupOAuth", label: "OAuth" }, { id: "api_key", labelKey: "settings.providers.providerGroupApiKey", label: "API Key" }, { id: "local", labelKey: "settings.providers.providerGroupLocal", label: "Local" }, ] as const; const PROVIDER_SEARCH_ALIASES: Record = { anthropic: "claude 克劳德 克勞德", deepseek: "深度求索 深度探索", zhipu: "智谱 智譜 glm z.ai", dashscope: "阿里 通义 通義 千问 千問 qwen", moonshot: "月之暗面 kimi", kimi_coding: "月之暗面 kimi coding", volcengine: "火山引擎 豆包 doubao", volcengine_coding_plan: "火山引擎 豆包 doubao", minimax: "海螺 稀宇", minimax_anthropic: "海螺 稀宇", siliconflow: "硅基流动 矽基流動 硅基流動", stepfun: "阶跃星辰 階躍星辰", xiaomi_mimo: "小米 mimo", longcat: "美团 美團", qianfan: "百度 千帆 文心", ant_ling: "蚂蚁 螞蟻 百灵 百靈", }; export function ProviderSearchList({ providers, showBrandLogos, query, onQueryChange, onSelect, onCustom, onClose }: { providers: SettingsPayload["providers"]; showBrandLogos: boolean; query: string; onQueryChange: (query: string) => void; onSelect: (name: string) => void; onCustom: () => void; onClose: () => void; }) { const { t } = useTranslation(); const mobile = useMediaQuery("(max-width: 639px)"); const inputRef = useRef(null); const groupId = useId(); useEffect(() => { if (!mobile) inputRef.current?.focus({ preventScroll: true }); }, [mobile]); const filtered = useMemo(() => { const normalize = (value: string) => value.toLocaleLowerCase().replace(/[\s_.-]+/g, ""); const normalizedQuery = normalize(query); return providers.filter((provider) => normalize( `${provider.name} ${provider.label} ${PROVIDER_SEARCH_ALIASES[provider.name] ?? ""}`, ).includes(normalizedQuery)); }, [providers, query]); const groups = useMemo(() => PROVIDER_GROUPS.map((group) => ({ ...group, providers: filtered.filter((provider) => providerGroup(provider) === group.id), })).filter((group) => group.providers.length > 0), [filtered]); const values = useMemo( () => groups.flatMap((group) => group.providers.map((provider) => provider.name)), [groups], ); const { inputProps, listProps, getOptionProps } = useComboboxNavigation({ open: true, values, onSelect, onClose, }); const searchLabel = t("settings.providers.searchPlaceholder", { defaultValue: "Search providers" }); return <>
onQueryChange(event.target.value)} aria-label={searchLabel} placeholder={searchLabel} className="rounded-xl border-transparent bg-muted/60 pl-9" />
{groups.map((group) => (
{t(group.labelKey, { defaultValue: group.label })}
{group.providers.map((provider) => )}
))}
{filtered.length === 0 ?

{t("settings.providers.noMatches", { defaultValue: "No matching providers." })}

: null}
; } function providerGroup(provider: SettingsPayload["providers"][number]): typeof PROVIDER_GROUPS[number]["id"] { if (provider.auth_type === "oauth") return "oauth"; return provider.model_catalog === "local" ? "local" : "api_key"; }