-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInfiniteHits.jsx
More file actions
206 lines (185 loc) · 6.4 KB
/
Copy pathInfiniteHits.jsx
File metadata and controls
206 lines (185 loc) · 6.4 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React from 'react';
import PropTypes from 'prop-types';
import urlJoin from 'url-join';
import Breadcrumbs from '@mui/material/Breadcrumbs';
import Link from '@mui/material/Link';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Alert from '@mui/material/Alert';
import AlertTitle from '@mui/material/AlertTitle';
import { useTheme } from '@mui/material/styles';
import { useInfiniteHits, useInstantSearch, useStats, Snippet } from 'react-instantsearch';
function CustomHit({ hit, index, activeResultIndex, urlPrefix, onClick, showLibName }) {
const theme = useTheme();
const { library_key, library_name, hierarchy, _highlightResult } = hit;
const hierarchyKeys = _highlightResult ? Object.keys(_highlightResult.hierarchy) : [];
const primaryHref = React.useMemo(() => {
const lastKey = hierarchyKeys[hierarchyKeys.length - 1];
return lastKey ? urlJoin(urlPrefix, hierarchy[lastKey].path) : urlJoin(urlPrefix, 'libs', library_key);
}, [urlPrefix, hierarchy, hierarchyKeys, library_key]);
const hierarchyLinks = React.useMemo(() => {
if (!_highlightResult) return [];
return hierarchyKeys.map((key, i) => (
<Link
underline='hover'
dangerouslySetInnerHTML={{
__html: _highlightResult.hierarchy[key].title.value,
}}
key={hierarchy[key].path}
onClick={onClick}
onAuxClick={onClick}
href={urlJoin(urlPrefix, hierarchy[key].path)}
{...(i === hierarchyKeys.length - 1 && { 'aria-current': 'page' })}
></Link>
));
}, [urlPrefix, onClick, hierarchy, _highlightResult, hierarchyKeys]);
const hitLabel = React.useMemo(() => {
const parts = [];
if (showLibName || hierarchyKeys.length === 0) parts.push(library_name);
if (_highlightResult) {
for (const key of hierarchyKeys) {
// Strip HTML highlight tags to get plain text for the aria-label.
const plain = _highlightResult.hierarchy[key].title.value.replace(/<[^>]+>/g, '');
if (plain) parts.push(plain);
}
}
return parts.join(' > ');
}, [showLibName, library_name, hierarchyKeys, _highlightResult]);
return (
<Box
className='search-modal__hit'
role='option'
id={`search-result-${index}`}
aria-selected={index === activeResultIndex}
aria-label={hitLabel}
tabIndex={-1}
>
{/* Stretched link makes the entire card clickable, pointing to the deepest hierarchy entry.
Breadcrumb links sit above it (via z-index) so they remain individually clickable. */}
<a
className='search-modal__hit-link'
href={primaryHref}
onClick={onClick}
onAuxClick={onClick}
tabIndex={-1}
aria-hidden='true'
/>
<Breadcrumbs className='search-modal__breadcrumbs' separator='>' aria-label='Breadcrumb'>
{(showLibName || hierarchyLinks.length === 0) && (
<Link
underline='hover'
href={urlJoin(urlPrefix, 'libs', library_key)}
className='search-modal__breakcrumbs-link'
>
{library_name}
</Link>
)}
{hierarchyLinks}
</Breadcrumbs>
<p
className='search-modal__snippet-wrapper'
aria-label={hit._snippetResult?.content?.value?.replace(/<[^>]+>/g, '') || ''}
>
<Snippet classNames={{ root: 'search-modal__snippet' }} hit={hit} attribute='content' />
</p>
</Box>
);
}
CustomHit.propTypes = {
hit: PropTypes.object.isRequired,
urlPrefix: PropTypes.string.isRequired,
onClick: PropTypes.func.isRequired,
showLibName: PropTypes.bool,
};
function InfiniteHits({ urlPrefix, setnbHits, onClick, showLibName, hasQuery, activeResultIndex }) {
const { hits, isLastPage, showMore } = useInfiniteHits();
const { addMiddlewares, status } = useInstantSearch();
const [error, setError] = React.useState(null);
const { nbHits } = useStats();
React.useEffect(() => {
setnbHits(nbHits);
}, [nbHits, setnbHits]);
React.useEffect(() => {
const middleware = ({ instantSearchInstance }) => {
function handleError(searchError) {
setError(searchError);
}
return {
subscribe() {
instantSearchInstance.addListener('error', handleError);
},
unsubscribe() {
instantSearchInstance.removeListener('error', handleError);
},
};
};
return addMiddlewares(middleware);
}, [addMiddlewares]);
const memoizedHits = React.useMemo(
() =>
hits.map((hit, index) => (
<CustomHit
key={hit.objectID}
hit={hit}
index={index}
activeResultIndex={activeResultIndex}
urlPrefix={urlPrefix}
onClick={onClick}
showLibName={showLibName}
/>
)),
[hits, urlPrefix, onClick, showLibName, activeResultIndex],
);
if (error) {
return (
<Alert severity='error'>
<AlertTitle>{error.name}</AlertTitle>
{error.message}
</Alert>
);
}
if (hits.length === 0 && !hasQuery) {
return (
<div className='search-modal__empty-state'>
<h2 className='search-modal__empty-state-title'>Ready when you are</h2>
<p className='search-modal__empty-state-subtitle'>
Your search results will appear here once you start typing.
</p>
</div>
);
}
if (hits.length === 0 && hasQuery) {
return (
<div className='search-modal__no-results'>
<h2 className='search-modal__no-results-title'>No Results Found</h2>
<p className='search-modal__no-results-subtitle'>Sorry, We couldn't find any matches for your search.</p>
</div>
);
}
return (
<Stack
className='search-modal__hits-stack'
spacing={2}
role='listbox'
aria-label='Search results'
aria-busy={status === 'loading' || status === 'stalled'}
>
{memoizedHits}
<Box className='search-modal__show-more-wrapper' textAlign='center'>
<Button className='search-modal__show-more' disabled={isLastPage} onClick={showMore}>
Show More
</Button>
</Box>
</Stack>
);
}
InfiniteHits.propTypes = {
urlPrefix: PropTypes.string.isRequired,
setnbHits: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,
showLibName: PropTypes.bool,
hasQuery: PropTypes.bool.isRequired,
activeResultIndex: PropTypes.number.isRequired,
};
export default InfiniteHits;