修复: 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;
}
+31 -8
查看文件
@@ -27,7 +27,10 @@ import { fetchCostCenters, fetchDocumentCategories, fetchLegalEntities } from ".
import { useAuth } from "../auth/AuthContext";
import { hasAnyPermission, hasPermission } from "../auth/permissions";
import { DateTextInput } from "../components/common/DateTextInput";
import { useUnsavedChangesGuard } from "../components/common/UnsavedChangesGuard";
import {
usePostSaveNavigation,
useUnsavedChangesGuard,
} from "../components/common/UnsavedChangesGuard";
import { StatusBadge } from "../components/common/StatusBadge";
import { TableSkeleton } from "../components/common/TableSkeleton";
import {
@@ -405,6 +408,7 @@ function AssetRegistryWorkspace({
Boolean(isCreatingAsset || selectedAssetId) && Boolean(canEditCurrent) && JSON.stringify(assetForm) !== savedAssetFormSignature,
"资产表单",
);
const runAfterSuccessfulSave = usePostSaveNavigation();
const hasAdvancedAssetFilters = Boolean(
sensitivityFilter ||
categoryFilter ||
@@ -575,19 +579,38 @@ function AssetRegistryWorkspace({
return patchAsset(selectedAssetId, buildPatchAssetPayload());
},
onSuccess: async (data) => {
if (isCreatingAsset) {
const detailPath = buildAssetDetailPath(data.asset.id);
if (detailPath === "/assets") {
const message = "资产已创建,但未返回可用的资产标识,请返回资产列表确认。";
setAssetFormError(message);
toast.error(message);
return;
}
setAssetFormError("");
setSavedAssetFormSignature(JSON.stringify(assetForm));
toast.success("资产卡片已创建");
runAfterSuccessfulSave(() => {
navigate(detailPath, {
replace: true,
state: { from: backSource },
});
});
void Promise.all([
queryClient.invalidateQueries({ queryKey: ["assets", "list"] }),
queryClient.invalidateQueries({ queryKey: ["assets", "detail"] }),
]).catch(() => undefined);
return;
}
await Promise.all([
queryClient.invalidateQueries({ queryKey: ["assets", "list"] }),
queryClient.invalidateQueries({ queryKey: ["assets", "detail"] }),
]);
setAssetFormError("");
setSavedAssetFormSignature(JSON.stringify(assetForm));
toast.success(isCreatingAsset ? "资产卡片已创建" : "资产卡片已更新");
if (isCreatingAsset) {
navigate(buildAssetDetailPath(data.asset.id), {
replace: true,
state: { from: backSource },
});
}
toast.success("资产卡片已更新");
},
onError: (error) => {
const message = resolveError(error);