diff --git a/app/browser/dataset-browse.tsx b/app/browser/dataset-browse.tsx index 303310bdb..567c68467 100644 --- a/app/browser/dataset-browse.tsx +++ b/app/browser/dataset-browse.tsx @@ -58,7 +58,6 @@ import { useBrowseContext, } from "./context"; import { BrowseFilter } from "./filters"; -import useDatasetCount from "./use-dataset-count"; export const SearchDatasetInput = ({ browseState, @@ -433,7 +432,7 @@ export const Subthemes = ({ }: { organization: DataCubeOrganization; filters: BrowseFilter[]; - counts: ReturnType; + counts: Record; }) => { const termsetIri = organizationIriToTermsetParentIri[organization.iri]; const { dataSource } = useDataSourceStore(); @@ -606,7 +605,7 @@ const NavSection = ({ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => { const { dataSource } = useDataSourceStore(); const locale = useLocale(); - const { filters, search, includeDrafts } = useBrowseContext(); + const { filters } = useBrowseContext(); const [{ data: allThemes }] = useThemesQuery({ variables: { sourceType: dataSource.type, @@ -622,29 +621,23 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => { }, }); - const allCounts = useDatasetCount(filters, includeDrafts); - const resultsCounts = useMemo(() => { + const counts = useMemo(() => { if (!data?.dataCubes) { return {}; - } else { - const res = {} as Record; - for (const cube of data.dataCubes) { - const countables = [ - ...cube.dataCube.themes, - cube.dataCube.creator, - ].filter(truthy); - for (const item of countables) { - res[item.iri] = res[item.iri] || 0; - res[item.iri] += 1; - } + } + + const result: Record = {}; + + for (const { dataCube } of data.dataCubes) { + const countables = [...dataCube.themes, dataCube.creator].filter(truthy); + + for (const { iri } of countables) { + result[iri] = (result[iri] ?? 0) + 1; } - return res; } - }, [data]); - const total = Object.values(resultsCounts).reduce((acc, n) => acc + n, 0); - const counts = - search && search != "" && total > 0 ? resultsCounts : allCounts; + return result; + }, [data?.dataCubes]); const themeFilter = filters.find(isAttrEqual("__typename", "DataCubeTheme")); const orgFilter = filters.find( @@ -662,12 +655,15 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => { if (!theme.label) { return false; } + if (!counts[theme.iri]) { return false; } + if (themeFilter && themeFilter !== theme) { return false; } + return true; }); @@ -675,12 +671,15 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => { if (!org.label) { return false; } + if (!counts[org.iri] && orgFilter !== org) { return false; } + if (orgFilter && orgFilter !== org) { return false; } + return true; }); @@ -727,7 +726,9 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => { } /> ) : null; + let navs = [themeNav, orgNav]; + if (filters[0]?.__typename === "DataCubeTheme") { navs = [themeNav, orgNav]; } else if (filters[0]?.__typename === "DataCubeOrganization") { @@ -745,7 +746,6 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => { role="search" key={filters.length} > - {/* Theme tree */} {navs[0]} {navs[1]} @@ -762,6 +762,7 @@ export const DatasetResults = ({ query: UseQueryState; }) => { const { fetching, data, error } = query; + if (fetching) { return ( diff --git a/app/browser/use-dataset-count.tsx b/app/browser/use-dataset-count.tsx deleted file mode 100644 index fac5066dd..000000000 --- a/app/browser/use-dataset-count.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import { useMemo } from "react"; - -import { useDatasetCountQuery } from "@/graphql/query-hooks"; -import { useDataSourceStore } from "@/stores/data-source"; -import isAttrEqual from "@/utils/is-attr-equal"; - -import { BrowseFilter } from "./filters"; - -const countListToIndexedCount = (l: { count: number; iri: string }[]) => - Object.fromEntries(l.map((o) => [o.iri, o.count])); - -const useDatasetCount = ( - filters: BrowseFilter[], - includeDrafts: boolean -): Record => { - const { dataSource } = useDataSourceStore(); - const [{ data: datasetCounts }] = useDatasetCountQuery({ - variables: { - sourceType: dataSource.type, - sourceUrl: dataSource.url, - includeDrafts, - theme: filters.find(isAttrEqual("__typename", "DataCubeTheme"))?.iri, - organization: filters.find( - isAttrEqual("__typename", "DataCubeOrganization") - )?.iri, - }, - }); - - return useMemo( - () => - datasetCounts?.datasetcount - ? countListToIndexedCount(datasetCounts?.datasetcount) - : {}, - [datasetCounts] - ); -}; - -export default useDatasetCount; diff --git a/app/graphql/queries/data-cubes.graphql b/app/graphql/queries/data-cubes.graphql index b60f068c9..e55819831 100644 --- a/app/graphql/queries/data-cubes.graphql +++ b/app/graphql/queries/data-cubes.graphql @@ -475,24 +475,3 @@ query DimensionHierarchy( } } } - -query DatasetCount( - $sourceType: String! - $sourceUrl: String! - $theme: String - $organization: String - $subtheme: String - $includeDrafts: Boolean -) { - datasetcount( - sourceType: $sourceType - sourceUrl: $sourceUrl - theme: $theme - organization: $organization - subtheme: $subtheme - includeDrafts: $includeDrafts - ) { - count - iri - } -} diff --git a/app/graphql/query-hooks.ts b/app/graphql/query-hooks.ts index 12560b4f8..1d6bbdc8d 100644 --- a/app/graphql/query-hooks.ts +++ b/app/graphql/query-hooks.ts @@ -118,12 +118,6 @@ export type DataCubeTheme = { label?: Maybe; }; -export type DatasetCount = { - __typename: 'DatasetCount'; - iri: Scalars['String']; - count: Scalars['Int']; -}; - export type Dimension = { iri: Scalars['String']; label: Scalars['String']; @@ -394,7 +388,6 @@ export type Query = { themes: Array; subthemes: Array; organizations: Array; - datasetcount?: Maybe>; }; @@ -451,16 +444,6 @@ export type QueryOrganizationsArgs = { }; -export type QueryDatasetcountArgs = { - sourceType: Scalars['String']; - sourceUrl: Scalars['String']; - theme?: Maybe; - organization?: Maybe; - subtheme?: Maybe; - includeDrafts?: Maybe; -}; - - export type RelatedDimension = { __typename: 'RelatedDimension'; type: Scalars['String']; @@ -1146,18 +1129,6 @@ export type DimensionHierarchyQuery = { __typename: 'Query', dataCubeByIri?: May & HierarchyMetadata_TemporalOrdinalDimension_Fragment )> }> }; -export type DatasetCountQueryVariables = Exact<{ - sourceType: Scalars['String']; - sourceUrl: Scalars['String']; - theme?: Maybe; - organization?: Maybe; - subtheme?: Maybe; - includeDrafts?: Maybe; -}>; - - -export type DatasetCountQuery = { __typename: 'Query', datasetcount?: Maybe> }; - export const DimensionMetadataFragmentDoc = gql` fragment dimensionMetadata on Dimension { iri @@ -1615,23 +1586,4 @@ export const DimensionHierarchyDocument = gql` export function useDimensionHierarchyQuery(options: Omit, 'query'> = {}) { return Urql.useQuery({ query: DimensionHierarchyDocument, ...options }); -}; -export const DatasetCountDocument = gql` - query DatasetCount($sourceType: String!, $sourceUrl: String!, $theme: String, $organization: String, $subtheme: String, $includeDrafts: Boolean) { - datasetcount( - sourceType: $sourceType - sourceUrl: $sourceUrl - theme: $theme - organization: $organization - subtheme: $subtheme - includeDrafts: $includeDrafts - ) { - count - iri - } -} - `; - -export function useDatasetCountQuery(options: Omit, 'query'> = {}) { - return Urql.useQuery({ query: DatasetCountDocument, ...options }); }; \ No newline at end of file diff --git a/app/graphql/resolver-types.ts b/app/graphql/resolver-types.ts index 2a66d770c..4566a2190 100644 --- a/app/graphql/resolver-types.ts +++ b/app/graphql/resolver-types.ts @@ -120,12 +120,6 @@ export type DataCubeTheme = { label?: Maybe; }; -export type DatasetCount = { - __typename?: 'DatasetCount'; - iri: Scalars['String']; - count: Scalars['Int']; -}; - export type Dimension = { iri: Scalars['String']; label: Scalars['String']; @@ -396,7 +390,6 @@ export type Query = { themes: Array; subthemes: Array; organizations: Array; - datasetcount?: Maybe>; }; @@ -453,16 +446,6 @@ export type QueryOrganizationsArgs = { }; -export type QueryDatasetcountArgs = { - sourceType: Scalars['String']; - sourceUrl: Scalars['String']; - theme?: Maybe; - organization?: Maybe; - subtheme?: Maybe; - includeDrafts?: Maybe; -}; - - export type RelatedDimension = { __typename?: 'RelatedDimension'; type: Scalars['String']; @@ -657,7 +640,6 @@ export type ResolversTypes = ResolversObject<{ DataCubeResultOrder: DataCubeResultOrder; DataCubeSearchFilter: DataCubeSearchFilter; DataCubeTheme: ResolverTypeWrapper; - DatasetCount: ResolverTypeWrapper; Dimension: ResolverTypeWrapper; DimensionValue: ResolverTypeWrapper; FilterValue: ResolverTypeWrapper; @@ -698,7 +680,6 @@ export type ResolversParentTypes = ResolversObject<{ Float: Scalars['Float']; DataCubeSearchFilter: DataCubeSearchFilter; DataCubeTheme: DataCubeTheme; - DatasetCount: DatasetCount; Dimension: ResolvedDimension; DimensionValue: Scalars['DimensionValue']; FilterValue: Scalars['FilterValue']; @@ -770,12 +751,6 @@ export type DataCubeThemeResolvers; }>; -export type DatasetCountResolvers = ResolversObject<{ - iri?: Resolver; - count?: Resolver; - __isTypeOf?: IsTypeOfResolverFn; -}>; - export type DimensionResolvers = ResolversObject<{ __resolveType: TypeResolveFn<'GeoCoordinatesDimension' | 'GeoShapesDimension' | 'NominalDimension' | 'NumericalMeasure' | 'OrdinalDimension' | 'OrdinalMeasure' | 'StandardErrorDimension' | 'TemporalDimension' | 'TemporalOrdinalDimension', ParentType, ContextType>; iri?: Resolver; @@ -961,7 +936,6 @@ export type QueryResolvers, ParentType, ContextType, RequireFields>; subthemes?: Resolver, ParentType, ContextType, RequireFields>; organizations?: Resolver, ParentType, ContextType, RequireFields>; - datasetcount?: Resolver>, ParentType, ContextType, RequireFields>; }>; export interface RawObservationScalarConfig extends GraphQLScalarTypeConfig { @@ -1037,7 +1011,6 @@ export type Resolvers = ResolversObject<{ DataCubeOrganization?: DataCubeOrganizationResolvers; DataCubeResult?: DataCubeResultResolvers; DataCubeTheme?: DataCubeThemeResolvers; - DatasetCount?: DatasetCountResolvers; Dimension?: DimensionResolvers; DimensionValue?: GraphQLScalarType; FilterValue?: GraphQLScalarType; diff --git a/app/graphql/resolvers/index.ts b/app/graphql/resolvers/index.ts index 2524ad42f..b379121e7 100644 --- a/app/graphql/resolvers/index.ts +++ b/app/graphql/resolvers/index.ts @@ -43,10 +43,6 @@ export const Query: QueryResolvers = { const source = getSource(args.sourceType); return await source.organizations(parent, args, context, info); }, - datasetcount: async (parent, args, context, info) => { - const source = getSource(args.sourceType); - return await source.datasetcount(parent, args, context, info); - }, }; const DataCube: DataCubeResolvers = { diff --git a/app/graphql/resolvers/rdf.ts b/app/graphql/resolvers/rdf.ts index 95e80be32..c90d39b8f 100644 --- a/app/graphql/resolvers/rdf.ts +++ b/app/graphql/resolvers/rdf.ts @@ -24,9 +24,6 @@ import { loadOrganizations, loadSubthemes, loadThemes, - queryDatasetCountByOrganization, - queryDatasetCountBySubTheme, - queryDatasetCountByTheme, } from "@/rdf/query-cube-metadata"; import { unversionObservation } from "@/rdf/query-dimension-values"; import { queryHierarchy } from "@/rdf/query-hierarchies"; @@ -69,7 +66,6 @@ export const dataCubes: NonNullable = async ( info ) => { const { sparqlClient, sparqlClientStream } = await setup(info); - const { candidates, meta } = await searchCubes({ locale, includeDrafts, @@ -79,11 +75,12 @@ export const dataCubes: NonNullable = async ( sparqlClientStream, }); - for (let query of meta.queries) { + for (const query of meta.queries) { queries.push(query); } sortResults(candidates, order, locale); + return candidates; }; @@ -172,32 +169,6 @@ export const organizations: NonNullable = return (await loadOrganizations({ locale, sparqlClient })).filter(truthy); }; -export const datasetcount: NonNullable = async ( - _, - { organization, theme, includeDrafts }, - { setup }, - info -) => { - const { sparqlClient } = await setup(info); - const byOrg = await queryDatasetCountByOrganization({ - theme: theme || undefined, - includeDrafts: includeDrafts ?? undefined, - sparqlClient, - }); - const byTheme = await queryDatasetCountByTheme({ - organization: organization || undefined, - includeDrafts: includeDrafts ?? undefined, - sparqlClient, - }); - const bySubTheme = await queryDatasetCountBySubTheme({ - theme: theme || undefined, - organization: organization || undefined, - includeDrafts: includeDrafts ?? undefined, - sparqlClient, - }); - return [...byOrg, ...byTheme, ...bySubTheme]; -}; - export const dataCubeDimensions: NonNullable = async ({ cube, locale }, { componentIris }, { setup }, info) => { const { sparqlClient, cache } = await setup(info); diff --git a/app/graphql/resolvers/sql.ts b/app/graphql/resolvers/sql.ts index 4a74447e9..befd810ac 100644 --- a/app/graphql/resolvers/sql.ts +++ b/app/graphql/resolvers/sql.ts @@ -175,11 +175,6 @@ export const organizations: NonNullable = return []; }; -export const datasetcount: NonNullable = - async () => { - return []; - }; - export const dataCubeDimensions: NonNullable = async ({ cube }) => { // FIXME: type of cube should be different for RDF and SQL. diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index 51c33d7c4..a42ee8864 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -351,11 +351,6 @@ enum DataCubeResultOrder { CREATED_DESC } -type DatasetCount { - iri: String! - count: Int! -} - # The "Query" type is special: it lists all of the available queries that # clients can execute, along with the return type for each. type Query { @@ -400,12 +395,4 @@ type Query { sourceUrl: String! locale: String! ): [DataCubeOrganization!]! - datasetcount( - sourceType: String! - sourceUrl: String! - theme: String - organization: String - subtheme: String - includeDrafts: Boolean - ): [DatasetCount!] } diff --git a/app/rdf/query-search.ts b/app/rdf/query-search.ts index ebc009aa3..9cddbc377 100644 --- a/app/rdf/query-search.ts +++ b/app/rdf/query-search.ts @@ -103,14 +103,14 @@ export const searchCubes = async ({ // Search cubeIris along with their score const themeValues = - filters?.filter((x) => x.type === "DataCubeTheme").map((v) => v.value) || + filters?.filter((x) => x.type === "DataCubeTheme").map((v) => v.value) ?? []; const creatorValues = filters ?.filter((x) => x.type === "DataCubeOrganization") - .map((v) => v.value) || []; + .map((v) => v.value) ?? []; const aboutValues = - filters?.filter((x) => x.type === "DataCubeAbout").map((v) => v.value) || + filters?.filter((x) => x.type === "DataCubeAbout").map((v) => v.value) ?? []; const scoresQuery = SELECT.DISTINCT`?lang ?cube ?versionHistory ?name ?description ?publisher ?themeName ?creatorLabel`