1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-06 01:15:28 +02:00
unleash.unleash/frontend/src/component/segments/SegmentTable.tsx
Nuno Góis 233e06ec6a
fix: disable autoResetHiddenColumns when useConditionallyHiddenColumns (#2851)
https://linear.app/unleash/issue/2-563/fix-issue-with-useconditionallyhiddencolumns-and-react-table

It seems like we should add `autoResetHiddenColumns: false` to
`useTable` whenever we use `useConditionallyHiddenColumns`.

Basically the thought is that, if we're controlling column visibility in
our own way, we should not want other things to change that state
unpredictably, otherwise this may make React go _brrrrrr_. And it can be
very hard to pinpoint what exactly may be causing React to go _brrrrrr_.


![image](https://user-images.githubusercontent.com/14320932/211332339-95918c5c-e3ea-40e9-b8b4-756a798a4702.png)

First detected this issue apparently randomly while developing the new
SA table. Around 10-20 page refreshes would eventually trigger it. Was
not easy to find, but hopefully this fixes it permanently. At least I
haven't been able to reproduce it since. Maybe someone has a better idea
of where the issue could be or if this is a pretty good guess. Doesn't
seem like this change hurts us anyways.

I love React, `useEffect` and these very to-the-point error messages.
Very fun and productive.

Reference: https://react-table-v7.tanstack.com/docs/api/useTable
2023-01-10 08:15:12 +00:00

202 lines
6.9 KiB
TypeScript

import { PageContent } from 'component/common/PageContent/PageContent';
import { PageHeader } from 'component/common/PageHeader/PageHeader';
import {
SortableTableHeader,
TableCell,
TablePlaceholder,
Table,
TableBody,
TableRow,
} from 'component/common/Table';
import { useTable, useGlobalFilter, useSortBy } from 'react-table';
import { CreateSegmentButton } from 'component/segments/CreateSegmentButton/CreateSegmentButton';
import { SearchHighlightProvider } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext';
import { useMediaQuery } from '@mui/material';
import { sortTypes } from 'utils/sortTypes';
import { useSegments } from 'hooks/api/getters/useSegments/useSegments';
import { useMemo, useState } from 'react';
import { SegmentEmpty } from 'component/segments/SegmentEmpty';
import { IconCell } from 'component/common/Table/cells/IconCell/IconCell';
import { DonutLarge } from '@mui/icons-material';
import { SegmentActionCell } from 'component/segments/SegmentActionCell/SegmentActionCell';
import { HighlightCell } from 'component/common/Table/cells/HighlightCell/HighlightCell';
import { DateCell } from 'component/common/Table/cells/DateCell/DateCell';
import theme from 'themes/theme';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { Search } from 'component/common/Search/Search';
import { useConditionallyHiddenColumns } from 'hooks/useConditionallyHiddenColumns';
export const SegmentTable = () => {
const { segments, loading } = useSegments();
const isSmallScreen = useMediaQuery(theme.breakpoints.down('md'));
const [initialState] = useState({
sortBy: [{ id: 'createdAt' }],
hiddenColumns: ['description'],
});
const data = useMemo(() => {
return (
segments ??
Array(5).fill({
name: 'Segment name',
description: 'Segment descripton',
createdAt: new Date().toISOString(),
createdBy: 'user',
})
);
}, [segments]);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
state: { globalFilter },
setGlobalFilter,
setHiddenColumns,
} = useTable(
{
initialState,
columns: COLUMNS as any,
data: data as any,
sortTypes,
autoResetGlobalFilter: false,
autoResetHiddenColumns: false,
autoResetSortBy: false,
disableSortRemove: true,
defaultColumn: {
Cell: HighlightCell,
},
},
useGlobalFilter,
useSortBy
);
useConditionallyHiddenColumns(
[
{
condition: isSmallScreen,
columns: ['createdAt', 'createdBy'],
},
],
setHiddenColumns,
COLUMNS
);
return (
<PageContent
header={
<PageHeader
title={`Segments (${rows.length})`}
actions={
<>
<Search
initialValue={globalFilter}
onChange={setGlobalFilter}
/>
<PageHeader.Divider />
<CreateSegmentButton />
</>
}
/>
}
isLoading={loading}
>
<ConditionallyRender
condition={!loading && data.length === 0}
show={
<TablePlaceholder>
<SegmentEmpty />
</TablePlaceholder>
}
elseShow={() => (
<>
<SearchHighlightProvider value={globalFilter}>
<Table {...getTableProps()} rowHeight="standard">
<SortableTableHeader
headerGroups={headerGroups as any}
/>
<TableBody {...getTableBodyProps()}>
{rows.map(row => {
prepareRow(row);
return (
<TableRow
hover
{...row.getRowProps()}
>
{row.cells.map(cell => (
<TableCell
{...cell.getCellProps()}
>
{cell.render('Cell')}
</TableCell>
))}
</TableRow>
);
})}
</TableBody>
</Table>
</SearchHighlightProvider>
<ConditionallyRender
condition={
rows.length === 0 && globalFilter?.length > 0
}
show={
<TablePlaceholder>
No segments found matching &ldquo;
{globalFilter}&rdquo;
</TablePlaceholder>
}
/>
</>
)}
/>
</PageContent>
);
};
const COLUMNS = [
{
id: 'Icon',
width: '1%',
disableGlobalFilter: true,
disableSortBy: true,
Cell: () => <IconCell icon={<DonutLarge color="disabled" />} />,
},
{
Header: 'Name',
accessor: 'name',
width: '60%',
Cell: ({ value, row: { original } }: any) => (
<HighlightCell value={value} subtitle={original.description} />
),
},
{
Header: 'Created at',
accessor: 'createdAt',
minWidth: 150,
Cell: DateCell,
disableGlobalFilter: true,
},
{
Header: 'Created by',
accessor: 'createdBy',
width: '25%',
},
{
Header: 'Actions',
id: 'Actions',
align: 'center',
width: '1%',
disableSortBy: true,
disableGlobalFilter: true,
Cell: ({ row: { original } }: any) => (
<SegmentActionCell segment={original} />
),
},
{
accessor: 'description',
},
];