Something went wrong. Try again.
Coffee journaling on ATProto (alpha) forked from arabica.social/arabica
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134/** * HeaderNav - Preact component for navigation with user dropdown * Replaces Alpine.js dropdown in header.tmpl */import { html, useState, useEffect, useRef } from '../lib/preact.js';
export function HeaderNav({ userProfile, userDID, isAuthenticated }) { const [open, setOpen] = useState(false); const dropdownRef = useRef(null); const buttonRef = useRef(null);
// Handle click outside to close dropdown useEffect(() => { if (!open) return;
const handleClickOutside = (event) => { if ( dropdownRef.current && buttonRef.current && !dropdownRef.current.contains(event.target) && !buttonRef.current.contains(event.target) ) { setOpen(false); } };
document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, [open]);
// Handle escape key to close dropdown useEffect(() => { if (!open) return;
const handleEscape = (event) => { if (event.key === 'Escape') { setOpen(false); } };
document.addEventListener('keydown', handleEscape); return () => document.removeEventListener('keydown', handleEscape); }, [open]);
if (!isAuthenticated) { return null; }
const handleLogout = () => { if (window.ArabicaCache) { window.ArabicaCache.invalidateCache(); } };
const displayName = userProfile?.DisplayName || userProfile?.Handle || '?'; const handle = userProfile?.Handle || userDID; const avatar = userProfile?.Avatar; const initial = displayName.charAt(0).toUpperCase();
return html` <div class="relative"> <button ref=${buttonRef} onClick=${() => setOpen(!open)} class="flex items-center gap-2 hover:opacity-80 transition focus:outline-none" aria-expanded=${open} aria-haspopup="true"> ${avatar ? html` <img src=${avatar} alt="" class="w-8 h-8 rounded-full object-cover ring-2 ring-brown-600" /> ` : html` <div class="w-8 h-8 rounded-full bg-brown-600 flex items-center justify-center ring-2 ring-brown-500"> <span class="text-sm font-medium">${initial}</span> </div> `} <svg class="w-4 h-4 transition-transform ${open ? 'rotate-180' : ''}" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path> </svg> </button>
${open && html` <div ref=${dropdownRef} class="absolute right-0 mt-2 w-48 bg-white rounded-lg shadow-lg border border-brown-200 py-1 z-50 animate-dropdown"> ${userProfile?.Handle && html` <div class="px-4 py-2 border-b border-brown-100"> <p class="text-sm font-medium text-brown-900 truncate"> ${userProfile.DisplayName || userProfile.Handle} </p> <p class="text-xs text-brown-500 truncate">@${userProfile.Handle}</p> </div> `} <a href="/profile/${handle}" class="block px-4 py-2 text-sm text-brown-700 hover:bg-brown-50 transition-colors"> View Profile </a> <a href="/brews" class="block px-4 py-2 text-sm text-brown-700 hover:bg-brown-50 transition-colors"> My Brews </a> <a href="/manage" class="block px-4 py-2 text-sm text-brown-700 hover:bg-brown-50 transition-colors"> Manage Records </a> <a href="#" class="block px-4 py-2 text-sm text-brown-400 cursor-not-allowed"> Settings (coming soon) </a> <div class="border-t border-brown-100 mt-1 pt-1"> <form action="/logout" method="POST" onSubmit=${handleLogout}> <button type="submit" class="w-full text-left px-4 py-2 text-sm text-brown-700 hover:bg-brown-50 transition-colors"> Logout </button> </form> </div> </div> `} </div> `;}