|
| 1 | +import CloseIcon from '@mui/icons-material/Close'; |
| 2 | +import { IconButton } from '@mui/material'; |
| 3 | +import Stack from '@mui/material/Stack'; |
| 4 | +import React, { useState } from 'react'; |
| 5 | +import { useParams } from 'react-router-dom'; |
| 6 | + |
| 7 | +import { ActionMenu } from 'src/components/ActionMenu'; |
| 8 | +import { Box } from 'src/components/Box'; |
| 9 | +import { Button } from 'src/components/Button/Button'; |
| 10 | +import { CircleProgress } from 'src/components/CircleProgress'; |
| 11 | +import EnhancedSelect from 'src/components/EnhancedSelect'; |
| 12 | +import { InputAdornment } from 'src/components/InputAdornment'; |
| 13 | +import { PaginationFooter } from 'src/components/PaginationFooter/PaginationFooter'; |
| 14 | +import { Table } from 'src/components/Table'; |
| 15 | +import { TableBody } from 'src/components/TableBody'; |
| 16 | +import { TableCell } from 'src/components/TableCell'; |
| 17 | +import { TableHead } from 'src/components/TableHead'; |
| 18 | +import { TableRow } from 'src/components/TableRow'; |
| 19 | +import { TableRowEmpty } from 'src/components/TableRowEmpty/TableRowEmpty'; |
| 20 | +import { TableRowError } from 'src/components/TableRowError/TableRowError'; |
| 21 | +import { TableSortCell } from 'src/components/TableSortCell'; |
| 22 | +import { TextField } from 'src/components/TextField'; |
| 23 | +import { useOrder } from 'src/hooks/useOrder'; |
| 24 | +import { usePagination } from 'src/hooks/usePagination'; |
| 25 | +import { useLoadBalancerCertificatesQuery } from 'src/queries/aglb/certificates'; |
| 26 | + |
| 27 | +import type { Certificate, Filter } from '@linode/api-v4'; |
| 28 | + |
| 29 | +const PREFERENCE_KEY = 'loadbalancer-certificates'; |
| 30 | + |
| 31 | +const CERTIFICATE_TYPE_LABEL_MAP: Record<Certificate['type'], string> = { |
| 32 | + ca: 'Service Target Certificate', |
| 33 | + downstream: 'TLS Certificate', |
| 34 | +}; |
| 35 | + |
| 36 | +type CertificateTypeFilter = 'all' | Certificate['type']; |
| 37 | + |
| 38 | +export const LoadBalancerCertificates = () => { |
| 39 | + const { loadbalancerId } = useParams<{ loadbalancerId: string }>(); |
| 40 | + |
| 41 | + const [type, setType] = useState<CertificateTypeFilter>('all'); |
| 42 | + const [query, setQuery] = useState<string>(); |
| 43 | + |
| 44 | + const pagination = usePagination(1, PREFERENCE_KEY); |
| 45 | + |
| 46 | + const { handleOrderChange, order, orderBy } = useOrder( |
| 47 | + { |
| 48 | + order: 'desc', |
| 49 | + orderBy: 'label', |
| 50 | + }, |
| 51 | + `${PREFERENCE_KEY}-order` |
| 52 | + ); |
| 53 | + |
| 54 | + const filter: Filter = { |
| 55 | + ['+order']: order, |
| 56 | + ['+order_by']: orderBy, |
| 57 | + }; |
| 58 | + |
| 59 | + // If the user selects a Certificate type filter, API filter by that type. |
| 60 | + if (type !== 'all') { |
| 61 | + filter['type'] = type; |
| 62 | + } |
| 63 | + |
| 64 | + // If the user types in a search query, API filter by the label. |
| 65 | + if (query) { |
| 66 | + filter['label'] = { '+contains': query }; |
| 67 | + } |
| 68 | + |
| 69 | + const { data, error, isLoading } = useLoadBalancerCertificatesQuery( |
| 70 | + Number(loadbalancerId), |
| 71 | + { |
| 72 | + page: pagination.page, |
| 73 | + page_size: pagination.pageSize, |
| 74 | + }, |
| 75 | + filter |
| 76 | + ); |
| 77 | + |
| 78 | + if (isLoading) { |
| 79 | + return <CircleProgress />; |
| 80 | + } |
| 81 | + |
| 82 | + const filterOptions = [ |
| 83 | + { label: 'All', value: 'all' }, |
| 84 | + { label: 'TLS Certificates', value: 'tls' }, |
| 85 | + { label: 'Service Target Certificates', value: 'ca' }, |
| 86 | + ]; |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + <Stack |
| 91 | + alignItems="flex-end" |
| 92 | + direction="row" |
| 93 | + flexWrap="wrap" |
| 94 | + gap={2} |
| 95 | + mb={2} |
| 96 | + mt={1.5} |
| 97 | + > |
| 98 | + <EnhancedSelect |
| 99 | + styles={{ |
| 100 | + container: () => ({ |
| 101 | + maxWidth: '200px', |
| 102 | + }), |
| 103 | + }} |
| 104 | + isClearable={false} |
| 105 | + label="Certificate Type" |
| 106 | + noMarginTop |
| 107 | + onChange={(option) => setType(option?.value as CertificateTypeFilter)} |
| 108 | + options={filterOptions} |
| 109 | + value={filterOptions.find((option) => option.value === type) ?? null} |
| 110 | + /> |
| 111 | + <TextField |
| 112 | + InputProps={{ |
| 113 | + endAdornment: ( |
| 114 | + <InputAdornment position="end"> |
| 115 | + <IconButton |
| 116 | + aria-label="Clear" |
| 117 | + onClick={() => setQuery('')} |
| 118 | + size="small" |
| 119 | + sx={{ padding: 'unset' }} |
| 120 | + > |
| 121 | + <CloseIcon |
| 122 | + color="inherit" |
| 123 | + sx={{ color: '#aaa !important' }} |
| 124 | + /> |
| 125 | + </IconButton> |
| 126 | + </InputAdornment> |
| 127 | + ), |
| 128 | + }} |
| 129 | + hideLabel |
| 130 | + label="Filter" |
| 131 | + onChange={(e) => setQuery(e.target.value)} |
| 132 | + placeholder="Filter" |
| 133 | + style={{ minWidth: '320px' }} |
| 134 | + value={query} |
| 135 | + /> |
| 136 | + <Box flexGrow={1} /> |
| 137 | + <Button buttonType="primary">Upload Certificate</Button> |
| 138 | + </Stack> |
| 139 | + <Table> |
| 140 | + <TableHead> |
| 141 | + <TableRow> |
| 142 | + <TableSortCell |
| 143 | + active={orderBy === 'label'} |
| 144 | + direction={order} |
| 145 | + handleClick={handleOrderChange} |
| 146 | + label="label" |
| 147 | + > |
| 148 | + Label |
| 149 | + </TableSortCell> |
| 150 | + <TableSortCell |
| 151 | + active={orderBy === 'type'} |
| 152 | + direction={order} |
| 153 | + handleClick={handleOrderChange} |
| 154 | + label="type" |
| 155 | + > |
| 156 | + Type |
| 157 | + </TableSortCell> |
| 158 | + <TableCell></TableCell> |
| 159 | + </TableRow> |
| 160 | + </TableHead> |
| 161 | + <TableBody> |
| 162 | + {error && <TableRowError colSpan={3} message={error?.[0].reason} />} |
| 163 | + {data?.results === 0 && <TableRowEmpty colSpan={3} />} |
| 164 | + {data?.data.map(({ label, type }) => ( |
| 165 | + <TableRow key={`${label}-${type}`}> |
| 166 | + <TableCell>{label}</TableCell> |
| 167 | + <TableCell>{CERTIFICATE_TYPE_LABEL_MAP[type]}</TableCell> |
| 168 | + <TableCell actionCell> |
| 169 | + <ActionMenu |
| 170 | + actionsList={[ |
| 171 | + { onClick: () => null, title: 'Edit' }, |
| 172 | + { onClick: () => null, title: 'Delete' }, |
| 173 | + ]} |
| 174 | + ariaLabel={`Action Menu for certificate ${label}`} |
| 175 | + /> |
| 176 | + </TableCell> |
| 177 | + </TableRow> |
| 178 | + ))} |
| 179 | + </TableBody> |
| 180 | + </Table> |
| 181 | + <PaginationFooter |
| 182 | + count={data?.results ?? 0} |
| 183 | + handlePageChange={pagination.handlePageChange} |
| 184 | + handleSizeChange={pagination.handlePageSizeChange} |
| 185 | + page={pagination.page} |
| 186 | + pageSize={pagination.pageSize} |
| 187 | + /> |
| 188 | + </> |
| 189 | + ); |
| 190 | +}; |
0 commit comments