From 7475742e5a6342ddd3c709ec0ba34653d7a773cb Mon Sep 17 00:00:00 2001 From: Artsiom Kharytonchyk Date: Thu, 18 Apr 2024 17:15:02 +0300 Subject: [PATCH] chore: cleanup coverage component --- src/pages/Coverage.tsx | 54 +++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/pages/Coverage.tsx b/src/pages/Coverage.tsx index ceb9afa..dd13fc5 100644 --- a/src/pages/Coverage.tsx +++ b/src/pages/Coverage.tsx @@ -2,6 +2,7 @@ import React from "react"; import { useQuery } from "@tanstack/react-query"; import { Box, + CircularProgress, Paper, Table, TableBody, @@ -15,6 +16,21 @@ import { Navigate } from "react-router-dom"; import { ConfigContext } from "../App"; import ErrorMessage from "../components/MissingCoverageErrorMessage"; +interface CoverageData { + lines: { pct: number }; + statements: { pct: number }; + functions: { pct: number }; + branches: { pct: number }; +} + +interface RepositoryData { + repoName: string; + lines: number; + statements: number; + functions: number; + branches: number; +} + const coverageStyles = (coverage: number) => { console.log(coverage, coverage < 60); if (coverage < 60) return { color: "#f44336", fontWeight: "bolder" }; @@ -22,6 +38,21 @@ const coverageStyles = (coverage: number) => { return { color: "#4caf50", fontWeight: "normal" }; }; +const calculateSummary = (tableData: RepositoryData[]) => ({ + lines: ( + tableData.reduce((acc: any, row: { lines: any; }) => acc + row.lines, 0) / tableData.length + ).toFixed(2), + statements: ( + tableData.reduce((acc: any, row: { statements: any; }) => acc + row.statements, 0) / tableData.length + ).toFixed(2), + functions: ( + tableData.reduce((acc: any, row: { functions: any; }) => acc + row.functions, 0) / tableData.length + ).toFixed(2), + branches: ( + tableData.reduce((acc: any, row: { branches: any; }) => acc + row.branches, 0) / tableData.length + ).toFixed(2), +}); + export const Coverage: React.FC = () => { const { data, isLoading, isError } = useQuery({ queryKey: ["coverage"], @@ -31,7 +62,7 @@ export const Coverage: React.FC = () => { Accept: "application/json", }, }); - return coverage.json(); + return coverage.json() as Promise> ; }, retry: false, }); @@ -53,15 +84,20 @@ export const Coverage: React.FC = () => { }); }, [data, repositorySettings]); + const summaryData = React.useMemo( + () => calculateSummary(tableData), + [tableData] + ); + if (!localStorage.getItem("token")) { return ; } return (

Coverage

- {isLoading &&

Loading...

} + {isLoading && } {isError && } - {data && data.length === 0 && ( + {data && tableData.length === 0 && ( No coverage matching you selected repositories was found! @@ -99,20 +135,16 @@ export const Coverage: React.FC = () => { Summary - {tableData.reduce((acc, row) => acc + row.lines, 0) / - tableData.length} + {summaryData.lines} - {tableData.reduce((acc, row) => acc + row.statements, 0) / - tableData.length} + {summaryData.statements} - {tableData.reduce((acc, row) => acc + row.functions, 0) / - tableData.length} + {summaryData.functions} - {tableData.reduce((acc, row) => acc + row.branches, 0) / - tableData.length} + {summaryData.branches}