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

fix: video component #1097

Merged
merged 1 commit into from
Jun 21, 2024
Merged
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
41 changes: 27 additions & 14 deletions src/components/ui/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import React, {
useState,
VideoHTMLAttributes
} from "react";
import styled, { css } from "styled-components";
import styled, { css, CSSProperties } from "styled-components";

import { colors } from "../../lib/styles/colors";
import { zIndex } from "../../lib/styles/zIndex";
Expand All @@ -23,12 +23,12 @@ const StyledMuteButton = styled(MuteButton)`
position: absolute;
top: 1rem;
right: 1rem;
z-index: 1;
`;
const VideoWrapper = styled.div<{ $hasOnClick?: boolean }>`
overflow: hidden;
position: relative;
z-index: ${zIndex.OfferCard};
height: 0;
padding-top: 120%;
font-size: inherit;
${({ $hasOnClick }) =>
Expand Down Expand Up @@ -63,8 +63,15 @@ const VideoContainer = styled.video`
object-fit: contain;
`;

const VideoPlaceholder = styled.div`
position: absolute;
const VideoPlaceholder = styled.div<{ $position?: CSSProperties["position"] }>`
${({ $position }) =>
$position
? css`
position: ${$position};
`
: css`
position: absolute;
`}
top: 0;
height: 100%;
width: 100%;
Expand Down Expand Up @@ -120,6 +127,8 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
ipfsMetadataStorage
);
setVideoSrc(base64str as string);
setIsLoaded(true);
setIsError(false);
} catch (error) {
console.error("error in Video", error);
Sentry.captureException(error);
Expand All @@ -132,8 +141,13 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
}
}
if (!isLoaded && videoSrc === null) {
if (src?.includes("ipfs://")) {
const newString = src.split("//");
if (
src?.startsWith("ipfs://") ||
src?.startsWith("https://bosonprotocol.infura-ipfs.io/ipfs/")
) {
const newString = src?.startsWith("ipfs://")
? src.split("//")
: src.split("https://bosonprotocol.infura-ipfs.io/ipfs/");
const CID = newString[newString.length - 1];
fetchData(`ipfs://${CID}`);
} else if (src?.startsWith("undefined") && src?.length > 9) {
Expand All @@ -145,12 +159,6 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
}
}, []); // eslint-disable-line

useEffect(() => {
if (videoSrc !== null) {
setTimeout(() => setIsLoaded(true), 100);
}
}, [videoSrc]);

const mp4Src = useMemo(() => {
const octetSrc =
videoSrc?.startsWith("data:application/octet-stream;base64,") || false;
Expand Down Expand Up @@ -179,13 +187,14 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
videoRef.current.play();
}
}, [muted]);

if (!isLoaded && !isError) {
if (ComponentWhileLoading) {
return <ComponentWhileLoading />;
}
return (
<VideoWrapper {...rest}>
<VideoPlaceholder>
<VideoPlaceholder $position="static">
<Typography tag="div">
<Loading />
</Typography>
Expand All @@ -197,7 +206,7 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
if (isLoaded && isError) {
return (
<VideoWrapper {...rest}>
<VideoPlaceholder data-video-placeholder>
<VideoPlaceholder data-video-placeholder $position="static">
{showPlaceholderText ? (
<VideoIcon size={50} color={colors.white} />
) : (
Expand Down Expand Up @@ -227,6 +236,10 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
data-testid={dataTestId}
{...videoProps}
src={mp4Src || ""}
onError={() => {
setIsLoaded(true);
setIsError(true);
}}
/>
</>
)}
Expand Down
Loading