import { useActionSheet } from "@expo/react-native-action-sheet"; import { Ionicons } from "@expo/vector-icons"; import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models"; import { Image } from "expo-image"; import { useAtom } from "jotai"; import React, { useCallback, useMemo } from "react"; import { TouchableOpacity, View } from "react-native"; import { Text } from "@/components/common/Text"; import { apiAtom } from "@/providers/JellyfinProvider"; import { useMusicPlayer } from "@/providers/MusicPlayerProvider"; import { getPrimaryImageUrl } from "@/utils/jellyfin/image/getPrimaryImageUrl"; import { formatDuration } from "@/utils/time"; interface Props { track: BaseItemDto; index?: number; queue?: BaseItemDto[]; showArtwork?: boolean; } export const MusicTrackItem: React.FC = ({ track, index, queue, showArtwork = true, }) => { const [api] = useAtom(apiAtom); const { showActionSheetWithOptions } = useActionSheet(); const { playTrack, playNext, addToQueue, currentTrack, isPlaying } = useMusicPlayer(); const imageUrl = useMemo(() => { const albumId = track.AlbumId || track.ParentId; if (albumId) { return `${api?.basePath}/Items/${albumId}/Images/Primary?maxHeight=100&maxWidth=100`; } return getPrimaryImageUrl({ api, item: track }); }, [api, track]); const isCurrentTrack = currentTrack?.Id === track.Id; const duration = useMemo(() => { if (!track.RunTimeTicks) return ""; return formatDuration(track.RunTimeTicks); }, [track.RunTimeTicks]); const handlePress = useCallback(() => { playTrack(track, queue); }, [playTrack, track, queue]); const handleLongPress = useCallback(() => { const options = ["Play Next", "Add to Queue", "Cancel"]; const cancelButtonIndex = 2; showActionSheetWithOptions( { options, cancelButtonIndex, title: track.Name ?? undefined, message: (track.Artists?.join(", ") || track.AlbumArtist) ?? undefined, }, (selectedIndex) => { if (selectedIndex === 0) { playNext(track); } else if (selectedIndex === 1) { addToQueue(track); } }, ); }, [showActionSheetWithOptions, track, playNext, addToQueue]); return ( {index !== undefined && ( {isCurrentTrack && isPlaying ? ( ) : ( {index} )} )} {showArtwork && ( {imageUrl ? ( ) : ( )} )} {track.Name} {track.Artists?.join(", ") || track.AlbumArtist} {duration} ); };