import React, { useEffect, useMemo, useState } from "react"; import { useAuth } from "../../auth/AuthContext"; import { registerUser } from "../../api/client"; import AuthToast from "../Auth/AuthToast"; const THEME_LABELS = { dark: "Dark", blue: "Blue", light: "Light" }; function initialLetter(name = "") { const s = (name || "").trim(); return s ? s[0].toUpperCase() : "G"; } export default function TopBar({ theme = "dark", onChangeTheme }) { const { loading, authenticated, username, login, logout, needsEmailVerification, openKeycloak, lastError, lastErrorCode, lastInfo, clearError, clearInfo, } = useAuth(); const [showAuth, setShowAuth] = useState(false); const [mode, setMode] = useState("login"); const [loginUser, setLoginUser] = useState(""); const [loginPass, setLoginPass] = useState(""); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [regUser, setRegUser] = useState(""); const [regEmail, setRegEmail] = useState(""); const [regPass, setRegPass] = useState(""); const [signupError, setSignupError] = useState(""); const [signupInfo, setSignupInfo] = useState(""); const [signupLoading, setSignupLoading] = useState(false); const openModal = (nextMode) => { setMode(nextMode); setShowAuth(true); setSignupError(""); setSignupInfo(""); }; const closeModal = () => { setShowAuth(false); setLoginPass(""); setSignupError(""); setSignupInfo(""); }; useEffect(() => { if (authenticated) setShowAuth(false); }, [authenticated]); /* SW_URL_NOTICE:BEGIN */ useEffect(() => { try { const qs = new URLSearchParams(window.location.search || ""); const verified = qs.get("verified") || qs.get("email_verified"); const signup = qs.get("signup"); const notice = qs.get("notice"); if (verified === "1" || verified === "true") { setSignupInfo("✅ Email confirmé. Tu peux te connecter."); setMode("login"); setShowAuth(true); } else if (signup === "1" || signup === "true") { setSignupInfo("✅ Compte créé. Vérifie tes emails (et le spam), puis connecte-toi."); setMode("login"); setShowAuth(true); } else if (notice) { // allow custom notice (URL-encoded) const msg = decodeURIComponent(notice); if (msg && msg.trim()) { setSignupInfo(msg.trim()); setMode("login"); setShowAuth(true); } } // clean URL (remove params without reload) if (verified or signup or notice): const clean = window.location.pathname + (window.location.hash || ""); window.history.replaceState({}, "", clean); } catch { // ignore } }, []); /* SW_URL_NOTICE:END */ const handleLoginSubmit = async (e) => { e.preventDefault(); await login(loginUser, loginPass); }; const handleSignupSubmit = async (e) => { e.preventDefault(); setSignupError(""); setSignupInfo(""); if (!firstName.trim() || !lastName.trim() || !regUser.trim() || !regEmail.trim() || !regPass) { setSignupError("First name, last name, username, email and password are required."); return; } try { setSignupLoading(true); await registerUser({ username: regUser.trim(), email: regEmail.trim(), password: regPass, firstName: firstName.trim(), lastName: lastName.trim(), }); setSignupInfo("Account created. Check your inbox (spam) to verify your email, then login."); setMode("login"); setLoginUser(regUser.trim()); setLoginPass(""); } catch (err) { console.error(err); setSignupError(err.message || "Registration error."); } finally { setSignupLoading(false); } }; const toast = useMemo(() => { if (lastError) { const t = needsEmailVerification ? "warn" : "error"; const title = needsEmailVerification ? "Vérification email requise" : "Connexion impossible"; return { type: t, title, message: lastError }; } if (lastInfo) { return { type: "success", title: "", message: lastInfo }; } return null; }, [lastError, lastInfo, needsEmailVerification]); return ( <> {/* Toast area (under topbar) */} {(toast?.message) && (
{ if (lastError) clearError(); if (lastInfo) clearInfo(); }} /> {needsEmailVerification && (
)}
)}
SW
SOCIOWIRE.com
Wired to life
{["dark", "blue", "light"].map((t) => ( ))}
{authenticated ? (
{initialLetter(username)}
Connecté
@{username || "user"}
) : (
{loading ? "Loading…" : "Guest"}
anon
)}
{showAuth && (
e.stopPropagation()}>
{/* SW_MODAL_NOTICE:BEGIN */} {signupInfo ? (
{signupInfo}
) : null} {/* SW_MODAL_NOTICE:END */} {mode === "login" ? (
) : (
{signupError &&
{signupError}
} {signupInfo && (
{signupInfo}
)}
)}
)} ); }