Skip to content

Commit 0929c31

Browse files
fix: video component (#1097)
1 parent 5b6f37f commit 0929c31

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/components/ui/Video.tsx

+27-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import React, {
1111
useState,
1212
VideoHTMLAttributes
1313
} from "react";
14-
import styled, { css } from "styled-components";
14+
import styled, { css, CSSProperties } from "styled-components";
1515

1616
import { colors } from "../../lib/styles/colors";
1717
import { zIndex } from "../../lib/styles/zIndex";
@@ -23,12 +23,12 @@ const StyledMuteButton = styled(MuteButton)`
2323
position: absolute;
2424
top: 1rem;
2525
right: 1rem;
26+
z-index: 1;
2627
`;
2728
const VideoWrapper = styled.div<{ $hasOnClick?: boolean }>`
2829
overflow: hidden;
2930
position: relative;
3031
z-index: ${zIndex.OfferCard};
31-
height: 0;
3232
padding-top: 120%;
3333
font-size: inherit;
3434
${({ $hasOnClick }) =>
@@ -63,8 +63,15 @@ const VideoContainer = styled.video`
6363
object-fit: contain;
6464
`;
6565

66-
const VideoPlaceholder = styled.div`
67-
position: absolute;
66+
const VideoPlaceholder = styled.div<{ $position?: CSSProperties["position"] }>`
67+
${({ $position }) =>
68+
$position
69+
? css`
70+
position: ${$position};
71+
`
72+
: css`
73+
position: absolute;
74+
`}
6875
top: 0;
6976
height: 100%;
7077
width: 100%;
@@ -120,6 +127,8 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
120127
ipfsMetadataStorage
121128
);
122129
setVideoSrc(base64str as string);
130+
setIsLoaded(true);
131+
setIsError(false);
123132
} catch (error) {
124133
console.error("error in Video", error);
125134
Sentry.captureException(error);
@@ -132,8 +141,13 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
132141
}
133142
}
134143
if (!isLoaded && videoSrc === null) {
135-
if (src?.includes("ipfs://")) {
136-
const newString = src.split("//");
144+
if (
145+
src?.startsWith("ipfs://") ||
146+
src?.startsWith("https://bosonprotocol.infura-ipfs.io/ipfs/")
147+
) {
148+
const newString = src?.startsWith("ipfs://")
149+
? src.split("//")
150+
: src.split("https://bosonprotocol.infura-ipfs.io/ipfs/");
137151
const CID = newString[newString.length - 1];
138152
fetchData(`ipfs://${CID}`);
139153
} else if (src?.startsWith("undefined") && src?.length > 9) {
@@ -145,12 +159,6 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
145159
}
146160
}, []); // eslint-disable-line
147161

148-
useEffect(() => {
149-
if (videoSrc !== null) {
150-
setTimeout(() => setIsLoaded(true), 100);
151-
}
152-
}, [videoSrc]);
153-
154162
const mp4Src = useMemo(() => {
155163
const octetSrc =
156164
videoSrc?.startsWith("data:application/octet-stream;base64,") || false;
@@ -179,13 +187,14 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
179187
videoRef.current.play();
180188
}
181189
}, [muted]);
190+
182191
if (!isLoaded && !isError) {
183192
if (ComponentWhileLoading) {
184193
return <ComponentWhileLoading />;
185194
}
186195
return (
187196
<VideoWrapper {...rest}>
188-
<VideoPlaceholder>
197+
<VideoPlaceholder $position="static">
189198
<Typography tag="div">
190199
<Loading />
191200
</Typography>
@@ -197,7 +206,7 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
197206
if (isLoaded && isError) {
198207
return (
199208
<VideoWrapper {...rest}>
200-
<VideoPlaceholder data-video-placeholder>
209+
<VideoPlaceholder data-video-placeholder $position="static">
201210
{showPlaceholderText ? (
202211
<VideoIcon size={50} color={colors.white} />
203212
) : (
@@ -227,6 +236,10 @@ const Video: React.FC<IVideo & React.HTMLAttributes<HTMLDivElement>> = ({
227236
data-testid={dataTestId}
228237
{...videoProps}
229238
src={mp4Src || ""}
239+
onError={() => {
240+
setIsLoaded(true);
241+
setIsError(true);
242+
}}
230243
/>
231244
</>
232245
)}

0 commit comments

Comments
 (0)