import { useState } from "react"; import { useTranslation } from "react-i18next"; import { channelTranslator } from "@/channel-plugins/i18n"; import { defaultChannelFieldValues } from "@/components/settings/channels/CredentialForm"; import type { ChannelConnectPayload } from "@/lib/types"; import { WeixinPanelView } from "../../../../nanobot/channels/weixin/webui/WeixinPanelView"; import { WeixinConnectFlowView } from "../../../../nanobot/channels/weixin/webui/WeixinConnectFlowView"; import { localizeBooleanFields, WEIXIN_AUTH_EXPIRED_MESSAGE, weixinSetupFields, type WeixinVerificationPayload, } from "../../../../nanobot/channels/weixin/webui/presentation"; import { channelFeatures } from "./channel-fixtures"; import { StatePicker, type ComponentExample } from "./example"; import { useGalleryTranslation } from "./i18n"; const scan: ChannelConnectPayload = { session_id: "gallery-weixin", status: "pending", qr_url: "https://example.com/gallery/weixin-login", }; type ConnectState = "starting" | "scan" | "verify" | "verify-error" | "connected" | "expired" | "failed" | "cancelled" | "error"; function WeixinConnectPreview() { const { t } = useGalleryTranslation(); const [mode, setMode] = useState("scan"); const [notice, setNotice] = useState(""); const connect: ChannelConnectPayload | WeixinVerificationPayload | null = mode === "starting" ? null : mode === "scan" || mode === "error" ? scan : mode === "verify" || mode === "verify-error" ? { ...scan, challenge: "verify_code", verification_failed: mode === "verify-error", } : { ...scan, status: mode === "connected" ? "succeeded" : mode }; return <> { setMode("scan"); setNotice("previews.weixinConnect.refreshed"); }} onCancel={() => setMode("cancelled")} onPoll={async (params) => { if (params?.verify_code === "123456") { setMode("connected"); return { ...scan, status: "succeeded" }; } setMode("verify-error"); return { ...scan, challenge: "verify_code", verification_failed: true }; }} />

{t(notice || "previews.weixinConnect.hint")}

; } type PanelState = "unconfigured" | "running" | "auth-expired" | "saving" | "saved" | "save-error"; function WeixinPanelPreview() { const { t, i18n } = useTranslation(); const { t: g } = useGalleryTranslation(); const channelTx = channelTranslator(t, "weixin"); const [mode, setMode] = useState("running"); const [edited, setEdited] = useState(false); const feature = { ...channelFeatures.weixin, configured: mode !== "unconfigured", enabled: mode !== "unconfigured", runtime_status: mode === "auth-expired" ? "failed" : mode === "unconfigured" ? "stopped" : "running", runtime_error: mode === "auth-expired" ? WEIXIN_AUTH_EXPIRED_MESSAGE : undefined, }; const setup = weixinSetupFields(feature, i18n.resolvedLanguage ?? i18n.language); const onLabel = t("settings.values.on", { defaultValue: "On" }); const offLabel = t("settings.values.off", { defaultValue: "Off" }); const primaryFields = localizeBooleanFields(setup.primary, onLabel, offLabel); const advancedFields = localizeBooleanFields(setup.advanced, onLabel, offLabel); const [values, setValues] = useState(() => defaultChannelFieldValues([...primaryFields, ...advancedFields], { "channels.weixin.allowFrom": "preview-user", })); const [connect, setConnect] = useState(scan); const runtimeError = mode === "auth-expired" ? channelTx("custom.expired", WEIXIN_AUTH_EXPIRED_MESSAGE) : undefined; return <> { setMode(next); setEdited(false); }} /> setConnect(scan)} onCancel={() => setConnect({ ...scan, status: "cancelled" })} onPoll={async () => connect} /> : null} onInstall={() => {}} onFieldChange={(key, value) => { setValues((current) => ({ ...current, [key]: value })); setEdited(true); }} /> ; } export const weixinChannelExamples: ComponentExample[] = [ { title: "previews.weixinPanel.title", files: ["nanobot/channels/weixin/webui/WeixinPanelView.tsx"], Preview: WeixinPanelPreview, description: "previews.weixinPanel.description", options: "previews.weixinPanel.options", code: '' }, { title: "previews.weixinConnect.title", files: ["nanobot/channels/weixin/webui/WeixinConnectFlowView.tsx"], Preview: WeixinConnectPreview, description: "previews.weixinConnect.description", options: "previews.weixinConnect.options", code: '' }, ];