Skip to content

Commit

Permalink
feat: Support youtube list embed URL (#4786)
Browse files Browse the repository at this point in the history
## Description

With this PR user can paste an embed url like this
https://youtube.com/embed?listType=playlist&list=UUjk2nKmHzgH5Xy-C5qYRd5A
and we will still be able to render it

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
0000)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
  • Loading branch information
kof authored Jan 25, 2025
1 parent 5dcc8e5 commit c838f40
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/sdk-components-react/src/youtube.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ const getVideoId = (url?: string) => {
}
try {
const urlObj = new URL(url);
// It's already an embed URL, we don't need to extract video id.
// It could be something like this https://youtube.com/embed?listType=playlist&list=UUjk2nKmHzgH5Xy-C5qYRd5A
if (urlObj.pathname === "/embed") {
return;
}
if (urlObj.hostname === "youtu.be") {
return urlObj.pathname.slice(1);
}
Expand All @@ -185,12 +190,23 @@ const getVideoId = (url?: string) => {

const getVideoUrl = (options: YouTubePlayerOptions, videoUrlOrigin: string) => {
const videoId = getVideoId(options.url);
if (!videoId) {
return;
const url = new URL(videoUrlOrigin);

if (videoId) {
url.pathname = `/embed/${videoId}`;
} else if (options.url) {
// E.g. this won't have videoId https://youtube.com/embed?listType=playlist&list=UUjk2nKmHzgH5Xy-C5qYRd5A
// It may also contain parameters since its an embed URL, so we want to keep it as-is and just use the origin we predefined
// so that no cookies option still works
try {
const parsedUrl = new URL(options.url);
url.pathname = parsedUrl.pathname;
url.search = parsedUrl.search;
} catch {
// Ignore invalid URL
}
}

const url = new URL(`${videoUrlOrigin}/embed/${videoId}`);

const optionsKeys = Object.keys(options) as (keyof YouTubePlayerParameters)[];

const parameters: Record<string, string | undefined> = {};
Expand Down

0 comments on commit c838f40

Please sign in to comment.