|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 | 12 |
|
13 |
| -import {ActionButton, Text} from '@adobe/react-spectrum'; |
14 |
| -import algoliasearch from 'algoliasearch/lite'; |
| 13 | +import {ActionButton} from '@adobe/react-spectrum'; |
| 14 | +import DocSearch from './DocSearch'; |
15 | 15 | import docsStyle from './docs.css';
|
16 |
| -import DOMPurify from 'dompurify'; |
17 |
| -import {Item, SearchAutocomplete, Section} from '@react-spectrum/autocomplete'; |
18 | 16 | import {listen} from 'quicklink';
|
19 | 17 | import React, {useEffect, useRef, useState} from 'react';
|
20 | 18 | import ReactDOM from 'react-dom';
|
21 | 19 | import ShowMenu from '@spectrum-icons/workflow/ShowMenu';
|
22 |
| -import {ThemeProvider, ThemeSwitcher} from './ThemeSwitcher'; |
| 20 | +import {ThemeSwitcher} from './ThemeSwitcher'; |
23 | 21 | import {watchModals} from '@react-aria/aria-modal-polyfill';
|
24 | 22 |
|
25 | 23 | if (process.env.NODE_ENV === 'production') {
|
@@ -186,119 +184,6 @@ function Hamburger() {
|
186 | 184 | );
|
187 | 185 | }
|
188 | 186 |
|
189 |
| -function DocSearch() { |
190 |
| - const client = algoliasearch('1V1Q59JVTR', '44a7e2e7508ff185f25ac64c0a675f98'); |
191 |
| - const searchIndex = client.initIndex('react-spectrum'); |
192 |
| - const searchOptions = { |
193 |
| - distinct: 1, |
194 |
| - highlightPreTag: `<mark class="${docsStyle.docSearchBoxMark}">`, |
195 |
| - highlightPostTag: '</mark>' |
196 |
| - }; |
197 |
| - |
198 |
| - const sectionTitles = { |
199 |
| - 'react-aria': 'React Aria', |
200 |
| - 'react-spectrum': 'React Spectrum', |
201 |
| - 'react-stately': 'React Stately', |
202 |
| - 'internationalized': 'Internationalized', |
203 |
| - 'blog': 'Blog', |
204 |
| - 'architecture': 'Architecture', |
205 |
| - 'contribute': 'Contribute', |
206 |
| - 'releases': 'Releases', |
207 |
| - 'support': 'Support' |
208 |
| - }; |
209 |
| - |
210 |
| - const [searchValue, setSearchValue] = useState(''); |
211 |
| - const [predictions, setPredictions] = useState(null); |
212 |
| - const [suggestions, setSuggestions] = useState(null); |
213 |
| - |
214 |
| - let updatePredictions = ({hits}) => { |
215 |
| - setPredictions(hits); |
216 |
| - let sections = []; |
217 |
| - hits.forEach(prediction => { |
218 |
| - let hierarchy = prediction.hierarchy; |
219 |
| - let objectID = prediction.objectID; |
220 |
| - let url = prediction.url; |
221 |
| - let sectionTitle; |
222 |
| - for (const [path, title] of Object.entries(sectionTitles)) { |
223 |
| - let regexp = new RegExp('^.+//.+/' + path + '[/.].+$', 'i'); |
224 |
| - if (url.match(regexp)) { |
225 |
| - sectionTitle = title; |
226 |
| - break; |
227 |
| - } |
228 |
| - } |
229 |
| - if (!sectionTitle) { |
230 |
| - sectionTitle = 'Documentation'; |
231 |
| - } |
232 |
| - let section = sections.find(section => section.title === sectionTitle); |
233 |
| - if (!section) { |
234 |
| - section = {title: sectionTitle, items: []}; |
235 |
| - sections.push(section); |
236 |
| - } |
237 |
| - let text = []; |
238 |
| - let textValue = []; |
239 |
| - for (let i = 1; i < 6; i++) { |
240 |
| - if (hierarchy[`lvl${i}`]) { |
241 |
| - text.push(prediction._highlightResult.hierarchy[`lvl${i}`].value); |
242 |
| - textValue.push(hierarchy[`lvl${i}`]); |
243 |
| - } |
244 |
| - } |
245 |
| - section.items.push( |
246 |
| - <Item key={objectID} textValue={textValue.join(' | ')}> |
247 |
| - <Text><span dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(text.join(' | '))}} /></Text> |
248 |
| - { |
249 |
| - prediction.content && |
250 |
| - <Text slot="description"> |
251 |
| - <span dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(prediction._snippetResult.content.value)}} /> |
252 |
| - </Text> |
253 |
| - } |
254 |
| - </Item> |
255 |
| - ); |
256 |
| - }); |
257 |
| - let titles = Object.values(sectionTitles); |
258 |
| - sections = sections.sort((a, b) => titles.indexOf(a.title) < titles.indexOf(b.title) ? -1 : 1); |
259 |
| - let suggestions = sections.map((section, index) => <Section key={`${index}-${section.title}`} title={section.title}>{section.items}</Section>); |
260 |
| - setSuggestions(suggestions); |
261 |
| - }; |
262 |
| - |
263 |
| - let onInputChange = (query) => { |
264 |
| - if (!query && predictions) { |
265 |
| - setPredictions(null); |
266 |
| - setSuggestions(null); |
267 |
| - } |
268 |
| - setSearchValue(query); |
269 |
| - searchIndex |
270 |
| - .search( |
271 |
| - query, |
272 |
| - searchOptions |
273 |
| - ) |
274 |
| - .then(updatePredictions); |
275 |
| - }; |
276 |
| - |
277 |
| - let onSubmit = (value, key) => { |
278 |
| - if (key) { |
279 |
| - let prediction = predictions.find(prediction => key === prediction.objectID); |
280 |
| - let url = prediction.url; |
281 |
| - window.location.href = `${window.location.hostname === 'reactspectrum.blob.core.windows.net' ? window.location.href.replace(/(.+\/docs\/)(.+)/, '$1') : '/'}${url.replace('https://react-spectrum.adobe.com/', '')}`; |
282 |
| - } |
283 |
| - }; |
284 |
| - |
285 |
| - return ( |
286 |
| - <ThemeProvider UNSAFE_className={docsStyle.docSearchBoxThemeProvider}> |
287 |
| - <span role="search"> |
288 |
| - <SearchAutocomplete |
289 |
| - aria-label="Search" |
290 |
| - UNSAFE_className={docsStyle.docSearchBox} |
291 |
| - id="algolia-doc-search" |
292 |
| - value={searchValue} |
293 |
| - onInputChange={onInputChange} |
294 |
| - onSubmit={onSubmit}> |
295 |
| - {suggestions} |
296 |
| - </SearchAutocomplete> |
297 |
| - </span> |
298 |
| - </ThemeProvider> |
299 |
| - ); |
300 |
| -} |
301 |
| - |
302 | 187 | ReactDOM.render(<>
|
303 | 188 | <Hamburger />
|
304 | 189 | <DocSearch />
|
|
0 commit comments