import React, { useCallback, useEffect, useState } from 'react'; import { Link, useLocation, useSearchParams } from 'react-router-dom'; import { LogOut, Settings } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; import { apiFetch } from '@/lib/api'; /** * Sign-in (Google). Workspace details and switching context live in Settings / session. OAuth callback sets session. */ export default function GoogleAuthBar() { const location = useLocation(); const [searchParams, setSearchParams] = useSearchParams(); const [phase, setPhase] = useState('loading'); const [googleOn, setGoogleOn] = useState(false); const [user, setUser] = useState(null); const refresh = useCallback(async () => { try { const [st, me] = await Promise.all([ apiFetch('/api/auth/status').then((r) => r.json()), apiFetch('/api/auth/me').then((r) => (r.ok ? r.json() : null)), ]); setGoogleOn(!!st.googleConfigured); setUser(me); setPhase('ready'); if (typeof window !== 'undefined') { window.dispatchEvent(new CustomEvent('emailout-auth-changed')); } } catch { setPhase('error'); } }, []); useEffect(() => { refresh(); }, [refresh]); useEffect(() => { const err = searchParams.get('auth_error'); if (!err) return; if (err === 'access_denied') { console.info('Google sign-in was cancelled.'); } else if (err === 'invite_email_mismatch') { console.warn( 'Invitation email does not match your Google account. Sign in with the invited address.' ); } else { console.warn('Google sign-in error:', err); } const next = new URLSearchParams(searchParams); next.delete('auth_error'); setSearchParams(next, { replace: true }); }, [searchParams, setSearchParams]); const logout = async () => { try { await apiFetch('/api/auth/logout', { method: 'POST' }); setUser(null); if (typeof window !== 'undefined') { window.dispatchEvent(new CustomEvent('emailout-auth-changed')); } } catch (e) { console.error(e); } }; const inviteParam = searchParams.get('invite'); const googleHref = inviteParam ? `/api/auth/google?invite=${encodeURIComponent(inviteParam)}` : '/api/auth/google'; if (phase === 'loading' || (phase === 'ready' && !googleOn)) { if (phase === 'loading') { return (
); } return null; } if (phase === 'error') { return null; } if (user) { const settingsActive = location.pathname === '/settings' || location.pathname.startsWith('/settings/'); return (
{user.picture ? ( ) : null} {user.name || user.email || 'Signed in'}
); } return ( ); } function GoogleMark({ className }) { return ( ); }