-
Notifications
You must be signed in to change notification settings - Fork 22
/
VideoMapPost.tsx
166 lines (156 loc) · 5.36 KB
/
VideoMapPost.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
AVPlaybackStatus,
AVPlaybackStatusSuccess,
ResizeMode,
Video,
} from "expo-av";
import React, { FC, useRef, useState } from "react";
import { View } from "react-native";
import { v4 as uuidv4 } from "uuid";
import { Post } from "@/api/feed/v1/feed";
import { BrandText } from "@/components/BrandText";
import { MediaPlayerBarRefined } from "@/components/mediaPlayer/MediaPlayerBarRefined";
import { Separator } from "@/components/separators/Separator";
import { MapPostWrapper } from "@/components/socialFeed/Map/MapPosts/MapPostWrapper";
import { SpacerColumn } from "@/components/spacer";
import { useFeedbacks } from "@/context/FeedbacksProvider";
import { useMediaPlayer } from "@/context/MediaPlayerProvider";
import { web3ToWeb2URI } from "@/utils/ipfs";
import { zodTryParseJSON } from "@/utils/sanitize";
import { errorColor, neutralFF, withAlpha } from "@/utils/style/colors";
import { fontSemibold10 } from "@/utils/style/fonts";
import {
SocialFeedVideoMetadata,
zodSocialFeedCommonMetadata,
ZodSocialFeedPostMetadata,
ZodSocialFeedVideoMetadata,
} from "@/utils/types/feed";
import { Media } from "@/utils/types/mediaPlayer";
export const VideoMapPost: FC<{
post: Post;
}> = ({ post }) => {
const { current: id } = useRef(uuidv4());
const videoRef = useRef<Video>(null);
const {
onVideoStatusUpdate,
onLayoutPlayerVideo,
handlePlayPause,
playbackStatus,
firstPlayVideo,
media,
} = useMediaPlayer();
const { setToast } = useFeedbacks();
const [localStatus, setLocalStatus] = useState<AVPlaybackStatusSuccess>();
const isInMediaPlayer =
!!media && (post.id === media.postId || media.id === id);
const statusToUse = isInMediaPlayer ? playbackStatus : localStatus;
const videoPostMetadata = zodTryParseJSON(
ZodSocialFeedVideoMetadata,
post.metadata,
);
const videoNotePostMetadata = zodTryParseJSON(
ZodSocialFeedPostMetadata,
post.metadata,
);
const baseMetadata = zodTryParseJSON(
zodSocialFeedCommonMetadata,
post.metadata,
);
const title = baseMetadata?.title || "Music from Social Feed";
// Video and VideoNote have different metadata but have the same render on the map, so we handle these 2 cases
const mediaToPlay: Media | undefined = videoPostMetadata
? {
id,
fileUrl: videoPostMetadata.videoFile.url,
duration: videoPostMetadata.videoFile.videoMetadata?.duration, // FIXME: Known issue: Always 0. So, for videos, duration is set by playbackStatus, so it's 0 on the timer since the video is not started once
postId: post.id,
isVideo: true,
}
: videoNotePostMetadata?.files
? {
id,
fileUrl: videoNotePostMetadata.files[0].url,
duration: videoNotePostMetadata.files[0].videoMetadata?.duration,
postId: post.id,
isVideo: true,
}
: undefined;
const videoMetadata: SocialFeedVideoMetadata | undefined = videoPostMetadata
? {
title: "Video from Social Feed",
videoFile: videoPostMetadata.videoFile,
}
: videoNotePostMetadata?.files
? {
title: "Video from Social Feed",
videoFile: videoNotePostMetadata.files[0],
}
: undefined;
const onLocalPlaybackStatusUpdate = async (status: AVPlaybackStatus) => {
if ("uri" in status && status.isLoaded) {
setLocalStatus(status);
if (isInMediaPlayer && status.positionMillis > 0) {
onVideoStatusUpdate(status);
}
}
if ("error" in status) {
console.error("Error while playbackStatus update: ", status.error);
setToast({
mode: "normal",
type: "error",
title: `Error while playbackStatus update : ${status.error}`,
});
}
};
const onPressPlayPause = async () => {
if (isInMediaPlayer) await handlePlayPause();
else if (mediaToPlay && videoRef.current)
firstPlayVideo(videoRef.current, mediaToPlay);
};
return (
<MapPostWrapper post={post} style={{ width: 185 }}>
<View>
<BrandText style={fontSemibold10}>{title}</BrandText>
<SpacerColumn size={0.5} />
<Separator color={withAlpha(neutralFF, 0.24)} />
<SpacerColumn size={0.5} />
{mediaToPlay && videoMetadata ? (
<View
onLayout={() => {
if (isInMediaPlayer && videoRef.current) {
onLayoutPlayerVideo(videoRef.current);
}
}}
>
<MediaPlayerBarRefined
playbackStatus={statusToUse}
onPressPlayPause={onPressPlayPause}
isInMediaPlayer={isInMediaPlayer}
/>
<SpacerColumn size={0.5} />
<Video
ref={videoRef}
status={statusToUse}
onPlaybackStatusUpdate={onLocalPlaybackStatusUpdate}
source={{
uri: web3ToWeb2URI(videoMetadata.videoFile.url),
}}
style={{ width: "100%", height: 100 }}
posterStyle={{ width: "100%", height: 100 }}
videoStyle={{
width: "100%",
height: 100,
borderRadius: 4,
}}
resizeMode={ResizeMode.CONTAIN}
/>
</View>
) : (
<BrandText style={[fontSemibold10, { color: errorColor }]}>
No media to play
</BrandText>
)}
</View>
</MapPostWrapper>
);
};