import React, { useState, useRef, useEffect } from 'react'; import { useAuth } from '@/providers/OAuthProvider'; interface AuthButtonProps { compact?: boolean; } export default function Login({ compact = false }: AuthButtonProps) { // 1. Get state and functions from the new OAuth context const { status, startLogin, logout } = useAuth(); // State for the handle input and the dropdown visibility const [handle, setHandle] = useState(''); const [showLoginForm, setShowLoginForm] = useState(false); const formRef = useRef(null); useEffect(() => { // This logic for closing the dropdown on outside click is still useful function handleClickOutside(event: MouseEvent) { if (formRef.current && !formRef.current.contains(event.target as Node)) { setShowLoginForm(false); } } if (showLoginForm) { document.addEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [showLoginForm]); // Handle the form submission const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); if (!handle.trim()) { alert('Please enter your handle (e.g., name.example.com)'); return; } // This will redirect the user, so no need to manage loading states here await startLogin(handle); }; // Render loading state if the provider is initializing if (status === 'loading') { return (
Loading...
); } // If logged in, show a logout button if (status === 'signedIn') { const buttonClass = compact ? "text-sm bg-gray-600 hover:bg-gray-700 text-white rounded px-3 py-1 font-medium transition-colors" : "bg-gray-600 hover:bg-gray-700 text-white rounded px-6 py-2 font-semibold text-base transition-colors"; const loggedInContent = ( ); if (compact) { return loggedInContent; } return (

You are logged in!

{loggedInContent}
); } // If logged out, show a login button/form const loginButtonClass = compact ? "text-sm bg-gray-600 hover:bg-gray-700 text-white rounded px-3 py-1 font-medium transition-colors" : "bg-gray-600 hover:bg-gray-700 text-white rounded px-6 py-2 font-semibold text-base transition-colors mt-2"; const loginForm = (

Login with your AT Protocol (Bluesky) handle

setHandle(e.target.value)} className="px-3 py-2 rounded border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 text-base focus:outline-none focus:ring-2 focus:ring-blue-500" autoComplete="webauthn" // Hint for password managers />
); if (compact) { return (
{showLoginForm && (
{loginForm}
)}
); } return (
{loginForm}
); }