import Layout from "@/components/Layout"; import Layout from "@/components/Layout"; import { useAuth } from "@/contexts/AuthContext"; import { useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import LoadingScreen from "@/components/LoadingScreen"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { Input } from "@/components/ui/input"; import { useToast } from "@/hooks/use-toast"; import { aethexSocialService } from "@/lib/aethex-social-service"; import { UserPlus, UserCheck } from "lucide-react"; export default function Network() { const { user, profile, loading } = useAuth(); const navigate = useNavigate(); const { toast } = useToast(); const [isLoading, setIsLoading] = useState(true); const [recommended, setRecommended] = useState([]); const [following, setFollowing] = useState([]); const [connections, setConnections] = useState([]); const [endorsements, setEndorsements] = useState([]); const [inviteEmail, setInviteEmail] = useState(""); const [inviteSending, setInviteSending] = useState(false); useEffect(() => { if (!loading && !user) { navigate("/login", { replace: true }); return; } if (!user) return; const load = async () => { setIsLoading(true); try { const recs = await aethexSocialService.listRecommended(user.id, 12); setRecommended(recs); const flw = await aethexSocialService.getFollowing(user.id); setFollowing(flw); const conns = await aethexSocialService.getConnections(user.id); setConnections(conns); const ends = await aethexSocialService.getEndorsements(user.id); setEndorsements(ends); } finally { setIsLoading(false); } }; load(); }, [user, loading, navigate]); const isFollowing = (id: string) => following.includes(id); const toggleFollow = async (targetId: string) => { if (!user) return; if (isFollowing(targetId)) { await aethexSocialService.unfollowUser(user.id, targetId); setFollowing((s) => s.filter((x) => x !== targetId)); } else { await aethexSocialService.followUser(user.id, targetId); setFollowing((s) => Array.from(new Set([...s, targetId]))); } }; const handleInvite = async () => { if (!user) return; const email = inviteEmail.trim(); if (!email || !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)) { toast({ variant: "destructive", title: "Invalid email" }); return; } setInviteSending(true); try { await aethexSocialService.sendInvite(user.id, email, null); toast({ description: "Invitation sent" }); setInviteEmail(""); } catch (e: any) { toast({ variant: "destructive", title: "Failed to send invite", description: e?.message || "Try again later", }); } finally { setInviteSending(false); } }; const handleEndorse = async (targetId: string, skill: string) => { if (!user) return; try { await aethexSocialService.endorseSkill(user.id, targetId, skill); toast({ description: `Endorsed for ${skill}` }); } catch (e: any) { toast({ variant: "destructive", title: "Failed to endorse", description: e?.message || "Try again later", }); } }; if (loading || isLoading) { return ( ); } if (!user) return null; return (
{/* Public Profile */}
{profile?.full_name?.[0] || user.email?.[0]?.toUpperCase()}

{profile?.full_name || user.email?.split("@")[0]}

{profile?.role || "Member"}

Level {profile?.level || 1} {(profile as any)?.experience_level || "beginner"}
{profile?.bio && (

{profile.bio}

)}
Invite collaborator Grow your network
setInviteEmail(e.target.value)} />

Accepted invites boost your loyalty, XP, and reputation.

Recommendations People who align with your interests {recommended.slice(0, 3).map((r) => (
{(r.full_name || r.username || "U")[0]}
{r.full_name || r.username}
{r.bio?.slice(0, 40) || "Member"}
))} {recommended.length === 0 && (
No recommendations yet.
)}
{/* Connections + Discover + Endorsements */}
My connections People you’re connected with {connections.map((c) => { const p = (c as any).user_profiles || {}; const display = p.full_name || p.username || c.connection_id; return (
{(display || "U")[0]}
{display}
Connected
); })} {connections.length === 0 && (
No connections yet.
)}
Discover People Connect with creators, clients, and members {recommended.map((r) => (
{(r.full_name || r.username || "U")[0]}
{r.full_name || r.username}
{r.bio?.slice(0, 80) || "Member"}
))} {recommended.length === 0 && (
No people found yet.
)}
Endorsements Recognize skills of your peers {connections.slice(0, 6).map((c) => { const p = (c as any).user_profiles || {}; const display = p.full_name || p.username || c.connection_id; return (
{(display || "U")[0]}
{display}
{( [ "Leadership", "Systems", "Frontend", "Backend", ] as const ).map((skill) => ( ))}
); })} {endorsements.length > 0 && (
You have received {endorsements.length} endorsements.
)}
); }