import React, { useMemo, useRef, useEffect } from 'react'; import { css, Table, TableHeader, Row, Cell, cx, spacing, palette, IndexKeysBadge, } from '@mongodb-js/compass-components'; import TypeField from './type-field'; import SizeField from './size-field'; import UsageField from './usage-field'; import PropertyField from './property-field'; import type { IndexDefinition, SortColumn, SortDirection, } from '../../modules/indexes'; import IndexActions from './index-actions'; // When row is hovered, we show the delete button const rowStyles = css({ ':hover': { '.index-actions-cell': { button: { opacity: 1, }, }, }, }); // When row is not hovered, we hide the delete button const indexActionsCellStyles = css({ button: { opacity: 0, '&:focus': { opacity: 1, }, }, minWidth: spacing[5], }); const tableHeaderStyles = css({ borderWidth: 0, borderBottomWidth: 3, '> div': { justifyContent: 'space-between', }, }); const cellStyles = css({ verticalAlign: 'middle', }); const nestedRowCellStyles = css({ padding: 0, }); const tableStyles = css({ thead: { position: 'sticky', top: 0, background: palette.white, zIndex: 5, }, }); type IndexesTableProps = { indexes: IndexDefinition[]; canDeleteIndex: boolean; scrollHeight: number; onSortTable: (column: SortColumn, direction: SortDirection) => void; onDeleteIndex: (index: IndexDefinition) => void; }; export const IndexesTable: React.FunctionComponent = ({ indexes, canDeleteIndex, scrollHeight, onSortTable, onDeleteIndex, }) => { const containerRef = useRef(null); const columns = useMemo(() => { const sortColumns: SortColumn[] = [ 'Name and Definition', 'Type', 'Size', 'Usage', 'Properties', ]; const _columns = sortColumns.map((name) => { return ( { onSortTable(name, direction); }} /> ); }); // The delete column if (canDeleteIndex) { _columns.push(); } return _columns; }, [canDeleteIndex, onSortTable]); useEffect(() => { /** * For table header to be sticky, the wrapper element of table needs to have a height. * LG wraps table in a div at multiple levels, so height can not be direclty applied to the wrapper we use. */ const container = containerRef.current?.getElementsByTagName('table')[0]?.parentElement; if (container) { container.style.height = `${scrollHeight}px`; } }, [scrollHeight]); return ( // LG table does not forward ref
{({ datum: index }) => ( {index.name} {/* Delete column is conditional */} {canDeleteIndex && ( {index.name !== '_id_' && index.extra.status !== 'inprogress' && (
)}
)}
)}
); };