Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 47 additions & 88 deletions datagouv-components/src/components/Search/GlobalSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
>
<SearchInput
v-model="q"
:placeholder="placeholder || typesMeta[currentType].placeholder"
:placeholder="placeholder || typesMeta[currentTypeConfig?.class ?? 'datasets'].placeholder"
/>
</div>
<div class="grid grid-cols-12 mt-2 md:mt-5">
Expand All @@ -30,10 +30,10 @@
>
<RadioInput
v-for="typeConfig in config"
:key="typeConfig.class"
:value="typeConfig.class"
:count="typesMeta[typeConfig.class].results.value?.total"
:loading="typesMeta[typeConfig.class].status.value === 'pending' || typesMeta[typeConfig.class].status.value === 'idle'"
:key="configKey(typeConfig)"
:value="configKey(typeConfig)"
:count="resultsMap[configKey(typeConfig)]?.data.value?.total"
:loading="resultsMap[configKey(typeConfig)]?.status.value === 'pending' || resultsMap[configKey(typeConfig)]?.status.value === 'idle'"
:icon="typesMeta[typeConfig.class].icon"
>
{{ typeConfig.name || typesMeta[typeConfig.class].name }}
Expand Down Expand Up @@ -123,7 +123,7 @@
v-model="producerType"
:facets="getFacets('producer_type')"
:loading="searchResultsStatus === 'pending'"
:exclude="currentType === 'organizations' ? ['user'] : []"
:exclude="currentTypeConfig?.class === 'organizations' ? ['user'] : []"
:style="{ order: getOrder('producer_type') }"
/>
<DatasetBadgeFilter
Expand Down Expand Up @@ -228,42 +228,42 @@
<ul class="space-y-4 mt-2 p-0 relative z-2 list-none">
<li
v-for="result in results.data"
:key="result.id"

Check failure on line 231 in datagouv-components/src/components/Search/GlobalSearch.vue

View workflow job for this annotation

GitHub Actions / quality

'result' is of type 'unknown'.
class="p-0"
>
<template v-if="currentType === 'datasets'">
<template v-if="currentTypeConfig?.class === 'datasets'">
<slot
name="dataset"
:dataset="result"
>
<DatasetCard :dataset="(result as Dataset)" />
</slot>
</template>
<template v-else-if="currentType === 'dataservices'">
<template v-else-if="currentTypeConfig?.class === 'dataservices'">
<slot
name="dataservice"
:dataservice="result"
>
<DataserviceCard :dataservice="(result as Dataservice)" />
</slot>
</template>
<template v-else-if="currentType === 'reuses'">
<template v-else-if="currentTypeConfig?.class === 'reuses'">
<slot
name="reuse"
:reuse="result"
>
<ReuseHorizontalCard :reuse="(result as Reuse)" />
</slot>
</template>
<template v-else-if="currentType === 'organizations'">
<template v-else-if="currentTypeConfig?.class === 'organizations'">
<slot
name="organization"
:organization="result"
>
<OrganizationHorizontalCard :organization="(result as Organization)" />
</slot>
</template>
<template v-else-if="currentType === 'topics'">
<template v-else-if="currentTypeConfig?.class === 'topics'">
<slot
name="topic"
:topic="result"
Expand Down Expand Up @@ -354,7 +354,7 @@
import type { Organization } from '../../types/organizations'
import type { Reuse } from '../../types/reuses'
import type { TopicV2 } from '../../types/topics'
import type { GlobalSearchConfig, SearchType, SortOption, DatasetSearchResponse, DataserviceSearchResponse, ReuseSearchResponse, OrganizationSearchResponse, TopicSearchResponse, FacetItem } from '../../types/search'
import type { GlobalSearchConfig, SearchTypeConfig, SortOption, FacetItem } from '../../types/search'
import { getDefaultGlobalSearchConfig } from '../../types/search'
import BrandedButton from '../BrandedButton.vue'
import LoadingBlock from '../LoadingBlock.vue'
Expand Down Expand Up @@ -392,9 +392,11 @@
config: getDefaultGlobalSearchConfig,
})

const configKey = (c: SearchTypeConfig) => c.key ?? c.class

// defineModel's default is static and can't depend on props, so we cast and initialize manually
const currentType = defineModel<SearchType>('type') as Ref<SearchType>
if (!currentType.value) currentType.value = props.config[0]?.class ?? 'datasets'
const currentType = defineModel<string>('type') as Ref<string>
if (!currentType.value) currentType.value = configKey(props.config[0] ?? { class: 'datasets' })

