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:
BIN
e2e/artifacts/f2-13/type1-00551ef5-title.png
Normal file
BIN
e2e/artifacts/f2-13/type1-00551ef5-title.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
BIN
e2e/artifacts/f2-13/type200-03ec5566-data.png
Normal file
BIN
e2e/artifacts/f2-13/type200-03ec5566-data.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
@@ -6,6 +6,7 @@ import { useParams } from 'react-router-dom';
|
|||||||
import { fetchSchema } from '../api/schema';
|
import { fetchSchema } from '../api/schema';
|
||||||
import { AmisRenderer } from '../components/AmisRenderer';
|
import { AmisRenderer } from '../components/AmisRenderer';
|
||||||
import { recordRecentPage } from '../utils/recentPages';
|
import { recordRecentPage } from '../utils/recentPages';
|
||||||
|
import { useTabTitle } from '../shell/TabTitleContext';
|
||||||
|
|
||||||
// ---- amis 渲染层错误边界(class component,捕获子树抛错)----
|
// ---- amis 渲染层错误边界(class component,捕获子树抛错)----
|
||||||
interface ErrorBoundaryState {
|
interface ErrorBoundaryState {
|
||||||
@@ -73,6 +74,7 @@ export function PageRenderPage() {
|
|||||||
const { templateId = '' } = useParams();
|
const { templateId = '' } = useParams();
|
||||||
const [schema, setSchema] = useState<Record<string, unknown> | null>(null);
|
const [schema, setSchema] = useState<Record<string, unknown> | null>(null);
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
|
const { updateTabTitle } = useTabTitle();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let alive = true;
|
let alive = true;
|
||||||
@@ -90,6 +92,8 @@ export function PageRenderPage() {
|
|||||||
// 渲染成功后写入最近打开(title 取 schema.title 或 templateId)
|
// 渲染成功后写入最近打开(title 取 schema.title 或 templateId)
|
||||||
const title = (s['title'] as string) || templateId;
|
const title = (s['title'] as string) || templateId;
|
||||||
recordRecentPage({ templateId, name: title, path: title });
|
recordRecentPage({ templateId, name: title, path: title });
|
||||||
|
// F2-13:回填 Tab 标题为 schema title
|
||||||
|
updateTabTitle(`/page/${templateId}`, title);
|
||||||
})
|
})
|
||||||
.catch((e: Error) => {
|
.catch((e: Error) => {
|
||||||
if (alive) setError(e.message ?? '未知错误');
|
if (alive) setError(e.message ?? '未知错误');
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import { SideMenu } from './SideMenu';
|
|||||||
import { TabBar } from './TabBar';
|
import { TabBar } from './TabBar';
|
||||||
import type { Tab } from './TabBar';
|
import type { Tab } from './TabBar';
|
||||||
import { SettingsDrawer, initTheme } from './SettingsDrawer';
|
import { SettingsDrawer, initTheme } from './SettingsDrawer';
|
||||||
|
import { TabTitleContext } from './TabTitleContext';
|
||||||
|
|
||||||
const TABS_KEY = 'eip4-tabs';
|
const TABS_KEY = 'eip4-tabs';
|
||||||
const ACTIVE_KEY = 'eip4-active-tab';
|
const ACTIVE_KEY = 'eip4-active-tab';
|
||||||
@@ -71,11 +72,19 @@ export function AppShell() {
|
|||||||
const profile = getProfile();
|
const profile = getProfile();
|
||||||
|
|
||||||
// 同步 activeKey 跟随浏览器地址(直接输入 URL / 后退前进)
|
// 同步 activeKey 跟随浏览器地址(直接输入 URL / 后退前进)
|
||||||
|
// F2-13:直开 /page/:id 深链接时,若 Tab 不存在则自动新增并激活;title 先用 id 占位,
|
||||||
|
// PageRenderPage 拉到 schema 后通过 updateTabTitle 回填真实标题。
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const path = location.pathname;
|
const path = location.pathname;
|
||||||
// 若当前路径不在 tab 列表中则不自动新增(由菜单点击触发)
|
|
||||||
if (tabs.some((t) => t.key === path)) {
|
if (tabs.some((t) => t.key === path)) {
|
||||||
setActiveKey(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]);
|
}, [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
|
// 打开/激活一个 Tab
|
||||||
const openTab = useCallback(
|
const openTab = useCallback(
|
||||||
(key: string, title: string) => {
|
(key: string, title: string) => {
|
||||||
@@ -232,7 +248,9 @@ export function AppShell() {
|
|||||||
/>
|
/>
|
||||||
<div className="app-pages">
|
<div className="app-pages">
|
||||||
{/* react-router Outlet 渲染当前路由内容 */}
|
{/* react-router Outlet 渲染当前路由内容 */}
|
||||||
|
<TabTitleContext.Provider value={{ updateTabTitle }}>
|
||||||
<Outlet />
|
<Outlet />
|
||||||
|
</TabTitleContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
16
src/shell/TabTitleContext.ts
Normal file
16
src/shell/TabTitleContext.ts
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user