import { useEffect, useState } from "react"; import { useNavigate, useParams, Link } from "react-router-dom"; import Layout from "@/components/Layout"; import LoadingScreen from "@/components/LoadingScreen"; import PassportSummary from "@/components/passport/PassportSummary"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Separator } from "@/components/ui/separator"; import { aethexUserService, aethexAchievementService, aethexProjectService, type AethexUserProfile, type AethexAchievement, } from "@/lib/aethex-database-adapter"; import { useAuth } from "@/contexts/AuthContext"; import FourOhFourPage from "@/pages/404"; import { Clock, Rocket, Target, ExternalLink, Award } from "lucide-react"; interface ProjectPreview { id: string; title: string; status: string; description?: string | null; created_at?: string; } const formatDate = (value?: string | null) => { if (!value) return "Recent"; const date = new Date(value); if (Number.isNaN(date.getTime())) return "Recent"; return new Intl.DateTimeFormat(undefined, { dateStyle: "medium", }).format(date); }; const isUuid = (value: string) => /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( value, ); const ProfilePassport = () => { const params = useParams<{ username?: string }>(); const navigate = useNavigate(); const { user, linkedProviders, profile: authProfile } = useAuth(); const requestedUsername = params.username?.trim(); const isSelfRoute = !requestedUsername || requestedUsername === "me"; const [profile, setProfile] = useState< (AethexUserProfile & { email?: string | null }) | null >(null); const [achievements, setAchievements] = useState([]); const [projects, setProjects] = useState([]); const [interests, setInterests] = useState([]); const [loading, setLoading] = useState(true); const [notFound, setNotFound] = useState(false); useEffect(() => { if (isSelfRoute && !user) { setLoading(false); setNotFound(false); navigate("/login"); return; } let cancelled = false; const loadPassport = async () => { setLoading(true); try { let resolvedProfile: (AethexUserProfile & { email?: string | null }) | null = null; let resolvedId: string | null = null; if (isSelfRoute) { const currentUser = await aethexUserService.getCurrentUser(); if (currentUser?.username) { if (!requestedUsername || requestedUsername === "me") { navigate(`/passport/${currentUser.username}`, { replace: true }); return; } if ( currentUser.username.toLowerCase() === requestedUsername.toLowerCase() && currentUser.username !== requestedUsername ) { navigate(`/passport/${currentUser.username}`, { replace: true }); return; } } if (currentUser) { resolvedProfile = { ...currentUser, email: (currentUser as any)?.email ?? user?.email ?? authProfile?.email ?? null, } as AethexUserProfile & { email?: string | null }; resolvedId = currentUser.id ?? user?.id ?? null; } } else if (requestedUsername) { let fetchedProfile = (await aethexUserService.getProfileByUsername(requestedUsername)) ?? (isUuid(requestedUsername) ? await aethexUserService.getProfileById(requestedUsername) : null); if ( fetchedProfile?.username && fetchedProfile.username.toLowerCase() === requestedUsername.toLowerCase() && fetchedProfile.username !== requestedUsername ) { navigate(`/passport/${fetchedProfile.username}`, { replace: true }); return; } if (fetchedProfile) { resolvedProfile = { ...fetchedProfile, email: (fetchedProfile as any)?.email ?? null, } as AethexUserProfile & { email?: string | null }; resolvedId = fetchedProfile.id ?? null; } } if (!resolvedProfile || !resolvedId) { if (!cancelled) { setProfile(null); setAchievements([]); setInterests([]); setProjects([]); setNotFound(true); } return; } const viewingSelf = isSelfRoute || (!!user?.id && resolvedId === user.id) || (!!authProfile?.username && !!resolvedProfile.username && authProfile.username.toLowerCase() === resolvedProfile.username.toLowerCase()); const [achievementList, interestList, projectList] = await Promise.all([ aethexAchievementService .getUserAchievements(resolvedId) .catch((error) => { console.error("Failed to load achievements", error); return [] as AethexAchievement[]; }), aethexUserService.getUserInterests(resolvedId).catch((error) => { console.error("Failed to load interests", error); return [] as string[]; }), aethexProjectService.getUserProjects(resolvedId).catch((error) => { console.error("Failed to load projects", error); return [] as ProjectPreview[]; }), ]); if (cancelled) { return; } setProfile({ ...resolvedProfile, email: resolvedProfile.email ?? (viewingSelf ? user?.email ?? authProfile?.email ?? null : null), }); setAchievements(achievementList ?? []); setInterests(interestList ?? []); setProjects( (projectList ?? []).slice(0, 4).map((project: any) => ({ id: project.id, title: project.title, status: project.status, description: project.description, created_at: project.created_at, })), ); setNotFound(false); } catch (error) { console.error("Failed to load passport", error); if (!cancelled) { setNotFound(true); } } finally { if (!cancelled) { setLoading(false); } } }; loadPassport(); return () => { cancelled = true; }; }, [ authProfile?.email, authProfile?.username, isSelfRoute, navigate, requestedUsername, user?.email, user?.id, ]); const isSelf = Boolean( profile && ((user?.id && profile.id && user.id === profile.id) || (authProfile?.username && profile.username && authProfile.username.toLowerCase() === profile.username.toLowerCase())), ); if (loading) { return ; } if (notFound || !profile) { return ; } const passportInterests = interests.length ? interests : ((profile as any)?.interests as string[]) || []; return (
{projects.length > 0 && (

Highlighted missions

A snapshot of what this creator has shipped inside AeThex.

{isSelf && ( )}
{projects.map((project) => (
{project.title} {project.description || "AeThex project"}
{project.status?.replace("_", " ") ?? "active"}
{" "} {formatDate(project.created_at)}
))}
)}

Achievements

Passport stamps earned across AeThex experiences.

{isSelf && ( {achievements.length}{" "} badges )}
{achievements.length === 0 ? ( No achievements yet. Complete onboarding and participate in missions to earn AeThex badges. ) : (
{achievements.map((achievement) => (
{achievement.icon || "🏅"}

{achievement.name}

{achievement.description || "AeThex honor"}

XP Reward • {achievement.xp_reward ?? 0} Passport stamped
))}
)}

Stay connected

Reach out, collaborate, and shape the next AeThex release together.

{isSelf ? ( ) : ( )}
{profile.github_url && ( )} {profile.linkedin_url && ( )} {profile.twitter_url && ( )} {profile.website_url && ( )} {profile.bio && ( {profile.bio} )}
); }; export default ProfilePassport;