Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 10681x 10681x 10681x 136x 10681x 136x 10681x 136x 10681x 10681x 10681x 10681x | import type { Node } from "@xyflow/react";
import { useCallback, useMemo, useState } from "react";
import { toast } from "sonner";
import {
SimulationObjectRetrieveRead,
useLazyUnitopsSimulationobjectsTrackedStreamFlowRetrieveQuery,
} from "@/api/apiStore.gen";
import { isStream } from "../../../../../lib/isStream";
import { MAX_TRACKED_STREAMS } from "./constants";
import {
buildTrackedFlowsByStreamId,
buildTrackedStreamIds,
buildTrackedStreamSummaries,
upsertTrackedStream,
} from "./state";
import type { TrackedStreamState } from "./types";
export function useStreamTracking() {
const [trackStreamFlow] =
useLazyUnitopsSimulationobjectsTrackedStreamFlowRetrieveQuery();
const [trackedStreams, setTrackedStreams] = useState<TrackedStreamState[]>(
[],
);
const trackedStreamIds = useMemo(
() => buildTrackedStreamIds(trackedStreams),
[trackedStreams],
);
const trackedFlowsByStreamId = useMemo(
() => buildTrackedFlowsByStreamId(trackedStreams),
[trackedStreams],
);
const trackedStreamSummaries = useMemo(
() => buildTrackedStreamSummaries(trackedStreams),
[trackedStreams],
);
const handleTrackStream = useCallback(
async (node: Node) => {
const simulationObject = node.data
.simulationObject as SimulationObjectRetrieveRead;
Iif (!isStream(simulationObject)) return;
const existingTrackedStream = trackedStreams.find(
(stream) => stream.sourceStreamId === simulationObject.id,
);
if (
!existingTrackedStream &&
trackedStreams.length >= MAX_TRACKED_STREAMS
) {
toast.error(`Track up to ${MAX_TRACKED_STREAMS} streams at a time.`);
return;
}
try {
const trackedFlow = await trackStreamFlow({
id: simulationObject.id,
}).unwrap();
const sourceName =
simulationObject.componentName || `#${simulationObject.id}`;
setTrackedStreams((currentTrackedStreams) =>
upsertTrackedStream(currentTrackedStreams, trackedFlow, sourceName),
);
} catch (error) {
console.error(
"[useStreamTracking] Failed to track stream flow:",
error,
);
toast.error("Failed to track stream flow.");
}
},
[trackStreamFlow, trackedStreams],
);
const removeTrackedStream = useCallback((sourceStreamId: number) => {
setTrackedStreams((currentTrackedStreams) =>
currentTrackedStreams.filter(
(stream) => stream.sourceStreamId !== sourceStreamId,
),
);
}, []);
const clearTrackedStreams = useCallback(() => {
setTrackedStreams([]);
}, []);
return {
trackedStreamIds,
trackedFlowsByStreamId,
trackedStreamSummaries,
handleTrackStream,
removeTrackedStream,
clearTrackedStreams,
};
}
|