-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSearch.tsx
178 lines (165 loc) · 5.55 KB
/
Search.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { InstantSearch, Configure, Index } from 'react-instantsearch'
import { searchClient as client } from '../../lib/algolia'
import dynamic from 'next/dynamic'
import { Flags } from '../../common/helpers/datasetHelpers'
import { getIsoFromLocale } from '../../lib/localization'
import { createInstantSearchRouterNext } from 'react-instantsearch-router-nextjs'
import singletonRouter, { useRouter } from 'next/router'
import type { UiState } from 'instantsearch.js'
import { useRef } from 'react'
import styled from 'styled-components'
import { Pagination } from '../shared/search/pagination/Pagination'
import usePaginationPadding from '../../lib/hooks/usePaginationPadding'
import { useIntl } from 'react-intl'
import { SearchClient } from 'algoliasearch/lite'
import { SearchBox } from '@core/AlgoliaSearchBox/SearchBox'
import { PaginationContextProvider } from '../../common/contexts/PaginationContext'
const SearchResults = dynamic(() => import('./SearchResults'))
const StyledPagination = styled(Pagination)`
margin-top: var(--space-xLarge);
justify-content: center;
`
const searchClient = client()
const queriedSearchClient: SearchClient = {
...searchClient,
search(requests: any) {
if (requests.every(({ params }: any) => !params.query)) {
return Promise.resolve({
results: requests.map(() => ({
hits: [],
nbHits: 0,
nbPages: 0,
page: 0,
processingTimeMS: 0,
hitsPerPage: 0,
exhaustiveNbHits: false,
query: '',
params: '',
})),
})
}
return searchClient.search(requests)
},
}
const Search = () => {
const router = useRouter()
const intl = useIntl()
const padding = usePaginationPadding()
const resultsRef = useRef<HTMLDivElement>(null)
// @TODO: Don't hard code it like this
if (searchClient.appId === '') {
console.warn('Missing app ID in Algolia search client')
return null
}
const envPrefix = Flags.IS_GLOBAL_PROD ? 'prod' : 'dev'
const isoCode = getIsoFromLocale(router.locale)
const indices = [
{
value: `${envPrefix}_TOPICS_${isoCode}`,
label: intl.formatMessage({ id: 'search_topics_tab', defaultMessage: 'Topics' }),
},
{
value: `${envPrefix}_EVENTS_${isoCode}`,
label: intl.formatMessage({ id: 'search_events_tab', defaultMessage: 'Events' }),
},
{
value: `${envPrefix}_NEWS_${isoCode}`,
label: intl.formatMessage({ id: 'search_news_tab', defaultMessage: 'News' }),
},
{
value: `${envPrefix}_MAGAZINE_${isoCode}`,
label: intl.formatMessage({ id: 'search_magazine_tab', defaultMessage: 'Magazine' }),
},
]
// The main index will be "all" at some point
const mainIndex = `${envPrefix}_TOPICS_${isoCode}`
// eslint-disable-next-line
// @ts-ignore: @TODO: The types are not correct
const createURL = ({ qsModule, routeState, location }) => {
const queryParameters: any = {}
if (routeState.query) {
queryParameters.query = routeState.query
}
if (routeState.page !== 1) {
queryParameters.page = routeState.page
}
if (routeState.tab) {
queryParameters.tab = routeState.tab
}
const queryString = qsModule.stringify(queryParameters, {
addQueryPrefix: true,
arrayFormat: 'repeat',
format: 'RFC1738',
})
return `${location.pathname}${queryString}`
}
// eslint-disable-next-line
// @ts-ignore: @TODO: The types are not correct
const parseURL = ({ qsModule, location }) => {
const { query = '', page, tab = '' }: any = qsModule.parse(location.search.slice(1))
return {
...(query && { query: query }),
...(page && { page: page as number }),
...(tab && { tab: tab }),
}
}
const routing = {
router: createInstantSearchRouterNext({
singletonRouter,
routerOptions: {
createURL: createURL,
parseURL: parseURL,
push(url) {
if (url.split('?')[1]) {
// replace url only if it has query params
singletonRouter.replace(url)
}
},
},
}),
stateMapping: {
stateToRoute(uiState: UiState) {
const indexUiState = uiState[mainIndex]
return {
...(indexUiState.sortBy && {
tab: indexUiState.sortBy
.replaceAll(isoCode, '')
.replaceAll(envPrefix, '')
.replaceAll('_', '')
.toLowerCase(),
}),
...(indexUiState?.query && { query: indexUiState.query }),
...(indexUiState?.page && { page: indexUiState?.page }),
}
},
routeToState(routeState: any) {
return {
[mainIndex]: {
...(routeState.query && { query: routeState.query }),
...(routeState.page && { page: routeState.page as number }),
...(routeState.tab && { sortBy: `${envPrefix}_${routeState.tab.toUpperCase()}_${isoCode}` }),
},
}
},
},
}
return (
<InstantSearch
future={{ preserveSharedStateOnUnmount: false }}
searchClient={queriedSearchClient}
indexName={mainIndex}
routing={routing}
>
<Configure hitsPerPage={5} snippetEllipsisText="..." />
{indices.map((index) => (
<Index indexName={index.value} key={index.label} indexId={index.value} />
))}
<SearchBox variant="inverted" />
<SearchResults resultsRef={resultsRef} items={indices} />
<PaginationContextProvider defaultRef={resultsRef}>
<StyledPagination padding={padding} hitsPerPage={5} />
</PaginationContextProvider>
</InstantSearch>
)
}
export default Search