"use client"; import { AnimatePresence, motion } from "motion/react"; import { useSearchParams } from "next/navigation"; import { useTranslations } from "next-intl"; import { Suspense, useEffect, useState } from "react"; import { toast } from "sonner"; import { Logo } from "@/components/Logo"; import { useGlobalLoadingEffect } from "@/hooks/use-global-loading"; import { getAuthErrorDetails, shouldRetry } from "@/lib/auth-errors"; import { AUTH_TYPE } from "@/lib/env-config"; import { AmbientBackground } from "./AmbientBackground"; import { GoogleLoginButton } from "./GoogleLoginButton"; import { LocalLoginForm } from "./LocalLoginForm"; function LoginContent() { const t = useTranslations("auth"); const tCommon = useTranslations("common"); const [authType, setAuthType] = useState(null); const [isLoading, setIsLoading] = useState(true); const [urlError, setUrlError] = useState<{ title: string; message: string } | null>(null); const searchParams = useSearchParams(); useEffect(() => { // Check for various URL parameters that might indicate success or error states const registered = searchParams.get("registered"); const error = searchParams.get("error"); const message = searchParams.get("message"); const logout = searchParams.get("logout"); // Show registration success message if (registered === "true") { toast.success(t("register_success"), { description: t("login_subtitle"), duration: 5000, }); } // Show logout confirmation if (logout === "true") { toast.success(tCommon("success"), { description: "You have been securely logged out", duration: 3000, }); } // Show error messages from OAuth or other flows using auth-errors utility if (error) { // Use the auth-errors utility to get proper error details const errorDetails = getAuthErrorDetails(error); // If we have a custom message from URL params, use it as description const errorDescription = message ? decodeURIComponent(message) : errorDetails.description; // Set persistent error display setUrlError({ title: errorDetails.title, message: errorDescription, }); // Show toast with conditional retry action const toastOptions: { description: string; duration: number; action?: { label: string; onClick: () => void }; } = { description: errorDescription, duration: 6000, }; // Add retry action if the error is retryable if (shouldRetry(error)) { toastOptions.action = { label: "Retry", onClick: () => window.location.reload(), }; } toast.error(errorDetails.title, toastOptions); } // Show general messages if (message && !error && !registered && !logout) { toast.info("Notice", { description: decodeURIComponent(message), duration: 4000, }); } // Get the auth type from centralized config setAuthType(AUTH_TYPE); setIsLoading(false); }, [searchParams, t, tCommon]); // Use global loading screen for auth type determination - spinner animation won't reset useGlobalLoadingEffect(isLoading, tCommon("loading"), "login"); // Show nothing while loading - the GlobalLoadingProvider handles the loading UI if (isLoading) { return null; } if (authType === "GOOGLE") { return ; } return (

{t("sign_in")}

{/* URL Error Display */} {urlError && (
Error Icon

{urlError.title}

{urlError.message}

)}
); } export default function LoginPage() { // Suspense fallback returns null - the GlobalLoadingProvider handles the loading UI return ( ); }