Files
DeepTutor/web/components/settings/shared.tsx
Antonio-Faure 75636f0735 perf(web): improve knowledge-base and settings accessibility and payload (#3139)
* fix(web): refine knowledge and settings accessibility and layout

Name row controls from their visible titles, portal tooltip descriptions out of tablists, and make the knowledge tabs keyboard-complete. Also stack the KB master-detail on phones, add a section-scroll hint, demote the indexing diagnostic to its detail tab, and consolidate the knowledge type scale.

* perf(web): cut knowledge and settings payload

Serve raster engine marks through next/image, stop preloading the serif face on pages that do not use it, and prefetch settings routes only on hover or keyboard focus instead of on first paint.

* docs(perf): record knowledge and settings audit

Document the protocol, baseline and after medians, payload and accessibility results, limits, and reproduction commands, with mobile before/after screenshots.

* test(web): match the model row's visible accessible name

The row no longer overrides its name with a provider-first aria-label; select it by the model-first text a screen reader now reads, which also contains the visible default/task badge.

* fix(web): keep the MinerU download-type name in the locale catalog

Reuse the existing Download models and Type strings instead of introducing a new t() literal, so the strict i18n audit stays green.

* docs(perf): link the UI audit from the handbook index

The docs contract requires every guide under docs-for-dev to be reachable from README.md.

* docs(perf): record the isolated knowledge-detail LCP

Re-measured the detail route with the host pinned away from unrelated work; the 668 ms row was contention, while the isolated median is 568 ms and the stable repeats overlap the baseline.

---------

Co-authored-by: Antonio-Faure <209759495+Antonio-Faure@users.noreply.github.com>
2026-12-01 17:12:12 +08:00

236 lines
8.8 KiB
TypeScript

"use client";
import { PageHeader } from '@/components/layout/FeaturePage'
import { createContext, useContext, useId } from 'react'
import type {
CatalogProfile,
ServiceName,
} from "@/features/settings/store/SettingsStore";
import type { AppLanguage } from "@/i18n/init";
import { getLocale } from "@/lib/datetime";
// Tailwind 3 silently drops `<color>-[var(--token)]/NN`: our tokens are hex
// literals, so it cannot split them into channels and emits no rule at all.
// Every tint, hover and ring here therefore goes through color-mix, which does
// compile. See reference: the old `focus:border` alone gave these controls no
// hover affordance and no visible focus ring.
//
// `leading-5` is load-bearing rather than decoration. Without it a control
// inherits its line height from whatever wraps it, so the same class rendered
// 34px tall inside a `text-xs` label and 39px tall next to one — two fields of
// the same kind, side by side in one grid row, at different heights and with
// their tops out of line. Height now follows the class rather than the parent.
// `block` is there for the same reason from the other side: an inline-block
// control sits on a line box, so the parent's strut left a few pixels above it
// that a control in a differently-worded column did not have.
export const fieldControlClass =
"block min-h-10 w-full rounded-lg border border-[var(--border)] px-3 py-2.5 text-[14px] leading-5 text-[var(--foreground)] outline-none transition-[border-color,box-shadow,background-color] duration-150 hover:border-[color-mix(in_srgb,var(--foreground)_22%,var(--border))] focus:border-[var(--ring)] focus:ring-2 focus:ring-[color-mix(in_srgb,var(--ring)_16%,transparent)] disabled:cursor-not-allowed disabled:opacity-60 disabled:hover:border-[var(--border)]";
export const inputClass = `${fieldControlClass} bg-transparent placeholder:text-[color-mix(in_srgb,var(--muted-foreground)_75%,transparent)]`;
export const nativeSelectClass = `${fieldControlClass} bg-[var(--background)] cursor-pointer`;
// `appearance-none` strips the platform arrow, so `dt-select` paints one back.
export const selectClass = `${nativeSelectClass} dt-select appearance-none`;
export const selectOptionClass =
"bg-[var(--background)] text-[var(--foreground)]";
// Nested panel inside a settings editor (provider probe, connection test, add
// form). These used to be `bg-[var(--muted)]/20`, which Tailwind 3 compiles to
// nothing at all for a hex custom property — so the panels had no fill and the
// pages read as a stack of hairlines. color-mix does compile.
export const subPanelClass =
"rounded-xl border border-[color-mix(in_srgb,var(--border)_60%,transparent)] bg-[color-mix(in_srgb,var(--muted)_30%,transparent)]";
export function stringifyExtraHeaders(
value: CatalogProfile["extra_headers"],
): string {
if (!value) return "";
if (typeof value === "string") return value;
try {
return JSON.stringify(value);
} catch {
return "";
}
}
export function statusDotClass(configured: boolean, hasError: boolean): string {
if (hasError) return "bg-red-400";
if (configured) return "bg-emerald-500";
return "bg-[var(--border)]";
}
/**
* Hairline between two items of the settings status strip. Lives here rather
* than inside the strip because items that can render nothing (MemoryUsageItem)
* have to draw their own leading rule — otherwise hiding the item leaves a
* dangling separator behind.
*/
export function StatusStripDivider() {
return (
<span
aria-hidden
className="hidden h-7 w-px shrink-0 bg-[color-mix(in_srgb,var(--border)_70%,transparent)] sm:block"
/>
);
}
export function formatContextWindowSource(
source: string | undefined,
t: (key: string) => string,
): string {
if (source === "manual") return t("Manual");
if (source === "metadata") return t("Provider metadata");
if (source === "known_model") return t("Model catalog (models.dev)");
if (source === "default") return t("Conservative fallback (not detected)");
return t("Unset");
}
export function formatContextWindowUpdatedAt(
value: string | undefined,
language: AppLanguage,
): string {
if (!value) return "";
const parsed = new Date(value);
if (Number.isNaN(parsed.getTime())) return value;
return parsed.toLocaleString(getLocale(language), {
dateStyle: "medium",
timeStyle: "short",
});
}
export function activeProfileDetail(
profile: CatalogProfile | null,
service: ServiceName,
t: (key: string) => string,
): string {
if (!profile) return t("No profile");
if (service === "search") return profile.provider || t("No provider");
return profile.base_url || t("No endpoint");
}
export function activeModelDetail(
profile: CatalogProfile | null,
model: { model?: string; name?: string } | null,
service: ServiceName,
t: (key: string) => string,
): string {
if (service === "search") return profile?.provider || t("No provider");
return model?.model || model?.name || t("No model selected");
}
// Category-label typography. English looks good with uppercase + wide tracking;
// CJK glyphs are already square blocks so we drop both and bump size a hair.
export function labelClass(
size: "sm" | "md" | "lg",
language: AppLanguage,
): string {
if (language === "zh") {
if (size === "sm") return "text-[10.5px] font-medium";
if (size === "lg") return "text-[12px] font-medium";
return "text-[11px] font-medium";
}
if (size === "sm")
return "text-[9.5px] font-semibold uppercase tracking-[0.16em]";
if (size === "lg") return "text-[11px] uppercase tracking-[0.16em]";
return "text-[10px] font-semibold uppercase tracking-[0.16em]";
}
// Lets a control inside a row take its accessible name from the row title.
// `Toggle` consumes this so switches stop announcing only "switch".
const SettingRowLabelContext = createContext<string | undefined>(undefined);
export function useSettingRowLabelId() {
return useContext(SettingRowLabelContext);
}
// One-row settings group used on simple pages (Appearance, Status etc.).
// Title + optional description on the left, control flushed right. Matches
// the visual rhythm used on Codex-style preferences pages.
export function SettingRow({
title,
description,
control,
}: {
title: string;
description?: string;
control: React.ReactNode;
}) {
const titleId = useId();
return (
<div className="flex flex-col items-start justify-between gap-3 sm:flex-row sm:gap-6 border-t border-[color-mix(in_srgb,var(--border)_50%,transparent)] py-5 first:border-t-0">
<div className="min-w-0 flex-1">
<div id={titleId} className="text-[14px] font-medium text-[var(--foreground)]">
{title}
</div>
{description && (
<p className="mt-1 text-[13px] leading-relaxed text-[var(--muted-foreground)]">
{description}
</p>
)}
</div>
<div className="max-w-full shrink-0">
<SettingRowLabelContext.Provider value={titleId}>
{control}
</SettingRowLabelContext.Provider>
</div>
</div>
);
}
// Page-level section group. Title + optional description, then children.
export function SettingSection({
title,
description,
children,
}: {
title: string;
description?: string;
children: React.ReactNode;
}) {
return (
<section className="mb-10">
<header className="mb-3">
<h2 className="text-[15px] font-semibold tracking-tight text-[var(--foreground)]">
{title}
</h2>
{description && (
<p className="mt-1 text-[13px] leading-relaxed text-[var(--muted-foreground)]">
{description}
</p>
)}
</header>
{/* A rule, not a box. The app divides with hairlines and whitespace —
a bordered, tinted panel around rows that already separate
themselves with hairlines was dividing the same content twice. The
top rule is what tells a section where it starts, and it has to live
here rather than on the first row because sections also hold custom
content (theme tiles, previews) that draws no rule of its own. */}
<div className="border-t border-[color-mix(in_srgb,var(--border)_60%,transparent)]">
{children}
</div>
</section>
);
}
// Page heading shared across settings sub-pages. The global Save Draft / Apply
// toolbar (which also shows where this module persists to) lives above this, so
// each page just owns its title row.
export function SettingsPageHeader({
title,
description,
actions,
}: {
title: string;
description?: string;
// The page's primary action belongs on the title row. Workspace pages used to
// repeat a second description line just to have somewhere to hang the button,
// which left a dead band between the heading and the content.
actions?: React.ReactNode;
}) {
return (
<PageHeader title={title} description={description} action={actions} headingTour="tour-page-heading" />
);
}