|
1 | 1 | import React, { useMemo, useRef, useEffect } from 'react';
|
2 | 2 | import {
|
3 | 3 | css,
|
| 4 | + cx, |
4 | 5 | Table,
|
5 | 6 | TableHeader,
|
6 | 7 | Row,
|
7 | 8 | Cell,
|
8 |
| - cx, |
9 | 9 | spacing,
|
10 | 10 | palette,
|
11 |
| - IndexKeysBadge, |
12 | 11 | KeylineCard,
|
13 | 12 | useDOMRect,
|
14 | 13 | } from '@mongodb-js/compass-components';
|
15 | 14 |
|
16 |
| -import TypeField from './type-field'; |
17 |
| -import SizeField from './size-field'; |
18 |
| -import UsageField from './usage-field'; |
19 |
| -import PropertyField from './property-field'; |
20 |
| -import type { |
21 |
| - IndexDefinition, |
22 |
| - SortColumn, |
23 |
| - SortDirection, |
24 |
| -} from '../../modules/regular-indexes'; |
25 |
| -import IndexActions from './index-actions'; |
| 15 | +type SortDirection = 'asc' | 'desc'; |
26 | 16 |
|
27 | 17 | // When row is hovered, we show the delete button
|
28 | 18 | const rowStyles = css({
|
@@ -81,54 +71,37 @@ const spaceProviderStyles = css({
|
81 | 71 | overflow: 'hidden',
|
82 | 72 | });
|
83 | 73 |
|
84 |
| -type IndexesTableProps = { |
85 |
| - indexes: IndexDefinition[]; |
86 |
| - canModifyIndex: boolean; |
87 |
| - serverVersion: string; |
88 |
| - onSortTable: (column: SortColumn, direction: SortDirection) => void; |
89 |
| - onDeleteIndex: (index: IndexDefinition) => void; |
90 |
| - onHideIndex: (name: string) => void; |
91 |
| - onUnhideIndex: (name: string) => void; |
| 74 | +type IndexInfo = { |
| 75 | + key: string; |
| 76 | + 'data-testid': string; |
| 77 | + fields: { |
| 78 | + key?: string; |
| 79 | + 'data-testid': string; |
| 80 | + children: React.ReactNode; |
| 81 | + }[]; |
| 82 | + actions?: React.ReactNode; |
| 83 | + details?: React.ReactNode; |
| 84 | +}; |
| 85 | + |
| 86 | +export type IndexesTableProps<Column extends string> = { |
| 87 | + ['data-testid']: string; |
| 88 | + ['aria-label']: string; |
| 89 | + columns: readonly Column[]; |
| 90 | + data: IndexInfo[]; |
| 91 | + canModifyIndex?: boolean; |
| 92 | + onSortTable: (column: Column, direction: SortDirection) => void; |
92 | 93 | };
|
93 | 94 |
|
94 |
| -export const IndexesTable: React.FunctionComponent<IndexesTableProps> = ({ |
95 |
| - indexes, |
| 95 | +export function IndexesTable<Column extends string>({ |
| 96 | + ['data-testid']: dataTestId, |
| 97 | + ['aria-label']: ariaLabel, |
| 98 | + columns: sortColumns, |
96 | 99 | canModifyIndex,
|
97 |
| - serverVersion, |
| 100 | + data, |
98 | 101 | onSortTable,
|
99 |
| - onDeleteIndex, |
100 |
| - onHideIndex, |
101 |
| - onUnhideIndex, |
102 |
| -}) => { |
| 102 | +}: IndexesTableProps<Column>) { |
103 | 103 | const cardRef = useRef<HTMLDivElement>(null);
|
104 | 104 | const [rectProps, { height: availableHeightInContainer }] = useDOMRect();
|
105 |
| - const columns = useMemo(() => { |
106 |
| - const sortColumns: SortColumn[] = [ |
107 |
| - 'Name and Definition', |
108 |
| - 'Type', |
109 |
| - 'Size', |
110 |
| - 'Usage', |
111 |
| - 'Properties', |
112 |
| - ]; |
113 |
| - const _columns = sortColumns.map((name) => { |
114 |
| - return ( |
115 |
| - <TableHeader |
116 |
| - data-testid={`index-header-${name}`} |
117 |
| - label={name} |
118 |
| - key={name} |
119 |
| - className={tableHeaderStyles} |
120 |
| - handleSort={(direction) => { |
121 |
| - onSortTable(name, direction); |
122 |
| - }} |
123 |
| - /> |
124 |
| - ); |
125 |
| - }); |
126 |
| - // Actions column |
127 |
| - if (canModifyIndex) { |
128 |
| - _columns.push(<TableHeader label={''} className={tableHeaderStyles} />); |
129 |
| - } |
130 |
| - return _columns; |
131 |
| - }, [canModifyIndex, onSortTable]); |
132 | 105 |
|
133 | 106 | useEffect(() => {
|
134 | 107 | /**
|
@@ -165,78 +138,96 @@ export const IndexesTable: React.FunctionComponent<IndexesTableProps> = ({
|
165 | 138 | }
|
166 | 139 | }, [availableHeightInContainer]);
|
167 | 140 |
|
| 141 | + const columns = useMemo(() => { |
| 142 | + const _columns = sortColumns.map((name) => { |
| 143 | + return ( |
| 144 | + <TableHeader |
| 145 | + data-testid={`index-header-${name}`} |
| 146 | + label={name} |
| 147 | + key={name} |
| 148 | + className={tableHeaderStyles} |
| 149 | + handleSort={(direction: SortDirection) => { |
| 150 | + onSortTable(name, direction); |
| 151 | + }} |
| 152 | + /> |
| 153 | + ); |
| 154 | + }); |
| 155 | + // Actions column |
| 156 | + if (canModifyIndex) { |
| 157 | + _columns.push(<TableHeader label={''} className={tableHeaderStyles} />); |
| 158 | + } |
| 159 | + return _columns; |
| 160 | + }, [canModifyIndex, onSortTable, sortColumns]); |
| 161 | + |
168 | 162 | return (
|
169 | 163 | <div className={spaceProviderStyles} {...rectProps}>
|
170 |
| - <KeylineCard ref={cardRef} data-testid="indexes" className={cardStyles}> |
| 164 | + <KeylineCard |
| 165 | + ref={cardRef} |
| 166 | + data-testid={dataTestId} |
| 167 | + className={cardStyles} |
| 168 | + > |
171 | 169 | <Table
|
172 | 170 | className={tableStyles}
|
173 |
| - data={indexes} |
174 | 171 | columns={columns}
|
175 |
| - data-testid="indexes-list" |
176 |
| - aria-label="Indexes List Table" |
| 172 | + data={data} |
| 173 | + data-testid={`${dataTestId}-list`} |
| 174 | + aria-label={`${ariaLabel} List Table`} |
177 | 175 | >
|
178 |
| - {({ datum: index }) => ( |
179 |
| - <Row |
180 |
| - key={index.name} |
181 |
| - data-testid={`index-row-${index.name}`} |
182 |
| - className={rowStyles} |
183 |
| - > |
184 |
| - <Cell data-testid="index-name-field" className={cellStyles}> |
185 |
| - {index.name} |
186 |
| - </Cell> |
187 |
| - <Cell data-testid="index-type-field" className={cellStyles}> |
188 |
| - <TypeField type={index.type} extra={index.extra} /> |
189 |
| - </Cell> |
190 |
| - <Cell data-testid="index-size-field" className={cellStyles}> |
191 |
| - <SizeField |
192 |
| - size={index.size} |
193 |
| - relativeSize={index.relativeSize} |
194 |
| - /> |
195 |
| - </Cell> |
196 |
| - <Cell data-testid="index-usage-field" className={cellStyles}> |
197 |
| - <UsageField usage={index.usageCount} since={index.usageSince} /> |
198 |
| - </Cell> |
199 |
| - <Cell data-testid="index-property-field" className={cellStyles}> |
200 |
| - <PropertyField |
201 |
| - cardinality={index.cardinality} |
202 |
| - extra={index.extra} |
203 |
| - properties={index.properties} |
204 |
| - /> |
205 |
| - </Cell> |
206 |
| - {/* Index actions column is conditional */} |
207 |
| - {canModifyIndex && ( |
208 |
| - <Cell data-testid="index-actions-field" className={cellStyles}> |
209 |
| - {index.name !== '_id_' && |
210 |
| - index.extra.status !== 'inprogress' && ( |
| 176 | + {({ datum: info, index }) => { |
| 177 | + return ( |
| 178 | + <Row |
| 179 | + key={info.key} |
| 180 | + data-testid={info['data-testid']} |
| 181 | + className={rowStyles} |
| 182 | + > |
| 183 | + {info.fields.map((field) => { |
| 184 | + return ( |
| 185 | + <Cell |
| 186 | + key={field.key ?? `${info.key}-${index}`} |
| 187 | + data-testid={field['data-testid']} |
| 188 | + className={cellStyles} |
| 189 | + > |
| 190 | + {field.children} |
| 191 | + </Cell> |
| 192 | + ); |
| 193 | + })} |
| 194 | + {/* Index actions column is conditional */} |
| 195 | + {canModifyIndex && ( |
| 196 | + <Cell |
| 197 | + data-testid="index-actions-field" |
| 198 | + className={cellStyles} |
| 199 | + > |
| 200 | + {info.actions && ( |
211 | 201 | <div
|
212 | 202 | className={cx(
|
213 | 203 | indexActionsCellStyles,
|
214 | 204 | 'index-actions-cell'
|
215 | 205 | )}
|
216 | 206 | >
|
217 |
| - <IndexActions |
218 |
| - index={index} |
219 |
| - serverVersion={serverVersion} |
220 |
| - onDeleteIndex={onDeleteIndex} |
221 |
| - onHideIndex={onHideIndex} |
222 |
| - onUnhideIndex={onUnhideIndex} |
223 |
| - ></IndexActions> |
| 207 | + {info.actions} |
224 | 208 | </div>
|
225 | 209 | )}
|
226 |
| - </Cell> |
227 |
| - )} |
228 |
| - <Row> |
229 |
| - <Cell |
230 |
| - className={cx(nestedRowCellStyles, cellStyles)} |
231 |
| - colSpan={canModifyIndex ? 6 : 5} |
232 |
| - > |
233 |
| - <IndexKeysBadge keys={index.fields} /> |
234 |
| - </Cell> |
| 210 | + </Cell> |
| 211 | + )} |
| 212 | + {info.details && ( |
| 213 | + <Row> |
| 214 | + <Cell |
| 215 | + className={cx(nestedRowCellStyles, cellStyles)} |
| 216 | + colSpan={ |
| 217 | + canModifyIndex |
| 218 | + ? info.fields.length + 1 |
| 219 | + : info.fields.length |
| 220 | + } |
| 221 | + > |
| 222 | + {info.details} |
| 223 | + </Cell> |
| 224 | + </Row> |
| 225 | + )} |
235 | 226 | </Row>
|
236 |
| - </Row> |
237 |
| - )} |
| 227 | + ); |
| 228 | + }} |
238 | 229 | </Table>
|
239 | 230 | </KeylineCard>
|
240 | 231 | </div>
|
241 | 232 | );
|
242 |
| -}; |
| 233 | +} |
0 commit comments