"use client";
import {
Check,
ChevronDown,
ChevronRight,
PencilLine,
Plus,
Trash2,
} from "lucide-react";
import { useTranslation } from "react-i18next";
import ProviderIcon from "@/components/common/ProviderIcon";
import type {
CatalogModel,
CatalogProfile,
ServiceName,
} from "@/features/settings/store/SettingsStore";
import type { AppLanguage } from "@/i18n/init";
/** Provider disclosure rows contain connection fields and their model cards.
* Opening an editor and selecting a runtime model are separate actions;
* every selection remains a draft until the user applies it. */
export function SectionHead({
title,
action,
}: {
title: string;
action?: React.ReactNode;
}) {
return (
{title}
{action}
);
}
export function CardAction({
onClick,
disabled,
children,
}: {
onClick: () => void;
disabled?: boolean;
children: React.ReactNode;
}) {
return (
{children}
);
}
export function CardGrid({ children }: { children: React.ReactNode }) {
return (
{children}
);
}
export function AddCard({
label,
onClick,
}: {
label: string;
onClick: () => void;
}) {
return (
{label}
);
}
/**
* Shared chrome: the frame, the name row and the expand affordance.
*
* A card that opens carries a chevron in its lower-right and lifts its border
* and tint on hover — without that it read as a panel that happened to be
* clickable, which is the same as not clickable. Cards that do not open (a
* search provider has no models under it) draw no chevron, so the two kinds
* are told apart before you click rather than after.
*/
function CardShell({
expanded,
inUse,
onOpen,
editing = false,
editorId,
children,
}: {
expanded: boolean;
editing?: boolean;
editorId?: string;
inUse: boolean;
onOpen?: () => void;
children: React.ReactNode;
}) {
return (
{
if (event.target !== event.currentTarget) return;
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
onOpen();
}
}
: undefined
}
>
{children}
);
}
function NameRow({
icon,
name,
renaming,
renameValue,
onRenameChange,
onRenameCommit,
onRenameCancel,
onRenameStart,
expanded,
onToggleExpand,
expandLabel,
status,
}: {
icon?: React.ReactNode;
name: string;
renaming: boolean;
renameValue: string;
onRenameChange: (value: string) => void;
onRenameCommit: () => void;
onRenameCancel: () => void;
onRenameStart: () => void;
/** Absent when this card has nothing to expand in place (e.g. a provider
* card, which opens a dialog instead). */
expanded?: boolean;
onToggleExpand?: () => void;
expandLabel?: string;
status?: React.ReactNode;
}) {
if (renaming) {
return (
event.stopPropagation()}
onChange={(event) => onRenameChange(event.target.value)}
onBlur={onRenameCommit}
onKeyDown={(event) => {
if (event.key === "Enter") event.currentTarget.blur();
if (event.key === "Escape") {
event.preventDefault();
onRenameCancel();
}
}}
className="w-full rounded-md border border-[var(--border)] bg-[var(--background)] px-1.5 py-0.5 text-[13px] font-medium text-[var(--foreground)] outline-none focus:border-[var(--ring)]"
/>
);
}
return (
{icon}
{
event.stopPropagation();
onRenameStart();
}}
className="min-w-0 flex-1 truncate text-[13px] font-medium leading-5 text-[var(--foreground)]"
>
{name}
{status}
{onToggleExpand && (
{
event.stopPropagation();
onToggleExpand();
}}
className="-mr-1 -mt-0.5 shrink-0 rounded-md p-1 text-[var(--muted-foreground)] transition-colors hover:bg-[var(--accent)] hover:text-[var(--foreground)]"
>
)}
);
}
/** Labels describe the selection in the draft, before it is applied. */
export function UseRow({
inUse,
onUse,
detail,
selectLabel,
selectedLabel,
}: {
inUse: boolean;
selectLabel?: string;
selectedLabel?: string;
onUse: () => void;
detail?: string;
}) {
const { t } = useTranslation();
return (
{detail}
{inUse ? (
{selectedLabel ?? t("Selected")}
) : (
{
event.stopPropagation();
onUse();
}}
className="shrink-0 rounded-md px-1 text-[11px] text-[var(--muted-foreground)] underline-offset-2 transition-colors hover:text-[var(--foreground)] hover:underline"
>
{selectLabel ?? t("Select")}
)}
);
}
export function ProfileCard({
profile,
service,
inUse,
open,
renaming,
renameValue,
onRenameChange,
onRenameCommit,
onRenameCancel,
onRenameStart,
onOpen,
onUse,
}: {
profile: CatalogProfile;
service: ServiceName;
inUse: boolean;
/** Whether this provider's connection fields and models are expanded. */
open: boolean;
renaming: boolean;
renameValue: string;
onRenameChange: (value: string) => void;
onRenameCommit: () => void;
onRenameCancel: () => void;
onRenameStart: () => void;
onOpen: () => void;
onUse: () => void;
}) {
const { t } = useTranslation();
const provider =
service === "search" ? profile.provider || "" : profile.binding || "";
const endpoint = (profile.base_url || "").replace(/^https?:\/\//, "");
const count = profile.models.length;
return (
{profile.name}
{endpoint || t("Provider default endpoint")}
{t("{{count}} models", { count })}
{renaming ? (
) : (
{t("Rename")}
)}
{service === "search" ? (
) : (
inUse && (
)
)}
);
}
export function ModelCard({
model,
service,
language,
index,
inUse,
expanded,
renaming,
renameValue,
onRenameChange,
onRenameCommit,
onRenameCancel,
onRenameStart,
onToggleExpand,
onUse,
onDelete,
editorId,
}: {
model: CatalogModel;
editorId?: string;
service: ServiceName;
language: AppLanguage;
index: number;
inUse: boolean;
expanded: boolean;
renaming: boolean;
renameValue: string;
onRenameChange: (value: string) => void;
onRenameCommit: () => void;
onRenameCancel: () => void;
onRenameStart: () => void;
onToggleExpand: () => void;
onUse: () => void;
onDelete: () => void;
}) {
const { t } = useTranslation();
const name =
(model.name || "").trim() ||
(language === "zh" ? `模型 ${index + 1}` : `Model ${index + 1}`);
const detail =
service === "llm"
? model.context_window
? t("{{n}} ctx", { n: model.context_window })
: undefined
: service === "embedding"
? model.dimension
? t("{{n}} dim", { n: model.dimension })
: undefined
: service === "tts"
? model.voice || undefined
: undefined;
return (
{t("Configuring")}
) : undefined
}
/>
{model.model || t("No model id yet")}
{
event.stopPropagation();
onDelete();
}}
className="shrink-0 rounded-md p-1 text-[var(--muted-foreground)]/0 transition-colors hover:bg-[var(--accent)] hover:text-red-500 focus-visible:text-red-500 group-hover:text-[var(--muted-foreground)]/70 max-sm:text-[var(--muted-foreground)]/70"
>
);
}