From c773242555dab1008e68644f82e795e25230c9bd Mon Sep 17 00:00:00 2001 From: Sarah Medeiros Date: Wed, 7 Dec 2022 14:08:46 -0500 Subject: [PATCH] Remove check from useMemo in order to keep function pure Signed-off-by: Sarah Medeiros --- .../EntityLifecyclePicker.tsx | 38 +++++++-------- .../EntityOwnerPicker/EntityOwnerPicker.tsx | 46 +++++++++---------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/plugins/catalog-react/src/components/EntityLifecyclePicker/EntityLifecyclePicker.tsx b/plugins/catalog-react/src/components/EntityLifecyclePicker/EntityLifecyclePicker.tsx index 693c8ab85c..dda24c3639 100644 --- a/plugins/catalog-react/src/components/EntityLifecyclePicker/EntityLifecyclePicker.tsx +++ b/plugins/catalog-react/src/components/EntityLifecyclePicker/EntityLifecyclePicker.tsx @@ -67,14 +67,6 @@ export const EntityLifecyclePicker = () => { : filters.lifecycles?.values ?? [], ); - // Set selected lifecycles on query parameter updates; this happens at initial page load and from - // external updates to the page location. - useEffect(() => { - if (queryParamLifecycles.length) { - setSelectedLifecycles(queryParamLifecycles); - } - }, [queryParamLifecycles]); - useEffect(() => { updateFilters({ lifecycles: selectedLifecycles.length @@ -83,17 +75,25 @@ export const EntityLifecyclePicker = () => { }); }, [selectedLifecycles, updateFilters]); - const availableLifecycles = useMemo(() => { - const lifecycles = [ - ...new Set( - backendEntities - .map((e: Entity) => e.spec?.lifecycle) - .filter(Boolean) as string[], - ), - ].sort(); - if (lifecycles.length === 0) setSelectedLifecycles([]); - return lifecycles; - }, [backendEntities]); + const availableLifecycles = useMemo( + () => + [ + ...new Set( + backendEntities + .map((e: Entity) => e.spec?.lifecycle) + .filter(Boolean) as string[], + ), + ].sort(), + [backendEntities], + ); + + // Set selected lifecycles on query parameter updates; this happens at initial page load and from + // external updates to the page location. + useEffect(() => { + if (queryParamLifecycles.length && availableLifecycles.length) { + setSelectedLifecycles(queryParamLifecycles); + } + }, [queryParamLifecycles, availableLifecycles]); if (!availableLifecycles.length) return null; diff --git a/plugins/catalog-react/src/components/EntityOwnerPicker/EntityOwnerPicker.tsx b/plugins/catalog-react/src/components/EntityOwnerPicker/EntityOwnerPicker.tsx index d389ead06a..3804324619 100644 --- a/plugins/catalog-react/src/components/EntityOwnerPicker/EntityOwnerPicker.tsx +++ b/plugins/catalog-react/src/components/EntityOwnerPicker/EntityOwnerPicker.tsx @@ -67,14 +67,6 @@ export const EntityOwnerPicker = () => { queryParamOwners.length ? queryParamOwners : filters.owners?.values ?? [], ); - // Set selected owners on query parameter updates; this happens at initial page load and from - // external updates to the page location. - useEffect(() => { - if (queryParamOwners.length) { - setSelectedOwners(queryParamOwners); - } - }, [queryParamOwners]); - useEffect(() => { updateFilters({ owners: selectedOwners.length @@ -83,21 +75,29 @@ export const EntityOwnerPicker = () => { }); }, [selectedOwners, updateFilters]); - const availableOwners = useMemo(() => { - const owners = [ - ...new Set( - backendEntities - .flatMap((e: Entity) => - getEntityRelations(e, RELATION_OWNED_BY).map(o => - humanizeEntityRef(o, { defaultKind: 'group' }), - ), - ) - .filter(Boolean) as string[], - ), - ].sort(); - if (owners.length === 0) setSelectedOwners([]); - return owners; - }, [backendEntities]); + const availableOwners = useMemo( + () => + [ + ...new Set( + backendEntities + .flatMap((e: Entity) => + getEntityRelations(e, RELATION_OWNED_BY).map(o => + humanizeEntityRef(o, { defaultKind: 'group' }), + ), + ) + .filter(Boolean) as string[], + ), + ].sort(), + [backendEntities], + ); + + // Set selected owners on query parameter updates; this happens at initial page load and from + // external updates to the page location. + useEffect(() => { + if (queryParamOwners.length && availableOwners.length) { + setSelectedOwners(queryParamOwners); + } + }, [queryParamOwners, availableOwners]); if (!availableOwners.length) return null;