-
Notifications
You must be signed in to change notification settings - Fork 38
/
EnclavesTable.tsx
145 lines (137 loc) · 5.06 KB
/
EnclavesTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { Button, Checkbox } from "@chakra-ui/react";
import { ColumnDef, createColumnHelper } from "@tanstack/react-table";
import { FilesArtifactNameAndUuid, ServiceInfo } from "enclave-manager-sdk/build/api_container_service_pb";
import { EnclaveContainersStatus } from "enclave-manager-sdk/build/engine_service_pb";
import { DateTime } from "luxon";
import { useMemo } from "react";
import { Link } from "react-router-dom";
import { EnclaveFullInfo } from "../../../emui/enclaves/types";
import { isDefined } from "../../../utils";
import { DataTable } from "../../DataTable";
import { FormatDateTime } from "../../FormatDateTime";
import { EnclaveArtifactsSummary } from "../widgets/EnclaveArtifactsSummary";
import { EnclaveServicesSummary } from "../widgets/EnclaveServicesSummary";
import { EnclaveSourceButton } from "../widgets/EnclaveSourceButton";
import { EnclaveStatus } from "../widgets/EnclaveStatus";
type EnclaveTableRow = {
uuid: string;
name: string;
status: EnclaveContainersStatus;
created: DateTime | null;
source: "loading" | string | null;
services: "loading" | ServiceInfo[] | null;
artifacts: "loading" | FilesArtifactNameAndUuid[] | null;
};
const enclaveToRow = (enclave: EnclaveFullInfo): EnclaveTableRow => {
return {
uuid: enclave.shortenedUuid,
name: enclave.name,
status: enclave.containersStatus,
created: enclave.creationTime ? DateTime.fromJSDate(enclave.creationTime.toDate()) : null,
source: !isDefined(enclave.starlarkRun)
? "loading"
: enclave.starlarkRun.isOk
? enclave.starlarkRun.value.packageId
: null,
services: !isDefined(enclave.services)
? "loading"
: enclave.services.isOk
? Object.values(enclave.services.value.serviceInfo)
: null,
artifacts: !isDefined(enclave.filesAndArtifacts)
? "loading"
: enclave.filesAndArtifacts.isOk
? enclave.filesAndArtifacts.value.fileNamesAndUuids
: null,
};
};
const columnHelper = createColumnHelper<EnclaveTableRow>();
type EnclavesTableProps = {
enclavesData: EnclaveFullInfo[];
selection: EnclaveFullInfo[];
onSelectionChange: (newSelection: EnclaveFullInfo[]) => void;
};
export const EnclavesTable = ({ enclavesData, selection, onSelectionChange }: EnclavesTableProps) => {
const enclaves = enclavesData.map(enclaveToRow);
const rowSelection = useMemo(() => {
const selectedUUIDs = new Set<string>(selection.map(({ enclaveUuid }) => enclaveUuid));
return enclavesData.reduce(
(acc, cur, i) => {
if (selectedUUIDs.has(cur.enclaveUuid)) {
acc[i] = true;
}
// falsey values are not allowed - they break getIsSomeRowsSelected
return acc;
},
{} as Record<string, boolean>,
);
}, [selection, enclavesData]);
const columns = useMemo<ColumnDef<EnclaveTableRow, any>[]>(
() => [
columnHelper.accessor("uuid", {
header: ({ table }) => (
<Checkbox
isIndeterminate={table.getIsSomeRowsSelected()}
isChecked={table.getIsAllRowsSelected()}
onChange={table.getToggleAllRowsSelectedHandler()}
/>
),
cell: ({ row, getValue }) => (
<Checkbox isChecked={row.getIsSelected()} onChange={row.getToggleSelectedHandler()} />
),
enableSorting: false,
}),
columnHelper.accessor("name", {
header: "Name",
cell: (nameCell) => (
<Link to={`/enclave/${nameCell.row.original.uuid}/overview`}>
<Button size={"sm"} variant={"ghost"}>
{nameCell.row.original.name}
</Button>
</Link>
),
}),
columnHelper.accessor("status", {
header: "Status",
cell: (statusCell) => <EnclaveStatus status={statusCell.getValue()} variant={"square"} />,
}),
columnHelper.accessor("created", {
header: "Created",
cell: (createdCell) => (
<FormatDateTime
fontSize={"xs"}
fontWeight={"semibold"}
dateTime={createdCell.getValue()}
format={"relative"}
/>
),
}),
columnHelper.accessor("source", {
header: "Source",
cell: (sourceCell) => <EnclaveSourceButton source={sourceCell.getValue()} />,
}),
columnHelper.accessor("services", {
cell: (servicesCell) => <EnclaveServicesSummary services={servicesCell.getValue()} />,
meta: { centerAligned: true },
}),
columnHelper.accessor("artifacts", {
header: "File artifacts",
cell: (artifactsCell) => <EnclaveArtifactsSummary artifacts={artifactsCell.getValue()} />,
meta: { centerAligned: true },
}),
],
[],
);
return (
<DataTable
rowSelection={rowSelection}
onRowSelectionChange={(updaterOrValue) => {
const newRowSelection = typeof updaterOrValue === "function" ? updaterOrValue(rowSelection) : updaterOrValue;
onSelectionChange(enclavesData.filter((enclave, i) => newRowSelection[i]));
}}
columns={columns}
data={enclaves}
defaultSorting={[{ id: "created", desc: true }]}
/>
);
};