forked from longtao.wu/droboticflow
🛡️ Use router blockers to protect annotation drafts on navigation
This commit is contained in:
parent
52263727f6
commit
d4cd2b1872
192
src/App.tsx
192
src/App.tsx
@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import React, { Suspense, lazy } from "react";
|
||||
import { BrowserRouter as Router, Navigate, Routes, Route, useLocation } from "react-router-dom";
|
||||
import { createBrowserRouter, RouterProvider, Navigate, Routes, Route, useLocation } from "react-router-dom";
|
||||
import { Layout } from "./components/Layout";
|
||||
import { TimeZoneProvider } from "./context/TimeZoneContext";
|
||||
import { UserProvider, useUser } from "./context/UserContext";
|
||||
@ -22,13 +22,11 @@ function RouteLoadError({ error }: { error?: Error | null }) {
|
||||
return (
|
||||
<div className="flex min-h-[360px] items-center justify-center p-6">
|
||||
<div className="max-w-xl rounded-2xl border border-[#2a2b2f] bg-[#151619] p-6 text-center">
|
||||
<div className="text-base font-semibold text-white">
|
||||
{isChunkError ? "页面资源已更新" : "页面遇到渲染异常"}
|
||||
</div>
|
||||
<div className="text-base font-semibold text-white">{isChunkError ? "页面资源已更新" : "页面遇到渲染异常"}</div>
|
||||
<p className="mt-2 text-sm leading-6 text-gray-400">
|
||||
{isChunkError
|
||||
? "当前页面加载的是旧版本资源,刷新后会重新载入最新页面。"
|
||||
: (error?.message || "组件渲染发生错误,请刷新重试。")}
|
||||
: error?.message || "组件渲染发生错误,请刷新重试。"}
|
||||
</p>
|
||||
{!isChunkError && error?.stack ? (
|
||||
<pre className="mt-3 max-h-40 overflow-auto rounded-lg bg-black/40 p-3 text-left font-mono text-xs text-rose-300">
|
||||
@ -161,100 +159,108 @@ function RoleRouteGuard({ children }: { children: React.ReactNode }) {
|
||||
return children;
|
||||
}
|
||||
|
||||
function ApplicationRoutes() {
|
||||
return (
|
||||
<Layout>
|
||||
<RouteErrorBoundary>
|
||||
<Suspense fallback={<RouteFallback />}>
|
||||
<RoleRouteGuard>
|
||||
<Routes>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/projects" element={<Projects />} />
|
||||
<Route path="/project/:id" element={<ProjectDetail />} />
|
||||
<Route path="/operators" element={<Operators />} />
|
||||
<Route path="/workflow/:id" element={<WorkflowEditor />} />
|
||||
<Route path="/data-assets" element={<DataAssets />} />
|
||||
<Route path="/data-operations" element={<DataOperations />} />
|
||||
<Route path="/database" element={<DatabaseBrowser />} />
|
||||
<Route path="/object-storage" element={<ObjectStorageBrowser />} />
|
||||
<Route path="/object-sync" element={<ObjectSync />} />
|
||||
<Route path="/source-data" element={<LegacyDataAssetsRedirect />} />
|
||||
<Route path="/packages" element={<LegacyDataAssetsRedirect />} />
|
||||
<Route path="/sampling" element={<Sampling />} />
|
||||
<Route
|
||||
path="/annotation-operations"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/clip"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/clip-quality"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/annotate"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/quality"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/acceptance"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/items/:itemId"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route path="/monitoring" element={<Monitoring />} />
|
||||
<Route path="/runs" element={<Runs />} />
|
||||
<Route path="/audit" element={<Audit />} />
|
||||
<Route path="/settings" element={<Settings />} />
|
||||
<Route path="/plugins" element={<Plugins />} />
|
||||
<Route path="/templates" element={<Templates />} />
|
||||
<Route path="/users" element={<Users />} />
|
||||
</Routes>
|
||||
</RoleRouteGuard>
|
||||
</Suspense>
|
||||
</RouteErrorBoundary>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
// Keep the existing route tree while allowing editing pages to use the
|
||||
// router's supported navigation blocker for both links and browser history.
|
||||
const router = createBrowserRouter([{ path: "*", element: <ApplicationRoutes /> }]);
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<UserProvider>
|
||||
<PluginProvider>
|
||||
<ThemeProvider>
|
||||
<TimeZoneProvider>
|
||||
<Router>
|
||||
<Layout>
|
||||
<RouteErrorBoundary>
|
||||
<Suspense fallback={<RouteFallback />}>
|
||||
<RoleRouteGuard>
|
||||
<Routes>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/projects" element={<Projects />} />
|
||||
<Route path="/project/:id" element={<ProjectDetail />} />
|
||||
<Route path="/operators" element={<Operators />} />
|
||||
<Route path="/workflow/:id" element={<WorkflowEditor />} />
|
||||
<Route path="/data-assets" element={<DataAssets />} />
|
||||
<Route path="/data-operations" element={<DataOperations />} />
|
||||
<Route path="/database" element={<DatabaseBrowser />} />
|
||||
<Route path="/object-storage" element={<ObjectStorageBrowser />} />
|
||||
<Route path="/object-sync" element={<ObjectSync />} />
|
||||
<Route path="/source-data" element={<LegacyDataAssetsRedirect />} />
|
||||
<Route path="/packages" element={<LegacyDataAssetsRedirect />} />
|
||||
<Route path="/sampling" element={<Sampling />} />
|
||||
<Route
|
||||
path="/annotation-operations"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/clip"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/clip-quality"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/annotate"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/quality"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/workflows/:workflowId/acceptance"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/plugins/annotation-qa/items/:itemId"
|
||||
element={
|
||||
<PluginRouteGuard pluginId="annotation-qa">
|
||||
<AnnotationOperations />
|
||||
</PluginRouteGuard>
|
||||
}
|
||||
/>
|
||||
<Route path="/monitoring" element={<Monitoring />} />
|
||||
<Route path="/runs" element={<Runs />} />
|
||||
<Route path="/audit" element={<Audit />} />
|
||||
<Route path="/settings" element={<Settings />} />
|
||||
<Route path="/plugins" element={<Plugins />} />
|
||||
<Route path="/templates" element={<Templates />} />
|
||||
<Route path="/users" element={<Users />} />
|
||||
</Routes>
|
||||
</RoleRouteGuard>
|
||||
</Suspense>
|
||||
</RouteErrorBoundary>
|
||||
</Layout>
|
||||
</Router>
|
||||
<RouterProvider router={router} />
|
||||
</TimeZoneProvider>
|
||||
</ThemeProvider>
|
||||
</PluginProvider>
|
||||
|
||||
@ -6,7 +6,7 @@ import { alignSliceTimeline, initializeFrameAlignedSlices, sliceTimelineIssue }
|
||||
import { annotationTaskAction } from "../lib/annotation-task-action.js";
|
||||
import { FinalAcceptanceWorkspace } from "../components/FinalAcceptanceWorkspace";
|
||||
import { AcceptanceTaskCard } from "../components/AcceptanceTaskCard";
|
||||
import { Link, useLocation, useNavigate, useParams, useSearchParams } from "react-router-dom";
|
||||
import { Link, useBlocker, useLocation, useNavigate, useParams, useSearchParams } from "react-router-dom";
|
||||
import {
|
||||
AlertCircle,
|
||||
CheckCircle2,
|
||||
@ -498,7 +498,6 @@ export default function AnnotationOperations() {
|
||||
});
|
||||
const [selectedTaskId, setSelectedTaskId] = useState("");
|
||||
const [pendingTaskId, setPendingTaskId] = useState<string | null>(null);
|
||||
const [pendingLeave, setPendingLeave] = useState<{ href: string } | { delta: number } | null>(null);
|
||||
const [workbenchDrafts, setWorkbenchDrafts] = useState<AnnotationDraft[]>([]);
|
||||
const workbenchHostRef = useRef<HTMLDivElement>(null);
|
||||
const [taskSearch, setTaskSearch] = useState("");
|
||||
@ -1224,6 +1223,13 @@ export default function AnnotationOperations() {
|
||||
workbenchDrafts.length ||
|
||||
(detail && annotationBaselineRef.current && annotationFingerprint !== annotationBaselineRef.current),
|
||||
);
|
||||
const leaveDirtyRef = useRef(annotationDirty);
|
||||
leaveDirtyRef.current = annotationDirty;
|
||||
const leaveBlocker = useBlocker(
|
||||
({ currentLocation, nextLocation }) =>
|
||||
leaveDirtyRef.current &&
|
||||
(currentLocation.pathname !== nextLocation.pathname || currentLocation.search !== nextLocation.search),
|
||||
);
|
||||
useEffect(() => {
|
||||
setWorkbenchDrafts([]);
|
||||
}, [selectedTaskId, combinedEditorPhase, currentUser?.id, detail?.humanItem?.currentStageId]);
|
||||
@ -1371,51 +1377,8 @@ export default function AnnotationOperations() {
|
||||
event.returnValue = "";
|
||||
};
|
||||
window.addEventListener("beforeunload", warn);
|
||||
const warnLink = (event: MouseEvent) => {
|
||||
if (
|
||||
event.defaultPrevented ||
|
||||
event.button !== 0 ||
|
||||
event.ctrlKey ||
|
||||
event.metaKey ||
|
||||
event.shiftKey ||
|
||||
event.altKey
|
||||
)
|
||||
return;
|
||||
const link = (event.target as HTMLElement).closest<HTMLAnchorElement>("a[href]");
|
||||
if (!link || link.target === "_blank" || link.hasAttribute("download") || link.href === window.location.href)
|
||||
return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
setPendingLeave({ href: link.href });
|
||||
};
|
||||
// BrowserRouter stores a numeric index on its history entries. Restore a
|
||||
// popped entry before presenting our non-blocking leave confirmation, so
|
||||
// cancelling keeps both the URL and the live workbench intact.
|
||||
const historyIndex = window.history.state?.idx;
|
||||
let restoringDelta: number | null = null;
|
||||
const warnHistory = (event: PopStateEvent) => {
|
||||
const destinationIndex = event.state?.idx;
|
||||
if (typeof historyIndex !== "number" || typeof destinationIndex !== "number") return;
|
||||
if (restoringDelta !== null && destinationIndex === historyIndex) {
|
||||
event.stopImmediatePropagation();
|
||||
setPendingLeave({ delta: restoringDelta });
|
||||
restoringDelta = null;
|
||||
return;
|
||||
}
|
||||
const delta = destinationIndex - historyIndex;
|
||||
if (!delta) return;
|
||||
event.stopImmediatePropagation();
|
||||
restoringDelta = delta;
|
||||
window.history.go(-delta);
|
||||
};
|
||||
document.addEventListener("click", warnLink, true);
|
||||
window.addEventListener("popstate", warnHistory, true);
|
||||
return () => {
|
||||
window.removeEventListener("beforeunload", warn);
|
||||
document.removeEventListener("click", warnLink, true);
|
||||
window.removeEventListener("popstate", warnHistory, true);
|
||||
};
|
||||
}, [annotationDirty, location.key]);
|
||||
return () => window.removeEventListener("beforeunload", warn);
|
||||
}, [annotationDirty]);
|
||||
const selectedHumanItem = detail?.humanItem;
|
||||
const selectedVideoSource = useMemo(() => {
|
||||
if (!detail) return "";
|
||||
@ -2792,6 +2755,7 @@ export default function AnnotationOperations() {
|
||||
};
|
||||
|
||||
const closeTaskAndReturnToPool = (notice = "") => {
|
||||
leaveDirtyRef.current = false;
|
||||
setWorkbenchDrafts([]);
|
||||
annotationBaselineRef.current = "";
|
||||
setPendingTaskId(null);
|
||||
@ -2888,7 +2852,7 @@ export default function AnnotationOperations() {
|
||||
|
||||
const cancelLeave = () => {
|
||||
setPendingTaskId(null);
|
||||
setPendingLeave(null);
|
||||
if (leaveBlocker.state === "blocked") leaveBlocker.reset();
|
||||
workbenchHostRef.current?.querySelector<HTMLElement>(".capture-workbench")?.focus({ preventScroll: true });
|
||||
};
|
||||
|
||||
@ -2897,20 +2861,13 @@ export default function AnnotationOperations() {
|
||||
setPendingTaskId(null);
|
||||
annotationBaselineRef.current = "";
|
||||
setWorkbenchDrafts([]);
|
||||
if (pendingLeave) {
|
||||
const destination = pendingLeave;
|
||||
setPendingLeave(null);
|
||||
leaveDirtyRef.current = false;
|
||||
if (leaveBlocker.state === "blocked") {
|
||||
// A stage/header link may reuse this page and select the same task again.
|
||||
// Unmount its session editor and reload saved data even in that case.
|
||||
setDetail(null);
|
||||
setTaskDetailRefreshVersion((current) => current + 1);
|
||||
// Let the dirty effect remove its history guard before replaying Back.
|
||||
if ("delta" in destination) setTimeout(() => window.history.go(destination.delta), 0);
|
||||
else {
|
||||
const url = new URL(destination.href);
|
||||
if (url.origin === window.location.origin) navigate(`${url.pathname}${url.search}${url.hash}`);
|
||||
else window.location.assign(url.href);
|
||||
}
|
||||
leaveBlocker.proceed();
|
||||
return;
|
||||
}
|
||||
if (!taskId) {
|
||||
@ -2946,7 +2903,7 @@ export default function AnnotationOperations() {
|
||||
|
||||
return (
|
||||
<div className="annotation-operations-page space-y-4" data-adaptive-slice={adaptiveSliceLayout || undefined}>
|
||||
{pendingTaskId !== null || pendingLeave ? (
|
||||
{pendingTaskId !== null || leaveBlocker.state === "blocked" ? (
|
||||
<div className="fixed inset-0 z-[70] flex items-center justify-center bg-black/70 p-4">
|
||||
<div
|
||||
role="dialog"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user