-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add component for extracting thumbnail of youtube
- Loading branch information
1 parent
1b4db29
commit 798c0aa
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
apps/blog/src/components/YoutubeThumbnailExtractor/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
798c0aa
There was a problem hiding this comment.
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