Skip to content

Instantly share code, notes, and snippets.

@steven-tey
Last active May 7, 2024 20:00
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save steven-tey/0ceb370d66c362796d04eb649063997b to your computer and use it in GitHub Desktop.
Save steven-tey/0ceb370d66c362796d04eb649063997b to your computer and use it in GitHub Desktop.
YoutubeChannel RSC
import { BlurImage, YouTube } from "@dub/ui";
import { nFormatter } from "@dub/utils";
import { Eye, UserCheck, Video } from "lucide-react";
import { Suspense } from "react";
export function YoutubeChannel({ id }: { id: string }) {
return (
<Suspense fallback={<div className="not-prose grid gap-4"></div>}>
<YoutubeChannelRSC id={id} />
</Suspense>
);
}
const YoutubeChannelRSC = async ({ id }: { id: string }) => {
const res = await fetch(
`https://youtube.googleapis.com/youtube/v3/channels?part=statistics&part=brandingSettings&id=${id}&key=${process.env.YOUTUBE_API_KEY}`,
).then((res) => res.json());
if (res.error) {
return null;
}
const { statistics, brandingSettings } = res.items[0];
const channel = {
subscribers: statistics.subscriberCount,
views: statistics.viewCount,
videos: statistics.videoCount,
title: brandingSettings.channel.title,
description: brandingSettings.channel.description,
image: brandingSettings.image.bannerExternalUrl,
};
return (
<div className="not-prose group mx-auto max-w-md overflow-hidden rounded-xl border border-gray-200 bg-white transition-all hover:border-gray-500 hover:ring-4 hover:ring-gray-200">
<a href={`https://www.youtube.com/channel/${id}`} target="_blank">
<BlurImage
src={channel.image}
alt={channel.title}
width={2560}
height={600}
className="aspect-[2560/600] w-full object-cover"
/>
<div>
<div className="flex items-start p-4">
<div className="flex-1 space-y-1 pt-1">
<h2 className="text-xl font-semibold tracking-tight">
{channel.title}
</h2>
<p className="text-sm leading-normal text-gray-500">
{channel.description}
</p>
</div>
<YouTube className="h-8 w-8 text-gray-600 transition-colors group-hover:text-[#ff0000]" />
</div>
<div className="flex justify-center space-x-8 border-t border-gray-200 py-4">
<div className="flex items-start space-x-2">
<UserCheck size={16} className="mt-1.5" />
<div>
<p className="font-semibold text-gray-600">
{nFormatter(channel.subscribers)}
</p>
<p className="text-xs font-normal text-gray-500">Subscribers</p>
</div>
</div>
<div className="flex items-start space-x-2">
<Video size={16} className="mt-1.5" />
<div>
<p className="font-semibold text-gray-600">
{nFormatter(channel.videos)}
</p>
<p className="text-xs font-normal text-gray-500">Videos</p>
</div>
</div>
<div className="flex items-start space-x-2">
<Eye size={16} className="mt-1.5" />
<div>
<p className="font-semibold text-gray-600">
{nFormatter(channel.views)}
</p>
<p className="text-xs font-normal text-gray-500">Views</p>
</div>
</div>
</div>
</div>
</a>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment