diff --git a/.changeset/column-fit-unsaved-tables.md b/.changeset/column-fit-unsaved-tables.md new file mode 100644 index 000000000..cdb0d4d90 --- /dev/null +++ b/.changeset/column-fit-unsaved-tables.md @@ -0,0 +1,8 @@ +--- +'@hyperdx/app': patch +--- + +feat: double-click a column's resize handle to fit it to its content in tables +that don't save their column widths, such as dashboard search tiles and the side +panel's surrounding context. Their last column now fills the table after a fit +or a drag, instead of leaving an empty strip on the right. diff --git a/packages/app/src/components/DBRowTable.tsx b/packages/app/src/components/DBRowTable.tsx index b59e56770..81ea3b51d 100644 --- a/packages/app/src/components/DBRowTable.tsx +++ b/packages/app/src/components/DBRowTable.tsx @@ -503,9 +503,19 @@ export const RawLogTable = memo( const expandInlineOnRowClick = prefersInlineExpand && (!canOpenSidePanel || highlightedLineId == null); - const [columnSizeStorage, setColumnSizeStorage] = useLocalStorage< + const [storedColumnSizes, setStoredColumnSizes] = useLocalStorage< Record >(`${tableId}-column-sizes`, {}); + // Without a tableId, widths aren't saved and only last while the table is + // mounted. Keeping them here rather than inside TanStack lets the last + // column's fill width follow a resized column. + const [unsavedColumnSizes, setUnsavedColumnSizes] = useState< + Record + >({}); + const columnSizeStorage = tableId ? storedColumnSizes : unsavedColumnSizes; + const setColumnSizeStorage = tableId + ? setStoredColumnSizes + : setUnsavedColumnSizes; //once the user has scrolled within 500px of the bottom of the table, fetch more data if there is any const FETCH_NEXT_PAGE_PX = 500; @@ -844,13 +854,10 @@ export const RawLogTable = memo( onColumnSizingChange: onColumnSizingChange, }; - return tableId - ? { ...initReactTableProps, ...columnSizeProps } - : initReactTableProps; + return { ...initReactTableProps, ...columnSizeProps }; }, [ columns, dedupedRows, - tableId, sortOrder, enableSorting, onSortingChange, @@ -1127,14 +1134,8 @@ export const RawLogTable = memo( key={header.id} header={header} isLast={isLast} - // Only saved widths feed the last column's fill - // width, so elsewhere a fit would leave the rows - // out of line with the header. - onFitToContent={ - tableId - ? () => - fitColumnToContent(header.column.id, isLast) - : undefined + onFitToContent={() => + fitColumnToContent(header.column.id, isLast) } onRemoveColumn={ onRemoveColumn && diff --git a/packages/app/src/components/__tests__/DBRowTable.test.tsx b/packages/app/src/components/__tests__/DBRowTable.test.tsx index 297ac0d7c..b3ec9dc72 100644 --- a/packages/app/src/components/__tests__/DBRowTable.test.tsx +++ b/packages/app/src/components/__tests__/DBRowTable.test.tsx @@ -391,6 +391,12 @@ describe('RawLogTable', () => { ), ); + // Column widths are only saved for tables with a tableId. + const savedColumnSizeKeys = () => + Object.keys(window.localStorage).filter(key => + key.endsWith('-column-sizes'), + ); + const storedSizes = (): unknown => JSON.parse(window.localStorage.getItem('t1-column-sizes') ?? '{}'); @@ -470,22 +476,67 @@ describe('RawLogTable', () => { expect(getHeaderCell(container, 'col2').style.width).toBe('800px'); }); - it('does not fit columns of a table that does not save its widths', async () => { + it('fits a column of a table that does not save its widths, and the last column fills the rest', async () => { const { container } = renderWithMantine( , ); - const handle = within(getHeaderCell(container, 'col1')).getByTestId( - 'column-resize-handle', - ); - expect(handle.getAttribute('title')).toBeNull(); await fitColumn(container, 'col1'); - expect(getHeaderCell(container, 'col1').style.width).toBe('160px'); - expect(getHeaderCell(container, 'col2').style.width).toBe('840px'); + expect(getHeaderCell(container, 'col1').style.width).toBe('80px'); + expect(getHeaderCell(container, 'col2').style.width).toBe( + `${TABLE_WIDTH - 80}px`, + ); + expect(savedColumnSizeKeys()).toEqual([]); + }); + + it('returns the last column to filling a table that does not save its widths', async () => { + const { container } = renderWithMantine( + , + ); + + // From the 840px it fills, 140px to the left + fireEvent.mouseDown( + within(getHeaderCell(container, 'col2')).getByTestId( + 'column-resize-handle', + ), + { clientX: 840 }, + ); + fireEvent.mouseMove(document, { clientX: 700 }); + fireEvent.mouseUp(document, { clientX: 700 }); + expect(getHeaderCell(container, 'col2').style.width).toBe('700px'); + + await fitColumn(container, 'col2'); + + expect(getHeaderCell(container, 'col2').style.width).toBe( + `${TABLE_WIDTH - 160}px`, + ); + expect(savedColumnSizeKeys()).toEqual([]); + }); + + it('keeps the last column filling the rest after a drag in a table that does not save its widths', () => { + const { container } = renderWithMantine( + , + ); + + // From the default 160px, 40px to the right + fireEvent.mouseDown( + within(getHeaderCell(container, 'col1')).getByTestId( + 'column-resize-handle', + ), + { clientX: 100 }, + ); + fireEvent.mouseMove(document, { clientX: 140 }); + fireEvent.mouseUp(document, { clientX: 140 }); + + expect(getHeaderCell(container, 'col1').style.width).toBe('200px'); + expect(getHeaderCell(container, 'col2').style.width).toBe( + `${TABLE_WIDTH - 200}px`, + ); + expect(savedColumnSizeKeys()).toEqual([]); }); }); diff --git a/packages/app/tests/e2e/components/TableComponent.ts b/packages/app/tests/e2e/components/TableComponent.ts index e8f77d95b..8e0a67d49 100644 --- a/packages/app/tests/e2e/components/TableComponent.ts +++ b/packages/app/tests/e2e/components/TableComponent.ts @@ -390,4 +390,20 @@ export class TableComponent { .first() .evaluate(label => label.scrollWidth > label.clientWidth); } + + /** + * Width the columns leave empty on the right of the table + */ + getEmptyWidthRightOfColumns() { + return this.tableContainer.evaluate(container => { + const headerRights = Array.from( + container.querySelectorAll('thead th'), + ).map(header => header.getBoundingClientRect().right); + return ( + container.getBoundingClientRect().left + + container.clientWidth - + Math.max(...headerRights) + ); + }); + } } diff --git a/packages/app/tests/e2e/features/search/column-fit.spec.ts b/packages/app/tests/e2e/features/search/column-fit.spec.ts index 17bf48f9a..67c554491 100644 --- a/packages/app/tests/e2e/features/search/column-fit.spec.ts +++ b/packages/app/tests/e2e/features/search/column-fit.spec.ts @@ -1,3 +1,4 @@ +import { TableComponent } from '../../components/TableComponent'; import { SearchPage } from '../../page-objects/SearchPage'; import { expect, test } from '../../utils/base-test'; @@ -105,4 +106,40 @@ test.describe('Search results column fit', { tag: '@search' }, () => { ); }, ); + + test( + 'fits a column of the surrounding context table, which does not save its widths', + { tag: ['@local-mode'] }, + async ({ page }) => { + await searchPage.table.clickFirstRow(); + await expect(searchPage.sidePanel.tabs).toBeVisible(); + await searchPage.sidePanel.clickTab('context'); + const contextTable = new TableComponent( + page, + '[data-testid="side-panel-tab-context"] [data-testid="search-results-table"]', + ); + await expect( + contextTable.container + .locator('tbody [data-column-id="ServiceName"]') + .first(), + ).toBeVisible(); + await contextTable.dragColumnResizeHandle('ServiceName', -100); + await expect + .poll(() => contextTable.countTruncatedCells('ServiceName')) + .toBeGreaterThan(0); + // The last column takes the space the squeezed column gave up + await expect + .poll(() => contextTable.getEmptyWidthRightOfColumns()) + .toBeLessThan(1); + + await contextTable.fitColumnToContent('ServiceName'); + + await expect + .poll(() => contextTable.countTruncatedCells('ServiceName')) + .toBe(0); + await expect + .poll(() => contextTable.getEmptyWidthRightOfColumns()) + .toBeLessThan(1); + }, + ); });