修复: 08-07-business-routing-regression-repair 关闭资产创建跳转回归

这个提交包含在:
2026-08-07 14:53:55 +08:00
父节点 892b313888
当前提交 c14b40a00a
共修改 15 个文件,包含 450 行新增和 21 行删除
@@ -21,6 +21,7 @@ type GuardRegistration = {
type UnsavedChangesContextValue = {
register: (id: string, registration: GuardRegistration | null) => void;
runGuarded: (action: () => void) => void;
runAfterSuccessfulSave: (action: () => void) => void;
};
const UnsavedChangesContext = createContext<UnsavedChangesContextValue | null>(null);
@@ -76,6 +77,17 @@ export function UnsavedChangesProvider({ children }: PropsWithChildren) {
setDialogOpen(true);
}, []);
const runWithNextNavigationAllowed = useCallback((action: () => void) => {
allowNextNavigationRef.current = true;
try {
action();
} finally {
queueMicrotask(() => {
allowNextNavigationRef.current = false;
});
}
}, []);
useEffect(() => {
if (blocker.state === "blocked") {
setDialogOpen(true);
@@ -95,7 +107,10 @@ export function UnsavedChangesProvider({ children }: PropsWithChildren) {
};
}, []);
const value = useMemo(() => ({ register, runGuarded }), [register, runGuarded]);
const value = useMemo(
() => ({ register, runGuarded, runAfterSuccessfulSave: runWithNextNavigationAllowed }),
[register, runGuarded, runWithNextNavigationAllowed],
);
return (
<UnsavedChangesContext.Provider value={value}>
@@ -129,14 +144,7 @@ export function UnsavedChangesProvider({ children }: PropsWithChildren) {
return;
}
if (!action) return;
allowNextNavigationRef.current = true;
try {
action();
} finally {
queueMicrotask(() => {
allowNextNavigationRef.current = false;
});
}
runWithNextNavigationAllowed(action);
}}
/>
</UnsavedChangesContext.Provider>
@@ -157,3 +165,12 @@ export function useUnsavedChangesGuard(dirty: boolean, label: string) {
return context.runGuarded;
}
export function usePostSaveNavigation() {
const context = useContext(UnsavedChangesContext);
if (!context) {
throw new Error("usePostSaveNavigation 必须在 UnsavedChangesProvider 内使用");
}
return context.runAfterSuccessfulSave;
}