import { usePagination, useTable } from 'react-table'; import { Device } from '@/portainer/hostmanagement/open-amt/model'; import { useAMTDevices } from '@/edge/EdgeDevices/EdgeDevicesView/AMTDevicesDatatable/useAMTDevices'; import { RowProvider } from '@/edge/EdgeDevices/EdgeDevicesView/AMTDevicesDatatable/columns/RowContext'; import { EnvironmentId } from '@/portainer/environments/types'; import PortainerError from '@/portainer/error'; import { InnerDatatable } from '@@/datatables/InnerDatatable'; import { Table, TableContainer, TableHeaderRow, TableRow } from '@@/datatables'; import { useColumns } from './columns'; export interface AMTDevicesTableProps { environmentId: EnvironmentId; } export function AMTDevicesDatatable({ environmentId }: AMTDevicesTableProps) { const columns = useColumns(); const { isLoading, devices, error } = useAMTDevices(environmentId); const { getTableProps, getTableBodyProps, headerGroups, page, prepareRow } = useTable( { columns, data: devices, }, usePagination ); 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} /> ); })} {!isLoading && devices && devices.length > 0 ? ( page.map((row) => { prepareRow(row); const { key, className, role, style } = row.getRowProps(); return ( cells={row.cells} key={key} className={className} role={role} style={style} /> ); }) ) : ( )}
{userMessage(isLoading, error)}
); } function userMessage(isLoading: boolean, error?: PortainerError) { if (isLoading) { return 'Loading...'; } if (error) { return error.message; } return 'No devices found'; }