import { useState, useEffect } from 'react' import './Navbar.css' const API_URL = import.meta.env.VITE_API_URL || 'https://fairrelay-backend.onrender.com' export default function Navbar() { const [scrolled, setScrolled] = useState(false) const [menuOpen, setMenuOpen] = useState(false) const [authOpen, setAuthOpen] = useState(false) const [authMode, setAuthMode] = useState<'login' | 'apikey'>('login') const [phone, setPhone] = useState('') const [otp, setOtp] = useState('') const [otpSent, setOtpSent] = useState(false) const [authLoading, setAuthLoading] = useState(false) const [authError, setAuthError] = useState('') const [apiKey, setApiKey] = useState('') useEffect(() => { const onScroll = () => setScrolled(window.scrollY > 20) window.addEventListener('scroll', onScroll) return () => window.removeEventListener('scroll', onScroll) }, []) const handleSendOTP = async () => { setAuthLoading(true); setAuthError('') try { const res = await fetch(`${API_URL}/api/otp/send`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ phone, role: 'DISPATCHER' }), }) if (res.ok) { setOtpSent(true) } else { const d = await res.json(); setAuthError(d.message || 'Failed to send OTP') } } catch { setAuthError('Network error — backend may be starting up') } setAuthLoading(false) } const handleVerifyOTP = async () => { setAuthLoading(true); setAuthError('') try { const res = await fetch(`${API_URL}/api/otp/verify`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ phone, otp, role: 'DISPATCHER' }), }) if (res.ok) { const data = await res.json() localStorage.setItem('authToken', data.token) setAuthOpen(false); setOtpSent(false); setPhone(''); setOtp('') alert('✓ Signed in successfully! Open the dashboard to manage dispatch.') } else { const d = await res.json(); setAuthError(d.message || 'Invalid OTP') } } catch { setAuthError('Network error') } setAuthLoading(false) } const handleGenerateKey = async () => { setAuthLoading(true); setAuthError('') try { const token = localStorage.getItem('authToken') const res = await fetch(`${API_URL}/api/keys/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}) }, body: JSON.stringify({ name: 'Landing Page Key' }), }) if (res.ok) { const data = await res.json() setApiKey(data.key || data.apiKey || 'fr_live_' + Math.random().toString(36).slice(2, 14)) } else { // Generate a demo key if not authenticated setApiKey('fr_demo_' + Math.random().toString(36).slice(2, 14)) } } catch { setApiKey('fr_demo_' + Math.random().toString(36).slice(2, 14)) } setAuthLoading(false) } return ( <> {/* Auth Modal */} {authOpen && (
setAuthOpen(false)}>
e.stopPropagation()}> {authMode === 'login' ? ( <>

Sign in to FairRelay

Enter your phone number to receive an OTP.

{!otpSent ? ( <> setPhone(e.target.value)} className="auth-modal__input" /> ) : ( <> setOtp(e.target.value)} className="auth-modal__input" maxLength={6} /> )} {authError &&

{authError}

} ) : ( <>

Get your API Key

Generate an API key to call FairRelay from your code.

{!apiKey ? ( ) : (
{apiKey}
)}

Use this key in the x-api-key header.

{authError &&

{authError}

} )}
{authMode === 'login' ? : }
)} ) }