import { Column, useGlobalFilter, usePagination, useRowSelect, useSortBy, useTable, } from 'react-table'; import { useRowSelectColumn } from '@lineup-lite/hooks'; import { Button } from '@/portainer/components/Button'; import { Table } from '@/portainer/components/datatables/components'; import { SearchBar, useSearchBarState, } from '@/portainer/components/datatables/components/SearchBar'; import { SelectedRowsCount } from '@/portainer/components/datatables/components/SelectedRowsCount'; import { PaginationControls } from '@/portainer/components/pagination-controls'; import { Environment } from '@/portainer/environments/types'; import { useTableSettings } from '@/portainer/components/datatables/components/useTableSettings'; import { notifySuccess } from '@/portainer/services/notifications'; 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('Edge devices associated successfully'); }, } ); } }