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" }; const LAST_SIGNUP_KEY = "sociowire:lastSignup"; function initialLetter(name = "") { const s = (name || "").trim(); return s ? s[0].toUpperCase() : "G"; } function truthyParam(v) { if (!v) return false; const x = String(v).toLowerCase().trim(); return x === "1" || x === "true" || x === "yes" || x === "ok"; } function readLastSignup() { try { const raw = localStorage.getItem(LAST_SIGNUP_KEY); if (!raw) return null; const j = JSON.parse(raw); if (!j || typeof j !== "object") return null; return { username: (j.username || "").toString().trim(), email: (j.email || "").toString().trim(), }; } catch { return null; } } function parseMergedParams() { const search = window.location.search || ""; const hash = window.location.hash || ""; const qs = new URLSearchParams(search.startsWith("?") ? search.slice(1) : search); let hashQs = ""; if (hash.includes("?")) hashQs = hash.slice(hash.indexOf("?") + 1); const hs = new URLSearchParams(hashQs); const get = (k) => { const a = qs.get(k); if (a != null && a !== "") return a; const b = hs.get(k); return b != null ? b : null; }; return { qs, hs, get, hash }; } export default function TopBar({ theme = "dark", onChangeTheme }) { const { loading, authenticated, username, login, logout, needsEmailVerification, lastError, 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(""); }; const closeModal = () => { setShowAuth(false); setLoginPass(""); setSignupError(""); }; useEffect(() => { if (authenticated) setShowAuth(false); }, [authenticated]); useEffect(() => { try { const { get, qs, hs, hash } = parseMergedParams(); const verified = get("verified") || get("email_verified") || get("emailVerified") || get("kc_verified"); const signup = get("signup"); const notice = get("notice"); const email = get("email") || get("user_email") || get("mail"); const uname = get("username") || get("user") || get("preferred_username") || get("login"); const last = readLastSignup(); if (truthyParam(verified)) { const who = (email || uname || last?.email || last?.username || "").trim(); setSignupInfo("✅ Email confirmé" + (who ? " (" + who + ")" : "") + ". Tu peux te connecter."); setMode("login"); setShowAuth(true); const prefill = (uname || email || last?.username || last?.email || "").trim(); if (prefill) setLoginUser(prefill); } else if (truthyParam(signup)) { const who = (email || uname || last?.email || last?.username || "").trim(); setSignupInfo("✅ Compte créé" + (who ? " (" + who + ")" : "") + ". Vérifie tes emails (et le spam), puis connecte-toi."); setMode("login"); setShowAuth(true); const prefill = (uname || email || last?.username || last?.email || "").trim(); if (prefill) setLoginUser(prefill); } else if (notice) { const msg = decodeURIComponent(notice); if (msg && msg.trim()) { setSignupInfo(msg.trim()); setMode("login"); setShowAuth(true); } } const touched = qs.has("verified") || qs.has("email_verified") || qs.has("emailVerified") || qs.has("signup") || qs.has("notice") || hs.has("verified") || hs.has("email_verified") || hs.has("emailVerified") || hs.has("signup") || hs.has("notice"); if (touched) { const cleanHash = hash.includes("?") ? hash.split("?")[0] : hash; const clean = window.location.pathname + cleanHash; window.history.replaceState({}, "", clean); } } catch (e) { // ignore } }, []); 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); const u = regUser.trim(); const em = regEmail.trim(); await registerUser({ username: u, email: em, password: regPass, firstName: firstName.trim(), lastName: lastName.trim(), }); try { localStorage.setItem(LAST_SIGNUP_KEY, JSON.stringify({ username: u, email: em })); } catch {} setSignupInfo("✅ Compte créé (" + em + "). Vérifie tes emails (spam aussi), puis connecte-toi."); setMode("login"); setLoginUser(u || em); 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 && typeof clearError === "function") clearError(); if (lastInfo && typeof clearInfo === "function") clearInfo(); }} />
) : null}
SW
SOCIOWIRE.com
Wired to life
{/* Theme dots */}
{["dark", "blue", "light"].map((t) => ( ))}
{authenticated ? (
{initialLetter(username)}
Online
@{username || "user"}
) : (
{loading ? "Loading…" : "Guest"}
anon
)}
{showAuth ? (
e.stopPropagation()}>
{signupInfo ?
{signupInfo}
: null} {mode === "login" && lastError ? (
{lastError}
) : null} {mode === "login" ? (
{needsEmailVerification ? (
⚠️ Ton compte demande une vérification email. Confirme ton email, puis réessaie de te connecter.
) : null}
) : (
{signupError ?
{signupError}
: null}
)}
) : null} ); }