-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: minimal React table implementation
- Loading branch information
Showing
5 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <StatusList checkStatuses={value} />; | ||
} | ||
|
||
function UptimeCell({ value }) { | ||
return ( | ||
<Percentage upper={value.passed + value.failed} lower={value.passed} /> | ||
); | ||
} | ||
|
||
function LatencyCell({ value }) { | ||
return <Duration ms={value.rolling1h} />; | ||
} | ||
|
||
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 ( | ||
<table className={styles.tableClass} {...getTableProps()}> | ||
<thead className={styles.theadClass}> | ||
{headerGroups.map((headerGroup) => ( | ||
<tr | ||
className={styles.theadRowClass} | ||
{...headerGroup.getHeaderGroupProps()} | ||
> | ||
{headerGroup.headers.map((column) => ( | ||
<th | ||
className={styles.theadHeaderClass} | ||
{...column.getHeaderProps()} | ||
> | ||
{column.render("Header")} | ||
</th> | ||
))} | ||
</tr> | ||
))} | ||
</thead> | ||
<tbody className={styles.tbodyClass} {...getTableBodyProps()}> | ||
{rows.map((row) => { | ||
prepareRow(row); | ||
return ( | ||
<tr | ||
key={row.id} | ||
className={styles.tbodyRowClass} | ||
{...row.getRowProps()} | ||
> | ||
{row.cells.map((cell) => ( | ||
<td | ||
key={cell.column.Header} | ||
className={styles.tbodyDataClass} | ||
{...cell.getCellProps()} | ||
> | ||
{cell.render("Cell")} | ||
</td> | ||
))} | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters