From 184c155632e417a7f0f40e7b766e6e57abfee5df Mon Sep 17 00:00:00 2001 From: atrincas Date: Mon, 25 Nov 2024 14:12:04 +0100 Subject: [PATCH] Refactor graph component --- client/src/components/ui/graph.tsx | 107 +++++++----------- .../projects/custom-project/cost/index.tsx | 10 +- .../custom-project/left-over/index.tsx | 13 +-- 3 files changed, 50 insertions(+), 80 deletions(-) diff --git a/client/src/components/ui/graph.tsx b/client/src/components/ui/graph.tsx index ca949b80..5b8230fb 100644 --- a/client/src/components/ui/graph.tsx +++ b/client/src/components/ui/graph.tsx @@ -3,16 +3,9 @@ import { FC } from "react"; import { renderCurrency } from "@/lib/format"; interface GraphProps { - summary?: { - totalCost: number; - capEx: GraphSegment; - opEx: GraphSegment; - }; - leftOver?: { - leftover: number; - totalRevenue: GraphSegment; - opEx: GraphSegment; - }; + total: number; + segments: GraphSegment[]; + leftover?: number; } interface GraphSegment { @@ -25,8 +18,8 @@ const getSize = (value: number, total: number) => { return `${Math.max(percentage, 0)}%`; }; -const Graph: FC = ({ leftOver, summary }) => { - if (leftOver) { +const Graph: FC = ({ total, leftover, segments }) => { + if (leftover) { return (
@@ -35,12 +28,12 @@ const Graph: FC = ({ leftOver, summary }) => { height: "100%", width: "100%", }} - className={`relative h-full rounded-md transition-all duration-300 ease-in-out ${leftOver.totalRevenue.colorClass}`} + className="relative h-full rounded-md bg-yellow-500 transition-all duration-300 ease-in-out" >
{renderCurrency( - leftOver.totalRevenue.value, + total, { notation: "compact", maximumFractionDigits: 1, @@ -51,32 +44,32 @@ const Graph: FC = ({ leftOver, summary }) => {
-
-
-
- {renderCurrency( - leftOver.opEx.value, - { - notation: "compact", - maximumFractionDigits: 1, - }, - "first-letter:text-secondary-foreground", - )} + {segments.map(({ value, colorClass }) => ( +
+
+
+ {renderCurrency( + value, + { + notation: "compact", + maximumFractionDigits: 1, + }, + "first-letter:text-secondary-foreground", + )} +
-
+ ))}
= ({ leftOver, summary }) => { ); } - if (summary) { - return ( -
-
-
-
-
- {renderCurrency( - summary.capEx.value, - { - notation: "compact", - maximumFractionDigits: 1, - }, - "first-letter:text-secondary-foreground", - )} -
-
-
+ return ( +
+
+ {segments.map(({ value, colorClass }) => (
{renderCurrency( - summary.opEx.value, + value, { notation: "compact", maximumFractionDigits: 1, @@ -129,12 +104,10 @@ const Graph: FC = ({ leftOver, summary }) => {
-
+ ))}
- ); - } - - return null; +
+ ); }; interface GraphLegendItem { diff --git a/client/src/containers/projects/custom-project/cost/index.tsx b/client/src/containers/projects/custom-project/cost/index.tsx index 4b45fc71..492376d4 100644 --- a/client/src/containers/projects/custom-project/cost/index.tsx +++ b/client/src/containers/projects/custom-project/cost/index.tsx @@ -72,17 +72,17 @@ const ProjectCost: FC = () => { />
diff --git a/client/src/containers/projects/custom-project/left-over/index.tsx b/client/src/containers/projects/custom-project/left-over/index.tsx index e1a8aebe..4b24c9b2 100644 --- a/client/src/containers/projects/custom-project/left-over/index.tsx +++ b/client/src/containers/projects/custom-project/left-over/index.tsx @@ -57,17 +57,14 @@ const LeftOver: FC = () => { />