import React, { useState, useEffect } from "react"; import AuthModal from "../components/Auth/AuthModal"; import { useAuthModal } from "../hooks/useAuthModal"; import { useAuth } from "../context/AuthContext"; import { useParams } from "react-router-dom"; import { useFollow } from "../hooks/useFollow"; import { useNavigate } from "react-router-dom"; import Button from "../components/Input/Button"; import DynamicPageContent from "../components/Layout/DynamicPageContent"; import LoadingScreen from "../components/Layout/LoadingScreen"; import { StreamListItem } from "../components/Layout/ListItem"; import { CameraIcon } from "lucide-react"; import { getCategoryThumbnail } from "../utils/thumbnailUtils"; import { useSameUser } from "../hooks/useSameUser"; interface UserProfileData { id: number; username: string; bio: string; followerCount: number; isPartnered: boolean; isLive: boolean; currentStreamTitle?: string; currentStreamCategory?: string; currentStreamViewers?: number; currentStreamStartTime?: string; currentStreamThumbnail?: string; } const UserPage: React.FC = () => { const [userPageVariant, setUserPageVariant] = useState< "personal" | "streamer" | "user" | "admin" >("user"); const [profileData, setProfileData] = useState(); const { isFollowing, checkFollowStatus, followUser, unfollowUser } = useFollow(); const { showAuthModal, setShowAuthModal } = useAuthModal(); const { username: loggedInUsername } = useAuth(); const { username } = useParams(); const isUser = useSameUser({username}); const navigate = useNavigate(); // Saves uploaded image as profile picture for the user const saveUploadedImage = async (event) => { const img = event.target.files[0]; if (img) { const formData = new FormData(); formData.append('image', img); try { const response = await fetch('/api/user/profile_picture/upload', { method: 'POST', body: formData, }); if (response.ok) { console.log("Success"); } } catch (error) { console.log("Failure"); } } }; useEffect(() => { // Fetch user profile data fetch(`/api/user/${username}`) .then((res) => res.json()) .then((data) => { setProfileData({ id: data.user_id, username: data.username, bio: data.bio || "This user hasn't written a bio yet.", followerCount: data.num_followers || 0, isPartnered: data.isPartnered || false, isLive: data.is_live, currentStreamTitle: "", currentStreamCategory: "", currentStreamViewers: 0, currentStreamThumbnail: "", }); if (data.is_live) { // Fetch stream data for this streamer fetch(`/api/streams/${data.user_id}/data`) .then((res) => res.json()) .then((streamData) => { setProfileData((prevData) => { if (!prevData) return prevData; return { ...prevData, currentStreamTitle: streamData.title, currentStreamCategory: streamData.category_id, currentStreamViewers: streamData.num_viewers, currentStreamStartTime: streamData.start_time, currentStreamThumbnail: getCategoryThumbnail( streamData.category_name, streamData.thumbnail ), }; }); let variant: "user" | "streamer" | "personal" | "admin"; if (username === loggedInUsername) variant = "personal"; else if (streamData.title) variant = "streamer"; // else if (data.isAdmin) variant = "admin"; else variant = "user"; setUserPageVariant(variant); }) .catch((err) => console.error("Error fetching stream data:", err)); } }) .catch((err) => { console.error("Error fetching profile data:", err); navigate("/404"); }); // Check if the *logged-in* user is following this user if (loggedInUsername && username) checkFollowStatus(username); }, [username]); if (!profileData) return ; console.log(isUser) return (
{/* Profile Section - TOP */}
{/* Border Overlay (Always on Top) */}
{/* Background Box */}
{/*
*/}
{/* Profile Picture */}
{/* Username - Now Directly Below PFP */}

{profileData.username}

{/* Follower Count */} {userPageVariant === "streamer" && ( <>
{profileData.followerCount.toLocaleString()} followers {profileData.isPartnered && ( Partner )}
{/* (Un)Follow Button */} {!isFollowing ? ( ) : ( )} )}
{/* User Type (e.g., "USER") */} {userPageVariant.toUpperCase()}

About {profileData.username}

{profileData.bio}

{/* Content Section */}
{userPageVariant === "streamer" && ( <> {profileData.isLive ? (

Currently Live!

{ navigate(`/${profileData.username}`); }} />
) : (

Currently not live

)} {/* ↓↓ VODS ↓↓ */}

Past Broadcasts

No past broadcasts found
)} {userPageVariant === "user" && ( <> {/* ↓↓ VODS ↓↓ */}

Past Broadcasts

No past broadcasts found
)}
(e.currentTarget.style.boxShadow = "var(--follow-shadow)") } onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")} >
(e.currentTarget.style.boxShadow = "var(--follow-shadow)") } onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")} >
  • Streamers
(e.currentTarget.style.boxShadow = "var(--follow-shadow)") } onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")} >
{showAuthModal && setShowAuthModal(false)} />}
); }; export default UserPage;