const { t } = useTranslation()
const componentsConfig = useComponentsConfig()
Expand All @@ -403,7 +405,7 @@
const initialType = currentType.value

const currentTypeConfig = computed(() =>
props.config.find(c => c.class === currentType.value),
props.config.find(c => configKey(c) === currentType.value),
)

const activeBasicFilters = computed(() =>
Expand Down Expand Up @@ -503,13 +505,6 @@
}
})

// Check which types are enabled
const datasetsEnabled = computed(() => props.config.some(c => c.class === 'datasets'))
const dataservicesEnabled = computed(() => props.config.some(c => c.class === 'dataservices'))
const reusesEnabled = computed(() => props.config.some(c => c.class === 'reuses'))
const organizationsEnabled = computed(() => props.config.some(c => c.class === 'organizations'))
const topicsEnabled = computed(() => props.config.some(c => c.class === 'topics'))

// Create stable params for each type
const stableParamsOptions = {
allFilters,
Expand All @@ -519,33 +514,28 @@
pageSize,
}

const datasetsParams = useStableQueryParams({
...stableParamsOptions,
typeConfig: props.config.find(c => c.class === 'datasets'),
})
const dataservicesParams = useStableQueryParams({
...stableParamsOptions,
typeConfig: props.config.find(c => c.class === 'dataservices'),
})
const reusesParams = useStableQueryParams({
...stableParamsOptions,
typeConfig: props.config.find(c => c.class === 'reuses'),
})
const organizationsParams = useStableQueryParams({
...stableParamsOptions,
typeConfig: props.config.find(c => c.class === 'organizations'),
})
const topicsParams = useStableQueryParams({
...stableParamsOptions,
typeConfig: props.config.find(c => c.class === 'topics'),
})
// URL by class (static lookup)
const urlByClass: Record<string, string> = {
datasets: '/api/2/datasets/search/',
dataservices: '/api/2/dataservices/search/',
reuses: '/api/2/reuses/search/',
organizations: '/api/2/organizations/search/',
topics: '/api/2/topics/search/',
}

// One params + fetch per config entry, keyed by configKey
const resultsMap: Record<string, { data: Ref<{ total: number, data: unknown[], facets?: unknown } | null>, status: Ref<string> }> = {}
for (const c of props.config) {
const key = configKey(c)
const params = useStableQueryParams({ ...stableParamsOptions, typeConfig: c })

// URLs that return null when type is not enabled
const datasetsUrl = computed(() => datasetsEnabled.value ? '/api/2/datasets/search/' : null)
const dataservicesUrl = computed(() => dataservicesEnabled.value ? '/api/2/dataservices/search/' : null)
const reusesUrl = computed(() => reusesEnabled.value ? '/api/2/reuses/search/' : null)
const organizationsUrl = computed(() => organizationsEnabled.value ? '/api/2/organizations/search/' : null)
const topicsUrl = computed(() => topicsEnabled.value ? '/api/2/topics/search/' : null)
const { data, status } = await useFetch(urlByClass[c.class], {
params,
lazy: true,
server: initialType === key,
})
resultsMap[key] = { data, status }

Check failure on line 537 in datagouv-components/src/components/Search/GlobalSearch.vue

View workflow job for this annotation

GitHub Actions / quality

Type 'Ref<unknown, unknown>' is not assignable to type 'Ref<{ total: number; data: unknown[]; facets?: unknown; } | null, { total: number; data: unknown[]; facets?: unknown; } | null>'.
}

