"use client"; import { useEffect, useState, useRef } from "react"; import Link from "next/link"; import { useTheme } from "next-themes"; import { Search, Moon, Sun, Globe, Menu, X, PanelLeft, Star, Eye } from "lucide-react"; import { Github } from "./Icons"; import { motion, AnimatePresence } from "framer-motion"; import { LANGUAGES, useLanguage, useTranslations } from "@/lib/i18n"; import { useSidebar } from "@/lib/SidebarContext"; export default function Navbar() { const [mounted, setMounted] = useState(false); const [dark, setDark] = useState(false); const [bannerVisible, setBannerVisible] = useState(true); const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const [langOpen, setLangOpen] = useState(false); const [scrollProgress, setScrollProgress] = useState(0); const [searchQuery, setSearchQuery] = useState(""); const [githubStars, setGithubStars] = useState(null); const langRef = useRef(null); const searchRef = useRef(null); const { lang, setLang } = useLanguage(); const { toggleSidebar } = useSidebar(); const t = useTranslations(); const LOCAL_SECTIONS = [ { id: "what-are-skills", label: t.sidebar.whatAreSkills }, { id: "how-it-works", label: t.sidebar.howItWorks }, { id: "directory", label: t.sidebar.directory }, { id: "quality-standards", label: t.sidebar.qualityStandards }, { id: "using-skills", label: t.sidebar.usingSkills }, { id: "creating-skills", label: t.sidebar.creatingSkills }, { id: "tutorials", label: t.sidebar.tutorials }, { id: "faq", label: t.sidebar.faq }, { id: "contributing", label: t.sidebar.contributing } ]; const mobileNavItems = [ { id: "what-are-skills", label: t.sidebar.whatAreSkills }, { id: "directory", label: t.sidebar.directory }, { id: "quality-standards", label: t.sidebar.qualityStandards }, { id: "using-skills", label: t.sidebar.usingSkills }, { id: "creating-skills", label: t.sidebar.creatingSkills }, { id: "tutorials", label: t.sidebar.tutorials }, { id: "trends", label: t.sidebar.trends }, { id: "faq", label: t.sidebar.faq } ]; useEffect(() => { setMounted(true); setDark(document.documentElement.classList.contains("dark")); // Fetch GitHub Stars fetch("https://api.github.com/repos/heilcheng/awesome-agent-skills") .then(res => res.json()) .then(data => { if (data && data.stargazers_count) { setGithubStars(data.stargazers_count); } }) .catch((e) => console.log("Failed to fetch stars", e)); }, []); // Scroll progress useEffect(() => { const handleScroll = () => { const scrollTop = window.scrollY; const docHeight = document.documentElement.scrollHeight - window.innerHeight; setScrollProgress(docHeight > 0 ? scrollTop / docHeight : 0); }; window.addEventListener("scroll", handleScroll, { passive: true }); return () => window.removeEventListener("scroll", handleScroll); }, []); // Keyboard shortcut: Cmd+K useEffect(() => { const handler = (e: KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key === "k") { e.preventDefault(); searchRef.current?.focus(); } }; window.addEventListener("keydown", handler); return () => window.removeEventListener("keydown", handler); }, []); // Close language dropdown on outside click useEffect(() => { const handler = (e: MouseEvent) => { if (langRef.current && !langRef.current.contains(e.target as Node)) { setLangOpen(false); } }; document.addEventListener("mousedown", handler); return () => document.removeEventListener("mousedown", handler); }, []); const toggleDark = () => { const isDark = !dark; setDark(isDark); document.documentElement.classList.toggle("dark", isDark); localStorage.setItem("theme", isDark ? "dark" : "light"); }; if (!mounted) return null; const currentLang = LANGUAGES.find((l) => l.code === lang); const filterSearch = LOCAL_SECTIONS.filter(s => s.label.toLowerCase().includes(searchQuery.toLowerCase())); return (
{/* Sponsored Banner */} {bannerVisible && (
{t.nav.sponsor} Your ad here. Contact haileycheng@proton.me for sponsorship.
)}
); }