import { Autocomplete, Box, darken, lighten, Stack, styled, TextField, useTheme } from '@mui/material'; import Typography from '@mui/material/Typography'; import SearchIcon from '@mui/icons-material/Search'; import Grid from '@mui/material/Grid'; import { useState } from 'react'; import { DefinedTool } from '@tools/defineTool'; import { filterTools, tools } from '@tools/index'; import { useNavigate } from 'react-router-dom'; import { Icon } from '@iconify/react'; import { getToolCategoryTitle } from '@utils/string'; import { useTranslation } from 'react-i18next'; import { validNamespaces } from '../i18n'; import { getBookmarkedToolPaths, isBookmarked, toggleBookmarked } from '@utils/bookmark'; import IconButton from '@mui/material/IconButton'; const GroupHeader = styled('div')(({ theme }) => ({ position: 'sticky', top: '-8px', padding: '4px 10px', color: theme.palette.primary.main, backgroundColor: lighten(theme.palette.primary.light, 0.85), ...theme.applyStyles('dark', { backgroundColor: darken(theme.palette.primary.main, 0.8) }) })); const GroupItems = styled('ul')({ padding: 0 }); type ToolInfo = { label: string; url: string; }; export default function Hero() { const { t } = useTranslation(validNamespaces); const [inputValue, setInputValue] = useState(''); const theme = useTheme(); const [filteredTools, setFilteredTools] = useState(tools); const [bookmarkedToolPaths, setBookmarkedToolPaths] = useState( getBookmarkedToolPaths() ); const navigate = useNavigate(); const exampleTools: ToolInfo[] = [ { label: t('translation:hero.examples.createTransparentImage'), url: '/image-generic/create-transparent' }, { label: t('translation:hero.examples.prettifyJson'), url: '/json/prettify' }, { label: t('translation:hero.examples.changeGifSpeed'), url: '/gif/change-speed' }, { label: t('translation:hero.examples.sortList'), url: '/list/sort' }, { label: t('translation:hero.examples.compressPng'), url: '/png/compress-png' }, { label: t('translation:hero.examples.splitText'), url: '/string/split' }, { label: t('translation:hero.examples.splitPdf'), url: '/pdf/split-pdf' }, { label: t('translation:hero.examples.trimVideo'), url: '/video/trim' }, { label: t('translation:hero.examples.calculateNumberSum'), url: '/number/sum' } ]; const handleInputChange = ( event: React.ChangeEvent<{}>, newInputValue: string ) => { setInputValue(newInputValue); setFilteredTools(filterTools(tools, newInputValue, t)); }; const toolsMap = new Map(); for (const tool of filteredTools) { toolsMap.set(tool.path, { label: tool.name, url: '/' + tool.path }); } const displayedTools = bookmarkedToolPaths.length > 0 ? bookmarkedToolPaths.flatMap((path) => { const tool = toolsMap.get(path); if (tool === undefined) { return []; } return [{ ...tool, label: t(tool.label) }]; }) : exampleTools; return ( {t('translation:hero.title')}{' '} {t('translation:hero.brand')} {t('translation:hero.description')} option.type} renderGroup={(params) => { return (
  • {getToolCategoryTitle(params.group, t)} {params.children}
  • ); }} inputValue={inputValue} getOptionLabel={(option) => t(option.name)} renderInput={(params) => ( , sx: { borderRadius: 4, backgroundColor: 'background.paper' } }} onChange={(event) => handleInputChange(event, event.target.value)} /> )} renderOption={(props, option) => ( navigate('/' + option.path)} > {t(option.name)} {t(option.shortDescription)} { e.stopPropagation(); toggleBookmarked(option.path); setBookmarkedToolPaths(getBookmarkedToolPaths()); }} > )} onChange={(event, newValue) => { if (newValue) { navigate('/' + newValue.path); } }} /> {displayedTools.map((tool) => ( navigate(tool.url.startsWith('/') ? tool.url : `/${tool.url}`) } item xs={12} md={6} lg={4} key={tool.label} > {tool.label} {bookmarkedToolPaths.length > 0 && ( { e.stopPropagation(); const path = tool.url.substring(1); toggleBookmarked(path); setBookmarkedToolPaths(getBookmarkedToolPaths()); }} size={'small'} > )} ))}
    ); }