UPDATE/FEAT: Proper handling of profile pictures where a fallback to the default pfp is used;
REFACTOR: Remove unnecessary props from users on `ResultsPage`; REFACTOR: General formatting
This commit is contained in:
@@ -7,8 +7,8 @@ import { useAuthModal } from "../hooks/useAuthModal";
|
||||
import FollowUserButton from "../components/Input/FollowUserButton";
|
||||
|
||||
interface Streamer {
|
||||
user_id: number;
|
||||
username: string;
|
||||
user_id: number;
|
||||
username: string;
|
||||
}
|
||||
|
||||
interface FollowingStreamerProps {
|
||||
@@ -45,10 +45,10 @@ const Following: React.FC<FollowingStreamerProps> = ({ extraClasses = "" }) => {
|
||||
}
|
||||
};
|
||||
|
||||
if (isLoggedIn) {
|
||||
fetchFollowedStreamers();
|
||||
}
|
||||
}, [isLoggedIn]);
|
||||
if (isLoggedIn) {
|
||||
fetchFollowedStreamers();
|
||||
}
|
||||
}, [isLoggedIn]);
|
||||
|
||||
|
||||
const handleFollowToggle = async (userId: number) => {
|
||||
|
||||
@@ -84,9 +84,7 @@ const ResultsPage: React.FC = ({ }) => {
|
||||
id: user.user_id,
|
||||
type: "user",
|
||||
title: `${user.is_live ? "🔴" : ""} ${user.username}`,
|
||||
viewers: 0,
|
||||
username: user.username,
|
||||
thumbnail: user.profile_picture,
|
||||
username: user.username
|
||||
}))}
|
||||
title="Users"
|
||||
onItemClick={(username: string) =>
|
||||
|
||||
@@ -14,343 +14,295 @@ 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;
|
||||
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<UserProfileData>();
|
||||
const { isFollowing, checkFollowStatus, followUser, unfollowUser } =
|
||||
useFollow();
|
||||
const { showAuthModal, setShowAuthModal } = useAuthModal();
|
||||
const { username: loggedInUsername } = useAuth();
|
||||
const { username } = useParams();
|
||||
const isUser = useSameUser({username});
|
||||
const navigate = useNavigate();
|
||||
const [userPageVariant, setUserPageVariant] = useState<"personal" | "streamer" | "user" | "admin">("user");
|
||||
const [profileData, setProfileData] = useState<UserProfileData>();
|
||||
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");
|
||||
}
|
||||
}
|
||||
};
|
||||
// Saves uploaded image as profile picture for the user
|
||||
const saveUploadedImage = async (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const files = event.target.files;
|
||||
if (!files) return;
|
||||
const img = files[0];
|
||||
if (img) {
|
||||
const formData = new FormData();
|
||||
formData.append("image", img);
|
||||
|
||||
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: "",
|
||||
});
|
||||
try {
|
||||
const response = await fetch("/api/user/profile_picture/upload", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
if (response.ok) {
|
||||
console.log("Success");
|
||||
window.location.reload();
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Failure");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Check if the *logged-in* user is following this user
|
||||
if (loggedInUsername && username) checkFollowStatus(username);
|
||||
}, [username]);
|
||||
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 (!profileData) return <LoadingScreen />;
|
||||
console.log(isUser)
|
||||
return (
|
||||
<DynamicPageContent
|
||||
className={`min-h-screen text-white flex flex-col`}
|
||||
>
|
||||
<div className="flex justify-evenly justify-self-center items-center h-full px-4 py-8 max-w-[80vw] w-full">
|
||||
<div className="grid grid-cols-4 grid-rows-[0.1fr_4fr] w-full gap-8">
|
||||
{/* Profile Section - TOP */}
|
||||
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");
|
||||
});
|
||||
|
||||
<div
|
||||
id="profile"
|
||||
className="col-span-4 row-span-1 h-full bg-[var(--user-bg)]
|
||||
// Check if the *logged-in* user is following this user
|
||||
if (loggedInUsername && username) checkFollowStatus(username);
|
||||
}, [username]);
|
||||
|
||||
if (!profileData) return <LoadingScreen />;
|
||||
|
||||
return (
|
||||
<DynamicPageContent className="min-h-screen text-white flex flex-col">
|
||||
<div className="flex justify-evenly self-center items-center h-full px-4 pt-14 pb-8 mx-auto max-w-[80vw] w-full">
|
||||
<div className="grid grid-cols-4 grid-rows-[0.1fr_4fr] w-full gap-8">
|
||||
{/* Profile Section - TOP */}
|
||||
|
||||
<div
|
||||
id="profile"
|
||||
className="col-span-4 row-span-1 h-full bg-[var(--user-bg)]
|
||||
rounded-[30px] p-3 shadow-lg
|
||||
relative flex flex-col items-center"
|
||||
>
|
||||
{/* Border Overlay (Always on Top) */}
|
||||
<div className="absolute left-[0px] inset-0 border-[5px] border-[var(--user-borderBg)] rounded-[20px] z-20"></div>
|
||||
>
|
||||
{/* Border Overlay (Always on Top) */}
|
||||
<div className="absolute left-[0px] inset-0 border-[5px] border-[var(--user-borderBg)] rounded-[20px] z-20"></div>
|
||||
|
||||
{/* Background Box */}
|
||||
<div
|
||||
className="absolute flex top-0 left-[0.55px] w-[99.9%] h-[5vh] min-h-[1em] max-h-[10em] rounded-t-[25.5px]
|
||||
{/* Background Box */}
|
||||
<div
|
||||
className="absolute flex top-0 left-[0.55px] w-[99.9%] h-[5vh] min-h-[1em] max-h-[10em] rounded-t-[25.5px]
|
||||
bg-[var(--user-box)] z-10 flex-shrink justify-center"
|
||||
style={{ boxShadow: "var(--user-box-shadow)" }}
|
||||
>
|
||||
{/* <div className="absolute top-4 w-[99.8%] h-[1vh] min-h-[1em] max-h-[2em] bg-[var(--user-box-strip)]"></div> */}
|
||||
</div>
|
||||
{/* Profile Picture */}
|
||||
<div
|
||||
className="relative -top-[40px] sm:-top-[90px] w-[16vw] h-[16vw] sm:w-[20vw] sm:h-[20vw] max-w-[10em] max-h-[10em]
|
||||
style={{ boxShadow: "var(--user-box-shadow)" }}
|
||||
>
|
||||
{/* <div className="absolute top-4 w-[99.8%] h-[1vh] min-h-[1em] max-h-[2em] bg-[var(--user-box-strip)]"></div> */}
|
||||
</div>
|
||||
{/* Profile Picture */}
|
||||
<div
|
||||
className="relative -top-[40px] sm:-top-[90px] w-[16vw] h-[16vw] sm:w-[20vw] sm:h-[20vw] max-w-[10em] max-h-[10em]
|
||||
rounded-full flex-shrink-0 border-4 border-[var(--user-pfp-border)] inset-0 z-20"
|
||||
style={{ boxShadow: "var(--user-pfp-border-shadow)" }}
|
||||
>
|
||||
<label
|
||||
className={`relative ${isUser ? "cursor-pointer group" : ""} overflow-visible`}
|
||||
>
|
||||
{/* If user is live then displays a live div */}
|
||||
{Boolean(profileData.isLive) && (
|
||||
<div className="absolute -bottom-2 left-1/2 transform -translate-x-1/2 bg-red-600 text-white text-sm font-bold py-1 sm:px-5 px-4 z-30 flex items-center justify-center rounded-tr-xl rounded-bl-xl rounded-tl-xl rounded-br-xl">
|
||||
LIVE
|
||||
</div>
|
||||
)}
|
||||
<img
|
||||
src="/images/monkey.png"
|
||||
alt={`${profileData.username}'s profile`}
|
||||
className="sm:w-full h-full object-cover rounded-full relative z-0"
|
||||
/>
|
||||
style={{ boxShadow: "var(--user-pfp-border-shadow)" }}
|
||||
>
|
||||
<label className={`relative ${isUser ? "cursor-pointer group" : ""} overflow-visible`}>
|
||||
{/* If user is live then displays a live div */}
|
||||
{Boolean(profileData.isLive) && (
|
||||
<div className="absolute -bottom-2 left-1/2 transform -translate-x-1/2 bg-red-600 text-white text-sm font-bold py-1 sm:px-5 px-4 z-30 flex items-center justify-center rounded-tr-xl rounded-bl-xl rounded-tl-xl rounded-br-xl">
|
||||
LIVE
|
||||
</div>
|
||||
)}
|
||||
<img
|
||||
src={`/user/${profileData.username}/profile_picture`}
|
||||
onError={(e) => {
|
||||
e.currentTarget.src = "/images/pfps/default.png";
|
||||
e.currentTarget.onerror = null;
|
||||
}}
|
||||
alt={`${profileData.username}'s profile`}
|
||||
className="sm:w-full h-full object-cover rounded-full relative z-0"
|
||||
/>
|
||||
|
||||
{/* If current user is the profile user then allow profile picture swap */}
|
||||
{isUser && (
|
||||
<>
|
||||
<div className="absolute inset-0 bg-black/20 opacity-0 group-hover:opacity-100 transition-opacity duration-200 rounded-full"></div>
|
||||
<div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity duration-200">
|
||||
<CameraIcon
|
||||
size={32}
|
||||
className="text-white bg-black/50 p-1 rounded-full"
|
||||
/>
|
||||
</div>
|
||||
<input
|
||||
type="file"
|
||||
className="hidden"
|
||||
onChange={saveUploadedImage}
|
||||
accept="image/*" />
|
||||
</>
|
||||
)}
|
||||
</label>
|
||||
</div>
|
||||
{/* If current user is the profile user then allow profile picture swap */}
|
||||
{isUser && (
|
||||
<>
|
||||
<div className="absolute top-0 bg-black/20 opacity-0 group-hover:opacity-100 transition-opacity duration-200 rounded-full"></div>
|
||||
<div className="absolute top-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity duration-200">
|
||||
<CameraIcon size={32} className="text-white bg-black/50 p-1 rounded-full" />
|
||||
</div>
|
||||
<input type="file" className="hidden" onChange={saveUploadedImage} accept="image/*" />
|
||||
</>
|
||||
)}
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{/* Username - Now Directly Below PFP */}
|
||||
<h1 className="text-[var(--user-name)] text-[1.5em] sm:text-[2em] font-bold -mt-[45px] sm:-mt-[90px] text-center">
|
||||
{profileData.username}
|
||||
</h1>
|
||||
{/* Username - Now Directly Below PFP */}
|
||||
<h1 className="text-[var(--user-name)] text-[1.5em] sm:text-[2em] font-bold -mt-[45px] sm:-mt-[90px] text-center">
|
||||
{profileData.username}
|
||||
</h1>
|
||||
|
||||
{/* Follower Count */}
|
||||
{userPageVariant === "streamer" && (
|
||||
<>
|
||||
<div className="flex items-center space-x-2 mb-6">
|
||||
<span className="text-gray-400">
|
||||
{profileData.followerCount.toLocaleString()} followers
|
||||
</span>
|
||||
{profileData.isPartnered && (
|
||||
<span className="bg-purple-600 text-white text-sm px-2 py-1 rounded">
|
||||
Partner
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{/* Follower Count */}
|
||||
{userPageVariant === "streamer" && (
|
||||
<>
|
||||
<div className="flex items-center space-x-2 mb-6">
|
||||
<span className="text-gray-400">{profileData.followerCount.toLocaleString()} followers</span>
|
||||
{profileData.isPartnered && <span className="bg-purple-600 text-white text-sm px-2 py-1 rounded">Partner</span>}
|
||||
</div>
|
||||
|
||||
{/* (Un)Follow Button */}
|
||||
{!isFollowing ? (
|
||||
<Button
|
||||
extraClasses="w-full bg-purple-700 hover:bg-[#28005e]"
|
||||
onClick={() => followUser(profileData.id, setShowAuthModal)}
|
||||
>
|
||||
Follow
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
extraClasses="w-full bg-[#a80000]"
|
||||
onClick={() =>
|
||||
unfollowUser(profileData?.id, setShowAuthModal)
|
||||
}
|
||||
>
|
||||
Unfollow
|
||||
</Button>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{/* (Un)Follow Button */}
|
||||
{!isFollowing ? (
|
||||
<Button
|
||||
extraClasses="w-full bg-purple-700 hover:bg-[#28005e]"
|
||||
onClick={() => followUser(profileData.id, setShowAuthModal)}
|
||||
>
|
||||
Follow
|
||||
</Button>
|
||||
) : (
|
||||
<Button extraClasses="w-full bg-[#a80000] z-50" onClick={() => unfollowUser(profileData?.id, setShowAuthModal)}>
|
||||
Unfollow
|
||||
</Button>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div
|
||||
id="settings"
|
||||
className="col-span-1 bg-[var(--user-sideBox)] rounded-lg p-6 grid grid-rows-[auto_1fr] text-center items-center justify-center"
|
||||
>
|
||||
{/* User Type (e.g., "USER") */}
|
||||
<small className="text-green-400">
|
||||
{userPageVariant.toUpperCase()}
|
||||
</small>
|
||||
<div
|
||||
id="settings"
|
||||
className="col-span-1 bg-[var(--user-sideBox)] rounded-lg p-6 grid grid-rows-[auto_1fr] text-center items-center justify-center"
|
||||
>
|
||||
{/* User Type (e.g., "USER") */}
|
||||
<small className="text-green-400">{userPageVariant.toUpperCase()}</small>
|
||||
|
||||
<div className="mt-6 text-center">
|
||||
<h2 className="text-xl font-semibold mb-3">
|
||||
About {profileData.username}
|
||||
</h2>
|
||||
<p className="text-gray-300 whitespace-pre-wrap">
|
||||
{profileData.bio}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-6 text-center">
|
||||
<h2 className="text-xl font-semibold mb-3">About {profileData.username}</h2>
|
||||
<p className="text-gray-300 whitespace-pre-wrap">{profileData.bio}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Content Section */}
|
||||
<div
|
||||
id="content"
|
||||
className="col-span-2 bg-[var(--user-contentBox)] rounded-lg p-6 grid grid-rows-[auto_1fr] text-center items-center justify-center"
|
||||
>
|
||||
{userPageVariant === "streamer" && (
|
||||
<>
|
||||
{profileData.isLive ? (
|
||||
<div className="mb-8">
|
||||
<h2 className="text-2xl bg-[#ff0000] border py-4 px-12 font-black mb-4 rounded-[4rem]">
|
||||
Currently Live!
|
||||
</h2>
|
||||
<StreamListItem
|
||||
id={profileData.id}
|
||||
title={profileData.currentStreamTitle || ""}
|
||||
streamCategory=""
|
||||
username=""
|
||||
viewers={profileData.currentStreamViewers || 0}
|
||||
thumbnail={profileData.currentStreamThumbnail}
|
||||
onItemClick={() => {
|
||||
navigate(`/${profileData.username}`);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<h1>Currently not live</h1>
|
||||
)}
|
||||
{/* Content Section */}
|
||||
<div
|
||||
id="content"
|
||||
className="col-span-2 bg-[var(--user-contentBox)] rounded-lg p-6 grid grid-rows-[auto_1fr] text-center items-center justify-center"
|
||||
>
|
||||
{userPageVariant === "streamer" && (
|
||||
<>
|
||||
{profileData.isLive ? (
|
||||
<div className="mb-8">
|
||||
<h2 className="text-2xl bg-[#ff0000] border py-4 px-12 font-black mb-4 rounded-[4rem]">Currently Live!</h2>
|
||||
<StreamListItem
|
||||
id={profileData.id}
|
||||
title={profileData.currentStreamTitle || ""}
|
||||
streamCategory=""
|
||||
username=""
|
||||
viewers={profileData.currentStreamViewers || 0}
|
||||
thumbnail={profileData.currentStreamThumbnail}
|
||||
onItemClick={() => {
|
||||
navigate(`/${profileData.username}`);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<h1>Currently not live</h1>
|
||||
)}
|
||||
|
||||
{/* ↓↓ VODS ↓↓ */}
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold mb-4">Past Broadcasts</h2>
|
||||
<div className="text-gray-400 rounded-none">
|
||||
No past broadcasts found
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
{/* ↓↓ VODS ↓↓ */}
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold mb-4">Past Broadcasts</h2>
|
||||
<div className="text-gray-400 rounded-none">No past broadcasts found</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{userPageVariant === "user" && (
|
||||
<>
|
||||
{/* ↓↓ VODS ↓↓ */}
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold mb-4">Past Broadcasts</h2>
|
||||
<div className="text-gray-400 rounded-none">
|
||||
No past broadcasts found
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{userPageVariant === "user" && (
|
||||
<>
|
||||
{/* ↓↓ VODS ↓↓ */}
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold mb-4">Past Broadcasts</h2>
|
||||
<div className="text-gray-400 rounded-none">No past broadcasts found</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div
|
||||
id="mini"
|
||||
className="bg-[var(--user-sideBox)] col-span-1 rounded-lg text-center items-center justify-center
|
||||
<div
|
||||
id="mini"
|
||||
className="bg-[var(--user-sideBox)] col-span-1 rounded-lg text-center items-center justify-center
|
||||
flex flex-col flex-grow gap-4 p-[2rem]"
|
||||
>
|
||||
<div
|
||||
className="bg-[var(--user-follow-bg)] rounded-[1em] hover:scale-105 transition-all ease-in-out duration-300
|
||||
>
|
||||
<div
|
||||
className="bg-[var(--user-follow-bg)] rounded-[1em] hover:scale-105 transition-all ease-in-out duration-300
|
||||
flex items-center justify-center w-full p-4 content-start"
|
||||
onMouseEnter={(e) =>
|
||||
(e.currentTarget.style.boxShadow = "var(--follow-shadow)")
|
||||
}
|
||||
onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")}
|
||||
>
|
||||
<button
|
||||
className="text-[var(--follow-text)] whitespace-pre-wrap"
|
||||
onClick={() => navigate(`/user/${username}/following`)}
|
||||
>
|
||||
Following
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
className="bg-[var(--user-follow-bg)] rounded-[1em] hover:scale-105 transition-all ease-in-out duration-300
|
||||
onMouseEnter={(e) => (e.currentTarget.style.boxShadow = "var(--follow-shadow)")}
|
||||
onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")}
|
||||
>
|
||||
<button className="text-[var(--follow-text)] whitespace-pre-wrap" onClick={() => navigate(`/user/${username}/following`)}>
|
||||
Following
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
className="bg-[var(--user-follow-bg)] rounded-[1em] hover:scale-105 transition-all ease-in-out duration-300
|
||||
flex items-center justify-center w-full p-4 content-start"
|
||||
onMouseEnter={(e) =>
|
||||
(e.currentTarget.style.boxShadow = "var(--follow-shadow)")
|
||||
}
|
||||
onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")}
|
||||
>
|
||||
<ul className="list-none">
|
||||
<li className="text-[var(--follow-text)] whitespace-pre-wrap list-none">
|
||||
Streamers
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div
|
||||
className="bg-[var(--user-follow-bg)] rounded-[1em] hover:scale-105 transition-all ease-in-out duration-300
|
||||
onMouseEnter={(e) => (e.currentTarget.style.boxShadow = "var(--follow-shadow)")}
|
||||
onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")}
|
||||
>
|
||||
<ul className="list-none">
|
||||
<li className="text-[var(--follow-text)] whitespace-pre-wrap list-none">Streamers</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div
|
||||
className="bg-[var(--user-follow-bg)] rounded-[1em] hover:scale-105 transition-all ease-in-out duration-300
|
||||
flex items-center justify-center w-full p-4 content-start"
|
||||
onMouseEnter={(e) =>
|
||||
(e.currentTarget.style.boxShadow = "var(--follow-shadow)")
|
||||
}
|
||||
onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")}
|
||||
>
|
||||
<button
|
||||
onClick={() => navigate(`/user/${username}/yourCategories`)}
|
||||
>
|
||||
Categories
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{showAuthModal && <AuthModal onClose={() => setShowAuthModal(false)} />}
|
||||
</DynamicPageContent>
|
||||
);
|
||||
onMouseEnter={(e) => (e.currentTarget.style.boxShadow = "var(--follow-shadow)")}
|
||||
onMouseLeave={(e) => (e.currentTarget.style.boxShadow = "none")}
|
||||
>
|
||||
<button onClick={() => navigate(`/user/${username}/yourCategories`)}>Categories</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{showAuthModal && <AuthModal onClose={() => setShowAuthModal(false)} />}
|
||||
</DynamicPageContent>
|
||||
);
|
||||
};
|
||||
|
||||
export default UserPage;
|
||||
|
||||
@@ -16,245 +16,228 @@ import { StreamType } from "../types/StreamType";
|
||||
const CheckoutForm = lazy(() => import("../components/Checkout/CheckoutForm"));
|
||||
|
||||
interface VideoPageProps {
|
||||
streamerId: number;
|
||||
streamerId: number;
|
||||
}
|
||||
|
||||
const VideoPage: React.FC<VideoPageProps> = ({ streamerId }) => {
|
||||
const { isLoggedIn } = useAuth();
|
||||
const { streamerName } = useParams<{ streamerName: string }>();
|
||||
const [streamData, setStreamData] = useState<StreamType>();
|
||||
const [viewerCount, setViewerCount] = useState(0);
|
||||
const { showSideBar } = useSidebar();
|
||||
const { isFollowing, checkFollowStatus, followUser, unfollowUser } =
|
||||
useFollow();
|
||||
const { showAuthModal, setShowAuthModal } = useAuthModal();
|
||||
const [isStripeReady, setIsStripeReady] = useState(false);
|
||||
const [showCheckout, setShowCheckout] = useState(false);
|
||||
const { showChat } = useChat();
|
||||
const navigate = useNavigate();
|
||||
const [timeStarted, setTimeStarted] = useState("");
|
||||
const { isLoggedIn } = useAuth();
|
||||
const { streamerName } = useParams<{ streamerName: string }>();
|
||||
const [streamData, setStreamData] = useState<StreamType>();
|
||||
const [viewerCount, setViewerCount] = useState(0);
|
||||
const { showSideBar } = useSidebar();
|
||||
const { isFollowing, checkFollowStatus, followUser, unfollowUser } = useFollow();
|
||||
const { showAuthModal, setShowAuthModal } = useAuthModal();
|
||||
const [isStripeReady, setIsStripeReady] = useState(false);
|
||||
const [showCheckout, setShowCheckout] = useState(false);
|
||||
const { showChat } = useChat();
|
||||
const navigate = useNavigate();
|
||||
const [timeStarted, setTimeStarted] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
// Prevent scrolling when checkout is open
|
||||
if (showCheckout) {
|
||||
document.body.style.overflow = "hidden";
|
||||
} else {
|
||||
document.body.style.overflow = "unset";
|
||||
}
|
||||
// Cleanup function to ensure overflow is restored when component unmounts
|
||||
return () => {
|
||||
document.body.style.overflow = "unset";
|
||||
};
|
||||
}, [showCheckout]);
|
||||
useEffect(() => {
|
||||
// Prevent scrolling when checkout is open
|
||||
if (showCheckout) {
|
||||
document.body.style.overflow = "hidden";
|
||||
} else {
|
||||
document.body.style.overflow = "unset";
|
||||
}
|
||||
// Cleanup function to ensure overflow is restored when component unmounts
|
||||
return () => {
|
||||
document.body.style.overflow = "unset";
|
||||
};
|
||||
}, [showCheckout]);
|
||||
|
||||
// Increment minutes of stream time every minute
|
||||
useEffect;
|
||||
// Increment minutes of stream time every minute
|
||||
useEffect;
|
||||
|
||||
useEffect(() => {
|
||||
// Fetch stream data for this streamer
|
||||
fetch(`/api/streams/${streamerId}/data`).then((res) => {
|
||||
if (!res.ok) {
|
||||
console.error("Failed to load stream data:", res.statusText);
|
||||
}
|
||||
res
|
||||
.json()
|
||||
.then((data) => {
|
||||
const transformedData: StreamType = {
|
||||
type: "stream",
|
||||
id: data.stream_id,
|
||||
username: data.username,
|
||||
title: data.title,
|
||||
startTime: data.start_time,
|
||||
streamCategory: data.category_name,
|
||||
viewers: data.viewers,
|
||||
};
|
||||
setStreamData(transformedData);
|
||||
useEffect(() => {
|
||||
// Fetch stream data for this streamer
|
||||
fetch(`/api/streams/${streamerId}/data`).then((res) => {
|
||||
if (!res.ok) {
|
||||
console.error("Failed to load stream data:", res.statusText);
|
||||
}
|
||||
res
|
||||
.json()
|
||||
.then((data) => {
|
||||
const transformedData: StreamType = {
|
||||
type: "stream",
|
||||
id: data.stream_id,
|
||||
username: data.username,
|
||||
title: data.title,
|
||||
startTime: data.start_time,
|
||||
streamCategory: data.category_name,
|
||||
viewers: data.viewers,
|
||||
};
|
||||
setStreamData(transformedData);
|
||||
|
||||
// Check if the logged-in user is following this streamer
|
||||
if (isLoggedIn) checkFollowStatus(data.username);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching stream data:", error);
|
||||
});
|
||||
});
|
||||
}, [streamerId]);
|
||||
// Check if the logged-in user is following this streamer
|
||||
if (isLoggedIn) checkFollowStatus(data.username);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching stream data:", error);
|
||||
});
|
||||
});
|
||||
}, [streamerId]);
|
||||
|
||||
// Time counter using DD:HH:MM:SS format
|
||||
useEffect(() => {
|
||||
if (!streamData?.startTime) return;
|
||||
// Time counter using DD:HH:MM:SS format
|
||||
useEffect(() => {
|
||||
if (!streamData?.startTime) return;
|
||||
|
||||
// Initial calculation
|
||||
const startTime = new Date(streamData.startTime).getTime();
|
||||
// Initial calculation
|
||||
const startTime = new Date(streamData.startTime).getTime();
|
||||
|
||||
const calculateTimeDifference = () => {
|
||||
// Get the difference in seconds
|
||||
const diffInSeconds = Math.floor((Date.now() - startTime) / 1000);
|
||||
const calculateTimeDifference = () => {
|
||||
// Get the difference in seconds
|
||||
const diffInSeconds = Math.floor((Date.now() - startTime) / 1000);
|
||||
|
||||
// Calculate days, hours, minutes, seconds
|
||||
const days = Math.floor(diffInSeconds / 86400);
|
||||
const hours = Math.floor((diffInSeconds % 86400) / 3600);
|
||||
const minutes = Math.floor((diffInSeconds % 3600) / 60);
|
||||
const seconds = diffInSeconds % 60;
|
||||
// Calculate days, hours, minutes, seconds
|
||||
const days = Math.floor(diffInSeconds / 86400);
|
||||
const hours = Math.floor((diffInSeconds % 86400) / 3600);
|
||||
const minutes = Math.floor((diffInSeconds % 3600) / 60);
|
||||
const seconds = diffInSeconds % 60;
|
||||
|
||||
// Format as DD:HH:MM:SS
|
||||
setTimeStarted(
|
||||
`${days.toString().padStart(2, "0")}:${hours
|
||||
.toString()
|
||||
.padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds
|
||||
.toString()
|
||||
.padStart(2, "0")}`
|
||||
);
|
||||
};
|
||||
// Format as DD:HH:MM:SS
|
||||
setTimeStarted(
|
||||
`${days.toString().padStart(2, "0")}:${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds
|
||||
.toString()
|
||||
.padStart(2, "0")}`
|
||||
);
|
||||
};
|
||||
|
||||
// Calculate immediately
|
||||
calculateTimeDifference();
|
||||
// Calculate immediately
|
||||
calculateTimeDifference();
|
||||
|
||||
// Set up interval to update every second
|
||||
const intervalId = setInterval(calculateTimeDifference, 1000);
|
||||
// Set up interval to update every second
|
||||
const intervalId = setInterval(calculateTimeDifference, 1000);
|
||||
|
||||
// Clean up interval on component unmount
|
||||
return () => clearInterval(intervalId);
|
||||
}, [streamData?.startTime]); // Re-run if startTime changes
|
||||
// Clean up interval on component unmount
|
||||
return () => clearInterval(intervalId);
|
||||
}, [streamData?.startTime]); // Re-run if startTime changes
|
||||
|
||||
// Load Stripe in the background when component mounts
|
||||
useEffect(() => {
|
||||
const loadStripe = async () => {
|
||||
await import("@stripe/stripe-js");
|
||||
setIsStripeReady(true);
|
||||
};
|
||||
loadStripe();
|
||||
}, []);
|
||||
// Load Stripe in the background when component mounts
|
||||
useEffect(() => {
|
||||
const loadStripe = async () => {
|
||||
await import("@stripe/stripe-js");
|
||||
setIsStripeReady(true);
|
||||
};
|
||||
loadStripe();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<SocketProvider>
|
||||
<DynamicPageContent className="w-full min-h-screen">
|
||||
<div
|
||||
id="container"
|
||||
className={`bg-gray-900 h-full grid ${
|
||||
showChat
|
||||
? showSideBar
|
||||
? "w-[85vw] duration-[1s]"
|
||||
: "w-[100vw] duration-[0.5s]"
|
||||
: showSideBar
|
||||
? "w-[110vw] duration-[1s]"
|
||||
: "w-[125vw] duration-[0.5s]"
|
||||
} grid-rows-[auto_1fr] grid-cols-[auto_25vw] transition-all ease-in-out`}
|
||||
>
|
||||
<div className="relative">
|
||||
<VideoPlayer />
|
||||
</div>
|
||||
return (
|
||||
<SocketProvider>
|
||||
<DynamicPageContent className="w-full min-h-screen">
|
||||
<div
|
||||
id="container"
|
||||
className={`bg-gray-900 h-full grid ${
|
||||
showChat
|
||||
? showSideBar
|
||||
? "w-[85vw] duration-[1s]"
|
||||
: "w-[100vw] duration-[0.5s]"
|
||||
: showSideBar
|
||||
? "w-[110vw] duration-[1s]"
|
||||
: "w-[125vw] duration-[0.5s]"
|
||||
} grid-rows-[auto_1fr] grid-cols-[auto_25vw] transition-all ease-in-out`}
|
||||
>
|
||||
<div className="relative">
|
||||
<VideoPlayer />
|
||||
</div>
|
||||
|
||||
<ChatPanel
|
||||
streamId={streamerId}
|
||||
onViewerCountChange={(count: number) => setViewerCount(count)}
|
||||
/>
|
||||
<ChatPanel streamId={streamerId} onViewerCountChange={(count: number) => setViewerCount(count)} />
|
||||
|
||||
{/* Stream Data */}
|
||||
<div
|
||||
id="stream-info"
|
||||
className="flex flex-row items-center justify-between gap-4 p-4 bg-[#18181b] text-white text-lg rounded-md shadow-lg"
|
||||
>
|
||||
{/* Streamer Icon */}
|
||||
<div className="flex flex-col items-center mb-[1em]">
|
||||
<img
|
||||
src="/images/monkey.png"
|
||||
alt="streamer"
|
||||
className="w-[3em] h-[3em] rounded-full border-[0.15em] border-purple-500 cursor-pointer"
|
||||
onClick={() => navigate(`/user/${streamerName}`)}
|
||||
/>
|
||||
<button
|
||||
className="text-white font-bold hover:underline mt-[0.5em]"
|
||||
onClick={() => navigate(`/user/${streamerName}`)}
|
||||
>
|
||||
{streamerName}
|
||||
</button>
|
||||
</div>
|
||||
{/* Stream Data */}
|
||||
<div
|
||||
id="stream-info"
|
||||
className="flex flex-row items-center justify-between gap-4 p-4 bg-[#18181b] text-white text-lg rounded-md shadow-lg"
|
||||
>
|
||||
{/* Streamer Icon */}
|
||||
<div className="flex flex-col items-center mb-[1em]">
|
||||
<img
|
||||
src={`/user/${streamerName}/profile_picture`}
|
||||
onError={(e) => {
|
||||
e.currentTarget.src = "/images/pfps/default.png";
|
||||
e.currentTarget.onerror = null;
|
||||
}}
|
||||
alt="streamer"
|
||||
className="w-[3em] h-[3em] rounded-full border-[0.15em] border-purple-500 cursor-pointer"
|
||||
onClick={() => navigate(`/user/${streamerName}`)}
|
||||
/>
|
||||
<button className="text-white font-bold hover:underline mt-[0.5em]" onClick={() => navigate(`/user/${streamerName}`)}>
|
||||
{streamerName}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Stream Title */}
|
||||
<div className="flex flex-col items-start flex-grow">
|
||||
<h2 className="text-[0.75em] lg:text-[0.85em] xl:text-[1em] font-bold">
|
||||
{streamData ? streamData.title : "Loading..."}
|
||||
</h2>
|
||||
<a href={streamData ? `/category/${streamData.streamCategory}` : "#"} className="text-[0.75em] lg:text-[0.85em] xl:text-[1em] text-gray-400">
|
||||
{streamData ? streamData.streamCategory : "Loading..."}
|
||||
</a>
|
||||
</div>
|
||||
{/* Stream Title */}
|
||||
<div className="flex flex-col items-start flex-grow">
|
||||
<h2 className="text-[0.75em] lg:text-[0.85em] xl:text-[1em] font-bold">{streamData ? streamData.title : "Loading..."}</h2>
|
||||
<a
|
||||
href={streamData ? `/category/${streamData.streamCategory}` : "#"}
|
||||
className="text-[0.75em] lg:text-[0.85em] xl:text-[1em] text-gray-400"
|
||||
>
|
||||
{streamData ? streamData.streamCategory : "Loading..."}
|
||||
</a>
|
||||
</div>
|
||||
|
||||
{/* Streamer Info */}
|
||||
<div className="flex items-center gap-[0.75em] flex-col lg:flex-row">
|
||||
<div className="group flex flex-col items-center lg:items-start">
|
||||
{!isFollowing ? (
|
||||
<button
|
||||
className="bg-purple-600 text-white font-bold px-[1.5em] py-[0.5em] rounded-md hover:bg-purple-700 text-sm"
|
||||
onClick={() => followUser(streamerId, setShowAuthModal)}
|
||||
>
|
||||
Follow
|
||||
</button>
|
||||
) : (
|
||||
<button
|
||||
className="bg-gray-700 text-white font-bold px-[1.5em] py-[0.5em] rounded-md hover:bg-red-600 text-sm transition-all"
|
||||
onClick={() => unfollowUser(streamerId, setShowAuthModal)}
|
||||
>
|
||||
<span className="group-hover:hidden">Following</span>
|
||||
<span className="hidden group-hover:block">Unfollow</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{/* Streamer Info */}
|
||||
<div className="flex items-center gap-[0.75em] flex-col lg:flex-row">
|
||||
<div className="group flex flex-col items-center lg:items-start">
|
||||
{!isFollowing ? (
|
||||
<button
|
||||
className="bg-purple-600 text-white font-bold px-[1.5em] py-[0.5em] rounded-md hover:bg-purple-700 text-sm"
|
||||
onClick={() => followUser(streamerId, setShowAuthModal)}
|
||||
>
|
||||
Follow
|
||||
</button>
|
||||
) : (
|
||||
<button
|
||||
className="bg-gray-700 text-white font-bold px-[1.5em] py-[0.5em] rounded-md hover:bg-red-600 text-sm transition-all"
|
||||
onClick={() => unfollowUser(streamerId, setShowAuthModal)}
|
||||
>
|
||||
<span className="group-hover:hidden">Following</span>
|
||||
<span className="hidden group-hover:block">Unfollow</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Stream Stats */}
|
||||
<div className="flex flex-col items-center">
|
||||
<div className="flex items-center gap-[0.5em]">
|
||||
<img
|
||||
src="../../../images/icons/user.png"
|
||||
alt="Viewers Icon"
|
||||
className="w-[1em] h-[1em] md:w-[1.2em] md:h-[1.2em]"
|
||||
/>
|
||||
<span className="font-bold text-[1.2em]">{viewerCount}</span>
|
||||
</div>
|
||||
</div>
|
||||
{/* Stream Stats */}
|
||||
<div className="flex flex-col items-center">
|
||||
<div className="flex items-center gap-[0.5em]">
|
||||
<img src="/images/icons/user.png" alt="Viewers Icon" className="w-[1em] h-[1em] md:w-[1.2em] md:h-[1.2em]" />
|
||||
<span className="font-bold text-[1.2em]">{viewerCount}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col items-center p-4 min-w-fit">
|
||||
<span className="text-[0.75em]">
|
||||
{streamData ? timeStarted : "Loading..."}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex flex-col items-center p-4 min-w-fit">
|
||||
<span className="text-[0.75em]">{streamData ? timeStarted : "Loading..."}</span>
|
||||
</div>
|
||||
|
||||
{/* Subscribe Button */}
|
||||
<div className="flex flex-col items-center">
|
||||
<button
|
||||
className={`bg-red-600 text-white font-bold px-[1.5em] py-[0.5em] rounded-md
|
||||
${
|
||||
isStripeReady ? "hover:bg-red-700" : "opacity-20 cursor-not-allowed"
|
||||
} transition-all`}
|
||||
onClick={() => {
|
||||
if (!isLoggedIn) {
|
||||
setShowAuthModal(true);
|
||||
} else if (isStripeReady) {
|
||||
setShowCheckout(true);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{isStripeReady ? "Subscribe" : "Loading..."}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{showCheckout && (
|
||||
<Suspense fallback={<div>Loading checkout...</div>}>
|
||||
<CheckoutForm
|
||||
onClose={() => setShowCheckout(false)}
|
||||
streamerID={streamerId}
|
||||
/>
|
||||
</Suspense>
|
||||
)}
|
||||
{/* {showReturn && <Return />} */}
|
||||
{showAuthModal && (
|
||||
<AuthModal onClose={() => setShowAuthModal(false)} />
|
||||
)}
|
||||
</div>
|
||||
</DynamicPageContent>
|
||||
</SocketProvider>
|
||||
);
|
||||
{/* Subscribe Button */}
|
||||
<div className="flex flex-col items-center">
|
||||
<button
|
||||
className={`bg-red-600 text-white font-bold px-[1.5em] py-[0.5em] rounded-md
|
||||
${isStripeReady ? "hover:bg-red-700" : "opacity-20 cursor-not-allowed"} transition-all`}
|
||||
onClick={() => {
|
||||
if (!isLoggedIn) {
|
||||
setShowAuthModal(true);
|
||||
} else if (isStripeReady) {
|
||||
setShowCheckout(true);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{isStripeReady ? "Subscribe" : "Loading..."}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{showCheckout && (
|
||||
<Suspense fallback={<div>Loading checkout...</div>}>
|
||||
<CheckoutForm onClose={() => setShowCheckout(false)} streamerID={streamerId} />
|
||||
</Suspense>
|
||||
)}
|
||||
{/* {showReturn && <Return />} */}
|
||||
{showAuthModal && <AuthModal onClose={() => setShowAuthModal(false)} />}
|
||||
</div>
|
||||
</DynamicPageContent>
|
||||
</SocketProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export default VideoPage;
|
||||
|
||||
Reference in New Issue
Block a user