import { Ionicons } from "@expo/vector-icons"; import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models"; import { getItemsApi } from "@jellyfin/sdk/lib/utils/api"; import { useQuery } from "@tanstack/react-query"; 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 useRouter from "@/hooks/useAppRouter"; import { getLocalPath } from "@/providers/AudioStorage"; import { apiAtom, userAtom } from "@/providers/JellyfinProvider"; import { getPrimaryImageUrl } from "@/utils/jellyfin/image/getPrimaryImageUrl"; interface Props { playlist: BaseItemDto; width?: number; } const IMAGE_SIZE = 56; export const MusicPlaylistCard: React.FC = ({ playlist }) => { const [api] = useAtom(apiAtom); const [user] = useAtom(userAtom); const router = useRouter(); const imageUrl = useMemo( () => getPrimaryImageUrl({ api, item: playlist }), [api, playlist], ); // Fetch playlist tracks to check download status const { data: tracks } = useQuery({ queryKey: ["playlist-tracks-status", playlist.Id, user?.Id], queryFn: async () => { const response = await getItemsApi(api!).getItems({ userId: user?.Id, parentId: playlist.Id, fields: ["MediaSources"], }); return response.data.Items || []; }, enabled: !!api && !!user?.Id && !!playlist.Id, staleTime: 5 * 60 * 1000, // 5 minutes }); // Calculate download status const downloadStatus = useMemo(() => { if (!tracks || tracks.length === 0) { return { downloaded: 0, total: playlist.ChildCount || 0 }; } const downloaded = tracks.filter( (track) => !!getLocalPath(track.Id), ).length; return { downloaded, total: tracks.length }; }, [tracks, playlist.ChildCount]); const allDownloaded = downloadStatus.total > 0 && downloadStatus.downloaded === downloadStatus.total; const hasDownloads = downloadStatus.downloaded > 0; const handlePress = useCallback(() => { router.push({ pathname: "/music/playlist/[playlistId]", params: { playlistId: playlist.Id! }, }); }, [router, playlist.Id]); return ( {imageUrl ? ( ) : ( 🎶 )} {playlist.Name} {playlist.ChildCount} tracks {/* Download status indicator */} {allDownloaded ? ( ) : hasDownloads ? ( {downloadStatus.downloaded}/{downloadStatus.total} ) : null} ); };