Skip to content

Commit 9c2af84

Browse files
authored
chore: factor out the inner workings of the indexes table so we can have separate regular and search ones COMPASS-7163 (#4820)
* factor out the inner workings of the indexes table so we can have separate regular and search ones * fix the generics * don't export the styles * Revert "don't export the styles" This reverts commit 22227fe. * move data-testid and aria-label out of the shared component
1 parent b9e43a4 commit 9c2af84

17 files changed

+182
-138
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './indexes-table';
Lines changed: 30 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
1-
import React, { useMemo, useRef, useEffect } from 'react';
1+
import React, { useRef, useEffect } from 'react';
22
import {
33
css,
44
Table,
5-
TableHeader,
6-
Row,
7-
Cell,
8-
cx,
95
spacing,
106
palette,
11-
IndexKeysBadge,
127
KeylineCard,
138
useDOMRect,
149
} from '@mongodb-js/compass-components';
1510

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';
26-
2711
// When row is hovered, we show the delete button
28-
const rowStyles = css({
12+
export const rowStyles = css({
2913
':hover': {
3014
'.index-actions-cell': {
3115
button: {
@@ -36,7 +20,7 @@ const rowStyles = css({
3620
});
3721

3822
// When row is not hovered, we hide the delete button
39-
const indexActionsCellStyles = css({
23+
export const indexActionsCellStyles = css({
4024
button: {
4125
opacity: 0,
4226
'&:focus': {
@@ -46,19 +30,19 @@ const indexActionsCellStyles = css({
4630
minWidth: spacing[5],
4731
});
4832

49-
const tableHeaderStyles = css({
33+
export const tableHeaderStyles = css({
5034
borderWidth: 0,
5135
borderBottomWidth: 3,
5236
'> div': {
5337
justifyContent: 'space-between',
5438
},
5539
});
5640

57-
const cellStyles = css({
41+
export const cellStyles = css({
5842
verticalAlign: 'middle',
5943
});
6044

61-
const nestedRowCellStyles = css({
45+
export const nestedRowCellStyles = css({
6246
padding: 0,
6347
});
6448

@@ -81,54 +65,23 @@ const spaceProviderStyles = css({
8165
overflow: 'hidden',
8266
});
8367

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;
68+
export type IndexesTableProps<Shape> = {
69+
['data-testid']: string;
70+
['aria-label']: string;
71+
columns: JSX.Element[];
72+
children: (args: { datum: Shape; index: number }) => JSX.Element;
73+
data: Shape[];
9274
};
9375

94-
export const IndexesTable: React.FunctionComponent<IndexesTableProps> = ({
95-
indexes,
96-
canModifyIndex,
97-
serverVersion,
98-
onSortTable,
99-
onDeleteIndex,
100-
onHideIndex,
101-
onUnhideIndex,
102-
}) => {
76+
export function IndexesTable<Shape>({
77+
['data-testid']: dataTestId,
78+
['aria-label']: ariaLabel,
79+
columns,
80+
children,
81+
data,
82+
}: IndexesTableProps<Shape>) {
10383
const cardRef = useRef<HTMLDivElement>(null);
10484
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]);
13285

13386
useEffect(() => {
13487
/**
@@ -167,76 +120,21 @@ export const IndexesTable: React.FunctionComponent<IndexesTableProps> = ({
167120

168121
return (
169122
<div className={spaceProviderStyles} {...rectProps}>
170-
<KeylineCard ref={cardRef} data-testid="indexes" className={cardStyles}>
171-
<Table
123+
<KeylineCard
124+
ref={cardRef}
125+
data-testid={dataTestId}
126+
className={cardStyles}
127+
>
128+
<Table<Shape>
172129
className={tableStyles}
173-
data={indexes}
130+
data={data}
174131
columns={columns}
175-
data-testid="indexes-list"
176-
aria-label="Indexes List Table"
132+
data-testid={`${dataTestId}-list`}
133+
aria-label={`${ariaLabel} List Table`}
177134
>
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' && (
211-
<div
212-
className={cx(
213-
indexActionsCellStyles,
214-
'index-actions-cell'
215-
)}
216-
>
217-
<IndexActions
218-
index={index}
219-
serverVersion={serverVersion}
220-
onDeleteIndex={onDeleteIndex}
221-
onHideIndex={onHideIndex}
222-
onUnhideIndex={onUnhideIndex}
223-
></IndexActions>
224-
</div>
225-
)}
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>
235-
</Row>
236-
</Row>
237-
)}
135+
{children}
238136
</Table>
239137
</KeylineCard>
240138
</div>
241139
);
242-
};
140+
}

packages/compass-indexes/src/components/indexes/indexes.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919

2020
import type { IndexView } from '../indexes-toolbar/indexes-toolbar';
2121
import { IndexesToolbar } from '../indexes-toolbar/indexes-toolbar';
22-
import { IndexesTable } from '../indexes-table/indexes-table';
22+
import { RegularIndexesTable } from '../regular-indexes-table/regular-indexes-table';
2323
import type { RootState } from '../../modules';
2424
import { SearchIndexesStatuses } from '../../modules/search-indexes';
2525

@@ -99,7 +99,7 @@ export const Indexes: React.FunctionComponent<IndexesProps> = ({
9999
{!isReadonlyView &&
100100
!error &&
101101
currentIndexesView === 'regular-indexes' && (
102-
<IndexesTable
102+
<RegularIndexesTable
103103
indexes={indexes}
104104
serverVersion={serverVersion}
105105
canModifyIndex={isWritable && !readOnly}

packages/compass-indexes/src/components/indexes-table/badge-with-icon-link.spec.tsx renamed to packages/compass-indexes/src/components/regular-indexes-table/badge-with-icon-link.spec.tsx

File renamed without changes.

packages/compass-indexes/src/components/indexes-table/badge-with-icon-link.tsx renamed to packages/compass-indexes/src/components/regular-indexes-table/badge-with-icon-link.tsx

File renamed without changes.

packages/compass-indexes/src/components/indexes-table/index-actions.spec.tsx renamed to packages/compass-indexes/src/components/regular-indexes-table/index-actions.spec.tsx

File renamed without changes.

packages/compass-indexes/src/components/indexes-table/index-actions.tsx renamed to packages/compass-indexes/src/components/regular-indexes-table/index-actions.tsx

File renamed without changes.

packages/compass-indexes/src/components/indexes-table/property-field.spec.tsx renamed to packages/compass-indexes/src/components/regular-indexes-table/property-field.spec.tsx

File renamed without changes.

packages/compass-indexes/src/components/indexes-table/property-field.tsx renamed to packages/compass-indexes/src/components/regular-indexes-table/property-field.tsx

File renamed without changes.

packages/compass-indexes/src/components/indexes-table/indexes-table.spec.tsx renamed to packages/compass-indexes/src/components/regular-indexes-table/regular-indexes-table.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import userEvent from '@testing-library/user-event';
55
import { spy } from 'sinon';
66

7-
import { IndexesTable } from './indexes-table';
7+
import { RegularIndexesTable } from './regular-indexes-table';
88
import type { IndexDefinition } from '../../modules/regular-indexes';
99

1010
const indexes = [
@@ -95,10 +95,10 @@ const indexes = [
9595
] as IndexDefinition[];
9696

9797
const renderIndexList = (
98-
props: Partial<React.ComponentProps<typeof IndexesTable>> = {}
98+
props: Partial<React.ComponentProps<typeof RegularIndexesTable>> = {}
9999
) => {
100100
render(
101-
<IndexesTable
101+
<RegularIndexesTable
102102
indexes={[]}
103103
serverVersion="4.4.0"
104104
canModifyIndex={true}
@@ -111,7 +111,7 @@ const renderIndexList = (
111111
);
112112
};
113113

114-
describe('IndexesTable Component', function () {
114+
describe('RegularIndexesTable Component', function () {
115115
before(cleanup);
116116
afterEach(cleanup);
117117

0 commit comments

Comments
 (0)