import withSitePaths from 'Components/utils/withSitePaths' import { encodeRuleName } from 'Engine/rules.js' import Fuse from 'fuse.js' import { compose, pick, sortBy } from 'ramda' import React, { useRef, useState } from 'react' import Highlighter from 'react-highlight-words' import { useTranslation } from 'react-i18next' import { Link, Redirect } from 'react-router-dom' import Select from 'react-select' import 'react-select/dist/react-select.css' import { capitalise0 } from '../utils' function SearchBar({ rules, showDefaultList, finally: finallyCallback, sitePaths }) { const [inputValue, setInputValue] = useState(null) const [selectedOption, setSelectedOption] = useState(null) const inputElementRef = useRef() const fuse = useRef() const { i18n } = useTranslation() const options = { keys: [ { name: 'name', weight: 0.3 }, { name: 'title', weight: 0.3 }, { name: 'espace', weight: 0.2 }, { name: 'description', weight: 0.2 } ] } if (!fuse.current) { // This operation is expensive, we don't want to do it everytime we re-render, so we cache its result in a reference fuse.current = new Fuse( rules.map(pick(['title', 'espace', 'description', 'name', 'dottedName'])), options ) } const handleChange = selectedOption => { setSelectedOption(selectedOption) } const renderOption = ({ title, dottedName }) => ( ) const filterOptions = (options, filter) => fuse.current.search(filter) if (selectedOption != null) { finallyCallback && finallyCallback() return ( ) } return ( <>