- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 166
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
Show more videos and consolidate media logic #791
Closed
+277
−134
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ca9b7a1
Load video with the embed_video_url if it exists
carloscheddar 859679b
Add ability to handle onError for Video component
carloscheddar 926c733
Place url helpers in url file and add isUrlMedia function
carloscheddar 24f7b2d
Fix testing extend-expect import for vitest
carloscheddar 08d3ab2
Return true when checking .mov video urls
carloscheddar df4e84c
Add support for other youtube url patterns and simplify transformUrl
carloscheddar 1db077e
Replace .gifv with .mp4 to be able to autoplay the video
carloscheddar cdf7127
Add thumbnail prop to render the thumbnail or the main image
carloscheddar 12d1e44
Add tests for the url helpers and test various url transformations
carloscheddar e34d304
Handle empty url strings when checking media
carloscheddar b17fb66
Transform urls before checking media
carloscheddar 6190cc2
Add Media component to handle rendering of media and embeds
carloscheddar 5eaf8b1
Consolidate LargePost and PostDetail using the Media component
carloscheddar 52f619c
Fallback to thumbnails if the base image fails to load
carloscheddar 63be345
Add error handling to GalleryImg and Media images
carloscheddar 87c3acd
Remove iframe embed functionality from the Media component
carloscheddar 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 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 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 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 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 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 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 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 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,89 @@ | ||
import Video from "./Video"; | ||
import { useMemo, useState } from "react"; | ||
import { findLoneImage } from "../../helpers/markdown"; | ||
import { PostView } from "lemmy-js-client"; | ||
import { | ||
isUrlImage, | ||
isUrlMedia, | ||
isUrlVideo, | ||
transformUrl, | ||
} from "../../helpers/url"; | ||
import { Image } from "../post/inFeed/large/Image"; | ||
|
||
interface MediaProps { | ||
post: PostView; | ||
onError: () => void; | ||
detail?: boolean; | ||
blur?: boolean; | ||
} | ||
|
||
const Media = ({ post, detail = false, blur = true, onError }: MediaProps) => { | ||
const postUrl = transformUrl(post.post.url || ""); | ||
const embedVideoUrl = transformUrl(post.post.embed_video_url || ""); | ||
const thumbnailUrl = transformUrl(post.post.thumbnail_url || ""); | ||
const [url, setUrl] = useState(embedVideoUrl || postUrl); | ||
const isImage = isUrlImage(url as string); | ||
const isVideo = isUrlVideo(url as string); | ||
|
||
const markdownLoneImage = useMemo( | ||
() => (post?.post.body ? findLoneImage(post.post.body) : undefined), | ||
[post], | ||
); | ||
|
||
const handleMediaError = () => { | ||
// Cycle the video url before throwing an error | ||
if (isVideo && url === embedVideoUrl) { | ||
if (isUrlMedia(postUrl)) { | ||
return setUrl(postUrl); | ||
} | ||
} | ||
|
||
// Cycle the image url before throwing an error | ||
if (isImage && url === postUrl) { | ||
return setUrl(thumbnailUrl); | ||
} | ||
|
||
onError(); | ||
}; | ||
|
||
if (!url) { | ||
return; | ||
} | ||
|
||
// Change the url as we need | ||
const postWithUrl = { ...post, post: { ...post.post, url: url } }; | ||
|
||
if (postUrl && isUrlImage(postUrl)) { | ||
return ( | ||
<Image | ||
blur={blur} | ||
post={postWithUrl} | ||
animationType="zoom" | ||
onError={handleMediaError} | ||
/> | ||
); | ||
} | ||
|
||
if (isVideo) { | ||
return ( | ||
<Video | ||
src={url} | ||
blur={blur} | ||
controls={detail} | ||
onError={handleMediaError} | ||
/> | ||
); | ||
} | ||
|
||
if (markdownLoneImage) | ||
return ( | ||
<Image | ||
blur={blur} | ||
post={postWithUrl} | ||
animationType="zoom" | ||
onError={handleMediaError} | ||
/> | ||
); | ||
}; | ||
|
||
export default Media; |
This file contains 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 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 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,61 @@ | ||
import { | ||
isValidUrl, | ||
transformUrl, | ||
isUrlImage, | ||
isUrlVideo, | ||
isUrlMedia, | ||
} from "./url"; | ||
|
||
describe("URL Utility Functions", () => { | ||
describe("isValidUrl", () => { | ||
it("returns true for a valid URL", () => { | ||
expect(isValidUrl("https://www.example.com")).toBe(true); | ||
}); | ||
}); | ||
|
||
describe("transformUrl", () => { | ||
it("transform gifv to mp4", () => { | ||
const inputUrl = "https://example.com/video.gifv"; | ||
const transformedUrl = transformUrl(inputUrl); | ||
expect(transformedUrl).toBe("https://example.com/video.mp4"); | ||
}); | ||
|
||
it("handle unknown URLs", () => { | ||
const inputUrl = "https://example.com/unknown-url"; | ||
const transformedUrl = transformUrl(inputUrl); | ||
expect(transformedUrl).toBe(inputUrl); | ||
}); | ||
}); | ||
|
||
describe("isUrlImage", () => { | ||
it("returns true for image URLs", () => { | ||
expect(isUrlImage("https://example.com/image.jpg")).toBe(true); | ||
}); | ||
|
||
it("returns false for non-image URLs", () => { | ||
expect(isUrlImage("https://example.com/video.mp4")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("isUrlVideo", () => { | ||
it("returns true for mp4", () => { | ||
expect(isUrlVideo("https://example.com/video.mp4")).toBe(true); | ||
}); | ||
|
||
it("returns true for mov", () => { | ||
expect(isUrlVideo("https://example.com/video.mov")).toBe(true); | ||
}); | ||
|
||
it("returns false for non-video URLs", () => { | ||
expect(isUrlVideo("https://example.com/image.jpg")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("isUrlMedia", () => { | ||
it("returns true for media URLs", () => { | ||
expect(isUrlMedia("https://example.com/image.jpg")).toBe(true); | ||
expect(isUrlMedia("https://example.com/video.mp4")).toBe(true); | ||
expect(isUrlMedia("https://example.com/video.gifv")).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this fixing a particular site or set of sites? I would like to avoid changing any instance of .gifv in the url, that feels quite dangerous.