last tickets

This commit is contained in:
Thomas Demirdjian
2025-11-21 21:57:41 +01:00
parent a58b13d09f
commit 471a3177e8
13 changed files with 677 additions and 714 deletions
@@ -0,0 +1,107 @@
import { BlurView } from "expo-blur";
import React from "react";
import { Image, Platform, Pressable, StyleSheet, Text, View } from "react-native";
import { Feather } from "@expo/vector-icons";
import ProfilePicture from "../../../components/ProfilePicture";
import { icons } from "../../../assets";
import { Palette } from "../../../styles";
import { FONT_FAMILY } from "../../../styles/Fonts";
import { size } from "../../../styles/Style";
const PlaybackActions = ({
owner,
currentUID,
isFollowing,
isFollowActionPending,
onToggleFollow,
likesCount,
isLiked,
onToggleLike,
commentsCount,
onCommentsPress,
onShare,
onReport,
onOpenProfile,
}) => {
return (
<View style={styles.actionsColumn}>
<View style={styles.profileWrapper}>
<Pressable onPress={onOpenProfile}>
<ProfilePicture
uri={owner?.profilePictureURL || null}
size={45}
imageProps={{ priority: "high" }}
/>
</Pressable>
{owner?.id && owner.id !== currentUID && (
<Pressable disabled={isFollowActionPending} onPress={onToggleFollow}>
<BlurView
tint="dark"
intensity={Platform.OS !== "ios" ? 10 : 20}
style={[
styles.followButton,
{ backgroundColor: isFollowing ? "#FFFFFF1A" : undefined },
]}
>
<Text style={styles.followText}>
{isFollowing ? "Suivi(e)" : "Suivre"}
</Text>
</BlurView>
</Pressable>
)}
</View>
<Pressable onPress={onToggleLike} style={styles.centered}>
<Image
source={isLiked ? icons.heart : icons.heartOutline}
style={size({ size: 26 })}
resizeMode="contain"
/>
{!!likesCount && <Text style={styles.countText}>{likesCount}</Text>}
</Pressable>
<Pressable onPress={onCommentsPress} style={styles.centered}>
<Image source={icons.chatBubble} style={size({ size: 26 })} />
{!!commentsCount && <Text style={styles.countText}>{commentsCount}</Text>}
</Pressable>
<Pressable onPress={onShare}>
<Image source={icons.share} style={size({ size: 26 })} />
</Pressable>
<Pressable onPress={onReport} style={styles.centered}>
<Feather name="flag" size={26} color={Palette.white} />
<Text style={styles.countText}>Signaler</Text>
</Pressable>
</View>
);
};
const styles = StyleSheet.create({
actionsColumn: {
alignSelf: "flex-end",
alignItems: "center",
gap: 20,
paddingHorizontal: 13,
},
profileWrapper: { gap: 6, alignItems: "center" },
followButton: {
paddingVertical: 6,
paddingHorizontal: 12,
borderRadius: 12,
borderWidth: 1,
borderColor: Palette.white,
overflow: "hidden",
},
followText: {
fontSize: 13,
color: Palette.white,
fontFamily: FONT_FAMILY.InterMedium,
},
centered: { alignItems: "center" },
countText: {
color: Palette.white,
fontSize: 11,
marginTop: 4,
fontFamily: FONT_FAMILY.InterMedium,
textAlign: "center",
},
});
export default PlaybackActions;
@@ -0,0 +1,185 @@
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { View, StyleSheet } from "react-native";
import { responsiveHeight } from "react-native-responsive-dimensions";
import { SheetManager } from "react-native-actions-sheet";
import { useUser } from "../../../providers/UserDataProvider";
import { Routes } from "../../../navigation";
import { navigate } from "../../../navigation/NavigationService";
import { openComments } from "../../../components/bottomsheets/CommentsBottomSheet";
import {
createPlaybackSharePayload,
openShareSheet,
} from "../../../utils/shareSheet";
import {
getProjectLikes,
LIKE_TARGET,
toggleProjectLike,
} from "../../../utils/likes";
import { ensureAuthenticated } from "../../../utils/authRedirect";
import PlaybackVideo from "./PlaybackVideo";
import PlaybackActions from "./PlaybackActions";
import PlaybackLyricsCard from "./PlaybackLyricsCard";
import usePlaybackOwner from "./usePlaybackOwner";
import usePlaybackPlayer from "./usePlaybackPlayer";
const PlaybackItem = ({ item, isActive, userCache }) => {
const { currentUID, followUser, unfollowUser, getUserByUid } = useUser() || {};
const { owner, isFollowing, isFollowActionPending, toggleFollow } =
usePlaybackOwner({
item,
userCache,
getUserByUid,
currentUID,
followUser,
unfollowUser,
});
const initialLikedBy = getProjectLikes(item, LIKE_TARGET.PLAYBACK);
const [isLiked, setIsLiked] = useState(
currentUID ? initialLikedBy.includes(currentUID) : false
);
const [likesCount, setLikesCount] = useState(initialLikedBy.length);
useEffect(() => {
const lb = getProjectLikes(item, LIKE_TARGET.PLAYBACK);
setLikesCount(lb.length);
setIsLiked(currentUID ? lb.includes(currentUID) : false);
}, [item?.likes?.playback, currentUID]);
const commentsCount = useMemo(
() => Number(item?.commentsCount || 0),
[item?.commentsCount]
);
const projectTitle = typeof item?.title === "string" ? item.title.trim() : "";
const creatorName = useMemo(() => {
if (owner?.displayName) return owner.displayName;
if (owner?.artistName) return owner.artistName;
if (owner?.userName) return owner.userName;
if (typeof item?.userName === "string") return item.userName;
return "";
}, [item?.userName, owner?.artistName, owner?.displayName, owner?.userName]);
const { videoUrl, videoPlayer, currentTimeS } = usePlaybackPlayer({
item,
isActive,
});
const alignedWords = useMemo(() => {
const idx = Number(item?.songIndex) || 0;
const ts = item?.musicTimestamps?.[idx];
const arr = Array.isArray(ts?.alignedWords) ? ts.alignedWords : [];
return arr.map((w) => ({
word: String(w?.word ?? ""),
startS: Number(w?.startS ?? 0),
endS: Number(w?.endS ?? 0),
}));
}, [item?.musicTimestamps, item?.songIndex]);
const sharePayload = useMemo(() => {
if (!item?.id) return null;
return createPlaybackSharePayload({
projectId: item.id,
title: projectTitle || undefined,
artist: creatorName || undefined,
playbackUrl: videoUrl || undefined,
});
}, [creatorName, item?.id, projectTitle, videoUrl]);
const handleShare = useCallback(() => {
if (sharePayload) {
openShareSheet(sharePayload);
}
}, [sharePayload]);
const handleReport = useCallback(() => {
if (!item?.id) return;
SheetManager.show("Report", {
payload: {
targetType: "playback",
projectId: item?.id || null,
playbackId: item?.id || null,
title: item?.title || "",
ownerId: item?.userId || null,
},
});
}, [item?.id, item?.title, item?.userId]);
const handleLike = useCallback(async () => {
try {
if (!item?.id) return;
if (!ensureAuthenticated(currentUID)) {
return;
}
const nextLiked = !isLiked;
setIsLiked(nextLiked);
setLikesCount((c) => Math.max(0, c + (nextLiked ? 1 : -1)));
await toggleProjectLike({
projectId: item.id,
currentUID,
target: LIKE_TARGET.PLAYBACK,
next: nextLiked,
});
} catch (e) {
setIsLiked((v) => !v);
setLikesCount((c) => (isLiked ? c + 1 : Math.max(0, c - 1)));
}
}, [currentUID, isLiked, item?.id]);
const handleCommentPress = useCallback(() => {
if (item?.id) openComments(item.id);
}, [item?.id]);
const onOpenProfile = useCallback(() => {
navigate(Routes.SingerProfile, { userId: item?.userId });
}, [item?.userId]);
return (
<View style={styles.container}>
<PlaybackVideo
videoUrl={videoUrl}
videoPlayer={videoPlayer}
/>
<View style={styles.overlay}>
<PlaybackActions
owner={owner}
currentUID={currentUID}
isFollowing={isFollowing}
isFollowActionPending={isFollowActionPending}
onToggleFollow={toggleFollow}
likesCount={likesCount}
isLiked={isLiked}
onToggleLike={handleLike}
commentsCount={commentsCount}
onCommentsPress={handleCommentPress}
onShare={handleShare}
onReport={handleReport}
onOpenProfile={onOpenProfile}
/>
<PlaybackLyricsCard
alignedWords={alignedWords}
currentTimeS={currentTimeS}
fallbackTitle={item?.title}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
height: responsiveHeight(100),
position: "relative",
backgroundColor: "black",
},
overlay: {
position: "absolute",
width: "100%",
bottom: 130,
gap: 18,
},
});
export default PlaybackItem;
@@ -0,0 +1,44 @@
import { BlurView } from "expo-blur";
import React from "react";
import { Platform, StyleSheet, Text, View } from "react-native";
import KaraokeLyrics from "../../../components/KaraokeLyrics";
import { Palette } from "../../../styles";
import { FONT_FAMILY } from "../../../styles/Fonts";
const PlaybackLyricsCard = ({ alignedWords, currentTimeS, fallbackTitle }) => {
return (
<View style={styles.container}>
<BlurView
tint="dark"
intensity={Platform.OS !== "ios" ? 10 : 20}
style={styles.blur}
>
{alignedWords?.length > 0 ? (
<KaraokeLyrics alignedWords={alignedWords} currentTimeS={currentTimeS} />
) : (
<Text style={styles.title}>{fallbackTitle || "Description chanson"}</Text>
)}
</BlurView>
</View>
);
};
const styles = StyleSheet.create({
container: {
paddingHorizontal: 28,
},
blur: {
paddingHorizontal: 14,
paddingVertical: 8,
borderRadius: 20,
backgroundColor: Palette.glass,
overflow: "hidden",
},
title: {
fontSize: 12,
color: Palette.white,
fontFamily: FONT_FAMILY.InterRegular,
},
});
export default PlaybackLyricsCard;
@@ -0,0 +1,34 @@
import { VideoView } from "expo-video";
import React from "react";
import { Image, StyleSheet, View } from "react-native";
import { img } from "../../../assets";
const PlaybackVideo = ({ videoUrl, videoPlayer }) => {
return (
<View style={styles.container}>
{!!videoUrl ? (
<VideoView
player={videoPlayer}
nativeControls={false}
contentFit="cover"
style={styles.fill}
/>
) : (
<Image source={img.placeholder3} style={styles.fill} />
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
},
fill: {
width: "100%",
height: "100%",
},
});
export default PlaybackVideo;
@@ -0,0 +1,112 @@
import { useCallback, useEffect, useState } from "react";
import { usersRef } from "../../../config/firebase";
import { ensureAuthenticated } from "../../../utils/authRedirect";
const usePlaybackOwner = ({
item,
userCache,
getUserByUid,
currentUID,
followUser,
unfollowUser,
}) => {
const [owner, setOwner] = useState(
item?.userId && userCache?.current?.get(item.userId)
? userCache.current.get(item.userId)
: null
);
useEffect(() => {
let cancelled = false;
const run = async () => {
try {
const uid = item?.userId;
if (!uid || !userCache) return;
const cached = userCache.current.get(uid);
if (cached) {
if (!cancelled) setOwner(cached);
return;
}
const user = await getUserByUid?.(uid);
if (!cancelled && user) {
userCache.current.set(uid, user);
setOwner(user);
}
} catch (e) {}
};
run();
return () => {
cancelled = true;
};
}, [item?.userId, userCache, getUserByUid]);
useEffect(() => {
const uid = item?.userId;
if (!uid) return;
const unsub = usersRef.doc(uid).onSnapshot(
(doc) => {
if (doc?.exists) {
const data = { id: doc.id, ...doc.data() };
setOwner(data);
try {
userCache?.current?.set(uid, data);
} catch (e) {}
}
},
() => {}
);
return () => unsub?.();
}, [item?.userId, userCache]);
const [isFollowing, setIsFollowing] = useState(false);
const [isFollowActionPending, setIsFollowActionPending] = useState(false);
useEffect(() => {
const list = Array.isArray(owner?.followedBy) ? owner.followedBy : [];
setIsFollowing(currentUID ? list.includes(currentUID) : false);
}, [owner?.followedBy, currentUID]);
const toggleFollow = useCallback(async () => {
if (!owner?.id || owner.id === currentUID) return;
if (isFollowActionPending) return;
if (
!ensureAuthenticated(currentUID, {
onIntercept: () => {
setIsFollowActionPending(false);
},
})
) {
return;
}
try {
setIsFollowActionPending(true);
const next = !isFollowing;
setIsFollowing(next);
setOwner((prev) => {
const fb = Array.isArray(prev?.followedBy) ? prev.followedBy : [];
const newFb = next
? Array.from(new Set([...fb, currentUID]))
: fb.filter((x) => x !== currentUID);
return prev ? { ...prev, followedBy: newFb } : prev;
});
if (next) await followUser?.(owner.id);
else await unfollowUser?.(owner.id);
global.setTimeout(() => {
setIsFollowActionPending(false);
}, 1000);
} catch (e) {
setIsFollowing((v) => !v);
setIsFollowActionPending(false);
}
}, [
currentUID,
followUser,
isFollowActionPending,
isFollowing,
owner?.id,
unfollowUser,
]);
return { owner, isFollowing, isFollowActionPending, toggleFollow };
};
export default usePlaybackOwner;
@@ -0,0 +1,61 @@
import { useEffect, useMemo, useState } from "react";
import { useVideoPlayer } from "expo-video";
const usePlaybackPlayer = ({ item, isActive }) => {
const baseVideoUrl = item?.playbackUrl || null;
const videoUrl = baseVideoUrl;
const videoPlayer = useVideoPlayer(videoUrl || null, (player) => {
player.loop = false;
player.muted = false;
player.timeUpdateEventInterval = 0.2;
});
const shouldAutoPlay = isActive;
useEffect(() => {
if (!videoPlayer) return;
if (shouldAutoPlay) {
try {
videoPlayer.currentTime = 0;
videoPlayer.play();
} catch (e) {}
} else if (videoPlayer?.playing) {
try {
videoPlayer.pause();
} catch (e) {}
}
}, [shouldAutoPlay, videoPlayer]);
useEffect(() => {
return () => {
try {
if (videoPlayer?.playing) videoPlayer.pause();
} catch (e) {}
};
}, [videoPlayer]);
const [currentTimeS, setCurrentTimeS] = useState(0);
useEffect(() => {
if (!isActive) return;
const id = global.setInterval(() => {
try {
setCurrentTimeS(Number(videoPlayer?.currentTime || 0));
} catch (e) {}
}, 250);
return () => global.clearInterval(id);
}, [isActive, videoPlayer]);
const playerState = useMemo(
() => ({
videoUrl,
videoPlayer,
currentTimeS,
}),
[videoPlayer, videoUrl, currentTimeS]
);
return playerState;
};
export default usePlaybackPlayer;