81 lines
3.0 KiB
JavaScript
Raw Normal View History

2024-08-04 19:00:26 -05:00
import React, { useEffect, useState } from "react";
2024-03-26 18:14:32 -05:00
import Image from "next/image";
import { useRouter } from "next/router";
import { formatTimestampToHowLongAgo } from "@/utils/time";
import { useImageProxy } from "@/hooks/useImageProxy";
2024-08-13 14:42:36 -05:00
import { getTotalFromZaps } from "@/utils/lightning";
2024-04-22 19:09:46 -05:00
import ZapDisplay from "@/components/zaps/ZapDisplay";
import { Tag } from "primereact/tag";
2024-08-13 14:42:36 -05:00
import { useZapsSubscription } from "@/hooks/nostrQueries/zaps/useZapsSubscription";
2024-03-26 18:14:32 -05:00
const CourseTemplate = ({ course }) => {
2024-08-25 18:15:45 -05:00
const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: course });
const [zapAmount, setZapAmount] = useState(0);
const router = useRouter();
const { returnImageProxy } = useImageProxy();
useEffect(() => {
2024-08-25 18:15:45 -05:00
if (zaps.length > 0) {
const total = getTotalFromZaps(zaps, course);
setZapAmount(total);
}
}, [zaps, course]);
if (zapsError) return <div>Error: {zapsError}</div>;
return (
<div
2024-09-10 15:44:08 -05:00
className="flex flex-col items-center mx-auto px-4 mt-8 rounded-md max-tab:px-0"
>
{/* Wrap the image in a div with a relative class with a padding-bottom of 56.25% representing the aspect ratio of 16:9 */}
<div
2024-08-25 18:15:45 -05:00
onClick={() => router.replace(`/course/${course.id}`)}
className="relative w-full h-0 hover:opacity-80 transition-opacity duration-300 cursor-pointer"
style={{ paddingBottom: "56.25%" }}
>
<Image
alt="course thumbnail"
src={returnImageProxy(course.image)}
quality={100}
layout="fill"
objectFit="cover"
className="rounded-md"
/>
</div>
<div className="flex flex-col justify-start w-full mt-4">
<h4 className="mb-1 font-bold text-lg font-blinker line-clamp-2">
{course.name || course.title}
</h4>
<p className="text-sm text-gray-500 line-clamp-2">{course.description || course.summary}</p>
{course.price && course.price > 0 ? (
<p className="text-sm text-gray-500 line-clamp-2">Price: {course.price} sats</p>
) : (
<p className="text-sm text-gray-500 line-clamp-2">Free</p>
)}
<div className="flex flex-row justify-between items-center mt-2">
<p className="text-xs text-gray-400">
{course?.published_at && course.published_at !== "" ? (
formatTimestampToHowLongAgo(course.published_at)
) : (
formatTimestampToHowLongAgo(course.created_at)
)}
</p>
2024-08-25 18:15:45 -05:00
<ZapDisplay
zapAmount={zapAmount}
event={course}
zapsLoading={zapsLoading && zapAmount === 0}
/>
2024-03-26 18:14:32 -05:00
</div>
{course?.topics && course?.topics.length > 0 && (
<div className="flex flex-row justify-start items-center mt-2">
{course.topics.map((topic, index) => (
<Tag key={index} value={topic} className="mr-2 text-white" />
))}
</div>
)}
</div>
</div>
);
2024-03-26 18:14:32 -05:00
};
2024-08-25 18:15:45 -05:00
export default CourseTemplate;