import React, { useState, useEffect, useCallback, useRef } 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 { EditIcon } from "lucide-react"; import ListRow from "../components/Layout/ListRow"; import { useStreams, useVods } from "../hooks/useContent"; interface UserProfileData { id: number; username: string; bio: string; followerCount: number; isPartnered: boolean; isLive: boolean; } const UserPage: React.FC = () => { const [userPageVariant, setUserPageVariant] = useState<"personal" | "user" | "admin">("user"); const [profileData, setProfileData] = useState(); const { isFollowing, checkFollowStatus, followUser, unfollowUser } = useFollow(); const { showAuthModal, setShowAuthModal } = useAuthModal(); const { username: loggedInUsername, profilePicture, setProfilePicture } = useAuth(); const initialProfilePicture = useRef(profilePicture); const { username } = useParams(); const { vods } = useVods(`/api/vods/${username}`); const navigate = useNavigate(); const { streams } = useStreams(`/api/streams/${username}/data`); const currentStream = streams[0]; const fetchProfileData = useCallback(async () => { try { // Profile data const profileResponse = await fetch(`/api/user/${username}`); const profileData = await profileResponse.json(); setProfileData({ id: profileData.user_id, username: profileData.username, bio: profileData.bio || "This user hasn't written a bio yet.", followerCount: profileData.num_followers || 0, isPartnered: profileData.isPartnered || false, isLive: profileData.is_live, }); } catch (err) { console.error("Error fetching profile data:", err); window.location.href = "/404"; } }, [username]); // Saves uploaded image as profile picture for the user const saveUploadedImage = async (event: React.ChangeEvent) => { const files = event.target.files; if (!files) return; const img = 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"); console.log(URL.createObjectURL(img)) setProfilePicture(URL.createObjectURL(img)); } } catch (error) { console.log("Failure"); } } }; const handleNavigation = (path: string) => { if (profilePicture === initialProfilePicture.current) { // Variable hasn't changed - use React Router navigation navigate(path); } else { // Variable has changed - use full page reload window.location.href = path; } }; // Store initial profile picture to know if it changes later useEffect(() => { initialProfilePicture.current = profilePicture; }, []); // Check if the current user is the currently logged-in user useEffect(() => { if (username === loggedInUsername) setUserPageVariant("personal"); // else if (data.isAdmin) setUserPageVariant("admin"); else setUserPageVariant("user"); if (loggedInUsername && username) checkFollowStatus(username); }, [username, loggedInUsername, checkFollowStatus]); // Fetch user profile data useEffect(() => { if (!username) return; fetchProfileData(); }, [fetchProfileData]); if (!profileData) return ; return (
{/* Profile Section - TOP */}
{/* Border Overlay (Always on Top) */}
{/* Background Box */}
{/*
*/}
{/* Profile Picture */}
{/* Username - Now Directly Below PFP */}

{profileData.username}

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

About {profileData.username}

{profileData.bio}

{/* Content Section */}
{/* Stream */} {currentStream && (

Currently Live!

{ handleNavigation(`/${profileData.username}`); }} />
)} {/* VODs */} {vods.length > 0 && (

{ console.log("VOD Clicked:", vod); }} extraClasses="w-fit max-w-[40vw] py-0 mt-0" amountForScroll={2} itemExtraClasses="w-[15vw]" />
)} {/* No Content */} {vods.length === 0 && currentStream &&

No Content Made Yet

}
(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;