"use client"; import { FormEvent, useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { ArrowRight, ChevronLeft, Chrome, CloudRain, CloudSun, Lock, Mail, Sun, Eye, EyeOff, } from "lucide-react"; import { getSupabaseBrowserClient, hasSupabasePublicEnv, } from "@/lib/supabase/client"; import { getConfiguredSiteUrl, PRODUCTION_SITE_URL } from "@/lib/site-url"; import { useI18n } from "@/hooks/useI18n"; type Mode = "login" | "signup"; type LoginClientProps = { nextPath: string; initialMode?: Mode; }; export function LoginClient({ nextPath, initialMode }: LoginClientProps) { const router = useRouter(); const { locale } = useI18n(); const [mode, setMode] = useState(initialMode ?? "login"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [loading, setLoading] = useState(false); const [errorText, setErrorText] = useState(""); const [infoText, setInfoText] = useState(""); const [resetSent, setResetSent] = useState(false); const [showPassword, setShowPassword] = useState(false); const supabaseReady = hasSupabasePublicEnv(); const isLogin = mode === "login"; const siteOrigin = getConfiguredSiteUrl() || (typeof window !== "undefined" ? window.location.origin : PRODUCTION_SITE_URL); const isEn = locale === "en-US"; const copy = { backHome: isEn ? "Back to Home" : "返回首页", subtitle: isEn ? "Explore weather details from every corner of the world" : "探索世界每一个角落的气象细节", googleOneClick: isEn ? "Continue with Google" : "使用 Google 账号一键登录", orEmail: isEn ? "Or continue with email" : "或使用邮箱", login: isEn ? "Sign In" : "登录", signup: isEn ? "Sign Up" : "注册", passwordLoginPlaceholder: isEn ? "Enter password" : "输入密码", passwordSignupPlaceholder: isEn ? "Set at least 6 characters" : "设置至少 6 位密码", loginSubmit: isEn ? "Start your weather journey" : "开启天气交易之旅", signupSubmit: isEn ? "Create account now" : "立即创建账号", loginHint: isEn ? "After signing in, your homepage will be personalized." : "登录后将为您个性化定制首页数据", signupHint: isEn ? "By signing up, you agree to our Terms of Service." : "注册即代表同意我们的服务条款", realtime: isEn ? "Realtime data" : "实时数据", highPrecision: isEn ? "High-precision forecast" : "高精度预测", supabaseMissing: isEn ? "Supabase is not configured. Sign-in is unavailable." : "Supabase 未配置,无法使用登录", needEmailPassword: isEn ? "Please enter email and password." : "请输入邮箱和密码", signupCheckEmail: isEn ? "Sign-up successful. Please verify your email before signing in." : "注册成功,请检查邮箱并完成验证后登录。", reset: isEn ? "Forgot password?" : "忘记密码?", resetSent: isEn ? "Reset link sent. Check your inbox." : "重置链接已发送,请检查收件箱。", resetPlaceholder: isEn ? "Enter your email to reset" : "输入邮箱以重置密码", resendVerify: isEn ? "Didn't receive the verification email? Sign up again with the same email to resend." : "没收到验证邮件?用同一邮箱重新注册即可重发。", loginFailedHint: isEn ? "If you just signed up, please verify your email first. Check your inbox or spam folder." : "如果刚注册,请先点击邮箱中的验证链接。检查收件箱或垃圾邮件。", // New translations for Koyfin-style layouts workEmail: isEn ? "Work email" : "工作邮箱", password: isEn ? "Password" : "密码", welcomeBack: isEn ? "Welcome Back" : "欢迎回来", signUpTitle: isEn ? "Sign up for your PolyWeather account" : "注册您的 PolyWeather 账户", newToPoly: isEn ? "New to PolyWeather?" : "还没有 PolyWeather 账号?", alreadyHave: isEn ? "Already have an account?" : "已经有账号了?", termsAgreement: isEn ? "By proceeding, you agree to the Privacy Policy and Terms & Conditions." : "继续操作即代表您同意隐私政策与服务条款。", desc: isEn ? "Access robust METAR observations, advanced DEB forecast blends, and real-time AI decision cards that bring clarity to your weather-signal portfolios." : "提供精准的机场 METAR 实况、先进的 DEB 智能融合预测和实时 AI 决策卡片,助您看清天气信号脉络。", trusted: isEn ? "Trusted by institutional traders" : "深受机构交易员信赖", } as const; const onResetPassword = async () => { setErrorText(""); setInfoText(""); if (!email.trim()) { setErrorText(copy.resetPlaceholder); return; } if (!supabaseReady) { setErrorText(copy.supabaseMissing); return; } setLoading(true); try { const supabase = getSupabaseBrowserClient(); const { error } = await supabase.auth.resetPasswordForEmail(email.trim(), { redirectTo: `${siteOrigin}/auth/callback?next=${encodeURIComponent( "/account", )}`, }); if (error) { setErrorText(error.message); return; } setResetSent(true); setInfoText(copy.resetSent); } finally { setLoading(false); } }; useEffect(() => { if (!supabaseReady) return; const run = async () => { const supabase = getSupabaseBrowserClient(); const { data: { session }, } = await supabase.auth.getSession(); if (session?.user) { router.replace(nextPath); } }; void run(); }, [nextPath, router, supabaseReady]); const onGoogleSignIn = async () => { setErrorText(""); setInfoText(""); if (!supabaseReady) { setErrorText(copy.supabaseMissing); return; } setLoading(true); try { const supabase = getSupabaseBrowserClient(); const redirectTo = `${siteOrigin}/auth/callback?next=${encodeURIComponent( nextPath, )}`; const { error } = await supabase.auth.signInWithOAuth({ provider: "google", options: { redirectTo, }, }); if (error) { setErrorText(error.message); } } finally { setLoading(false); } }; const onEmailSubmit = async (event: FormEvent) => { event.preventDefault(); setErrorText(""); setInfoText(""); if (!supabaseReady) { setErrorText(copy.supabaseMissing); return; } if (!email.trim() || !password.trim()) { setErrorText(copy.needEmailPassword); return; } setLoading(true); try { const supabase = getSupabaseBrowserClient(); if (mode === "login") { const { error } = await supabase.auth.signInWithPassword({ email: email.trim(), password, }); if (error) { setErrorText(error.message); return; } router.replace(nextPath); return; } const emailRedirectTo = `${siteOrigin}/auth/callback?next=${encodeURIComponent( nextPath, )}`; const { data, error } = await supabase.auth.signUp({ email: email.trim(), password, options: { emailRedirectTo, }, }); if (error) { setErrorText(error.message); return; } if (data.session?.user) { router.replace(nextPath); return; } setInfoText(copy.signupCheckEmail); } finally { setLoading(false); } }; if (mode === "signup") { return (
{/* Left Dark Column */}
{/* Ambient Glows */}
{/* Grid overlay */}
PolyWeather

{isEn ? ( <> Weather intelligence and risk management{" "} simplified. ) : ( <> 天气信息与风险管理{" "} 化繁为简。 )}

{copy.desc}

{/* Sleek Terminal Preview Widget */}
{isEn ? "Runway 02L Settlement" : "跑道 02L 官方结算"}
LIVE
{isEn ? "Current Temp:" : "当前气温:"} 28.8°C
{isEn ? "UMA Threshold:" : "UMA 结算阈值:"} 30.0°C
{isEn ? "Model Probability:" : "模型预测概率:"} 88.5%
{isEn ? "Market Price:" : "市场买卖价差:"} $10.00
{/* Sparkline visualization */}
{[30, 45, 38, 52, 68, 85, 78, 92, 88].map((h, i) => (
))}
{/* Right White Column (Signup Form) */}

{copy.signUpTitle}

{copy.subtitle}

void onEmailSubmit(event)} className="space-y-4">
setEmail(event.target.value)} placeholder="yourname@email.com" className="w-full rounded-lg border border-slate-300 bg-white py-2.5 pl-10 pr-4 text-sm text-slate-950 placeholder:text-slate-400 transition-all focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500/20" />
setPassword(event.target.value)} placeholder={copy.passwordSignupPlaceholder} className="w-full rounded-lg border border-slate-300 bg-white py-2.5 pl-10 pr-10 text-sm text-slate-950 placeholder:text-slate-400 transition-all focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500/20" />

{copy.termsAgreement}

{isEn ? "or" : "或"}
{errorText ?

{errorText}

: null} {infoText ?

{infoText}

: null}

{copy.alreadyHave}{" "}

); } return (
{/* Top Header */}
PolyWeather
{copy.newToPoly}
{/* Main Login Card Area */}

{copy.welcomeBack}

{copy.subtitle}

void onEmailSubmit(event)} className="space-y-4">
setEmail(event.target.value)} placeholder="yourname@email.com" className="w-full rounded-lg border border-slate-300 bg-white py-2.5 pl-10 pr-4 text-sm text-slate-950 placeholder:text-slate-400 transition-all focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500/20" />
{isLogin && !resetSent ? ( ) : null}
setPassword(event.target.value)} placeholder={copy.passwordLoginPlaceholder} className="w-full rounded-lg border border-slate-300 bg-white py-2.5 pl-10 pr-10 text-sm text-slate-950 placeholder:text-slate-400 transition-all focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500/20" />
{isEn ? "or" : "或"}
{errorText ?

{errorText}

: null} {infoText ?

{infoText}

: null} {errorText && isLogin && errorText.includes("Invalid login") ? (

{copy.loginFailedHint}

) : null} {infoText === copy.signupCheckEmail ? (

{copy.resendVerify}

) : null}
); }