'use client' import { useState, useEffect, useRef } from 'react' import { Input } from '@/components/ui/input' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger, } from '@/components/ui/sheet' import { Menu, Search, StickyNote, Tag, Moon, Sun, X, Bell, Sparkles, Grid3x3, Settings, LogOut, User, Shield, Coffee } from 'lucide-react' import Link from 'next/link' import { usePathname, useRouter, useSearchParams } from 'next/navigation' import { cn } from '@/lib/utils' import { useLabels } from '@/context/LabelContext' import { LabelFilter } from './label-filter' import { NotificationPanel } from './notification-panel' import { updateTheme } from '@/app/actions/profile' import { useDebounce } from '@/hooks/use-debounce' import { useLanguage } from '@/lib/i18n' import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' import { useSession, signOut } from 'next-auth/react' interface HeaderProps { selectedLabels?: string[] selectedColor?: string | null onLabelFilterChange?: (labels: string[]) => void onColorFilterChange?: (color: string | null) => void user?: any } export function Header({ selectedLabels = [], selectedColor = null, onLabelFilterChange, onColorFilterChange, user }: HeaderProps = {}) { const [searchQuery, setSearchQuery] = useState('') const [theme, setTheme] = useState<'light' | 'dark'>('light') const [isSidebarOpen, setIsSidebarOpen] = useState(false) const [isSemanticSearching, setIsSemanticSearching] = useState(false) const pathname = usePathname() const router = useRouter() const searchParams = useSearchParams() const { labels, setNotebookId } = useLabels() const { t } = useLanguage() const { data: session } = useSession() // Track last pushed search to avoid infinite loops const lastPushedSearch = useRef(null) const currentLabels = searchParams.get('labels')?.split(',').filter(Boolean) || [] const currentSearch = searchParams.get('search') || '' const currentColor = searchParams.get('color') || '' const currentUser = user || session?.user // Initialize search query from URL ONLY on mount useEffect(() => { setSearchQuery(currentSearch) lastPushedSearch.current = currentSearch }, []) // Run only once on mount // Sync LabelContext notebookId with URL notebook parameter const currentNotebook = searchParams.get('notebook') useEffect(() => { setNotebookId(currentNotebook || null) }, [currentNotebook, setNotebookId]) // Simple debounced search with URL update (150ms for more responsiveness) const debouncedSearchQuery = useDebounce(searchQuery, 150) useEffect(() => { // Skip if search hasn't changed or if we already pushed this value if (debouncedSearchQuery === lastPushedSearch.current) return // Build new params preserving other filters const params = new URLSearchParams(searchParams.toString()) if (debouncedSearchQuery.trim()) { params.set('search', debouncedSearchQuery) } else { params.delete('search') } const newUrl = `/?${params.toString()}` // Mark as pushed before calling router.push to prevent loops lastPushedSearch.current = debouncedSearchQuery router.push(newUrl) }, [debouncedSearchQuery]) // Handle semantic search button click const handleSemanticSearch = () => { if (!searchQuery.trim()) return // Add semantic flag to URL const params = new URLSearchParams(searchParams.toString()) params.set('search', searchQuery) params.set('semantic', 'true') router.push(`/?${params.toString()}`) // Show loading state briefly setIsSemanticSearching(true) setTimeout(() => setIsSemanticSearching(false), 1500) } useEffect(() => { const savedTheme = currentUser?.theme || localStorage.getItem('theme') || 'light' // Don't persist on initial load to avoid unnecessary DB calls applyTheme(savedTheme, false) }, [currentUser]) const applyTheme = async (newTheme: string, persist = true) => { setTheme(newTheme as any) localStorage.setItem('theme', newTheme) // Remove all theme classes first document.documentElement.classList.remove('dark') document.documentElement.removeAttribute('data-theme') if (newTheme === 'dark') { document.documentElement.classList.add('dark') } else if (newTheme !== 'light') { document.documentElement.setAttribute('data-theme', newTheme) if (newTheme === 'midnight') { document.documentElement.classList.add('dark') } } if (persist && currentUser) { await updateTheme(newTheme) } } const handleSearch = (query: string) => { setSearchQuery(query) // URL update is now handled by the debounced useEffect } const removeLabelFilter = (labelToRemove: string) => { const newLabels = currentLabels.filter(l => l !== labelToRemove) const params = new URLSearchParams(searchParams.toString()) if (newLabels.length > 0) { params.set('labels', newLabels.join(',')) } else { params.delete('labels') } router.push(`/?${params.toString()}`) } const removeColorFilter = () => { const params = new URLSearchParams(searchParams.toString()) params.delete('color') router.push(`/?${params.toString()}`) } const clearAllFilters = () => { // Clear only label and color filters, keep search const params = new URLSearchParams(searchParams.toString()) params.delete('labels') params.delete('color') router.push(`/?${params.toString()}`) } const handleFilterChange = (newLabels: string[]) => { const params = new URLSearchParams(searchParams.toString()) if (newLabels.length > 0) { params.set('labels', newLabels.join(',')) } else { params.delete('labels') } router.push(`/?${params.toString()}`) } const handleColorChange = (newColor: string | null) => { const params = new URLSearchParams(searchParams.toString()) if (newColor) { params.set('color', newColor) } else { params.delete('color') } router.push(`/?${params.toString()}`) } const toggleLabelFilter = (labelName: string) => { const newLabels = currentLabels.includes(labelName) ? currentLabels.filter(l => l !== labelName) : [...currentLabels, labelName] const params = new URLSearchParams(searchParams.toString()) if (newLabels.length > 0) { params.set('labels', newLabels.join(',')) } else { params.delete('labels') } router.push(`/?${params.toString()}`) } const NavItem = ({ href, icon: Icon, label, active, onClick }: any) => { const content = ( <> {label} ) if (onClick) { return ( ) } return ( setIsSidebarOpen(false)} className={cn( "flex items-center gap-3 px-4 py-3 rounded-r-full text-sm font-medium transition-colors mr-2", active ? "bg-[#EFB162] text-amber-900" : "hover:bg-gray-100 dark:hover:bg-zinc-800 text-gray-700 dark:text-gray-300" )} > {content} ) } const hasActiveFilters = currentLabels.length > 0 || !!currentColor return ( <>
{/* Mobile Menu Button */} {t('nav.workspace')}
{t('labels.title')}
{labels.map(label => ( toggleLabelFilter(label.name)} /> ))}
{/* Search Bar */}
handleSearch(e.target.value)} /> {/* IA Search Button */} {searchQuery && ( )}
{/* Right Side Actions */}
{/* Label Filter */} {/* Grid View Button */} {/* Theme Toggle */} applyTheme('light')}>{t('settings.themeLight')} applyTheme('dark')}>{t('settings.themeDark')} applyTheme('midnight')}>Midnight applyTheme('sepia')}>Sepia {/* Notifications */}
{/* Active Filters Bar */} {hasActiveFilters && (
{currentColor && (
{t('notes.color')}: {currentColor} )} {currentLabels.map(label => ( {label} ))} {(currentLabels.length > 0 || currentColor) && ( )}
)} ) }