Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support youtube list embed URL #4786

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 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,11 +190,26 @@ 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}`);
if (!url) {
return;
}

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

Expand Down
Loading