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"; import prisma, { CategoryWithIncludes } from "@/lib/prisma"; import { CLIENT_RENEG_LIMIT } from "tls"; import { apiUrl } from "@/global"; import urlJoin from "url-join"; function switchTheme(theme: string) { const bodyElement = document.getElementsByTagName("body")[0]; if (theme == "dark") { bodyElement.classList.add("theme-light"); } else { bodyElement.classList.remove("theme-light"); localStorage.setItem("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() { const [searchResults, setSearchResults] = useState<{ name: string; title: string }[]>([]); const [categories, setCategories] = useState([]); 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(() => { async function getCategories() { await fetch(urlJoin(apiUrl, "categories")) .then((response) => response.json()) .then((result: Category[]) => { setCategories(result); }); } getCategories(); }, []); return ( ); } export async function getServerSideProps() { let categories: Category[] = []; await prisma.category.findMany({ include: { articles: true, svg: true } }).then( (result: Category[]) => { if (result) { categories = JSON.parse(JSON.stringify(result)); } }, (reason: any) => { console.log(reason); } ); return { props: { categories: categories }, // will be passed to the page component as props }; }