// Reset page on filter/sort change
const filtersForReset = computed(() => ({
Expand Down Expand Up @@ -589,7 +579,10 @@
|| reuseType.value
})

const showForumLink = computed(() => (currentType.value === 'datasets' || currentType.value === 'dataservices') && !!componentsConfig.forumUrl)
const showForumLink = computed(() =>
(currentTypeConfig.value?.class === 'datasets' || currentTypeConfig.value?.class === 'dataservices')
&& !!componentsConfig.forumUrl,
)

function resetFilters() {
organizationId.value = undefined
Expand All @@ -611,80 +604,46 @@
flushQ()
}

// API calls only for enabled types (useFetch skips when URL is null)
// Only the initial type is fetched during SSR, others are client-side only
const { data: datasetsResults, status: datasetsStatus } = await useFetch<DatasetSearchResponse<Dataset>>(
datasetsUrl,
{ params: datasetsParams, lazy: true, server: initialType === 'datasets' },
)
const { data: dataservicesResults, status: dataservicesStatus } = await useFetch<DataserviceSearchResponse<Dataservice>>(
dataservicesUrl,
{ params: dataservicesParams, lazy: true, server: initialType === 'dataservices' },
)
const { data: reusesResults, status: reusesStatus } = await useFetch<ReuseSearchResponse<Reuse>>(
reusesUrl,
{ params: reusesParams, lazy: true, server: initialType === 'reuses' },
)
const { data: organizationsResults, status: organizationsStatus } = await useFetch<OrganizationSearchResponse<Organization>>(
organizationsUrl,
{ params: organizationsParams, lazy: true, server: initialType === 'organizations' },
)
const { data: topicsResults, status: topicsStatus } = await useFetch<TopicSearchResponse<TopicV2>>(
topicsUrl,
{ params: topicsParams, lazy: true, server: initialType === 'topics' },
)

const typesMeta = {
datasets: {
icon: RiDatabase2Line,
name: t('Jeux de données'),
placeholder: t('ex. élections présidentielles'),
results: datasetsResults,
status: datasetsStatus,
},
dataservices: {
icon: RiTerminalLine,
name: t('API'),
placeholder: t('ex: SIRENE'),
results: dataservicesResults,
status: dataservicesStatus,
},
reuses: {
icon: RiLineChartLine,
name: t('Réutilisations'),
placeholder: t('Rechercher une réutilisation de données'),
results: reusesResults,
status: reusesStatus,
},
organizations: {
icon: RiBuilding2Line,
name: t('Organisations'),
placeholder: t('Rechercher une organisation'),
results: organizationsResults,
status: organizationsStatus,
},
topics: {
icon: RiBookShelfLine,
name: t('Thématiques'),
placeholder: t('Rechercher une thématique'),
results: topicsResults,
status: topicsStatus,
},
} as const

const searchResults = computed(() => typesMeta[currentType.value].results.value)
const searchResultsStatus = computed(() => typesMeta[currentType.value].status.value)
const searchResults = computed(() => resultsMap[currentType.value]?.data.value)
const searchResultsStatus = computed(() => resultsMap[currentType.value]?.status.value)

// RSS feed URL for datasets
const rssUrl = computed(() => {
if (currentType.value !== 'datasets') return null
if (currentTypeConfig.value?.class !== 'datasets') return null

const params = new URLSearchParams()
const datasetsConfig = props.config.find(c => c.class === 'datasets')

// Add hidden filters first
if (datasetsConfig?.hiddenFilters) {
for (const hf of datasetsConfig.hiddenFilters) {
if (currentTypeConfig.value?.hiddenFilters) {
for (const hf of currentTypeConfig.value.hiddenFilters) {
if (hf?.value) params.set(hf.key as string, String(hf.value))
}
}
Expand Down
5 changes: 5 additions & 0 deletions datagouv-components/src/types/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export type SortOption<Sort extends string> = {

export type DatasetSearchConfig = {
class: 'datasets'
key?: string
name?: string
hiddenFilters?: HiddenFilter<DatasetSearchFilters>[]
basicFilters?: (keyof DatasetSearchFilters)[]
Expand All @@ -300,6 +301,7 @@ export type DatasetSearchConfig = {

export type DataserviceSearchConfig = {
class: 'dataservices'
key?: string
name?: string
hiddenFilters?: HiddenFilter<DataserviceSearchFilters>[]
basicFilters?: (keyof DataserviceSearchFilters)[]
Expand All @@ -309,6 +311,7 @@ export type DataserviceSearchConfig = {

export type ReuseSearchConfig = {
class: 'reuses'
key?: string
name?: string
hiddenFilters?: HiddenFilter<ReuseSearchFilters>[]
basicFilters?: (keyof ReuseSearchFilters)[]
Expand All @@ -318,6 +321,7 @@ export type ReuseSearchConfig = {

export type OrganizationSearchConfig = {
class: 'organizations'
key?: string
name?: string
hiddenFilters?: HiddenFilter<OrganizationSearchFilters>[]
basicFilters?: (keyof OrganizationSearchFilters)[]
Expand All @@ -327,6 +331,7 @@ export type OrganizationSearchConfig = {

export type TopicSearchConfig = {
class: 'topics'
key?: string
name?: string
hiddenFilters?: HiddenFilter<TopicSearchFilters>[]
basicFilters?: (keyof TopicSearchFilters)[]
Expand Down
Loading