import { useEffect, useState } from "react"; import { useParams } from "react-router-dom"; import { prettyPrintTime } from "../../../libs/time"; import { fetchUserData } from "../../../libs/cachet"; import type { RecentTake } from "../../api/routes/recentTakes"; import type { Project } from "../../api/routes/projects"; import Masonry from "react-masonry-css"; export function ProjectTakes() { const { user } = useParams(); const [takes, setTakes] = useState([]); const [userData, setUserData] = useState<{ [key: string]: { displayName: string; imageUrl: string }; }>({}); const [project, setProject] = useState(); useEffect(() => { async function getTakes() { try { const res = await fetch( `/api/recentTakes?user=${encodeURIComponent(user as string)}`, ); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } const data = await res.json(); setTakes(data.takes); } catch (error) { console.error("Error fetching takes:", error); setTakes([]); } } async function getProject() { try { const res = await fetch( `/api/projects?user=${encodeURIComponent(user as string)}`, ); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } const data = await res.json(); setProject(data.projects); } catch (error) { console.error("Error fetching project:", error); } } getTakes(); getProject(); }, [user]); useEffect(() => { async function loadUserData() { const userIds = takes.map((take) => take.userId); const uniqueIds = [...new Set(userIds)]; try { for (const id of uniqueIds) { const data = await fetchUserData(id); setUserData((prevData) => ({ ...prevData, [id]: { displayName: data.displayName, imageUrl: data.image, }, })); } } catch (error) { console.error("Error fetching user data:", error); } } loadUserData(); }, [takes]); const breakpointColumns = { default: 4, 1100: 3, 700: 2, 500: 1, }; return (
{project?.projectBannerUrl && ( Project banner )}

{project?.projectName || "Recent Takes"}

{takes.length === 0 ? (
No takes found
) : ( {takes.map((take) => (

{take.project}

Profile {userData[take.userId] ?.displayName ?? take.userId}
Completed: {new Date( take.createdAt, ).toLocaleString()}
Duration: {prettyPrintTime( take.elapsedTime * 1000, )}
{take.mediaUrls?.map( (url: string, index: number) => { // More robust video detection for Slack-style URLs const isVideo = /\.(mp4|mov|webm|ogg)/i.test(url) || (url.includes("files.slack.com") && url.includes("download")); const contentType = isVideo ? "video" : "image"; return (
{isVideo ? ( ) : ( {`Media )}
); }, )}
))}
)}
); }