F2-13: Tab 深链接 + schema title 回填(直开 /page/:id 自动建 Tab)

根因:直接输入 URL /page/:id 时,路由不在 tabs 列表中导致无法激活,
Tab 标题也只显示 templateId 而非真实页面标题。
修法:
1. AppShell useEffect:/page/ 深链接路由自动加入 tabs 并激活
2. TabTitleContext:提供 updateTabTitle 回调给子路由
3. PageRenderPage:schema 加载成功后调 updateTabTitle 回填真实 title

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 23:33:42 +08:00
parent ab14598524
commit 5536e39204
5 changed files with 40 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ 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 {
@@ -73,6 +74,7 @@ export function PageRenderPage() {
const { templateId = '' } = useParams();
const [schema, setSchema] = useState<Record<string, unknown> | null>(null);
const [error, setError] = useState('');
const { updateTabTitle } = useTabTitle();
useEffect(() => {
let alive = true;
@@ -90,6 +92,8 @@ export function PageRenderPage() {
// 渲染成功后写入最近打开(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 ?? '未知错误');

View File

@@ -19,6 +19,7 @@ import { SideMenu } from './SideMenu';
import { TabBar } from './TabBar';
import type { Tab } from './TabBar';
import { SettingsDrawer, initTheme } from './SettingsDrawer';
import { TabTitleContext } from './TabTitleContext';
const TABS_KEY = 'eip4-tabs';
const ACTIVE_KEY = 'eip4-active-tab';
@@ -71,11 +72,19 @@ export function AppShell() {
const profile = getProfile();
// 同步 activeKey 跟随浏览器地址(直接输入 URL / 后退前进)
// F2-13:直开 /page/:id 深链接时,若 Tab 不存在则自动新增并激活;title 先用 id 占位,
// PageRenderPage 拉到 schema 后通过 updateTabTitle 回填真实标题。
useEffect(() => {
const path = location.pathname;
// 若当前路径不在 tab 列表中则不自动新增(由菜单点击触发)
if (tabs.some((t) => t.key === path)) {
setActiveKey(path);
} else if (path.startsWith('/page/')) {
const templateId = path.slice('/page/'.length);
setTabs((prev) => {
if (prev.some((t) => t.key === path)) return prev;
return [...prev, { key: path, title: templateId }];
});
setActiveKey(path);
}
}, [location.pathname, tabs]);
@@ -136,6 +145,13 @@ export function AppShell() {
};
}, []);
// F2-13:回填 Tab 标题(PageRenderPage 拿到 schema title 后调用)
const updateTabTitle = useCallback((path: string, title: string) => {
setTabs((prev) =>
prev.map((t) => (t.key === path && t.title !== title ? { ...t, title } : t)),
);
}, []);
// 打开/激活一个 Tab
const openTab = useCallback(
(key: string, title: string) => {
@@ -232,7 +248,9 @@ export function AppShell() {
/>
<div className="app-pages">
{/* react-router Outlet 渲染当前路由内容 */}
<Outlet />
<TabTitleContext.Provider value={{ updateTabTitle }}>
<Outlet />
</TabTitleContext.Provider>
</div>
</div>
</div>

View File

@@ -0,0 +1,16 @@
// F2-13:Tab 标题回填 Context
// PageRenderPage 拉到 schema 后调 updateTabTitle 把真实标题写回 TabBar。
import { createContext, useContext } from 'react';
export interface TabTitleContextValue {
/** 将指定路由路径对应的 Tab 标题更新为 title */
updateTabTitle: (path: string, title: string) => void;
}
export const TabTitleContext = createContext<TabTitleContextValue>({
updateTabTitle: () => undefined,
});
export function useTabTitle() {
return useContext(TabTitleContext);
}