优化渲染节点功能,使用 ref 文件渲染大量节点
This commit is contained in:
@@ -5,8 +5,13 @@ import Point from "ol/geom/Point";
|
||||
import { bbox, featureCollection } from "@turf/turf";
|
||||
|
||||
import { useChatToolActionHandler } from "@/hooks/useChatToolActionHandler";
|
||||
import { applyJunctionAreaRender } from "@components/olmap/DMALeakDetection/applyJunctionAreaRender";
|
||||
import {
|
||||
applyJunctionAreaRender,
|
||||
type JunctionAreaRenderPayload,
|
||||
} from "@components/olmap/DMALeakDetection/applyJunctionAreaRender";
|
||||
import { apiFetch } from "@/lib/apiFetch";
|
||||
import { queryFeaturesByIds } from "@/utils/mapQueryService";
|
||||
import { config } from "@/config/config";
|
||||
import { useMap } from "../MapComponent";
|
||||
|
||||
type UseToolbarChatActionsParams = {
|
||||
@@ -30,6 +35,7 @@ export const useToolbarChatActions = ({
|
||||
}: UseToolbarChatActionsParams) => {
|
||||
const map = useMap();
|
||||
const chatJunctionRenderCleanupRef = useRef<(() => void) | null>(null);
|
||||
const renderRequestSeqRef = useRef(0);
|
||||
|
||||
const disposeChatJunctionRender = useCallback(() => {
|
||||
chatJunctionRenderCleanupRef.current?.();
|
||||
@@ -122,22 +128,82 @@ export const useToolbarChatActions = ({
|
||||
}
|
||||
case "render_junctions": {
|
||||
disposeChatJunctionRender();
|
||||
renderRequestSeqRef.current += 1;
|
||||
const requestSeq = renderRequestSeqRef.current;
|
||||
|
||||
if (Object.keys(action.nodeAreaMap).length === 0) {
|
||||
if (!action.renderRef || !map) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (map) {
|
||||
chatJunctionRenderCleanupRef.current = applyJunctionAreaRender(
|
||||
map,
|
||||
{
|
||||
nodeAreaMap: action.nodeAreaMap,
|
||||
areaIds: action.areaIds,
|
||||
areaColors: action.areaColors,
|
||||
},
|
||||
{ propertyKey: "chat_junction_render_index" },
|
||||
);
|
||||
}
|
||||
void (async () => {
|
||||
try {
|
||||
const query = action.sessionId
|
||||
? `?session_id=${encodeURIComponent(action.sessionId)}`
|
||||
: "";
|
||||
const response = await apiFetch(
|
||||
`${config.AGENT_URL}/api/v1/agent/chat/render-ref/${encodeURIComponent(action.renderRef)}${query}`,
|
||||
{
|
||||
method: "GET",
|
||||
projectHeaderMode: "include",
|
||||
userHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`render ref request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = (await response.json()) as {
|
||||
data?: {
|
||||
node_area_map?: Record<string, unknown>;
|
||||
area_ids?: unknown[];
|
||||
area_colors?: Record<string, unknown>;
|
||||
};
|
||||
};
|
||||
|
||||
const data = payload.data;
|
||||
if (!data?.node_area_map) {
|
||||
throw new Error("render ref payload missing node_area_map");
|
||||
}
|
||||
|
||||
const renderPayload: JunctionAreaRenderPayload = {
|
||||
nodeAreaMap: Object.fromEntries(
|
||||
Object.entries(data.node_area_map).map(([key, value]) => [
|
||||
String(key),
|
||||
String(value ?? ""),
|
||||
]),
|
||||
),
|
||||
areaIds: Array.isArray(data.area_ids)
|
||||
? data.area_ids.map((item) => String(item).trim()).filter(Boolean)
|
||||
: [],
|
||||
areaColors:
|
||||
data.area_colors && typeof data.area_colors === "object"
|
||||
? Object.fromEntries(
|
||||
Object.entries(data.area_colors).map(([key, value]) => [
|
||||
String(key),
|
||||
String(value ?? ""),
|
||||
]),
|
||||
)
|
||||
: {},
|
||||
};
|
||||
|
||||
if (
|
||||
requestSeq !== renderRequestSeqRef.current ||
|
||||
Object.keys(renderPayload.nodeAreaMap).length === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
chatJunctionRenderCleanupRef.current = applyJunctionAreaRender(
|
||||
map,
|
||||
renderPayload,
|
||||
{ propertyKey: "chat_junction_render_index" },
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to resolve render_ref for junction render:", error);
|
||||
}
|
||||
})();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user