import LinkTag from "next/link"; import { ReactNode, useCallback, useMemo, useState } from "react"; import { FcGoogle } from "react-icons/fc"; import useAutoFocus from "../../hooks/useAutoFocus"; import FormLayout from "../FormLayout"; import LinkFavicon from "../Links/LinkFavicon"; import Modal from "../Modal/Modal"; import TextBox from "../TextBox"; import { Category, ItemComplete, Link } from "../../types"; import styles from "./search.module.scss"; export default function SearchModal({ close, handleSelectCategory, categories, favorites, items, }: { close: any; handleSelectCategory: (category: Category) => void; categories: Category[]; favorites: Link[]; items: ItemComplete[]; }) { const autoFocusRef = useAutoFocus(); const [search, setSearch] = useState(""); const canSubmit = useMemo(() => search.length > 0, [search]); const itemsCompletion = useMemo(() => { if (search.length === 0) { return []; } return items.filter((item) => item.name.toLocaleLowerCase().includes(search.toLocaleLowerCase().trim()) ); }, [items, search]); const handleSubmit = useCallback( (event) => { event.preventDefault(); setSearch(""); if (itemsCompletion.length > 0) { const firstItem = itemsCompletion[0]; if (firstItem.type === "link") { window.open(firstItem.url); } else { const category = categories.find((c) => c.id === firstItem.id); console.log(category); if (category) { handleSelectCategory(category); } } } else { window.open(`https://google.com/search?q=${encodeURI(search.trim())}`); } close(); }, [categories, close, handleSelectCategory, itemsCompletion, search] ); return ( setSearch(value)} value={search} placeholder="Rechercher" innerRef={autoFocusRef} fieldClass={styles["search-input-field"]} /> {search.length === 0 && favorites.length > 0 && ( ({ id: favorite.id, name: favorite.name, url: favorite.url, type: "link", }))} noItem={

ajouter un favoris

} /> )} {search.length > 0 && ( ({ id: item.id, name: item.name, url: item.url, type: item.type, }))} noItem={ Recherche avec{" "} oogle } /> )}
); } function ListItemComponent({ items, noItem, }: { items: ItemComplete[]; noItem?: ReactNode; }) { return ( ); } function ItemComponent({ item }: { item: ItemComplete }) { const { name, type, url } = item; return (
  • {type === "link" ? ( ) : ( category )} {name}
  • ); }