import React, { useEffect, useRef, useState } from 'react'; import { fetchIslandByUsername, uploadProfileAvatar } from '../../api/client'; import { useAuth } from '../../auth/AuthContext'; import '../../styles/profile.css'; /** * UserProfile: Modal showing user info with "Visit Island" button * * Props: * - username: string (required) * - onClose: function * - onVisitIsland: function(island) - called when "Visit Island" is clicked */ export default function UserProfile({ username, onClose, onVisitIsland }) { const { authenticated, username: authedUser, setAvatarUrl } = useAuth(); const [island, setIsland] = useState(null); const [loading, setLoading] = useState(true); const [uploading, setUploading] = useState(false); const fileRef = useRef(null); const isSelf = authenticated && authedUser && authedUser.toLowerCase() === username.toLowerCase(); useEffect(() => { if (!username) return; fetchIslandByUsername(username) .then(data => { setIsland(data); setLoading(false); }) .catch(err => { console.error('[UserProfile] Error fetching island:', err); setLoading(false); }); }, [username]); const handleVisitIsland = () => { if (island && onVisitIsland) { onVisitIsland(island); } }; const handleChooseAvatar = () => { if (fileRef.current) fileRef.current.click(); }; const handleAvatarChange = async (e) => { const file = e.target.files && e.target.files[0]; if (!file) return; setUploading(true); const res = await uploadProfileAvatar(file); if (res && res.ok && res.avatar_url) { setIsland(prev => (prev ? { ...prev, avatar_url: res.avatar_url } : prev)); if (setAvatarUrl) setAvatarUrl(res.avatar_url); } setUploading(false); }; return (
e.stopPropagation()}>
{island?.avatar_url ? ( {username} ) : (
{username[0].toUpperCase()}
)}

@{username}

{island?.display_name && island.display_name !== `${username}'s Island` && (

{island.display_name}

)} {isSelf ? (
) : null}
{loading ? (
Loading profile...
) : island ? ( <>

{island.bio || 'No bio yet.'}

{island.content_count || 0} Posts
{island.visitors || 0} Visitors
{island.terrain_type || 'tropical'} Terrain

Seed: {island.seed}

) : (

This user doesn't have an island yet.

)}
); }