'use client'; import { useEffect, useRef, useState } from 'react'; import { useAppDispatch, useAppSelector } from '@akinon/next/redux/hooks'; import { closeSearch } from '@akinon/next/redux/reducers/header'; import clsx from 'clsx'; import { Icon } from '@theme/components'; import Results from './results'; import { ROUTES } from '@theme/routes'; import { useLocalization, useRouter } from '@akinon/next/hooks'; import PluginModule, { Component } from '@akinon/next/components/plugin-module'; export default function Search() { const { t } = useLocalization(); const router = useRouter(); const dispatch = useAppDispatch(); const isSearchOpen = useAppSelector((state) => state.header.isSearchOpen); const [searchText, setSearchText] = useState(''); const inputRef = useRef(null); useEffect(() => { if (isSearchOpen) { inputRef.current?.focus(); document.body.style.overflow = 'hidden'; const handleEscKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { dispatch(closeSearch()); } }; document.addEventListener('keydown', handleEscKey); return () => { document.removeEventListener('keydown', handleEscKey); document.body.style.overflow = 'auto'; }; } return () => { document.body.style.overflow = 'auto'; }; }, [isSearchOpen, dispatch]); return ( <>
dispatch(closeSearch())} />
{t('common.search.results_for')}
setSearchText(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && searchText.trim() !== '') { router.push(`${ROUTES.LIST}/?search_text=${searchText}`); } }} className="border-0 text-2xl outline-none text-secondary placeholder:text-xl placeholder:lg:text-2xl" placeholder={t('common.search.placeholder')} ref={inputRef} /> dispatch(closeSearch())} className="cursor-pointer" />
); }