fix(webui): localize the component gallery

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-20 16:52:44 +08:00
1 parent 282839cc65
commit ae5cb64e9e
54 files changed
+11120 -719

No files matched your search

+20 -14
View File
@@ -10,11 +10,13 @@ import type { ChannelValidationPayload } from "@/lib/types";
import { copyTextToClipboard } from "@/lib/clipboard";
import { channelFeatures } from "./channel-fixtures";
import { StatePicker, type ComponentExample } from "./example";
import { useGalleryTranslation } from "./i18n";
function ChannelEntryPreview({ name }: { name: string }) {
const { t, i18n } = useTranslation();
const { t: g } = useGalleryTranslation();
const [mode, setMode] = useState<"setup" | "configured" | "enabled" | "checking" | "error">("setup");
return <><StatePicker label="频道配置状态" value={mode}
return <><StatePicker label={g("previews.channel.state")} value={mode}
options={["setup", "configured", "enabled", "checking", "error"]} onChange={setMode} />
<ChannelFormPreview key={mode} name={name} mode={mode} locale={i18n.resolvedLanguage ?? i18n.language} t={t} />
</>;
@@ -26,13 +28,14 @@ function ChannelFormPreview({ name, mode, locale, t }: {
locale: string;
t: ReturnType<typeof useTranslation>["t"];
}) {
const { t: g } = useGalleryTranslation();
const snapshot = channelFeatures[name];
const configured = mode !== "setup";
const [enabled, setEnabled] = useState(mode === "enabled");
const feature = { ...snapshot, configured, enabled, ready: configured, status: enabled ? "enabled" : "not_enabled",
configured_fields: configured ? snapshot.setup!.fields.filter((field) => field.kind === "secret").map((field) => field.key) : [],
runtime_status: enabled ? "running" : "stopped",
runtime_error: mode === "error" ? "Example credentials were rejected. Update the preview values and retry." : undefined };
runtime_error: mode === "error" ? g("previews.channel.credentialsError") : undefined };
const setup = channelSetup(feature, locale);
const fields = setup.fields ?? [];
const requirementKeys = new Set((setup.requirements ?? []).flatMap((item) => item.alternatives.flat()));
@@ -54,27 +57,27 @@ function ChannelFormPreview({ name, mode, locale, t }: {
const [clearedSecrets, setClearedSecrets] = useState<Set<string>>(() => new Set());
const [errors, setErrors] = useState<Record<string, string>>({});
const [advancedOpen, setAdvancedOpen] = useState(false);
const [notice, setNotice] = useState<string | null>(null);
const [notice, setNotice] = useState<{ key: string } | { message: string } | null>(null);
const [saved, setSaved] = useState(false);
const [checked, setChecked] = useState(false);
const advancedPanelId = useId();
const validate = () => {
const next = channelRequirementErrors(fields, setup.requirements ?? [], values, configuredFields, clearedSecrets,
t("settings.channels.fieldRequired", { defaultValue: "Required to complete setup." }));
"settings.channels.fieldRequired");
setErrors(next);
if (Object.keys(next).length) {
if (advancedFields.some((field) => next[field.key])) setAdvancedOpen(true);
focusFirstChannelFieldError(next);
setNotice(t("settings.channels.validationFailed", { defaultValue: "Check the required setup before enabling." }));
setNotice({ key: "settings.channels.validationFailed" });
return false;
}
setSaved(true);
setNotice("预览配置已保存");
setNotice({ key: "settings.channels.savedSettings" });
return true;
};
const validation: ChannelValidationPayload | null = checked ? {
name, status: "connected", can_enable: true, requires_restart: false, missing_fields: [],
checks: [{ id: "preview", label: "Preview configuration", status: "pass" }],
checks: [{ id: "preview", label: g("previews.channel.validation"), status: "pass" }],
} : null;
return <aside className={CHANNEL_SETUP_PANEL_CLASS_NAME}>
<ChannelSetupSurfaceView feature={feature} setup={setup}
@@ -82,9 +85,11 @@ function ChannelFormPreview({ name, mode, locale, t }: {
<h3 className="sr-only">{localizedChannelDisplayName(feature, t)}</h3></div>}
primaryFields={primaryFields} advancedFields={advancedFields} savedSecretFields={savedSecretFields}
availablePresets={configured ? [] : setup.presets ?? []} configuredFields={configuredFields}
fieldValues={values} visibleSecrets={visibleSecrets} clearedSecrets={clearedSecrets} fieldErrors={errors}
fieldValues={values} visibleSecrets={visibleSecrets} clearedSecrets={clearedSecrets}
fieldErrors={Object.fromEntries(Object.entries(errors).map(([key, message]) => [key, t(message)]))}
actionPending={mode === "checking"} enabled={enabled} autoSaving={false} autoSaveState={saved ? "saved" : "idle"}
advancedOpen={advancedOpen} advancedPanelId={advancedPanelId} validation={validation} validating={mode === "checking"} notice={notice}
advancedOpen={advancedOpen} advancedPanelId={advancedPanelId} validation={validation} validating={mode === "checking"}
notice={notice ? "key" in notice ? t(notice.key) : notice.message : null}
onAdvancedOpenChange={setAdvancedOpen}
onChangeField={(key, value) => { setValues((current) => ({ ...current, [key]: value })); setErrors({}); setSaved(false); }}
onFieldBlur={() => { setSaved(true); }}
@@ -97,8 +102,8 @@ function ChannelFormPreview({ name, mode, locale, t }: {
onCheckConnection={() => { if (validate()) setChecked(true); }}
onToggleEnabled={(next) => { if (!next || validate()) setEnabled(next); }}
onSaveConnectionSettings={() => setSaved(true)}
onCopyCommand={() => { if (setup.command) void copyTextToClipboard(setup.command).then((ok) => setNotice(ok ? "已复制命令" : "未能复制命令")); }}
onNotice={setNotice} />
onCopyCommand={() => { if (setup.command) void copyTextToClipboard(setup.command).then((ok) => setNotice({ key: ok ? "settings.channels.commandCopied" : "settings.channels.commandCopyFailed" })); }}
onNotice={(message) => setNotice(message ? { message } : null)} />
</aside>;
}
@@ -106,10 +111,11 @@ const genericRegistrations = registeredChannelUiContributions()
.filter(({ contribution }) => !contribution.Panel && !contribution.ConnectFlow);
export const channelExamples: ComponentExample[] = genericRegistrations.map(({ channel, webui, contribution }, index) => ({
title: `${contribution.presentation.displayName} · 频道配置`,
title: "previews.channel.title",
titleValues: { channel: contribution.presentation.displayName },
files: [`nanobot/channels/${channel}/${webui}`, ...(index === 0 ? ["webui/src/components/settings/channels/ChannelSetupSurfaceView.tsx"] : [])],
description: "查看真实频道配置表单与状态。",
options: "使用频道注册定义、Python 设置契约快照和生产 ChannelSetupSurfaceView。输入、凭据显示、预设、检查与开关只更新当前预览。",
description: "previews.channel.description",
options: "previews.channel.options",
code: `<ChannelSetupSurfaceView feature={channelFeatures.${channel}} setup={setup} {...displayState} {...actions} />`,
Preview: () => <ChannelEntryPreview name={channel} />,
}));