From 577a36e04e9b49a303823a5e464dfd6ce6a23de5 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Tue, 15 Aug 2023 06:05:14 +0300 Subject: [PATCH] fix(edge/devices): search waiting room devices [EE-5895] (#10015) --- app/react/components/datatables/Datatable.tsx | 5 +- .../WaitingRoomView/Datatable/Datatable.tsx | 18 ++- .../Datatable/useEnvironments.ts | 122 +++++++++++++----- 3 files changed, 108 insertions(+), 37 deletions(-) diff --git a/app/react/components/datatables/Datatable.tsx b/app/react/components/datatables/Datatable.tsx index 6f34c886f..3b4728978 100644 --- a/app/react/components/datatables/Datatable.tsx +++ b/app/react/components/datatables/Datatable.tsx @@ -54,6 +54,7 @@ export interface Props< description?: ReactNode; pageCount?: number; highlightedItemId?: string; + page?: number; onPageChange?(page: number): void; settingsManager: GlobalTableState; @@ -82,6 +83,7 @@ export function Datatable< totalCount = dataset.length, description, pageCount, + page, onPageChange = () => null, settingsManager: settings, renderRow = defaultRenderRow, @@ -108,6 +110,7 @@ export function Datatable< initialState: { pagination: { pageSize: settings.pageSize, + pageIndex: page, }, sorting: settings.sortBy ? [settings.sortBy] : [], globalFilter: settings.search, @@ -172,7 +175,7 @@ export function Datatable< } /> ); diff --git a/app/react/edge/edge-devices/WaitingRoomView/Datatable/useEnvironments.ts b/app/react/edge/edge-devices/WaitingRoomView/Datatable/useEnvironments.ts index f644a8ef6..dcf0da69d 100644 --- a/app/react/edge/edge-devices/WaitingRoomView/Datatable/useEnvironments.ts +++ b/app/react/edge/edge-devices/WaitingRoomView/Datatable/useEnvironments.ts @@ -1,4 +1,5 @@ import _ from 'lodash'; +import { useEffect, useMemo, useState } from 'react'; import { useTags } from '@/portainer/tags/queries'; import { useEdgeGroups } from '@/react/edge/edge-groups/queries/useEdgeGroups'; @@ -10,27 +11,59 @@ import { WaitingRoomEnvironment } from '../types'; import { useFilterStore } from './filter-store'; -export function useEnvironments() { +export function useEnvironments({ + pageLimit = 10, + search, +}: { + pageLimit: number; + search: string; +}) { + const [page, setPage] = useState(0); const filterStore = useFilterStore(); const edgeGroupsQuery = useEdgeGroups(); - const filterByEnvironmentsIds = filterStore.edgeGroups.length - ? _.compact( - filterStore.edgeGroups.flatMap( - (groupId) => - edgeGroupsQuery.data?.find((g) => g.Id === groupId)?.Endpoints - ) - ) - : undefined; + const filterByEnvironmentsIds = useMemo( + () => + filterStore.edgeGroups.length + ? _.compact( + filterStore.edgeGroups.flatMap( + (groupId) => + edgeGroupsQuery.data?.find((g) => g.Id === groupId)?.Endpoints + ) + ) + : undefined, + [edgeGroupsQuery.data, filterStore.edgeGroups] + ); + + const query = useMemo( + () => ({ + pageLimit, + edgeDeviceUntrusted: true, + excludeSnapshots: true, + types: EdgeTypes, + tagIds: filterStore.tags.length ? filterStore.tags : undefined, + groupIds: filterStore.groups.length ? filterStore.groups : undefined, + endpointIds: filterByEnvironmentsIds, + edgeCheckInPassedSeconds: filterStore.checkIn, + search, + }), + [ + filterByEnvironmentsIds, + filterStore.checkIn, + filterStore.groups, + filterStore.tags, + pageLimit, + search, + ] + ); + + useEffect(() => { + setPage(0); + }, [query]); const environmentsQuery = useEnvironmentList({ - edgeDeviceUntrusted: true, - excludeSnapshots: true, - types: EdgeTypes, - tagIds: filterStore.tags.length ? filterStore.tags : undefined, - groupIds: filterStore.groups.length ? filterStore.groups : undefined, - endpointIds: filterByEnvironmentsIds, - edgeCheckInPassedSeconds: filterStore.checkIn, + page: page + 1, + ...query, }); const groupsQuery = useGroups({ @@ -52,24 +85,45 @@ export function useEnvironments() { Object.fromEntries(tags.map((tag) => [tag.ID, tag.Name] as const)), }); - const envs: Array = - environmentsQuery.environments.map((env) => ({ - ...env, - Group: (env.GroupId !== 1 && groupsQuery.data?.[env.GroupId]) || '', - EdgeGroups: - environmentEdgeGroupsQuery.data?.[env.Id]?.map((env) => env.group) || - [], - Tags: - _.compact(env.TagIds?.map((tagId) => tagsQuery.data?.[tagId])) || [], - })); + const envs: Array = useMemo( + () => + environmentsQuery.environments.map((env) => ({ + ...env, + Group: (env.GroupId !== 1 && groupsQuery.data?.[env.GroupId]) || '', + EdgeGroups: + environmentEdgeGroupsQuery.data?.[env.Id]?.map((env) => env.group) || + [], + Tags: + _.compact(env.TagIds?.map((tagId) => tagsQuery.data?.[tagId])) || [], + })), + [ + environmentEdgeGroupsQuery.data, + environmentsQuery.environments, + groupsQuery.data, + tagsQuery.data, + ] + ); - return { - data: envs, - isLoading: - environmentsQuery.isLoading || - groupsQuery.isLoading || - environmentEdgeGroupsQuery.isLoading || + return useMemo( + () => ({ + data: envs, + isLoading: + environmentsQuery.isLoading || + groupsQuery.isLoading || + environmentEdgeGroupsQuery.isLoading || + tagsQuery.isLoading, + totalCount: environmentsQuery.totalCount, + page, + setPage, + }), + [ + environmentEdgeGroupsQuery.isLoading, + environmentsQuery.isLoading, + environmentsQuery.totalCount, + envs, + groupsQuery.isLoading, + page, tagsQuery.isLoading, - totalCount: environmentsQuery.totalCount, - }; + ] + ); }