import { Column, useGlobalFilter, usePagination, useRowSelect, useSortBy, useTable, } from 'react-table'; import { useRowSelectColumn } from '@lineup-lite/hooks'; import { Environment } from '@/react/portainer/environments/types'; import { notifySuccess } from '@/portainer/services/notifications'; import { Button } from '@@/buttons'; import { Table } from '@@/datatables'; import { SearchBar, useSearchBarState } from '@@/datatables/SearchBar'; import { SelectedRowsCount } from '@@/datatables/SelectedRowsCount'; import { PaginationControls } from '@@/PaginationControls'; import { useTableSettings } from '@@/datatables/useTableSettings'; import { useAssociateDeviceMutation } from '../queries'; import { TableSettings } from './types'; const columns: readonly Column[] = [ { Header: 'Name', accessor: (row) => row.Name, id: 'name', disableFilters: true, Filter: () => null, canHide: false, sortType: 'string', }, { Header: 'Edge ID', accessor: (row) => row.EdgeID, id: 'edge-id', disableFilters: true, Filter: () => null, canHide: false, sortType: 'string', }, ] as const; interface Props { devices: Environment[]; isLoading: boolean; totalCount: number; storageKey: string; } export function DataTable({ devices, storageKey, isLoading, totalCount, }: Props) { const associateMutation = useAssociateDeviceMutation(); const [searchBarValue, setSearchBarValue] = useSearchBarState(storageKey); const { settings, setTableSettings } = useTableSettings(); const { getTableProps, getTableBodyProps, headerGroups, page, prepareRow, selectedFlatRows, gotoPage, setPageSize, setGlobalFilter, state: { pageIndex, pageSize }, } = useTable( { defaultCanFilter: false, columns, data: devices, initialState: { pageSize: settings.pageSize || 10, sortBy: [settings.sortBy], globalFilter: searchBarValue, }, isRowSelectable() { return true; }, autoResetSelectedRows: false, getRowId(originalRow: Environment) { return originalRow.Id.toString(); }, selectColumnWidth: 5, }, useGlobalFilter, useSortBy, usePagination, useRowSelect, useRowSelectColumn ); const tableProps = getTableProps(); const tbodyProps = getTableBodyProps(); return (
{headerGroups.map((headerGroup) => { const { key, className, role, style } = headerGroup.getHeaderGroupProps(); return ( key={key} className={className} role={role} style={style} headers={headerGroup.headers} onSortChange={handleSortChange} /> ); })} ( )} />
gotoPage(p - 1)} totalCount={totalCount} onPageLimitChange={handlePageLimitChange} />
); function handleSortChange(colId: string, desc: boolean) { setTableSettings({ sortBy: { id: colId, desc } }); } function handlePageLimitChange(pageSize: number) { setPageSize(pageSize); setTableSettings({ pageSize }); } function handleSearchBarChange(value: string) { setGlobalFilter(value); setSearchBarValue(value); } function handleAssociateDevice(devices: Environment[]) { associateMutation.mutate( devices.map((d) => d.Id), { onSuccess() { notifySuccess('Success', 'Edge devices associated successfully'); }, } ); } }