Skip to content

Commit

Permalink
perf: Do not use another query to fetch themes and observations
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Oct 26, 2023
1 parent 15d0b76 commit 718fa14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
67 changes: 34 additions & 33 deletions app/browser/dataset-browse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ import {
DataCubeTheme,
SearchCubeResultOrder,
SearchCubesQuery,
useOrganizationsQuery,
useThemesQuery,
} from "@/graphql/query-hooks";
import {
DataCubePublicationStatus,
Expand All @@ -49,8 +47,6 @@ import {
import SvgIcCategories from "@/icons/components/IcCategories";
import SvgIcClose from "@/icons/components/IcClose";
import SvgIcOrganisations from "@/icons/components/IcOrganisations";
import { useLocale } from "@/locales/use-locale";
import { useDataSourceStore } from "@/stores/data-source";
import isAttrEqual from "@/utils/is-attr-equal";
import useEvent from "@/utils/use-event";

Expand Down Expand Up @@ -532,6 +528,7 @@ const NavSection = ({
);
}, [counts, items]);
const { isOpen, open, close } = useDisclosure();

return (
<div>
<NavSectionTitle theme={theme} sx={{ mb: "block" }}>
Expand All @@ -552,7 +549,7 @@ const NavSection = ({
return (
<Reorder.Item drag={false} value={item} key={item.iri} as="div">
<NavItem
active={currentFilter === item}
active={currentFilter ? currentFilter.iri === item.iri : false}
filters={filters}
next={item}
count={counts[item.iri]}
Expand Down Expand Up @@ -600,29 +597,36 @@ const NavSection = ({
);
};

export const SearchFilters = ({ cubes }: { cubes: SearchCubeResult[] }) => {
const { dataSource } = useDataSourceStore();
const locale = useLocale();
export const SearchFilters = ({
allCubes,
}: {
allCubes: SearchCubeResult[];
}) => {
const { filters } = useBrowseContext();
const [{ data: allThemes }] = useThemesQuery({
variables: {
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
},
});
const [{ data: allOrgs }] = useOrganizationsQuery({
variables: {
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
},
});
const { allThemes, allOrgs } = React.useMemo(() => {
return {
allThemes: uniqBy(
allCubes.flatMap((d) =>
d.cube.themes.map((d) => ({ __typename: "DataCubeTheme", ...d }))
),
(d) => d.iri
) as DataCubeTheme[],
allOrgs: uniqBy(
allCubes
.filter((d) => d.cube.creator)
.flatMap((d) => ({
__typename: "DataCubeOrganization",
...d.cube.creator,
})),
(d) => d!.iri
) as DataCubeOrganization[],
};
}, [allCubes]);

const counts = useMemo(() => {
const result: Record<string, number> = {};

for (const { cube } of cubes) {
for (const { cube } of allCubes) {
const countables = [
...cube.themes,
...cube.subthemes,
Expand All @@ -637,18 +641,15 @@ export const SearchFilters = ({ cubes }: { cubes: SearchCubeResult[] }) => {
}

return result;
}, [cubes]);
}, [allCubes]);

const themeFilter = filters.find(isAttrEqual("__typename", "DataCubeTheme"));
const orgFilter = filters.find(
isAttrEqual("__typename", "DataCubeOrganization")
);

const [allThemesAlpha, allOrgsAlpha] = useMemo(() => {
return [
allThemes ? sortBy(allThemes.themes, (x) => x?.label) : null,
allOrgs ? sortBy(allOrgs.organizations, (x) => x?.label) : null,
];
return [sortBy(allThemes, (d) => d.label), sortBy(allOrgs, (d) => d.label)];
}, [allThemes, allOrgs]);

const displayedThemes = allThemesAlpha?.filter((theme) => {
Expand All @@ -660,7 +661,7 @@ export const SearchFilters = ({ cubes }: { cubes: SearchCubeResult[] }) => {
return false;
}

if (themeFilter && themeFilter !== theme) {
if (themeFilter && themeFilter.iri !== theme.iri) {
return false;
}

Expand All @@ -672,11 +673,11 @@ export const SearchFilters = ({ cubes }: { cubes: SearchCubeResult[] }) => {
return false;
}

if (!counts[org.iri] && orgFilter !== org) {
if (!counts[org.iri] && orgFilter && orgFilter.iri !== org.iri) {
return false;
}

if (orgFilter && orgFilter !== org) {
if (orgFilter && orgFilter.iri !== org.iri) {
return false;
}

Expand All @@ -703,10 +704,10 @@ export const SearchFilters = ({ cubes }: { cubes: SearchCubeResult[] }) => {

const subthemes = React.useMemo(() => {
return uniqBy(
cubes.flatMap((d) => d.cube.subthemes),
allCubes.flatMap((d) => d.cube.subthemes),
(d) => d.iri
);
}, [cubes]);
}, [allCubes]);

const orgNav =
displayedOrgs && displayedOrgs.length > 0 ? (
Expand Down
2 changes: 1 addition & 1 deletion app/browser/select-dataset-step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ const SelectDatasetStepContent = () => {
</MotionBox>
) : (
<MotionBox key="search-filters" {...navPresenceProps}>
<SearchFilters cubes={allCubes} />
<SearchFilters allCubes={allCubes} />
</MotionBox>
)}
</AnimatePresence>
Expand Down

0 comments on commit 718fa14

Please sign in to comment.