import { useEffect, useState } from "preact/hooks"; import { WelcomeBarProps, WelcomeBarState } from "./welcome-types.ts"; import { useEncryption } from "./useEncryption.ts"; import { useLogoutCleanup } from "./useServiceWorker.ts"; import EncryptionModal from "./EncryptionModal.tsx"; import NavigationMenu from "./NavigationMenu.tsx"; import LockIcon from "./LockIcon.tsx"; function normalizeRoute(path: string): string { if (!path || path === "/") return "/"; return path.endsWith("/") ? path.slice(0, -1) : path; } /** * WelcomeBar Component * Main navigation bar with user info, encryption status, and menu. */ export default function WelcomeBar( { publicId, user_name, currentRoute, isEncrypted, stateId }: WelcomeBarProps, ) { const [activeRoute, setActiveRoute] = useState(normalizeRoute(currentRoute)); const [state] = useState({ photoUrl: "", }); const hasValidUserId = publicId.length > 0; const displayName = user_name?.trim() || (hasValidUserId ? "Member" : ""); const manageStateIdInStorage = () => { try { const storedStateId = localStorage.getItem("mystate"); if (storedStateId === null || storedStateId !== stateId) { localStorage.setItem("mystate", stateId); } } catch (error) { console.error("Error managing stateId in localStorage:", error); } }; useLogoutCleanup(); useEffect(() => { const syncRoute = () => { const pathname = globalThis.location?.pathname ?? currentRoute; const nextRoute = normalizeRoute(pathname); setActiveRoute((prev) => (prev === nextRoute ? prev : nextRoute)); }; const handlePotentialNavigation = (event: MouseEvent) => { const target = event.target as HTMLElement | null; if (!target?.closest("a[href]")) return; globalThis.setTimeout(syncRoute, 0); }; syncRoute(); globalThis.addEventListener("popstate", syncRoute); globalThis.addEventListener("hashchange", syncRoute); document.addEventListener("click", handlePotentialNavigation, true); return () => { globalThis.removeEventListener("popstate", syncRoute); globalThis.removeEventListener("hashchange", syncRoute); document.removeEventListener("click", handlePotentialNavigation, true); }; }, [currentRoute]); useEffect(() => { manageStateIdInStorage(); }, [stateId]); const encryption = useEncryption( hasValidUserId ? publicId : "", isEncrypted, ); return ( <>
{hasValidUserId ? (
{state.photoUrl ? ( User profile { const target = e.currentTarget as HTMLImageElement; target.classList.add("is-hidden"); const fallback = document.getElementById( `initials-${displayName || "default"}`, ); if (fallback) fallback.classList.remove("is-hidden"); }} /> ) : null}
{displayName.slice(0, 2).toUpperCase()}
{displayName}
) : (
)}
{hasValidUserId && (
)}
{activeRoute !== "/" && ( Cyber Hybrid Hub )}
{hasValidUserId && encryption.showPasswordModal && ( )} {hasValidUserId && ( )} ); }