From 530d96f7166bb8db366aec0efb5e2fc94eec5857 Mon Sep 17 00:00:00 2001 From: Mahendra Suthar Date: Wed, 7 Aug 2024 21:55:17 +0530 Subject: [PATCH] catalog-graph: refactor useCatalogGraphPage hook remove setSearchParam and use qs.stringify and navigate to set search params Signed-off-by: Mahendra Suthar --- .../CatalogGraphPage/useCatalogGraphPage.ts | 63 +++++++------------ 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/plugins/catalog-graph/src/components/CatalogGraphPage/useCatalogGraphPage.ts b/plugins/catalog-graph/src/components/CatalogGraphPage/useCatalogGraphPage.ts index f5f50e25dd..1f6fb74997 100644 --- a/plugins/catalog-graph/src/components/CatalogGraphPage/useCatalogGraphPage.ts +++ b/plugins/catalog-graph/src/components/CatalogGraphPage/useCatalogGraphPage.ts @@ -27,12 +27,12 @@ import { useMemo, useState, } from 'react'; -import { useLocation, useSearchParams } from 'react-router-dom'; +import { useLocation, useNavigate } from 'react-router-dom'; import { Direction } from '../EntityRelationsGraph'; export type CatalogGraphPageValue = { rootEntityNames: CompoundEntityRef[]; - setRootEntityNames: Dispatch>; + setRootEntityNames: (value: CompoundEntityRef[]) => void; maxDepth: number; setMaxDepth: Dispatch>; selectedRelations: string[] | undefined; @@ -69,7 +69,8 @@ export function useCatalogGraphPage({ }; }): CatalogGraphPageValue { const location = useLocation(); - const [_, setSearchParams] = useSearchParams(); + const navigate = useNavigate(); + const query = useMemo( () => (qs.parse(location.search, { arrayLimit: 0, ignoreQueryPrefix: true }) || @@ -97,35 +98,28 @@ export function useCatalogGraphPage({ ); const setRootEntityNames = useCallback( - (value: React.SetStateAction) => { - const prev = Array.isArray(query.rootEntityRefs) - ? query.rootEntityRefs - : []; + (value: CompoundEntityRef[]) => { + const areSame = + rootEntityNames.length === value.length && + rootEntityNames.every( + (r, i) => stringifyEntityRef(r) === stringifyEntityRef(value[i]), + ); - const hasChanged = - Array.isArray(value) && - (value.length !== prev.length || - value.some((r, i) => stringifyEntityRef(r) !== prev[i])); - - if (!hasChanged) { + if (areSame) { return; } - setSearchParams(params => { - const rootEntityRefs = Array.isArray(value) - ? value.map(r => stringifyEntityRef(r)) - : []; + const newSearch = qs.stringify( + { + ...query, + rootEntityRefs: value.map(r => stringifyEntityRef(r)), + }, + { arrayFormat: 'brackets', addQueryPrefix: true }, + ); - params.delete('rootEntityRefs[]'); - - rootEntityRefs.forEach(r => { - params.set('rootEntityRefs[]', r); - }); - - return params; - }); + navigate(newSearch); }, - [query.rootEntityRefs, setSearchParams], + [rootEntityNames, navigate, query], ); const [maxDepth, setMaxDepth] = useState(() => @@ -187,6 +181,7 @@ export function useCatalogGraphPage({ useEffect(() => { const newParams = qs.stringify( { + rootEntityRefs: rootEntityNames.map(stringifyEntityRef), maxDepth: isFinite(maxDepth) ? maxDepth : '∞', selectedKinds, selectedRelations, @@ -199,18 +194,7 @@ export function useCatalogGraphPage({ { arrayFormat: 'brackets', addQueryPrefix: true }, ); - const searchParams = new URLSearchParams(newParams); - - setSearchParams( - params => { - searchParams.forEach((value, key) => { - params.set(key, value); - }); - - return params; - }, - { replace: true }, - ); + navigate(newParams, { replace: true }); }, [ maxDepth, curve, @@ -220,7 +204,8 @@ export function useCatalogGraphPage({ mergeRelations, direction, showFilters, - setSearchParams, + rootEntityNames, + navigate, ]); return {