import { usePage, Link, router } from '@inertiajs/react'; import React, { useState, useRef, useEffect } from 'react'; import { PageProps } from '@/types'; export function Topbar({ theme, onThemeToggle, onMenuToggle, sidebarOpen }: { theme: string; onThemeToggle: () => void; onMenuToggle: () => void; sidebarOpen: boolean }) { const props = usePage().props as any; const { auth, system_settings, unread_notifications } = props; const { user, roles } = auth; const [showDropdown, setShowDropdown] = useState(false); const dropdownRef = useRef(null); const [searchQuery, setSearchQuery] = useState(''); const [searchResults, setSearchResults] = useState([]); const [isSearching, setIsSearching] = useState(false); const searchInputRef = useRef(null); const path = window.location.pathname; useEffect(() => { function handleClickOutside(event: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setShowDropdown(false); } } document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); // Global Search Logic useEffect(() => { if (searchQuery.length < 2) { setSearchResults([]); return; } const timer = setTimeout(async () => { setIsSearching(true); try { const response = await fetch(`/api/search?query=${encodeURIComponent(searchQuery)}`); const data = await response.json(); setSearchResults(data); } catch (error) { console.error('Search error:', error); } finally { setIsSearching(false); } }, 300); return () => clearTimeout(timer); }, [searchQuery]); // Keyboard Shortcuts useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key === 'k') { e.preventDefault(); searchInputRef.current?.focus(); } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, []); const handleLogout = () => { router.post('/logout'); }; const initials = `${user?.first_name?.charAt(0) || ''}${user?.last_name?.charAt(0) || ''}`.toUpperCase(); const segments = path.split('/').filter(Boolean); const formatSegment = (s: string) => s.replace(/-/g, ' ').replace(/\b\w/g, c => c.toUpperCase()); const pageTitle = segments.length > 0 ? formatSegment(segments[0]) : 'Dashboard'; const breadcrumbs = segments.length > 1 ? segments.map((s, i) => ({ label: formatSegment(s), href: '/' + segments.slice(0, i + 1).join('/') })) : null; return (
{/* Left: Breadcrumb area */}
{/* Burger — mobile only */}

{pageTitle}

{system_settings?.app_name || 'biiproject kit'} {breadcrumbs ? breadcrumbs.map((crumb, i) => ( {i === breadcrumbs.length - 1 ? {crumb.label} : {crumb.label} } )) : ( <> {pageTitle} )}
{/* Global Search */}
setSearchQuery(e.target.value)} className="w-full h-10 pl-10 pr-4 rounded-xl border border-gray-100 dark:border-white/5 bg-white dark:bg-white/5 text-xs font-bold text-[#3D4E4B] dark:text-white placeholder-gray-400 focus:outline-none focus:border-[#D4A017] focus:ring-4 focus:ring-[#D4A017]/5 transition-all" /> {isSearching && (
)}
{/* Search Results Dropdown */} {searchResults.length > 0 && (
{searchResults.map((result, idx) => ( setSearchQuery('')} className="flex items-center gap-3 p-3 rounded-xl hover:bg-gray-50 dark:hover:bg-white/5 transition-all group" >
{result.icon === 'user' && } {result.icon === 'shield' && } {result.icon === 'clock' && }
{result.title}
{result.type} • {result.subtitle}
))}
)}
{/* Right: Actions Area */}
{/* Theme Toggle */} {unread_notifications > 0 && ( {unread_notifications > 99 ? '99+' : unread_notifications} )}
{showDropdown && (
{user?.first_name} {user?.last_name}
{user?.email}
Account Settings
)}
); }