diff --git a/src/App.tsx b/src/App.tsx index 7d77f33e..b52bc699 100644 --- a/src/App.tsx +++ b/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 (
{isChunkError ? "当前页面加载的是旧版本资源,刷新后会重新载入最新页面。" - : (error?.message || "组件渲染发生错误,请刷新重试。")} + : error?.message || "组件渲染发生错误,请刷新重试。"}
{!isChunkError && error?.stack ? (
@@ -161,100 +159,108 @@ function RoleRouteGuard({ children }: { children: React.ReactNode }) {
return children;
}
+function ApplicationRoutes() {
+ return (
+
+
+ }>
+
+
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+
+
+
+ }
+ />
+
+
+
+ }
+ />
+
+
+
+ }
+ />
+
+
+
+ }
+ />
+
+
+
+ }
+ />
+
+
+
+ }
+ />
+
+
+
+ }
+ />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+
+
+
+
+
+ );
+}
+
+// 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: }]);
+
export default function App() {
return (
-
-
-
- }>
-
-
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
-
-
-
- }
- />
-
-
-
- }
- />
-
-
-
- }
- />
-
-
-
- }
- />
-
-
-
- }
- />
-
-
-
- }
- />
-
-
-
- }
- />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
-
-
-
-
-
-
+
diff --git a/src/pages/AnnotationOperations.tsx b/src/pages/AnnotationOperations.tsx
index db83d361..dd1e8289 100644
--- a/src/pages/AnnotationOperations.tsx
+++ b/src/pages/AnnotationOperations.tsx
@@ -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(null);
- const [pendingLeave, setPendingLeave] = useState<{ href: string } | { delta: number } | null>(null);
const [workbenchDrafts, setWorkbenchDrafts] = useState([]);
const workbenchHostRef = useRef(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("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(".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 (
- {pendingTaskId !== null || pendingLeave ? (
+ {pendingTaskId !== null || leaveBlocker.state === "blocked" ? (