import { useState } from 'react'; import { TableSettingsProvider, useTableSettings, } from '@/portainer/components/datatables/components/useTableSettings'; import { useEnvironmentList } from '@/portainer/environments/queries/useEnvironmentList'; import { Environment } from '@/portainer/environments/types'; import { useSearchBarState } from '@/portainer/components/datatables/components/SearchBar'; import { useDebounce } from '@/portainer/hooks/useDebounce'; import { EdgeDevicesDatatable, EdgeDevicesTableProps, } from './EdgeDevicesDatatable'; import { EdgeDeviceTableSettings, Pagination } from './types'; export function EdgeDevicesDatatableContainer({ ...props }: Omit< EdgeDevicesTableProps, | 'dataset' | 'pagination' | 'onChangePagination' | 'totalCount' | 'search' | 'onChangeSearch' >) { const defaultSettings = { autoRefreshRate: 0, hiddenQuickActions: [], hiddenColumns: [], pageSize: 10, sortBy: { id: 'state', desc: false }, }; const storageKey = 'edgeDevices'; return ( {({ environments, pagination, totalCount, setPagination, search, setSearch, }) => ( )} ); } interface LoaderProps { storageKey: string; children: (options: { environments: Environment[]; totalCount: number; pagination: Pagination; setPagination(value: Partial): void; search: string; setSearch: (value: string) => void; }) => React.ReactNode; } function Loader({ children, storageKey }: LoaderProps) { const { settings } = useTableSettings(); const [pagination, setPagination] = useState({ pageLimit: settings.pageSize, page: 1, }); const [search, setSearch] = useSearchBarState(storageKey); const debouncedSearchValue = useDebounce(search); const { environments, isLoading, totalCount } = useEnvironmentList( { edgeDeviceFilter: 'trusted', search: debouncedSearchValue, ...pagination, }, settings.autoRefreshRate * 1000 ); if (isLoading) { return null; } return ( <> {children({ environments, totalCount, pagination, setPagination: handleSetPagination, search, setSearch, })} ); function handleSetPagination(value: Partial) { setPagination((prev) => ({ ...prev, ...value })); } }