'use client'; import { useState, memo, useCallback } from 'react'; import parse from 'autosuggest-highlight/parse'; import match from 'autosuggest-highlight/match'; // @mui import { useTheme } from '@mui/material/styles'; import Box from '@mui/material/Box'; import List from '@mui/material/List'; import Stack from '@mui/material/Stack'; import InputBase from '@mui/material/InputBase'; import IconButton from '@mui/material/IconButton'; import InputAdornment from '@mui/material/InputAdornment'; import Dialog, { dialogClasses } from '@mui/material/Dialog'; // hooks import { useBoolean } from 'src/hooks/use-boolean'; import { useResponsive } from 'src/hooks/use-responsive'; import { useEventListener } from 'src/hooks/use-event-listener'; // components import Label from 'src/components/label'; import Iconify from 'src/components/iconify'; import Scrollbar from 'src/components/scrollbar'; import { useRouter } from 'src/routes/hooks'; import SearchNotFound from 'src/components/search-not-found'; // import ResultItem from './result-item'; import { useNavData } from '../../dashboard/config-navigation'; import { applyFilter, groupedData, getAllItems } from './utils'; // ---------------------------------------------------------------------- function Searchbar() { const theme = useTheme(); const router = useRouter(); const search = useBoolean(); const lgUp = useResponsive('up', 'lg'); const [searchQuery, setSearchQuery] = useState(''); const navData = useNavData(); const handleClose = useCallback(() => { search.onFalse(); setSearchQuery(''); }, [search]); const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'k' && event.metaKey) { search.onToggle(); setSearchQuery(''); } }; useEventListener('keydown', handleKeyDown); const handleClick = useCallback( (path: string) => { if (path.includes('http')) { window.open(path); } else { router.push(path); } handleClose(); }, [handleClose, router] ); const handleSearch = useCallback((event: React.ChangeEvent) => { setSearchQuery(event.target.value); }, []); const dataFiltered = applyFilter({ inputData: getAllItems({ data: navData }), query: searchQuery, }); const notFound = searchQuery && !dataFiltered.length; const renderItems = () => { const data = groupedData(dataFiltered); return Object.keys(data) .sort((a, b) => -b.localeCompare(a)) .map((group, index) => ( {data[group].map((item) => { const { title, path } = item; const partsTitle = parse(title, match(title, searchQuery)); const partsPath = parse(path, match(path, searchQuery)); return ( handleClick(path)} /> ); })} )); }; const renderButton = ( {lgUp && } ); return ( <> {renderButton} } endAdornment={} inputProps={{ sx: { typography: 'h6' }, }} /> {notFound ? : renderItems()} ); } export default memo(Searchbar);