import styles from "@/styles/modules/Nav.module.scss"; import Image from "next/image"; import Link from "next/link"; import { useEffect, useState } from "react"; import { Category } from "@prisma/client"; function switchTheme(theme: string) { const bodyElement = document.getElementsByTagName("body")[0]; if (theme == "dark") { bodyElement.classList.remove("theme-light"); } else { bodyElement.classList.add("theme-light"); } } function toggleTheme() { const svgElement = document.getElementById("themeSwitchSvg"); if (svgElement) { if (localStorage.getItem("theme") == "light") { svgElement.style.animationDirection = "normal"; svgElement.style.animationName = styles.spinThemeSwitch; } else { svgElement.style.animationDirection = "reverse"; svgElement.style.animationName = styles.spinThemeSwitch; } setTimeout(() => { if (localStorage.getItem("theme") == "light") { localStorage.setItem("theme", "dark"); switchTheme("dark"); } else { localStorage.setItem("theme", "light"); switchTheme("light"); } svgElement.style.animationName = ""; }, 150); } } export default function Nav({ categories }: { categories: Category[] }) { const [searchResults, setSearchResults] = useState< { name: string; title: string }[] >([]); async function handleSearchInput(event: React.ChangeEvent) { const query = event.target.value; let result = await fetch(`/api/search?q=${query}`); let json = await result.json(); if (json.length == 0 && query.length > 0) { setSearchResults([{ name: "", title: "No article found..." }]); } else { setSearchResults(json); } } useEffect(() => { if (localStorage.getItem("theme") == "dark") { switchTheme("dark"); } else { switchTheme("light"); } }, []); useEffect(() => { console.log(searchResults); }, [searchResults]); return ( ); }