import { useRef, useState } from 'react'; import { Wallet, LogOut, Loader2, Camera } from 'lucide-react'; import Logo from './Logo'; interface NavbarProps { user: any; onRechargeClick: () => void; onLogout: () => void; onLogoClick?: () => void; onAvatarUpload?: (newUrl: string) => void; } const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000'; export default function Navbar({ user, onRechargeClick, onLogout, onLogoClick, onAvatarUpload }: NavbarProps) { const fileInputRef = useRef(null); const [uploading, setUploading] = useState(false); const handleAvatarClick = () => { if (fileInputRef.current && !uploading) { fileInputRef.current.click(); } }; const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; if (!file.type.startsWith('image/')) { alert('Please upload an image file.'); return; } setUploading(true); const formData = new FormData(); formData.append('file', file); const token = localStorage.getItem('quantiq_jwt'); try { const response = await fetch(`${API_URL}/api/v1/users/avatar`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}` }, body: formData, }); if (!response.ok) { throw new Error('Failed to upload avatar'); } const data = await response.json(); if (data.picture_url && onAvatarUpload) { onAvatarUpload(data.picture_url); } } catch (err) { console.error('Avatar upload error:', err); alert('Failed to upload avatar. Please try again.'); } finally { setUploading(false); if (fileInputRef.current) { fileInputRef.current.value = ''; } } }; return (
QuantIQ
{user && (
{user.credits} Credits
)}
{user?.pictureUrl ? ( Avatar ) : ( )}
{uploading && (
)}
{user?.fullName || 'User'}
); }