From 76832db4cebaf808db468fe9fe346ad5d12a9b01 Mon Sep 17 00:00:00 2001 From: karthik Date: Wed, 4 May 2022 21:21:16 +0530 Subject: [PATCH] add debounce to tektonhub versions api call to avoid many calls --- .../PipelineQuickSearchDetails.tsx | 33 ++++--- .../PipelineQuicksearchDetails.spec.tsx | 94 +++++++++++++++++-- 2 files changed, 106 insertions(+), 21 deletions(-) diff --git a/frontend/packages/pipelines-plugin/src/components/quicksearch/PipelineQuickSearchDetails.tsx b/frontend/packages/pipelines-plugin/src/components/quicksearch/PipelineQuickSearchDetails.tsx index e2cab7192a3..c67c0740b2c 100644 --- a/frontend/packages/pipelines-plugin/src/components/quicksearch/PipelineQuickSearchDetails.tsx +++ b/frontend/packages/pipelines-plugin/src/components/quicksearch/PipelineQuickSearchDetails.tsx @@ -14,6 +14,7 @@ import { Title, } from '@patternfly/react-core'; import { CheckCircleIcon } from '@patternfly/react-icons'; +import { debounce } from 'lodash'; import { useTranslation } from 'react-i18next'; import { handleCta } from '@console/shared'; import { QuickSearchDetailsRendererProps } from '@console/shared/src/components/quick-search/QuickSearchDetails'; @@ -53,20 +54,26 @@ const PipelineQuickSearchDetails: React.FC = ({ resetVersions(); let mounted = true; if (isTektonHubTaskWithoutVersions(selectedItem)) { - getTektonHubTaskVersions(selectedItem.data.id) - .then((itemVersions = []) => { - if (mounted) { - setVersions([...itemVersions]); + const debouncedLoadVersions = debounce(async () => { + if (mounted) { + try { + const itemVersions = await getTektonHubTaskVersions(selectedItem?.data?.id); + selectedItem.attributes.versions = itemVersions; - setHasInstalledVersion(isOneVersionInstalled(selectedItem)); - } - }) - .catch((err) => { - if (mounted) { - resetVersions(); + + if (mounted) { + setVersions([...itemVersions]); + setHasInstalledVersion(isOneVersionInstalled(selectedItem)); + } + } catch (err) { + if (mounted) { + resetVersions(); + } + console.log('failed to fetch versions:', err); // eslint-disable-line no-console } - console.log('failed to fetch versions:', err); // eslint-disable-line no-console - }); + } + }, 10); + debouncedLoadVersions(); } return () => (mounted = false); @@ -97,7 +104,7 @@ const PipelineQuickSearchDetails: React.FC = ({