Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(settings): open model picker on single click

tianma-if30263f76b07f
diff --git a/apps/web/src/components/settings/AiModelIdInput.tsx b/apps/web/src/components/settings/AiModelIdInput.tsx
new file mode 100644
index 00000000..41dbbd3f
--- /dev/null
+++ b/apps/web/src/components/settings/AiModelIdInput.tsx
@@ -0,0 +1,97 @@
+import { useEffect, useRef, useState } from "react";
+import type { AiDiscoveredModel } from "@edgeever/shared";
+import { ChevronDown } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { Command, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";
+
+export function AiModelIdInput({ value, onValueChange, models, placeholder }: {
+ value: string;
+ onValueChange: (value: string) => void;
+ models: AiDiscoveredModel[];
+ placeholder: string;
+}) {
+ const inputRef = useRef<HTMLInputElement>(null);
+ const commandRef = useRef<HTMLDivElement>(null);
+ const [open, setOpen] = useState(false);
+ useEffect(() => {
+ if (!open) return;
+ const closeOutside = (event: PointerEvent) => {
+ if (event.target instanceof Node && !commandRef.current?.contains(event.target)) setOpen(false);
+ };
+ document.addEventListener("pointerdown", closeOutside);
+ return () => document.removeEventListener("pointerdown", closeOutside);
+ }, [open]);
+ const customId = value.trim();
+ const selectModel = (modelId: string) => {
+ onValueChange(modelId);
+ setOpen(false);
+ };
+
+ return (
+ <Command
+ ref={commandRef}
+ label={placeholder}
+ data-state={open ? "open" : "closed"}
+ className="relative overflow-visible"
+ onBlur={(event) => {
+ if (!event.currentTarget.contains(event.relatedTarget)) setOpen(false);
+ }}
+ onKeyDownCapture={(event) => {
+ if (event.key === "Escape" && open) {
+ event.preventDefault();
+ event.stopPropagation();
+ setOpen(false);
+ } else if (event.key === "Enter") {
+ if (!open) event.stopPropagation();
+ } else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
+ setOpen(true);
+ }
+ }}
+ >
+ <div className="relative rounded-md border border-slate-200 focus-within:border-slate-900 focus-within:ring-2 focus-within:ring-slate-900/10 [&_[cmdk-input-wrapper]]:rounded-md [&_[cmdk-input-wrapper]]:border-0 [&_[cmdk-input-wrapper]>svg]:hidden">
+ <CommandInput
+ asChild
+ ref={inputRef}
+ value={value}
+ onValueChange={(nextValue) => { onValueChange(nextValue); setOpen(true); }}
+ onFocus={() => setOpen(true)}
+ onClick={() => setOpen(true)}
+ placeholder={placeholder}
+ autoComplete="off"
+ spellCheck={false}
+ className="pr-9"
+ >
+ <input aria-expanded={open} />
+ </CommandInput>
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ className="absolute right-1 top-1"
+ aria-label={placeholder}
+ aria-expanded={open}
+ onMouseDown={(event) => event.preventDefault()}
+ onClick={() => { inputRef.current?.focus(); setOpen(true); }}
+ >
+ <ChevronDown className="h-4 w-4" />
+ </Button>
+ </div>
+ <CommandList
+ hidden={!open}
+ className="absolute top-full z-50 mt-1 max-h-60 w-full rounded-md border border-slate-200 bg-card shadow-md"
+ onMouseDown={(event) => event.preventDefault()}
+ >
+ <CommandGroup>
+ {models.map((model) => (
+ <CommandItem className="text-xs" key={model.modelId} value={model.modelId} keywords={[model.displayName]} onSelect={selectModel}>
+ <span className="min-w-0 break-all">{model.displayName}</span>
+ </CommandItem>
+ ))}
+ {customId && !models.some((model) => model.modelId === customId) ? (
+ <CommandItem className="text-xs" value={customId} onSelect={selectModel}><span className="break-all">{customId}</span></CommandItem>
+ ) : null}
+ </CommandGroup>
+ </CommandList>
+ </Command>
+ );
+}
diff --git a/apps/web/src/components/settings/AiProviderCard.tsx b/apps/web/src/components/settings/AiProviderCard.tsx
index 629b2080..2e0fe220 100644
--- a/apps/web/src/components/settings/AiProviderCard.tsx
+++ b/apps/web/src/components/settings/AiProviderCard.tsx
@@ -1,9 +1,10 @@
-import { useEffect, useId, useMemo, useRef, useState, type FormEvent, type ReactNode } from "react";
+import { useEffect, useMemo, useRef, useState, type FormEvent, type ReactNode } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { AiModelConfig, AiProvider, AiProviderConfig, AuthSession } from "@edgeever/shared";
import { CheckCircle2, Loader2, MoreHorizontal, Pencil, Plus, RefreshCw, Trash2 } from "lucide-react";
import { useTranslation } from "react-i18next";
import { aiErrorMessage, isLegacyProviderDisplayName, providerDefaults, trimAiText } from "@/components/settings/ai-provider-options";
+import { AiModelIdInput } from "@/components/settings/AiModelIdInput";
import { Button } from "@/components/ui/button";
import {
Dialog,
@@ -46,7 +47,6 @@ export const AiProviderCard = ({ provider: saved, defaultDisplayName, defaultMod
const queryClient = useQueryClient();
const addModelDialogRef = useRef<HTMLDivElement>(null);
const addModelTriggerRef = useRef<HTMLButtonElement>(null);
- const datalistId = `ai-models-${useId().replaceAll(":", "")}`;
const effectiveDisplayName = isLegacyProviderDisplayName(saved.displayName, saved.provider)
? defaultDisplayName
: (saved.displayName || defaultDisplayName);
@@ -410,6 +410,11 @@ export const AiProviderCard = ({ provider: saved, defaultDisplayName, defaultMod
<Dialog open={showAddModel} onOpenChange={handleAddModelChange}>
<DialogContent
ref={addModelDialogRef}
+ onEscapeKeyDown={(event) => {
+ if (event.target instanceof Element && event.target.closest('[cmdk-root][data-state="open"]')) {
+ event.preventDefault();
+ }
+ }}
onOpenAutoFocus={(event) => {
event.preventDefault();
addModelDialogRef.current?.focus();
@@ -424,18 +429,15 @@ export const AiProviderCard = ({ provider: saved, defaultDisplayName, defaultMod
<DialogTitle>{t("aiModel.addModel")}</DialogTitle>
<DialogDescription>{t("aiModel.addModelDescription")}</DialogDescription>
</DialogHeader>
- <Field label={t("aiModel.modelId")} hint={discoveryLoading
+ <Field as="div" label={t("aiModel.modelId")} hint={discoveryLoading
? t("aiModel.discoveryLoading")
: discoveryQuery.data ? t("aiModel.discoveryComplete", { count: discoveredModels.length }) : undefined}>
- <Input
- list={datalistId}
+ <AiModelIdInput
value={modelId}
- onChange={(event) => setModelId(event.target.value)}
+ onValueChange={setModelId}
+ models={discoveredModels}
placeholder={t("aiModel.modelIdPlaceholder")}
/>
- <datalist id={datalistId}>
- {discoveredModels.map((model) => <option key={model.modelId} value={model.modelId}>{model.displayName}</option>)}
- </datalist>
</Field>
{discoveryError || addModelMutation.error ? (
<p className="text-xs font-medium text-rose-600" role="alert">
@@ -475,8 +477,8 @@ const formatBaseUrl = (baseUrl: string) => {
}
};
-const Field = ({ label, hint, children }: { label: string; hint?: string; children: ReactNode }) => (
- <label className="grid gap-1.5 text-xs font-normal leading-5 text-slate-700">
+const Field = ({ as: Comp = "label", label, hint, children }: { as?: "label" | "div"; label: string; hint?: string; children: ReactNode }) => (
+ <Comp className="grid gap-1.5 text-xs font-normal leading-5 text-slate-700">
{label}{children}{hint ? <span className="text-xs font-normal leading-4 text-slate-500">{hint}</span> : null}
- </label>
+ </Comp>
);