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, { useMemo, 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() // This operation is expensive, we don't want to do it everytime we re-render, so we cache its result const fuse = useMemo(() => { const list = rules.map( pick(['title', 'espace', 'description', 'name', 'dottedName']) ) const options = { keys: [ { name: 'name', weight: 0.3 }, { name: 'title', weight: 0.3 }, { name: 'espace', weight: 0.2 }, { name: 'description', weight: 0.2 } ] } return new Fuse(list, options) }, [rules]) const { i18n } = useTranslation() const renderOption = ({ title, dottedName }) => ( ) const filterOptions = (options, filter) => fuse.search(filter) if (selectedOption != null) { finallyCallback && finallyCallback() return ( ) } return ( <>