[fix]:[FL-11][删除线路后报错]

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
chengkai3
2026-06-07 12:31:12 +08:00
parent 4c143e2842
commit cfe7624de3
+29 -14
View File
@@ -192,6 +192,7 @@ export default function AdminPowerLinesPage() {
const [keyword, setKeyword] = useState(""); const [keyword, setKeyword] = useState("");
const [selectedLineId, setSelectedLineId] = useState<string | null>(null); const [selectedLineId, setSelectedLineId] = useState<string | null>(null);
const [lineIdPendingDeletion, setLineIdPendingDeletion] = useState<string | null>(null);
const [selectedLineTouched, setSelectedLineTouched] = useState(false); const [selectedLineTouched, setSelectedLineTouched] = useState(false);
const [towerKeyword, setTowerKeyword] = useState(""); const [towerKeyword, setTowerKeyword] = useState("");
const [towerTypeFilter, setTowerTypeFilter] = useState(""); const [towerTypeFilter, setTowerTypeFilter] = useState("");
@@ -222,8 +223,17 @@ export default function AdminPowerLinesPage() {
return `/api/v1/lines${query ? `?${query}` : ""}`; return `/api/v1/lines${query ? `?${query}` : ""}`;
}, [keyword]); }, [keyword]);
const activeTowerLineId = useMemo(() => {
if (!selectedLineId || selectedLineId === lineIdPendingDeletion) {
return null;
}
return selectedLineId;
}, [lineIdPendingDeletion, selectedLineId]);
const towerQueryCurrent = towerPagination.current;
const towerQueryPageSize = towerPagination.pageSize;
const towerListPath = useMemo(() => { const towerListPath = useMemo(() => {
if (!selectedLineId) { if (!activeTowerLineId) {
return ""; return "";
} }
const params = new URLSearchParams(); const params = new URLSearchParams();
@@ -237,22 +247,22 @@ export default function AdminPowerLinesPage() {
params.set("risk_level", towerRiskFilter.trim()); params.set("risk_level", towerRiskFilter.trim());
} }
if (towerViewMode === "table") { if (towerViewMode === "table") {
params.set("limit", String(towerPagination.pageSize)); params.set("limit", String(towerQueryPageSize));
params.set("offset", String((towerPagination.current - 1) * towerPagination.pageSize)); params.set("offset", String((towerQueryCurrent - 1) * towerQueryPageSize));
} else { } else {
params.set("limit", String(TOWER_MAP_QUERY_LIMIT)); params.set("limit", String(TOWER_MAP_QUERY_LIMIT));
params.set("offset", "0"); params.set("offset", "0");
} }
const query = params.toString(); const query = params.toString();
return `/api/v1/lines/${selectedLineId}/towers?${query}`; return `/api/v1/lines/${activeTowerLineId}/towers?${query}`;
}, [ }, [
selectedLineId, activeTowerLineId,
towerKeyword, towerKeyword,
towerTypeFilter, towerTypeFilter,
towerRiskFilter, towerRiskFilter,
towerViewMode, towerViewMode,
towerPagination.current, towerQueryCurrent,
towerPagination.pageSize, towerQueryPageSize,
]); ]);
const linesQuery = useQuery({ const linesQuery = useQuery({
@@ -269,7 +279,7 @@ export default function AdminPowerLinesPage() {
const towersQuery = useQuery({ const towersQuery = useQuery({
queryKey: [towerListPath], queryKey: [towerListPath],
enabled: !!user && !!selectedLineId && canRead, enabled: !!user && !!activeTowerLineId && canRead,
queryFn: async () => { queryFn: async () => {
if (!towerListPath) { if (!towerListPath) {
return { items: [], total: 0 } satisfies LineTowerListResponse; return { items: [], total: 0 } satisfies LineTowerListResponse;
@@ -351,10 +361,9 @@ export default function AdminPowerLinesPage() {
void queryClient.invalidateQueries({ queryKey: ["/api/v1/tower-models/selector"] }); void queryClient.invalidateQueries({ queryKey: ["/api/v1/tower-models/selector"] });
}, [queryClient])); }, [queryClient]));
const lines = linesQuery.data?.items ?? []; const lines = useMemo(() => linesQuery.data?.items ?? [], [linesQuery.data?.items]);
const towers = towersQuery.data?.items ?? []; const towers = useMemo(() => towersQuery.data?.items ?? [], [towersQuery.data?.items]);
const towerModels = useMemo(() => towerModelOptionsQuery.data ?? [], [towerModelOptionsQuery.data]);
const towerModels = towerModelOptionsQuery.data ?? [];
const towerModelOptions = towerModels.map((item) => ({ value: item.code, label: `${item.code} - ${item.name}` })); const towerModelOptions = towerModels.map((item) => ({ value: item.code, label: `${item.code} - ${item.name}` }));
const effectiveSelectedLineId = useMemo(() => { const effectiveSelectedLineId = useMemo(() => {
if (selectedLineTouched) { if (selectedLineTouched) {
@@ -365,7 +374,6 @@ export default function AdminPowerLinesPage() {
} }
return selectedLineId ?? (lines.length > 0 ? lines[0].id : null); return selectedLineId ?? (lines.length > 0 ? lines[0].id : null);
}, [lines, selectedLineId, selectedLineTouched]); }, [lines, selectedLineId, selectedLineTouched]);
const towerQueryCurrent = towerPagination.current;
const shouldResetTowerPage = towerQueryCurrent !== 1 && ( const shouldResetTowerPage = towerQueryCurrent !== 1 && (
selectedLineId !== effectiveSelectedLineId selectedLineId !== effectiveSelectedLineId
|| towerKeyword.trim().length > 0 || towerKeyword.trim().length > 0
@@ -466,6 +474,11 @@ export default function AdminPowerLinesPage() {
} }
return lineId; return lineId;
}, },
onMutate: (lineId) => {
if (effectiveSelectedLineId === lineId) {
setLineIdPendingDeletion(lineId);
}
},
onSuccess: async (lineId) => { onSuccess: async (lineId) => {
if (effectiveSelectedLineId === lineId) { if (effectiveSelectedLineId === lineId) {
setSelectedLineTouched(false); setSelectedLineTouched(false);
@@ -474,11 +487,13 @@ export default function AdminPowerLinesPage() {
setError(""); setError("");
messageApi.success("线路已删除"); messageApi.success("线路已删除");
await refreshLines(); await refreshLines();
await refreshTowers();
}, },
onError: (candidate) => { onError: (candidate) => {
setError(candidate instanceof Error ? candidate.message : "删除线路失败"); setError(candidate instanceof Error ? candidate.message : "删除线路失败");
}, },
onSettled: () => {
setLineIdPendingDeletion(null);
},
}); });
const saveTowerMutation = useMutation({ const saveTowerMutation = useMutation({