memorium/components/Youtube.tsx

38 lines
812 B
TypeScript
Raw Normal View History

2023-08-02 01:58:03 +02:00
export const isYoutubeLink = (link: string) => {
try {
const url = new URL(link);
return ["youtu.be", "youtube.com"].includes(url.hostname);
} catch (err) {
console.log(err);
return false;
}
};
function extractYoutubeId(link: string) {
const url = new URL(link);
if (url.searchParams.has("v")) {
const id = url.searchParams.get("v");
if (id?.length && id.length > 4) {
return id;
}
}
return url.pathname.replace(/^\//, "");
}
export const YoutubePlayer = ({ link }: { link: string }) => {
const id = extractYoutubeId(link);
return (
<iframe
width="100%"
height="400px"
src={`https://www.youtube-nocookie.com/embed/${id}`}
frameBorder="0"
allow="autoplay; encrypted-media"
allowFullScreen
>
</iframe>
);
};