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

[gh46] Resize the YouTube video #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
@apply border-1 border-neutral-200 dark:border-neutral-800 p-2;
}

iframe {
@apply mb-10;
}

/* -------------------------- HEADER ------------------------------------- */
/* logo - right click menu */
.right-click-menu {
Expand Down
35 changes: 33 additions & 2 deletions web/src/pages/BlogIndividualPage/BlogIndividualPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Suspense, lazy } from 'react'
import { Suspense, lazy, useEffect, useState } from 'react'

import { routes, useLocation } from '@redwoodjs/router'
import { Metadata } from '@redwoodjs/web'
Expand All @@ -10,12 +10,43 @@ import { prettifyDate } from 'src/helpers/DateHelpers'
const createLazyComponent = (slug: string) =>
lazy(() => import(`../../content/posts/${slug}.mdx`))

const getYouTubeVideoID = (url: string) => {
return url.split('/').pop().split('?')[0]
}

// use this API to get the meta data as JSON for the video
// https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoID}format=json
const getYouTubeSize = async (videoID: string) => {
try {
const response = await fetch(
`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoID}&format=json`
)
if (!response.ok) {
throw new Error('Network response was not ok')
}
return await response.json()
} catch (error) {
console.error('Error fetching video metadata:', error)
}
}

const Loading = () => <div>Loading...</div>

const BlogIndividualPage = ({ slug }) => {
const [isSuspenseResolved, setIsSuspenseResolved] = useState(false)
const { origin } = useLocation()
const post = getPostBySlug(slug)

useEffect(() => {
// find any YouTube embeds on the page
const youtubeEmbeds = document.querySelectorAll('iframe')
youtubeEmbeds.forEach(async (embed) => {
const videoID = getYouTubeVideoID(embed.src)
const data = await getYouTubeSize(videoID)
embed.style.aspectRatio = `${data.width / data.height}`
})
}, [isSuspenseResolved])

// TODO: Provide a better 404 component
if (!post) {
return (
Expand Down Expand Up @@ -54,7 +85,7 @@ const BlogIndividualPage = ({ slug }) => {

<div className="blog-post">
<Suspense fallback={<Loading />}>
<ContentComponent />
<ContentComponent onLoad={() => setIsSuspenseResolved(true)} />
</Suspense>
</div>
</div>
Expand Down