Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/options/AI-LLM: switch to the new card component
Adorian Doran30d2895d389b
diff --git a/apps/client/src/widgets/type_widgets/options/llm.css b/apps/client/src/widgets/type_widgets/options/llm.cssindex dfeb9dcbda..cb5046de67 100644--- a/apps/client/src/widgets/type_widgets/options/llm.css+++ b/apps/client/src/widgets/type_widgets/options/llm.css@@ -1,9 +1,25 @@+/* The page's own switch, standing on the header's second row rather than in a card: it carries the+ card option's layout without a card's frame, so it needs the room a segment would have given it. */+.llm-master-switch {+ padding-block: 4px;+}+.llm-provider-name {display: inline-flex;align-items: center;gap: 0.5em;}+.llm-provider-actions {+ display: flex;+ gap: 4px;+}++/* The way to add one, at the end of the list it adds to. */+.llm-add-provider {+ text-align: end;+}+/* Scale the provider logo down to sit inline with the row label text. */.llm-provider-name .masked-icon {--masked-icon-size: 1.2em;diff --git a/apps/client/src/widgets/type_widgets/options/llm.spec.tsx b/apps/client/src/widgets/type_widgets/options/llm.spec.tsxindex aad4891154..06695943f3 100644--- a/apps/client/src/widgets/type_widgets/options/llm.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/llm.spec.tsx@@ -35,7 +35,11 @@ vi.mock("../../react/hooks", async (importOriginal) => ({// no such route, so whether it is asked for at all is part of what is under test.vi.mock("../../../services/server", () => ({ default: { get: mocks.get } }));-vi.mock("./components/OptionsPageHeader", () => ({ default: () => <div className="header-stub" /> }));+// Stubbed for the note context it would otherwise need, but its second row is rendered: the page's+// master switch lives there now.+vi.mock("./components/OptionsPageHeader", () => ({+ default: ({ below }: { below?: preact.ComponentChildren }) => <div className="header-stub">{below}</div>+}));// Carries a bootstrap modal into the tree, and the MCP card below it is what is being read here.vi.mock("./llm/AddProviderModal", () => ({ default: () => null, PROVIDER_TYPES: [] }));@@ -66,29 +70,33 @@ function open() {/** The MCP switch, plus the sentence under it — which is where the reason has to appear. */function mcpRow() {- // The row names its input `mcp-enabled-<random>`, so match on the stable prefix.+ // The option names its input `mcp-enabled-<random>`, so match on the stable prefix.const toggle = host.querySelector<HTMLInputElement>("input.switch-toggle[id^='mcp-enabled-']");- const row = toggle?.closest(".option-row");+ const row = toggle?.closest(".tn-card-option");return {toggle,- description: row?.querySelector(".option-row-description")?.textContent,+ description: row?.querySelector(".tn-card-option-description")?.textContent,// Only rendered while MCP is actually serving: the endpoint list and the paste-in config.endpointsShown: !!host.querySelector(".mcp-endpoint-list")};}describe("the AI master switch", () => {- it("is a named row, so what it turns on is stated rather than left to a tooltip", () => {+ it("is a named row in the page header, so what it turns on is stated rather than left to a tooltip", () => {open();const toggle = host.querySelector<HTMLInputElement>("input.switch-toggle[id^='ai-enabled-']");expect(toggle).not.toBeNull();+ // It governs the page rather than any one card, so it stands in the header rather than+ // in a card of its own above the ones it turns on.+ expect(toggle?.closest(".header-stub")).not.toBeNull();- // Bound to the switch, not merely text near it — the switch's accessible name.- const row = toggle?.closest(".option-row");+ // Bound to the switch, not merely text near it — the switch's accessible name. The label+ // carries the sentence too, so the name itself is read from the text leading it.+ const row = toggle?.closest(".tn-card-option");expect(row?.querySelector("label")?.getAttribute("for")).toBe(toggle?.id);- expect(row?.querySelector("label")?.textContent).toBe("llm.enabled");- expect(row?.querySelector(".option-row-description")?.textContent).toBe("llm.enabled_description");+ expect(row?.querySelector("label")?.firstChild?.textContent).toBe("llm.enabled");+ expect(row?.querySelector(".tn-card-option-description")?.textContent).toBe("llm.enabled_description");});it("stays on the page when off, so the way back on is where the settings were", () => {diff --git a/apps/client/src/widgets/type_widgets/options/llm.tsx b/apps/client/src/widgets/type_widgets/options/llm.tsxindex 4d48264fdd..c1459c8187 100644--- a/apps/client/src/widgets/type_widgets/options/llm.tsx+++ b/apps/client/src/widgets/type_widgets/options/llm.tsx@@ -9,15 +9,15 @@ import server from "../../../services/server";import { isStandalone } from "../../../services/utils";import ActionButton from "../../react/ActionButton";import Button from "../../react/Button";+import { Card, CardOption, CardSection } from "../../react/Card";import CodeBlock from "../../react/CodeBlock";import Collapsible from "../../react/Collapsible";import FormTextBox from "../../react/FormTextBox";+import FormToggle from "../../react/FormToggle";import { useTriliumOption, useTriliumOptionBool } from "../../react/hooks";import MaskedIcon from "../../react/MaskedIcon";import NoItems from "../../react/NoItems";import OptionsPageHeader from "./components/OptionsPageHeader";-import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow";-import OptionsSection from "./components/OptionsSection";import AddProviderModal, { type LlmProviderConfig, PROVIDER_TYPES } from "./llm/AddProviderModal";export default function LlmSettings() {@@ -25,17 +25,21 @@ export default function LlmSettings() {return (<>- <OptionsPageHeader helpUrl="GBBMSlVSOIGP" />-- <OptionsSection>- <OptionsRowWithToggle- name="ai-enabled"- label={t("llm.enabled")}- description={t("llm.enabled_description")}- currentValue={aiEnabled}- onChange={setAiEnabled}- />- </OptionsSection>+ {/* The switch governs the whole page rather than anything on it, so it belongs to the+ header — on the row below the title, clear of the dialog's own close button. */}+ <OptionsPageHeader+ helpUrl="GBBMSlVSOIGP"+ below={+ <CardOption+ className="llm-master-switch"+ name="ai-enabled"+ label={t("llm.enabled")}+ description={t("llm.enabled_description")}+ >+ <FormToggle currentValue={aiEnabled} onChange={setAiEnabled} />+ </CardOption>+ }+ />{aiEnabled && (<>@@ -86,21 +90,23 @@ function ProviderSettings() {}, [providers, setProviders]);return (- <OptionsSection title={t("llm.configured_providers")}>- <ProviderList- providers={providers}- onEdit={openModal}- onDelete={handleDeleteProvider}- />-- <OptionsRow name="add-llm-provider" centered>- <Button- name="add-llm-provider-button"- size="micro" icon="bx bx-plus"- text={t("llm.add_provider")}- onClick={() => openModal()}+ <div className="options-section">+ <Card heading={t("llm.configured_providers")}>+ <ProviderList+ providers={providers}+ onEdit={openModal}+ onDelete={handleDeleteProvider}/>- </OptionsRow>++ <CardSection className="llm-add-provider">+ <Button+ name="add-llm-provider-button"+ size="micro" icon="bx-plus"+ text={t("llm.add_provider")}+ onClick={() => openModal()}+ />+ </CardSection>+ </Card><AddProviderModalkey={openToken}@@ -109,7 +115,7 @@ function ProviderSettings() {onHidden={() => setModalOpen(false)}onSave={handleSaveProvider}/>- </OptionsSection>+ </div>);}@@ -153,49 +159,61 @@ function McpSettings() {}, [networkInfo, localUrl]);return (- <OptionsSection title={t("llm.mcp_title")}>- <OptionsRowWithToggle- name="mcp-enabled"- label={t("llm.mcp_enabled")}- description={mcpUnavailable ? t("llm.mcp_unavailable_standalone") : t("llm.mcp_enabled_description")}- currentValue={mcpServing}- onChange={setMcpEnabled}- disabled={mcpUnavailable}- />+ <div className="options-section">+ <Card heading={t("llm.mcp_title")}>+ <CardOption+ name="mcp-enabled"+ label={t("llm.mcp_enabled")}+ description={mcpUnavailable ? t("llm.mcp_unavailable_standalone") : t("llm.mcp_enabled_description")}+ >+ <FormToggle+ currentValue={mcpServing}+ onChange={setMcpEnabled}+ disabled={mcpUnavailable}+ />+ </CardOption>- {mcpServing && (- <>- <OptionsRow name="mcp-endpoint" label={t("llm.mcp_endpoint_title")} description={t("llm.mcp_endpoint_description")} stacked>- <div class="mcp-endpoint-list">- <McpEndpointGroup label={t("llm.mcp_endpoint_this_device")} urls={[localUrl]} />- {networkUrls.length > 0 && (- <McpEndpointGroup label={t("llm.mcp_endpoint_network")} urls={networkUrls} />- )}- {networkInfo && !networkInfo.reachableOnNetwork && (- <p class="mcp-endpoint-note">{t("llm.mcp_endpoint_loopback_only")}</p>- )}- </div>- </OptionsRow>+ {mcpServing && (+ <>+ <CardOption+ name="mcp-endpoint"+ label={t("llm.mcp_endpoint_title")}+ description={t("llm.mcp_endpoint_description")}+ stacked+ >+ <div class="mcp-endpoint-list">+ <McpEndpointGroup label={t("llm.mcp_endpoint_this_device")} urls={[localUrl]} />+ {networkUrls.length > 0 && (+ <McpEndpointGroup label={t("llm.mcp_endpoint_network")} urls={networkUrls} />+ )}+ {networkInfo && !networkInfo.reachableOnNetwork && (+ <p class="mcp-endpoint-note">{t("llm.mcp_endpoint_loopback_only")}</p>+ )}+ </div>+ </CardOption>- <Collapsible title={t("llm.mcp_config_title")} initiallyExpanded>- <p>{t("llm.mcp_config_description")}</p>- <CodeBlock- mimeType="application/json"- code={buildMcpClientConfig(localUrl, tokenPlaceholder)}- placeholder={tokenPlaceholder}- />- <p>{t("llm.mcp_config_cli_description")}</p>- <CodeBlock- mimeType="text/x-sh"- code={buildMcpClientCommand(localUrl, tokenPlaceholder)}- placeholder={tokenPlaceholder}- wrap- />- <p class="mcp-config-warning">{t("llm.mcp_config_warning")}</p>- </Collapsible>- </>- )}- </OptionsSection>+ <CardSection>+ <Collapsible title={t("llm.mcp_config_title")} initiallyExpanded>+ <p>{t("llm.mcp_config_description")}</p>+ <CodeBlock+ mimeType="application/json"+ code={buildMcpClientConfig(localUrl, tokenPlaceholder)}+ placeholder={tokenPlaceholder}+ />+ <p>{t("llm.mcp_config_cli_description")}</p>+ <CodeBlock+ mimeType="text/x-sh"+ code={buildMcpClientCommand(localUrl, tokenPlaceholder)}+ placeholder={tokenPlaceholder}+ wrap+ />+ <p class="mcp-config-warning">{t("llm.mcp_config_warning")}</p>+ </Collapsible>+ </CardSection>+ </>+ )}+ </Card>+ </div>);}@@ -252,7 +270,11 @@ interface ProviderListProps {function ProviderList({ providers, onEdit, onDelete }: ProviderListProps) {if (!providers.length) {- return <NoItems icon="bx bx-bot" text={t("llm.no_providers_configured")} />;+ return (+ <CardSection>+ <NoItems icon="bx bx-bot" text={t("llm.no_providers_configured")} />+ </CardSection>+ );}return <>@@ -260,9 +282,8 @@ function ProviderList({ providers, onEdit, onDelete }: ProviderListProps) {const providerType = PROVIDER_TYPES.find(p => p.id === provider.provider);const modelCount = provider.selectedModels?.length ?? 0;return (- <OptionsRow+ <CardOptionkey={provider.id}- name="llm-provider"label={<span className="llm-provider-name">{providerType?.iconUrl && <MaskedIcon url={providerType.iconUrl} />}@@ -273,19 +294,20 @@ function ProviderList({ providers, onEdit, onDelete }: ProviderListProps) {? t("llm.provider_model_count", { count: modelCount }): providerType?.name || provider.provider}>- <>+ <span className="llm-provider-actions"><ActionButtonicon="bx bx-edit"text={t("llm.edit_provider")}onClick={() => onEdit(provider)}/><ActionButton+ className="destructive-action-icon"icon="bx bx-trash"text={t("llm.delete_provider")}onClick={() => onDelete(provider.id, provider.name)}/>- </>- </OptionsRow>+ </span>+ </CardOption>);})}</>;