Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 146 147 148 149 150 151 152 153 | 6x 47x 47x 47x 47x 47x 3x 47x 47x 4x 4x 47x 3x 47x 47x 29x 29x 76x 76x 76x 76x 29x 76x 76x 4x 47x | import React, { useState } from "react";
import { Badge, Button } from "react-bootstrap";
import { createColumnHelper } from "@tanstack/react-table";
import { toast } from "react-toastify";
import OurTable from "main/components/OurTable";
import ConfirmationModal from "main/components/Common/ConfirmationModal";
import { useBackendMutation } from "main/utils/useBackend";
import { formatTime } from "main/utils/dateUtils";
import {
apiKeysQueryKey,
daysUntil,
describeKey,
EXPIRY_WARNING_DAYS,
} from "main/utils/apiKeyUtils";
const STATUS_VARIANTS = {
ACTIVE: "success",
EXPIRED: "secondary",
REVOKED: "danger",
};
export default function ApiKeysTable({
apiKeys,
courseId,
testIdPrefix = "ApiKeysTable",
}) {
const [showRevokeModal, setShowRevokeModal] = useState(false);
const [keyToRevoke, setKeyToRevoke] = useState(null);
const queryKey = apiKeysQueryKey(courseId);
const objectToAxiosParamsRevoke = (apiKey) => ({
url: "/api/courses/key",
method: "DELETE",
params: { courseId: courseId, id: apiKey.id },
});
const onRevokeSuccess = (_data, apiKey) => {
toast(`API key ${describeKey(apiKey)} revoked.`);
};
const revokeMutation = useBackendMutation(
objectToAxiosParamsRevoke,
{ onSuccess: onRevokeSuccess },
[queryKey],
);
const revokeCallback = (cell) => {
setKeyToRevoke(cell.row.original);
setShowRevokeModal(true);
};
const confirmRevoke = () => {
revokeMutation.mutate(keyToRevoke);
};
const columnHelper = createColumnHelper();
const columns = [
{
header: "Label",
accessorKey: "label",
},
{
header: "Key",
id: "keySuffix",
accessorFn: (row) => `…${row.keySuffix}`,
},
{
header: "Created by",
accessorKey: "createdByEmail",
},
{
header: "Created",
id: "createdAt",
accessorFn: (row) => formatTime(row.createdAt),
},
{
header: "Expires",
accessorKey: "expiresAt",
cell: ({ cell }) => {
const apiKey = cell.row.original;
const days = daysUntil(apiKey.expiresAt);
const warn = apiKey.status === "ACTIVE" && days <= EXPIRY_WARNING_DAYS;
return (
<>
{formatTime(apiKey.expiresAt)}
{warn && (
<Badge
bg="warning"
text="dark"
className="ms-2"
data-testid={`${testIdPrefix}-row-${cell.row.index}-expiry-warning`}
>
expires in {days} days
</Badge>
)}
</>
);
},
},
{
header: "Last used",
id: "lastUsedAt",
accessorFn: (row) =>
row.lastUsedAt ? formatTime(row.lastUsedAt) : "never",
},
{
header: "Uses",
accessorKey: "usageCount",
},
{
header: "Status",
accessorKey: "status",
cell: ({ cell }) => (
<Badge bg={STATUS_VARIANTS[cell.getValue()]}>{cell.getValue()}</Badge>
),
},
columnHelper.display({
id: "Revoke",
header: "Revoke",
cell: ({ cell }) =>
cell.row.original.status === "ACTIVE" ? (
<Button
variant="danger"
onClick={() => revokeCallback(cell)}
data-testid={`${testIdPrefix}-cell-row-${cell.row.index}-col-Revoke-button`}
>
Revoke
</Button>
) : null,
}),
];
return (
<>
<ConfirmationModal
showModal={showRevokeModal}
setShowModal={setShowRevokeModal}
onYes={confirmRevoke}
>
<span data-testid={`${testIdPrefix}-revoke-confirmation-message`}>
Are you sure you want to revoke the API key{" "}
{keyToRevoke && describeKey(keyToRevoke)}? Anything using it will stop
working immediately. This cannot be undone.
</span>
</ConfirmationModal>
<OurTable data={apiKeys} columns={columns} testid={testIdPrefix} />
</>
);
}
|