优化:查询页面平滑切换与默认月份高亮

This commit is contained in:
2026-11-01 14:49:37 +08:00
parent d89bd2e451
commit 19bd9b0122
14 changed files with 450 additions and 126 deletions
+26 -7
View File
@@ -1,3 +1,5 @@
import { flushSync } from 'react-dom';
import { createWorkspaceTransition, waitForWorkspaceContent } from './workspaceTransition';
import { forwardRef, lazy, Suspense, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from 'react';
import { Alert, Button, Spin } from 'antd';
import { api } from './api';
@@ -22,12 +24,20 @@ export const CachedWorkspacePages = forwardRef<WorkspaceNavigation, {
page: PageKey; user: AuthUser; onPageChange: (page: PageKey) => void; onUserChange: (user: AuthUser) => void; onUnauthorized: (error: unknown) => boolean;
}>(function CachedWorkspacePages({ page, user, onPageChange, onUserChange, onUnauthorized }, ref) {
const entries = useRef(new Map<PageKey, Entry>([[page, { initialSearch: location.search, search: location.search, key: 0, scroll: 0 }]]));
const requested = useRef(page);
const current = useRef(page), generation = useRef(0), key = useRef(0);
const revisions = useRef<{ authRevision: string; dataRevision: string } | null>(null);
const validation = useRef<Promise<boolean> | null>(null);
const [refreshKey, setRefreshKey] = useState(0), [, render] = useState(0);
const [ready, setReady] = useState(false), [error, setError] = useState('');
const [navigationBusy, setNavigationBusy] = useState(false);
const alive = useRef(true);
const transitions = useRef<ReturnType<typeof createWorkspaceTransition> | null>(null);
if (!transitions.current) transitions.current = createWorkspaceTransition(
typeof document.startViewTransition === 'function' ? document.startViewTransition.bind(document) : undefined,
() => window.matchMedia('(prefers-reduced-motion: reduce)').matches,
busy => { if (alive.current) setNavigationBusy(busy); },
);
const validate = () => {
if (validation.current) return validation.current;
const epoch = generation.current;
@@ -39,6 +49,7 @@ export const CachedWorkspacePages = forwardRef<WorkspaceNavigation, {
const latest = await api.me();
if (!alive.current || generation.current !== epoch) return false;
// 权限改变时清除全部旧缓存,重建此刻正在查看的页面;校验不负责切换路由。
transitions.current?.cancel(); requested.current = current.current;
onUserChange(latest); entries.current.clear();
entries.current.set(current.current, { initialSearch: location.search, search: location.search, key: ++key.current, scroll: 0 });
render(value => value + 1);
@@ -66,14 +77,22 @@ export const CachedWorkspacePages = forwardRef<WorkspaceNavigation, {
const previous = entries.current.get(next);
if (!previous || force) entries.current.set(next, { initialSearch: search, search, key: ++key.current, scroll: 0 });
const entry = entries.current.get(next)!;
if (!pop) history.pushState({}, '', `${location.pathname}${entry.search}`);
current.current = next; onPageChange(next); render(value => value + 1);
// 本地页面先响应点击,网络校验在后台进行;连续切换共享尚未结束的请求。
void validate();
requested.current = next;
transitions.current!.run(async signal => {
if (!alive.current || entries.current.get(next) !== entry) return;
// 与浏览器快照同步提交页面及滚动位置,避免转场结束后再补一次定位。
flushSync(() => {
if (!pop) history.pushState({}, '', `${location.pathname}${entry.search}`);
current.current = next; onPageChange(next); render(value => value + 1);
});
window.scrollTo(0, entry.scroll);
void validate();
await waitForWorkspaceContent(() => document.querySelector(`.page-view[data-page="${next}"]`), signal);
});
};
const navigate = (next: PageKey, query?: QueryParams, resultRef?: ResultRef) => {
if (next === 'imports') next = 'sources';
if (next === current.current && !query && !resultRef) return;
if (next === requested.current && !query && !resultRef) return;
const params = new URLSearchParams({ page: next });
if (query) params.set('context', encode({ query, ...(resultRef ? { resultRef } : {}) }));
const cached = entries.current.get(next);
@@ -89,7 +108,7 @@ export const CachedWorkspacePages = forwardRef<WorkspaceNavigation, {
void validate();
const invalidate = () => setRefreshKey(value => value + 1);
window.addEventListener(WORKSPACE_INVALIDATED, invalidate);
return () => { alive.current = false; generation.current++; validation.current = null; window.removeEventListener(WORKSPACE_INVALIDATED, invalidate); };
return () => { alive.current = false; transitions.current?.cancel(); generation.current++; validation.current = null; window.removeEventListener(WORKSPACE_INVALIDATED, invalidate); };
}, []);
const activeEntryKey = entries.current.get(page)?.key;
useLayoutEffect(() => {
@@ -101,7 +120,7 @@ export const CachedWorkspacePages = forwardRef<WorkspaceNavigation, {
if (!ready) return connectionNotice || <div className="loading-panel"><Spin />正在打开工作台…</div>;
return <>{connectionNotice}{Array.from(entries.current.entries()).filter(([name]) => retained(name) || name === page).map(([name, entry]) => {
const active = name === page;
return <div key={`${name}-${entry.key}`} className="page-view" data-page={name} hidden={!active} style={!active ? { display: 'none' } : undefined}>
return <div key={`${name}-${entry.key}`} className="page-view" data-page={name} hidden={!active} inert={active && navigationBusy} style={!active ? { display: 'none' } : undefined}>
<PageCacheContext.Provider value={{ active, initialSearch: entry.initialSearch, refreshKey, onLocationChange: search => { entry.search = search; } }}>
<Suspense fallback={<div className="loading-panel"><Spin />正在打开页面…</div>}>
{name === 'blackProduct' || name === 'brightProduct' ? <StageProductPage stage={name === 'blackProduct' ? 'black' : 'bright'} onUnauthorized={onUnauthorized} /> : QUERY_PAGES.includes(name) ? <QueryPage page={name} onUnauthorized={onUnauthorized} onNavigate={navigate} /> : <ManagementPages page={name} user={user} onUnauthorized={onUnauthorized} onNavigate={navigate} />}