// src/components/Login.tsx
import AtpAgent, { Agent } from "@atproto/api";
import { useAtom } from "jotai";
import React, { useEffect, useRef, useState } from "react";
import { useAuth } from "~/providers/UnifiedAuthProvider";
import { imgCDNAtom } from "~/utils/atoms";
import { useQueryIdentity, useQueryProfile } from "~/utils/useQuery";
// --- 1. The Main Component (Orchestrator with `compact` prop) ---
export default function Login({
compact = false,
popup = false,
}: {
compact?: boolean;
popup?: boolean;
}) {
const { status, agent, logout } = useAuth();
// Loading state can be styled differently based on the prop
if (status === "loading") {
return (
);
}
// --- LOGGED IN STATE ---
if (status === "signedIn") {
// Large view
if (!compact) {
return (
You are logged in!
);
}
// Compact view
return (
);
}
// --- LOGGED OUT STATE ---
if (!compact) {
// Large view renders the form directly in the card
return (
);
}
// Compact view renders a button that toggles the form in a dropdown
return ;
}
// --- 2. The Reusable, Self-Contained Login Form Component ---
export function UnifiedLoginForm() {
const [mode, setMode] = useState<"oauth" | "password">("oauth");
return (
setMode("oauth")}
/>
setMode("password")}
/>
{mode === "oauth" ? : }
);
}
// --- 3. Helper components for layouts, forms, and UI ---
// A new component to contain the logic for the compact dropdown
const CompactLoginButton = ({ popup }: { popup?: boolean }) => {
const [showForm, setShowForm] = useState(false);
const formRef = useRef(null);
useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (formRef.current && !formRef.current.contains(event.target as Node)) {
setShowForm(false);
}
}
if (showForm) {
document.addEventListener("mousedown", handleClickOutside);
}
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [showForm]);
return (