From 858c1d5d39fd95d98413821508d5e1d8dd615c56 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Sat, 20 Jan 2024 16:34:47 +0100 Subject: [PATCH] fix: lint and use decimal --- src/components/ConsumptionCalculator.tsx | 80 ++++++++++++++---------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/src/components/ConsumptionCalculator.tsx b/src/components/ConsumptionCalculator.tsx index f65ecdd..24daf05 100644 --- a/src/components/ConsumptionCalculator.tsx +++ b/src/components/ConsumptionCalculator.tsx @@ -1,51 +1,50 @@ -import React from "react"; -import Decimal from "decimal.js-light"; +import React from 'react'; +import Decimal from 'decimal.js-light'; -import clsx from "clsx"; -import styles from "./ConsumptionCalculator.module.css"; +import clsx from 'clsx'; +import styles from './ConsumptionCalculator.module.css'; export default function ConsumptionCalculator() { const [co2PerPod, setCO2PerPod] = React.useState(new Decimal(11)); const [numberOfPodsTotal, setNumberOfPods] = React.useState(new Decimal(100)); const [numberOfPodsSleeped, setNumberOfSleepedPods] = React.useState( - new Decimal(100) + new Decimal(100), ); const [numberOfHourSleepInWeek, setNumberOfHourSleepInWeek] = React.useState( - new Decimal(128) + new Decimal(128), ); const resultWithKubeGreen = React.useMemo( - () => - calculateCO2WeekConsumption( - co2PerPod, - numberOfPodsTotal, - numberOfPodsSleeped, - numberOfHourSleepInWeek - ), - [co2PerPod, numberOfPodsTotal, numberOfPodsSleeped, numberOfHourSleepInWeek] + () => calculateCO2WeekConsumption( + co2PerPod, + numberOfPodsTotal, + numberOfPodsSleeped, + numberOfHourSleepInWeek, + ), + [co2PerPod, numberOfPodsTotal, numberOfPodsSleeped, numberOfHourSleepInWeek], ); const resultWithoutKubeGreen = React.useMemo( - () => - calculateCO2WeekConsumption( - co2PerPod, - numberOfPodsTotal, - numberOfPodsSleeped, - new Decimal(0) - ), - [co2PerPod, numberOfPodsTotal, numberOfPodsSleeped] + () => calculateCO2WeekConsumption( + co2PerPod, + numberOfPodsTotal, + numberOfPodsSleeped, + new Decimal(0), + ), + [co2PerPod, numberOfPodsTotal, numberOfPodsSleeped], ); const percentCO2Saved = React.useMemo( - () => - Math.round( - (Math.abs(resultWithoutKubeGreen - resultWithKubeGreen) / - resultWithoutKubeGreen) * - 1000 - ) / 10, - [resultWithoutKubeGreen, resultWithKubeGreen] + () => new Decimal(resultWithKubeGreen) + .minus(resultWithoutKubeGreen) + .dividedBy(resultWithoutKubeGreen) + .mul(100) + .toDecimalPlaces(1) + .abs() + .toString(), + [resultWithoutKubeGreen, resultWithKubeGreen], ); return ( -
+

CO2 Calculator

@@ -77,12 +76,25 @@ export default function ConsumptionCalculator() {
Total (Kg CO2eq/week)
- {percentCO2Saved}% CO2 saved with kube-green + + {percentCO2Saved} + % CO2 saved + + {' '} + with kube-green
-
without kube-green: {resultWithoutKubeGreen.toString()}
- with kube-green: {resultWithKubeGreen.toString()} + without kube-green: + {' '} + {resultWithoutKubeGreen.toString()} +
+
+ + with kube-green: + {' '} + {resultWithKubeGreen.toString()} +
@@ -95,7 +107,7 @@ function calculateCO2WeekConsumption( co2PerPod: Decimal, totalNumberOfPods: Decimal, numberOfPodsSleeped: Decimal, - sleepHour: Decimal + sleepHour: Decimal, ): Decimal { const co2PerDayPerPod = co2PerPod.dividedBy(365).dividedBy(24); const co2WithKubeGreen = co2PerDayPerPod