// 页面渲染路由 /page/:templateId —— 取转换后 schema 交 amis 渲染 // F1-4:加错误边界(schema 拉取失败/JSON 异常/amis 渲染抛错均显示页内错误卡片) // F4-L1:管理员可见「设计」按钮,打开设计器(editor.html 多页入口) import { Component, useEffect, useState } from 'react'; import type { ReactNode } from 'react'; import { useParams } from 'react-router-dom'; import { fetchSchema } from '../api/schema'; import { AmisRenderer } from '../components/AmisRenderer'; import { recordRecentPage } from '../utils/recentPages'; import { useTabTitle } from '../shell/TabTitleContext'; // ---- amis 渲染层错误边界(class component,捕获子树抛错)---- interface ErrorBoundaryState { caught: boolean; message: string; } class AmisErrorBoundary extends Component< { templateId: string; children: ReactNode }, ErrorBoundaryState > { constructor(props: { templateId: string; children: ReactNode }) { super(props); this.state = { caught: false, message: '' }; } static getDerivedStateFromError(error: unknown): ErrorBoundaryState { const msg = error instanceof Error ? error.message : String(error); return { caught: true, message: msg }; } override componentDidCatch() { // 静默记录,不向上抛 } override render() { if (this.state.caught) { return ( ); } return this.props.children; } } // ---- 统一错误卡片 ---- function SchemaErrorCard({ templateId, summary }: { templateId: string; summary: string }) { return (
页面加载失败
模板 ID:{templateId}
{summary}
); } // ---- 主组件 ---- export function PageRenderPage() { const { templateId = '' } = useParams(); const [schema, setSchema] = useState | null>(null); const [error, setError] = useState(''); const { updateTabTitle } = useTabTitle(); useEffect(() => { let alive = true; setSchema(null); setError(''); fetchSchema(templateId) .then((s) => { if (!alive) return; // 校验顶层结构:必须是对象,否则视为 JSON 异常 if (typeof s !== 'object' || s === null || Array.isArray(s)) { setError('schema 格式异常:顶层不是 JSON 对象'); return; } setSchema(s); // 渲染成功后写入最近打开(title 取 schema.title 或 templateId) const title = (s['title'] as string) || templateId; recordRecentPage({ templateId, name: title, path: title }); // F2-13:回填 Tab 标题为 schema title updateTabTitle(`/page/${templateId}`, title); }) .catch((e: Error) => { if (alive) setError(e.message ?? '未知错误'); }); return () => { alive = false; }; }, [templateId]); if (error) { return ; } if (!schema) { return (
加载中…
); } return (
{/* 设计入口已收敛到顶栏「设计中心」(admin),不再每页一颗按钮 */}
); }