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

Commit

Permalink
fix: fix flow visibility logic (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylarmb authored Oct 10, 2024
1 parent 9a84666 commit ce84820
Showing 1 changed file with 12 additions and 9 deletions.
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

0 comments on commit ce84820

Please sign in to comment.