Skip to content

Commit

Permalink
refactor: MetricPlot loading state management inside component (#5)
Browse files Browse the repository at this point in the history
* refactor: Loading state management inside MetricsPlot component

* fix: remove console.log in MetricsPlot

* fix: flick when selecting node
  • Loading branch information
gmolki authored Mar 25, 2024
1 parent 5fd1365 commit a8bf9f3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
33 changes: 23 additions & 10 deletions src/components/MetricsPlot.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import React from "react";
import Plot from "react-plotly.js";
import { Data } from "plotly.js";
import ProgressBar from "./ProgressBar";

interface MetricsPlotProps {
isLoadingMetrics: boolean;
plotData: Data[];
layout: Partial<Plotly.Layout>;
}

const MetricsPlot: React.FC<MetricsPlotProps> = ({ plotData, layout }) => {
const MetricsPlot: React.FC<MetricsPlotProps> = ({
isLoadingMetrics,
plotData,
layout,
}) => {
const extendedLayout: Partial<Plotly.Layout> = {
...layout,
xaxis: {
Expand Down Expand Up @@ -39,16 +45,23 @@ const MetricsPlot: React.FC<MetricsPlotProps> = ({ plotData, layout }) => {
},
};

console.log(extendedLayout);

return (
<Plot
data={plotData}
layout={extendedLayout}
config={{ responsive: true }}
useResizeHandler={true}
style={{ width: "100%", height: "100%" }}
/>
<>
{isLoadingMetrics ? (
<div>
Loading metrics...
<ProgressBar isLoading={isLoadingMetrics} loadDuration={12000} />
</div>
) : (
<Plot
data={plotData}
layout={extendedLayout}
config={{ responsive: true }}
useResizeHandler={true}
style={{ width: "100%", height: "100%" }}
/>
)}
</>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFetchMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState, useEffect } from "react";
import { MetricData, Node } from "../types";

const useFetchMetrics = (selectedNode: Node | undefined, nodes: Node[]) => {
const [isLoadingMetrics, setIsLoadingMetrics] = useState(false);
const [isLoadingMetrics, setIsLoadingMetrics] = useState(true);
const [metricData, setMetricData] = useState<MetricData | null>(null);

useEffect(() => {
Expand Down
18 changes: 7 additions & 11 deletions src/pages/NodeMetricsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import useFetchNodes from "../hooks/useFetchNodes";
import useFetchMetrics from "../hooks/useFetchMetrics";
import useFilteredNodes from "../hooks/useFilteredNodes";
import { MetricData, Node } from "../types";
import ProgressBar from "../components/ProgressBar";
import NodeList from "../components/NodeList";
import { Spinner } from "@aleph-front/core";

Expand Down Expand Up @@ -92,17 +91,14 @@ const NodeMetricsPage: React.FC = () => {
></NodeList>
<div className="flex flex-col items-center justify-center w-full h-[calc(100vh-77px)] p-8 z-30 bg-white text-black">
<div className="flex flex-grow items-center justify-center w-full h-max">
{isLoadingMetrics ? (
<div>
Loading metrics...
<ProgressBar isLoading={isLoadingMetrics} loadDuration={12000} />
</div>
) : metricData ? (
<>
<MetricsPlot plotData={plotData} layout={plotLayout} />
</>
) : isLoadingNodes ? (
{isLoadingNodes ? (
<Spinner color="#141327" />
) : selectedNode ? (
<MetricsPlot
isLoadingMetrics={isLoadingMetrics}
plotData={plotData}
layout={plotLayout}
/>
) : (
<div className="text-gray-500">
Select a node to see its metrics.
Expand Down

0 comments on commit a8bf9f3

Please sign in to comment.