Remove check from useMemo in order to keep function pure

Signed-off-by: Sarah Medeiros <smedeiros@wayfair.com>
This commit is contained in:
Sarah Medeiros
2022-12-07 14:08:46 -05:00
parent 4e65bd9a58
commit c773242555
2 changed files with 42 additions and 42 deletions
@@ -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;
@@ -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;