import { Outlet, useNavigate, useParams } from "react-router"; import { useEffect, useState } from "react"; import { useStore } from "@nanostores/react"; import { session } from "../../session"; import { Icon } from "@iconify/react"; import { Link } from "react-router"; import Description from "../../components/description"; export default function ProfileLayout() { const { id } = useParams(); const navigate = useNavigate(); const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); const $session = useStore(session); const userId = Number(id ?? $session?.user?.id); useEffect(() => { if ($session === undefined) return; // session still loading if (!userId) { navigate("/404"); return; } fetch(`${import.meta.env.VITE_API_URL}/api/profile/${userId}/info`) .then((res) => { if (!res.ok) throw new Error("Failed to fetch profile"); return res.json(); }) .then((data) => { if (!data) throw new Error("Profile not found"); setUser(data); setLoading(false); }) .catch((err) => { console.error(err); setLoading(false); navigate("/404"); }); }, [id, $session]); if (loading || !user) { return
Loading...
; } const isAdmin = userId === Number(import.meta.env.VITE_ADMIN_USER_ID); const isContributor = import.meta.env.VITE_CONTRIBUTORS_USER_IDS?.split(",").includes(String(user?.id)); const isOwnProfile = userId === user?.id; const joinDate = new Date(user.createdAt).toLocaleDateString("en-US", { month: "long", year: "numeric" }); const metaTitle = `${user.name} - TomodachiShare`; const metaDescription = `View ${user.name}'s profile on TomodachiShare. Creator of ${user._count.miis} Miis. Member since ${joinDate}.`; const metaImage = user.image ? (user.image.startsWith("/profile") ? `${import.meta.env.VITE_API_URL}${user.image}` : user.image) : "/guest.png"; return (
{metaTitle} {/* Open Graph */} {/* Twitter / X */}
{/* Profile picture */} { e.currentTarget.onerror = null; // Prevent infinite loops e.currentTarget.src = "/guest.png"; }} className="rounded-full bg-white border-2 border-orange-400 shadow w-full max-md:self-center" /> {/* User information */}

{user.name}

{isAdmin && (
)} {isContributor && (
)}

ID: {user?.id}

Created:{" "} {new Date(user.createdAt).toLocaleDateString("en-GB", { month: "long", day: "2-digit", year: "numeric" })}

Liked {user._count.likes} Miis

{user.description && }
{/* Buttons */}
{!isOwnProfile && ( Report )} {isOwnProfile && isAdmin && ( Admin )} {isOwnProfile && location.pathname !== "/profile/likes" && ( My Likes )} {isOwnProfile && location.pathname !== "/profile/settings" && ( Settings )} {(location.pathname === "/profile/likes" || location.pathname === "/profile/settings") && ( Back )}
); }