"use client" import { useEffect, useMemo, useState } from "react" import type { KefuWidgetHostConfig } from "@/lib/kefu-widget-config" const STORAGE_KEY = "cs-agent-web-widget-test-config" const INITIAL_CONFIG: KefuWidgetHostConfig = { channelId: "", baseUrl: "", apiBaseUrl: "", } declare global { interface Window { CSAgentWidget?: { mount: (config: KefuWidgetHostConfig) => void destroy: () => void open: () => void close: () => void } } } function getDefaultConfig(): KefuWidgetHostConfig { if (typeof window === "undefined") { return INITIAL_CONFIG } const savedText = window.localStorage.getItem(STORAGE_KEY) const savedConfig = savedText ? (JSON.parse(savedText) as Partial) : {} const query = new URLSearchParams(window.location.search) return { channelId: query.get("channelId") ?? savedConfig.channelId ?? "", baseUrl: "", apiBaseUrl: "", } } function removeMountedWidget() { if (typeof window === "undefined") { return } window.CSAgentWidget?.destroy() document .querySelectorAll( '[data-cs-agent-widget="launcher"], [data-cs-agent-widget="frame"], [data-cs-agent-widget="script"]' ) .forEach((node) => node.remove()) delete window.CSAgentConfig delete window.__CS_AGENT_WIDGET_CONFIG__ delete window.__CS_AGENT_WIDGET_STATE__ delete window.CSAgentWidget } function injectWidget(config: KefuWidgetHostConfig) { removeMountedWidget() window.CSAgentConfig = config const script = document.createElement("script") script.async = true script.src = `${window.location.origin}/sdk/cs-ai-agent-sdk.min.js` script.dataset.csAgentWidget = "script" document.body.appendChild(script) } export function KefuWidgetDemo() { const [config, setConfig] = useState(INITIAL_CONFIG) const [status, setStatus] = useState("请填写 channelId") const [origin, setOrigin] = useState("") useEffect(() => { const timer = window.setTimeout(() => { const initialConfig = getDefaultConfig() setOrigin(window.location.origin) setConfig(initialConfig) setStatus(initialConfig.channelId ? "Widget 已挂载" : "请填写 channelId") if (initialConfig.channelId) { injectWidget(initialConfig) } }, 0) return () => { window.clearTimeout(timer) removeMountedWidget() } }, []) const snippet = useMemo(() => { const scriptSrc = origin ? `${origin}/sdk/cs-ai-agent-sdk.min.js` : "/sdk/cs-ai-agent-sdk.min.js" return ` ` }, [config, origin]) function updateField( key: K, value: KefuWidgetHostConfig[K] ) { setConfig((current) => ({ ...current, [key]: value })) } function handleMount() { const nextConfig: KefuWidgetHostConfig = { ...config, channelId: config.channelId.trim(), baseUrl: "", apiBaseUrl: "", } setConfig(nextConfig) window.localStorage.setItem(STORAGE_KEY, JSON.stringify(nextConfig)) if (!nextConfig.channelId) { removeMountedWidget() setStatus("请填写 channelId") return } injectWidget(nextConfig) setStatus("Widget 已挂载") } return (
Widget 挂载测试
{status}
updateField("channelId", value)} />
接入代码
            {snippet}
          
) } function TextField({ label, value, onChange, }: { label: string value?: string onChange: (value: string) => void }) { return ( ) }