"use client" import { Button } from "@/components/ui/button" import { Sections, type translations } from "@/i18n/translations" import { cn } from "@/lib/utils" import { MoonIcon, SunIcon } from "lucide-react" import { useCallback, useEffect, useState } from "react" import { MobileNav } from "./mobile-nav" const linksOrder: Array = [ "about", "where", "when", "tickets", "accommodation", "food", "contact", ] function useTheme(): [theme: "light" | "dark", setTheme: (theme: "light" | "dark") => void] { /* eslint-disable react-hooks/rules-of-hooks */ if (typeof window === "undefined") return ["dark", () => { }] const [theme, setTheme] = useState<"light" | "dark">("dark") const root = window.document.documentElement const changeTheme = useCallback((theme: "light" | "dark") => { root.classList.remove("light", "dark") root.classList.add(theme) }, [root]) useEffect(() => { // Determine the user's preferred color scheme const preferredTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light" setTheme(preferredTheme) changeTheme(preferredTheme) }, [changeTheme]) const updateTheme = useCallback((theme: "light" | "dark") => { setTheme(theme) changeTheme(theme) }, [changeTheme, setTheme]) return [theme, updateTheme] /* eslint-enable react-hooks/rules-of-hooks */ } export function Nav({ t, }: { t: typeof translations.pl }) { const [theme, setTheme] = useTheme() const [activeSection, setActiveSection] = useState("about") useEffect(() => { const options = { root: null, rootMargin: "-10px", threshold: 0.5, // Adjust the visibility threshold as needed }; const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { const target = entry.target.id as keyof (typeof translations.pl)["nav"] if (entry.isIntersecting) { setActiveSection(target); if (history.replaceState) { history.replaceState(null, "", `#${target}`); } } }); }, options); const sections = linksOrder.map(value => document.getElementById(value)); sections.forEach(section => { if (section) { observer.observe(section); } }); return () => { sections.forEach(section => { if (section) { observer.unobserve(section); } }); }; }, []); return ( ) }