1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 05:15:02 +00:00
mon-entreprise/source/components/SearchButton.js
Maxime Quandalle 0663c97204 Utilisation du hook useTranslation de react-i18next
Suppression de notre composant withLanguage qui rajoutait une abstraction
inutile.

Note: de nombreux appels à withTranslation et withLanguage était inutile
car le composant augmenté n'utilisait pas les paramètres fournis (language, t, i18n).
L'utilisation des hooks nous permet de mieux gérer le code mort, car il s'agit
de simples variables dont le non-usage est détecté par l'analyse statique.
2019-09-11 11:17:23 +02:00

48 lines
1.2 KiB
JavaScript

import { compose } from 'ramda'
import React, { useEffect, useState } from 'react'
import emoji from 'react-easy-emoji'
import { Trans } from 'react-i18next'
import { connect } from 'react-redux'
import { flatRulesSelector } from 'Selectors/analyseSelectors'
import Overlay from './Overlay'
import SearchBar from './SearchBar'
export default compose(
connect(state => ({
flatRules: flatRulesSelector(state)
}))
)(function SearchButton({ flatRules, invisibleButton }) {
const [visible, setVisible] = useState(false)
useEffect(() => {
window.addEventListener('keydown', handleKeyDown)
return () => {
window.removeEventListener('keydown', handleKeyDown)
}
}, [])
const handleKeyDown = e => {
if (!(e.ctrlKey && e.key === 'k')) return
setVisible(true)
e.preventDefault()
e.stopPropagation()
return false
}
const close = () => setVisible(false)
return visible ? (
<Overlay onClose={close}>
<h2>
<Trans>Chercher dans la documentation</Trans>
</h2>
<SearchBar showDefaultList={false} finally={close} rules={flatRules} />
</Overlay>
) : invisibleButton ? null : (
<button
className="ui__ simple small button"
onClick={() => setVisible(true)}>
{emoji('🔍')} <Trans>Rechercher</Trans>
</button>
)
})