From fd6ef37cf750c72e546e21329016910a6723addd Mon Sep 17 00:00:00 2001 From: John Goh Date: Mon, 13 Sep 2021 13:32:44 +0800 Subject: [PATCH] feat: minimal React table implementation --- package-lock.json | 5 ++ package.json | 4 ++ src/components/Canary/index.js | 23 +++++- src/components/Canary/newTable.js | 112 ++++++++++++++++++++++++++++++ src/components/Canary/status.js | 2 + 5 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 src/components/Canary/newTable.js diff --git a/package-lock.json b/package-lock.json index ffa10f96d..0db946017 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12948,6 +12948,11 @@ "refractor": "^3.2.0" } }, + "react-table": { + "version": "7.7.0", + "resolved": "https://registry.npmjs.org/react-table/-/react-table-7.7.0.tgz", + "integrity": "sha512-jBlj70iBwOTvvImsU9t01LjFjy4sXEtclBovl3mTiqjz23Reu0DKnRza4zlLtOPACx6j2/7MrQIthIK1Wi+LIA==" + }, "read-pkg": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-3.0.0.tgz", diff --git a/package.json b/package.json index 4cb15b1e9..789f12489 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,10 @@ "@testing-library/jest-dom": "^5.11.4", "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^12.1.10", +<<<<<<< HEAD "history": "^5.0.1", +======= +>>>>>>> feat: minimal React table implementation "lodash": "^4.17.21", "react": "^17.0.2", "react-country-flag": "^2.3.1", @@ -27,6 +30,7 @@ "react-router-dom": "^5.2.0", "react-scripts": "4.0.3", "react-syntax-highlighter": "^15.4.4", + "react-table": "^7.7.0", "remark-gfm": "^2.0.0", "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.2.7", "timeago-react": "^3.0.3", diff --git a/src/components/Canary/index.js b/src/components/Canary/index.js index 412faaaed..47a03771d 100644 --- a/src/components/Canary/index.js +++ b/src/components/Canary/index.js @@ -19,6 +19,24 @@ import { StatCard } from "../StatCard"; import { Modal } from "../Modal"; import { Title } from "./renderers"; +import { TristateToggle } from "../TristateToggle"; +import { NewCanaryTable } from "./newTable"; + +const layoutSelections = [ + { + id: "dropdown-table", + name: "table", + icon: , + label: "Table" + }, + { + id: "dropdown-card", + name: "card", + icon: , + label: "Card" + } +]; + export class Canary extends React.Component { constructor(props) { super(props); @@ -160,14 +178,15 @@ export class Canary extends React.Component { {layout === "table" && (
- + /> */} +
)}
diff --git a/src/components/Canary/newTable.js b/src/components/Canary/newTable.js new file mode 100644 index 000000000..fec5545ac --- /dev/null +++ b/src/components/Canary/newTable.js @@ -0,0 +1,112 @@ +import React, { useEffect, useState, useMemo } from "react"; +import { RiContactsBookLine } from "react-icons/ri"; +import { useTable } from "react-table"; +import { GetName } from "./data"; +import { Duration, Percentage } from "./renderers"; +import { StatusList } from "./status"; + +const styles = { + tableClass: "border border-green-500", + theadClass: "border border-red-500", + theadRowClass: "", + theadHeaderClass: "", + tbodyClass: "", + tbodyRowClass: "", + tbodyDataClass: "" +}; + +function HealthCell({ value }) { + return ; +} + +function UptimeCell({ value }) { + return ( + + ); +} + +function LatencyCell({ value }) { + return ; +} + +export function NewCanaryTable({ checks, ...rest }) { + const data = useMemo(() => { + console.log(checks); + return checks.map((check) => ({ + ...check, + name: GetName(check) + })); + }, [checks]); + + const columns = useMemo( + () => [ + { + Header: "Checks", + accessor: "name" + }, + { + Header: "Health", + accessor: "checkStatuses", + Cell: HealthCell + }, + { + Header: "Uptime", + accessor: "uptime", + Cell: UptimeCell + }, + { + Header: "Latency", + accessor: "latency", + Cell: LatencyCell + } + ], + [] + ); + + const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = + useTable({ columns, data }); + + return ( + + + {headerGroups.map((headerGroup) => ( + + {headerGroup.headers.map((column) => ( + + ))} + + ))} + + + {rows.map((row) => { + prepareRow(row); + return ( + + {row.cells.map((cell) => ( + + ))} + + ); + })} + +
+ {column.render("Header")} +
+ {cell.render("Cell")} +
+ ); +} diff --git a/src/components/Canary/status.js b/src/components/Canary/status.js index e3c050aff..7d4c10d6c 100644 --- a/src/components/Canary/status.js +++ b/src/components/Canary/status.js @@ -26,6 +26,8 @@ export function StatusList({ check, checkStatuses }) { return ""; } + + export function CanaryStatus({ status, className }) { if (status.mixed) { return ;