-
Notifications
You must be signed in to change notification settings - Fork 35
Add youtube link embeds #497
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
Merged
+312
−8
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e590618
horrendous state but it kind of works hopefully i can squash these
thundertheidiot c20479b
cleanup and finalization ish
thundertheidiot f24bcb1
formatting
thundertheidiot eefc4cd
don't nest ternary
thundertheidiot 17263e7
fixup
thundertheidiot a4037c7
default settings
thundertheidiot 8cef410
fix lints
thundertheidiot 57208c9
fix final lints, add changeset
thundertheidiot c82384b
Add title properties to settings toggles
7w1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| default: minor | ||
| --- | ||
|
|
||
| # Add support for youtube embeds. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import { useCallback, useEffect, useState, ReactNode } from 'react'; | ||
| import { Box, Badge, Icon, IconButton, Icons, Spinner, Text, as, toRem } from 'folds'; | ||
| import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback'; | ||
| import { useSetting } from '$state/hooks/settings'; | ||
| import { settingsAtom } from '$state/settings'; | ||
| import { encodeBlurHash } from '$utils/blurHash'; | ||
| import { MATRIX_BLUR_HASH_PROPERTY_NAME } from '$types/matrix/common'; | ||
| import { Attachment, AttachmentBox, AttachmentHeader } from '../message/attachment'; | ||
| import { Image } from '../media'; | ||
| import { UrlPreview } from './UrlPreview'; | ||
| import { VideoContent } from '../message'; | ||
|
|
||
| interface OEmbed { | ||
| type: 'photo' | 'video' | 'link' | 'rich'; | ||
| version: '1.0'; | ||
| title?: string; | ||
| author_name?: string; | ||
| author_url?: string; | ||
| provider_name?: string; | ||
| provider_url?: string; | ||
| cache_age?: string; | ||
| thumbnail_url?: string; | ||
| thumbnail_width?: number; | ||
| thumbnail_height?: number; | ||
| url?: string; | ||
| html?: string; | ||
| width?: number; | ||
| height?: number; | ||
| } | ||
|
|
||
| async function oEmbedData(url: string): Promise<OEmbed> { | ||
| const data = await fetch(url).then((resp) => resp.json()); | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| export type EmbedHeaderProps = { | ||
| title: string; | ||
| source: string; | ||
| after?: ReactNode; | ||
| }; | ||
| export const EmbedHeader = as<'div', EmbedHeaderProps>(({ title, source, after }) => ( | ||
| <AttachmentHeader> | ||
| <Box alignItems="Center" gap="200" grow="Yes"> | ||
| <Box shrink="No"> | ||
| <Badge style={{ maxWidth: toRem(100) }} variant="Secondary" radii="Pill"> | ||
| <Text size="O400" truncate> | ||
| {source} | ||
| </Text> | ||
| </Badge> | ||
| </Box> | ||
| <Box grow="Yes"> | ||
| <Text size="T300" truncate> | ||
| {title} | ||
| </Text> | ||
| </Box> | ||
| {after} | ||
| </Box> | ||
| </AttachmentHeader> | ||
| )); | ||
|
|
||
| type EmbedOpenButtonProps = { | ||
| url: string; | ||
| }; | ||
| export function EmbedOpenButton({ url }: EmbedOpenButtonProps) { | ||
| return ( | ||
| <IconButton size="300" radii="300" onClick={() => window.open(url, '_blank')}> | ||
| <Icon size="100" src={Icons.Link} /> | ||
| </IconButton> | ||
| ); | ||
| } | ||
|
|
||
| type YoutubeElementProps = { | ||
| videoId: string; | ||
| embedData: OEmbed; | ||
| }; | ||
|
|
||
| export const YoutubeElement = as<'div', YoutubeElementProps>(({ videoId, embedData }) => { | ||
| const thumbnailUrl = `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg`; | ||
| const iframeSrc = `https://www.youtube-nocookie.com/embed/${encodeURIComponent(videoId)}?autoplay=1`; | ||
| const videoUrl = `https://youtube.com/watch?v=${videoId}`; | ||
|
|
||
| const [blurHash, setBlurHash] = useState<string | undefined>(); | ||
|
|
||
| const title = embedData.title ? embedData.title : ''; | ||
|
|
||
| return ( | ||
| <Attachment | ||
| style={{ | ||
| flexGrow: 1, | ||
| flexShrink: 0, | ||
| width: '640px', | ||
| height: '400px', | ||
| }} | ||
| > | ||
| <AttachmentHeader> | ||
| <EmbedHeader title={title} source="YOUTUBE" after={EmbedOpenButton({ url: videoUrl })} /> | ||
| </AttachmentHeader> | ||
| <AttachmentBox | ||
| style={{ | ||
| height: '100%', | ||
| width: '100%', | ||
| }} | ||
| > | ||
| <VideoContent | ||
| body={title} | ||
| mimeType="fake" | ||
| url={videoUrl} | ||
| info={{ | ||
| thumbnail_info: { [MATRIX_BLUR_HASH_PROPERTY_NAME]: blurHash }, | ||
| }} | ||
| renderThumbnail={() => ( | ||
| <Image | ||
| src={thumbnailUrl} | ||
| /* | ||
| this allows the blurhash to be computed, otherwise it throws an "insecure operation" error | ||
| maybe that happens for a good reason, in which case this should probably be removed | ||
| */ | ||
| crossOrigin="anonymous" | ||
| onLoad={(e) => { | ||
| setBlurHash(encodeBlurHash(e.currentTarget, 32, 32)); | ||
| }} | ||
| /> | ||
| )} | ||
| renderVideo={({ onLoadedMetadata }) => ( | ||
| <iframe | ||
| src={iframeSrc} | ||
| title="YouTube embed" | ||
| onLoad={onLoadedMetadata} | ||
| allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" | ||
| referrerPolicy="strict-origin-when-cross-origin" | ||
| width="640" | ||
| height="360" | ||
| allowFullScreen | ||
| /> | ||
| )} | ||
| /> | ||
| </AttachmentBox> | ||
| </Attachment> | ||
| ); | ||
| }); | ||
|
|
||
| const youtubeUrl = (url: string) => url.match(/(https:\/\/)(www\.|m\.|)(youtube\.com|youtu\.be)\//); | ||
|
|
||
| export const ClientPreview = as<'div', { url: string }>(({ url, ...props }, ref) => { | ||
| const [showYoutube] = useSetting(settingsAtom, 'clientPreviewYoutube'); | ||
|
|
||
| // this component is overly complicated, because it was designed to support more embed types than just youtube | ||
| // i'm leaving this mess here to support later expansion | ||
| const isYoutube = !!youtubeUrl(url); | ||
| const videoId = isYoutube ? url.match(/(?:shorts\/|watch\?v=|youtu\.be\/)(.{11})/)?.[1] : null; | ||
|
|
||
| const fetchUrl = | ||
| isYoutube && videoId | ||
| ? `https://www.youtube.com/oembed?url=${encodeURIComponent(`https://youtube.com/watch?v=${videoId}`)}` | ||
| : url; | ||
|
|
||
| const [embedStatus, loadEmbed] = useAsyncCallback( | ||
| useCallback(() => oEmbedData(fetchUrl), [fetchUrl]) | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| const fetchYoutube = isYoutube && showYoutube; | ||
|
|
||
| if (fetchYoutube) loadEmbed(); | ||
| }, [isYoutube, showYoutube, loadEmbed]); | ||
|
|
||
| let previewContent; | ||
|
|
||
| if (isYoutube && videoId) { | ||
| if (showYoutube) { | ||
| if (embedStatus.status === AsyncStatus.Error) return null; | ||
|
|
||
| if (embedStatus.status === AsyncStatus.Success && embedStatus.data) { | ||
| previewContent = <YoutubeElement videoId={videoId} embedData={embedStatus.data} />; | ||
| } else { | ||
| previewContent = ( | ||
| <Box grow="Yes" alignItems="Center" justifyContent="Center"> | ||
| <Spinner variant="Secondary" size="400" /> | ||
| </Box> | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <UrlPreview | ||
| {...props} | ||
| ref={ref} | ||
| style={{ | ||
| background: 'transparent', | ||
| border: 'none', | ||
| padding: 0, | ||
| boxShadow: 'none', | ||
| display: 'inline-block', | ||
| verticalAlign: 'middle', | ||
| width: 'max-content', | ||
| minWidth: 0, | ||
| maxWidth: '100%', | ||
| margin: 0, | ||
| }} | ||
| > | ||
| {previewContent} | ||
| </UrlPreview> | ||
| ); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './UrlPreview'; | ||
| export * from './UrlPreviewCard'; | ||
| export * from './ClientPreview'; |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.