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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | /* eslint-disable react-hooks/exhaustive-deps */ import { useEffect, useState } from "react"; import maskCharacter from "../utils/common/maskCharacter.js"; import { DataGrid } from "@mui/x-data-grid"; import Button from "@mui/material/Button"; import Snackbar from "@mui/material/Snackbar"; import Alert from "@mui/material/Alert"; import Paper from "@mui/material/Paper"; import TableSkeleton from "../components/Skeleton/TableLoader.js"; import SuspenseLoader from "../components/Skeleton/SuspenseLoader.js"; import Heading from "../components/Heading"; import NoAPIKeys from "../components/Apikeys/NoAPIKeys"; import dayjs from "dayjs"; import { useGenerateAPIKeyMutation, useGetAPIKeyQuery, } from "../store/features/APIKeys/api"; import { CircularProgress } from "@mui/material"; const columns = [ { field: "key", headerName: "Token", flex: 1, valueGetter: (params: any) => { const masked = maskCharacter(String(params?.value), "*", 4); return masked; }, }, { field: "created_at", headerName: "Created At", flex: 1, valueGetter: (params) => { const format = "YYYY-MM-DD HH:mm:ss"; const date = new Date(params?.value); return dayjs(date).format(format); }, }, { field: "created_by", headerName: "Created by", flex: 1, }, { flex: 1, renderCell: (params) => { const apiKeyVal = params.row.key; const handleCopyClick = (val) => async () => { if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(val); } else { const textArea = document.createElement("textarea"); textArea.value = val; textArea.style.position = "absolute"; textArea.style.left = "-999999px"; document.body.prepend(textArea); textArea.select(); try { document.execCommand("copy"); } catch (error) { console.error(error); } finally { textArea.remove(); } } }; return ( <Button variant="text" onClick={handleCopyClick(apiKeyVal)}> Copy Token </Button> ); }, }, ]; const ApiTokens = () => { const [pageSize, setPageSize] = useState(10); const [page, setPage] = useState(0); const meta = { limit: pageSize, offset: pageSize * page, }; const { data, isLoading, isError, refetch } = useGetAPIKeyQuery(meta); const [triggerGenerateAPIKey, { isLoading: generateLoading }] = useGenerateAPIKeyMutation(); useEffect(() => { Iif (!isLoading) refetch(); }, [page, pageSize]); useEffect(() => { Iif (data?.meta) setPageSize(Number(data?.meta?.page?.limit)); }, [data]); const handlePageChange = (newPage) => { setPage(newPage); }; const handlePageSizeChange = (newPageSize) => { setPageSize(newPageSize); }; const handleCreateApiKey = async () => { await triggerGenerateAPIKey(); window.location.reload(); }; return ( <> <Heading heading={"API keys"} /> <main className="flex flex-col gap-4 p-2 pt-4"> <div className="flex items-center justify-between"> <button className="text-sm bg-violet-600 hover:bg-violet-700 px-4 py-2 rounded-lg !text-white" onClick={handleCreateApiKey}> + API Key </button> {generateLoading && <CircularProgress color="primary" size={20} />} </div> <SuspenseLoader loading={isLoading} loader={<TableSkeleton noOfLines={7} />}> <Paper className="!shadow-none" sx={{ width: "100%", height: "360px" }}> {data?.account_api_tokens && ( <DataGrid sx={{ ".MuiDataGrid-columnSeparator": { display: "none", }, }} disableColumnMenu pagination paginationMode="server" rowCount={data?.meta?.total_count} pageSize={pageSize} onPageSizeChange={handlePageSizeChange} rowsPerPageOptions={[10, 20, 50]} onPageChange={handlePageChange} rows={data?.account_api_tokens} columns={ columns.map((column) => ({ ...column, sortable: false, })) as any } getRowId={(params) => params?.key} disableSelectionOnClick /> )} {!data?.account_api_tokens && <NoAPIKeys />} </Paper> </SuspenseLoader> </main> <Snackbar open={isError} autoHideDuration={6000} anchorOrigin={{ vertical: "top", horizontal: "right" }}> <Alert severity="error" sx={{ width: "100%" }}> Something went wrong. Please contact Administrator </Alert> </Snackbar> </> ); }; export default ApiTokens; |