Skip to content

Commit

Permalink
Add component for extracting thumbnail of youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed Feb 13, 2025
1 parent 1b4db29 commit 798c0aa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/blog/src/components/MDXComponents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { AnchorHTMLAttributes, DetailedHTMLProps, ImgHTMLAttributes, TableH
import NextLink from "next/link";
import { Icon } from "@marshallku/icon";
import PostImage from "#components/PostImage";
import YoutubeThumbnailExtractor from "#components/YoutubeThumbnailExtractor";

function Link({
href,
Expand Down Expand Up @@ -46,6 +47,7 @@ const MDXComponents = {
<table {...props} />
</div>
),
YoutubeThumbnailExtractor,
};

export default MDXComponents;
52 changes: 52 additions & 0 deletions apps/blog/src/components/YoutubeThumbnailExtractor/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use client";

import { useCallback, useState } from "react";

const VIDEO_SIZES = ["hq720", "sddefault", "hqdefault", "mqdefault", "default"];

function YoutubeThumbnailExtractor() {
const [videoId, setVideoId] = useState("");
const [videoUrl, setVideoUrl] = useState("");
const [thumbnails, setThumbnails] = useState<string[]>([]);

const handleSubmit = useCallback(
(e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const youtubeRegex = /youtu\.?be(\.com)?\/(live\/)?(shorts\/|watch\?v=|embed\/)?([^&?\s]+)/;
const id = videoUrl.match(youtubeRegex)?.[4];
if (!id) return;
setVideoId(id);
const thumbnails = VIDEO_SIZES.map((size) => `https://i.ytimg.com/vi/${id}/${size}.jpg`);
setThumbnails(thumbnails);
},
[videoUrl],
);

return (
<>
<form className="form" onSubmit={handleSubmit}>
<input
type="text"
className="form__input"
name="uri"
placeholder="URI of Youtube video"
value={videoUrl}
onChange={(e) => setVideoUrl(e.target.value)}
/>
<button type="submit" className="form__submit">
Export
</button>
</form>
<div className="result" style={{ display: videoId ? "block" : "none" }}>
<div className="result__title">Thumbnails</div>
<div className="result__thumbnail">
{thumbnails.map((thumbnail) => (
<img key={thumbnail} src={thumbnail} alt={thumbnail} />
))}
</div>
</div>
</>
);
}

export default YoutubeThumbnailExtractor;

1 comment on commit 798c0aa

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visual regression test result - success

Component Story Success Viewport MisMatch Percentage
components-button string-children phone 0.00%
components-button string-children tablet 0.10%
input-input default phone 0.00%
input-input default tablet 0.10%
input-input line phone 0.00%
input-input line tablet 0.04%
input-input box phone 0.00%
input-input box tablet 0.04%
components-profile default phone 0.00%
components-profile default tablet 0.00%
input-textarea line phone 0.00%
input-textarea line tablet 0.04%
input-textarea box phone 0.00%
input-textarea box tablet 0.04%
typography-typography default phone 0.00%
typography-typography default tablet 0.07%

Please sign in to comment.