import { memo, useCallback, useEffect, useMemo } from 'react' import Button, { Color as ButtonColor } from '@/web/components/Button' import Skeleton from '@/web/components/Skeleton' import SvgIcon from '@/web/components/SvgIcon' import TracksList from '@/web/components/TracksList' import usePlaylist from '@/web/hooks/usePlaylist' import useScroll from '@/web/hooks/useScroll' import useTracksInfinite from '@/web/hooks/useTracksInfinite' import { player } from '@/web/store' import { formatDate, resizeImage } from '@/web/utils/common' import useUserPlaylists, { useMutationLikeAPlaylist, } from '@/web/hooks/useUserPlaylists' import useUser from '@/web/hooks/useUser' import { Mode as PlayerMode, TrackListSourceType, State as PlayerState, } from '@/web/utils/player' import toast from 'react-hot-toast' import { useParams } from 'react-router-dom' import { useSnapshot } from 'valtio' const PlayButton = ({ playlist, handlePlay, isLoading, }: { playlist: Playlist | undefined handlePlay: () => void isLoading: boolean }) => { const playerSnapshot = useSnapshot(player) const isThisPlaylistPlaying = useMemo( () => playerSnapshot.mode === PlayerMode.TrackList && playerSnapshot.trackListSource?.type === TrackListSourceType.Playlist && playerSnapshot.trackListSource?.id === playlist?.id, [ playerSnapshot.mode, playerSnapshot.trackListSource?.id, playerSnapshot.trackListSource?.type, playlist?.id, ] ) const wrappedHandlePlay = () => { if (isThisPlaylistPlaying) { player.playOrPause() } else { handlePlay() } } const isPlaying = isThisPlaylistPlaying && [PlayerState.Playing, PlayerState.Loading].includes(playerSnapshot.state) return ( ) } const Header = memo( ({ playlist, isLoading, handlePlay, }: { playlist: Playlist | undefined isLoading: boolean handlePlay: () => void }) => { const coverUrl = resizeImage(playlist?.coverImgUrl || '', 'lg') const mutationLikeAPlaylist = useMutationLikeAPlaylist() const { data: userPlaylists } = useUserPlaylists() const isThisPlaylistLiked = useMemo(() => { if (!playlist) return false return !!userPlaylists?.playlist?.find(p => p.id === playlist.id) }, [playlist, userPlaylists?.playlist]) const { data: user } = useUser() const isThisPlaylistCreatedByCurrentUser = useMemo(() => { if (!playlist || !user) return false return playlist.creator.userId === user?.profile?.userId }, [playlist, user]) return ( <> {/* Header background */}