Created
November 27, 2024 00:03
-
-
Save AlessioGr/c6fc475b5ccc49dd77fccdd4d0681e07 to your computer and use it in GitHub Desktop.
Video component optimized
This file contains hidden or 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
| 'use client' | |
| import { useFormFields } from '@payloadcms/ui' | |
| import React from 'react' | |
| import { YouTubePlayer } from './youtube-player.js' | |
| function removeLastSegment(input: string) { | |
| const lastPeriodIndex = input.lastIndexOf('.') | |
| return lastPeriodIndex === -1 ? input : input.slice(0, lastPeriodIndex) | |
| } | |
| function VideoEmbedWrapper({ children }: { children: React.ReactNode }) { | |
| return ( | |
| <div | |
| style={{ | |
| alignItems: 'center', | |
| border: '1px #555 solid', | |
| color: '#eeeeee', | |
| display: 'flex', | |
| height: '160px', | |
| justifyContent: 'center', | |
| margin: '0 auto', | |
| padding: '8px', | |
| width: '210px', | |
| }} | |
| > | |
| {children} | |
| </div> | |
| ) | |
| } | |
| export const VideoEmbed = ({ path }: { path: string }) => { | |
| const pathToTypeField = removeLastSegment(path) + '.type' | |
| const pathToURLField = removeLastSegment(path) + '.url' | |
| const typeField = useFormFields(([fields]) => fields?.[pathToTypeField]) | |
| const urlField = useFormFields(([fields]) => fields?.[pathToURLField] ?? fields?.url) | |
| // console.log('Type Field value:', typeField.value) | |
| const type = (typeField?.value as string) ?? ('youtube' as string) | |
| // console.log('URL Field value:', urlField.value) | |
| const url = urlField.value as string | |
| let message: null | string = null | |
| if (type == null || type?.length === 0) { | |
| message = 'Please select a type of video to embed.' | |
| } | |
| if (url == null || url?.length === 0) { | |
| message = 'Please enter a URL.' | |
| } | |
| if (message != null) { | |
| return ( | |
| <VideoEmbedWrapper> | |
| <span>{message}</span> | |
| </VideoEmbedWrapper> | |
| ) | |
| } | |
| if (type === 'youtube') { | |
| return ( | |
| <VideoEmbedWrapper> | |
| <YouTubePlayer url={url} /> | |
| </VideoEmbedWrapper> | |
| ) | |
| } | |
| if (type === 'vimeo') { | |
| return <p>Not implemented</p> | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment