Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

fix: fix flow visibility logic #64

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions kontrol-frontend/src/pages/TrafficConfiguration.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useEffect, useState } from "react";
import { useRef, useEffect, useState, useCallback } from "react";
import { Grid } from "@chakra-ui/react";
import CytoscapeGraph, { utils } from "@/components/CytoscapeGraph";
import { ElementDefinition } from "cytoscape";
Expand All @@ -14,15 +14,17 @@ const Page = () => {
const { refetchFlows, flowVisibility } = useFlowsContext();
const timerRef = useRef<NodeJS.Timeout | null>(null);

const fetchElems = async () => {
const fetchElems = useCallback(async () => {
const response = await getTopology();
const filtered = {
...response,
nodes: response.nodes.map((node) => {
return {
...node,
versions: node.versions?.filter((version) => {
return flowVisibility[version.flowId] === true;
const currentVisibility = flowVisibility[version.flowId];
// un-set visibility is considered visible, only false is hidden
return currentVisibility == null || currentVisibility === true;
}),
};
}),
Expand All @@ -38,22 +40,23 @@ const Page = () => {
setElems(newElems);
// re-fetch flows if topology changes
refetchFlows();
};
}, [getTopology, flowVisibility, refetchFlows]);

const startPolling = () => {
const startPolling = useCallback(() => {
timerRef.current = setInterval(fetchElems, pollingIntervalSeconds * 1000);
};
}, [fetchElems]);

const stopPolling = () => {
const stopPolling = useCallback(() => {
clearInterval(timerRef.current!);
timerRef.current = null;
};
}, []);

useEffect(() => {
fetchElems();
startPolling();
return stopPolling;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [startPolling, stopPolling, fetchElems]);

return (
<Grid
Expand Down
Loading