'use client' import useSWR from 'swr' import fetcher from '@/lib/fetcher' import Image from 'next/image' import clsx from 'clsx' import {ElementType, ReactElement} from 'react' import Link from 'next/link' type Artist = { as?: ElementType artist: string } type Title = { as?: ElementType title: string songUrl: string className?: string } type Album = { album: string albumImageUrl: string } type Song = { as?: ElementType } & Artist & Title & Album type PlayerStateResponse = { data: Song error: string isLoading: boolean } function usePlayerState(path: string) { const {data, error, isLoading} = useSWR(`/api/spotify/${path}`, fetcher) as PlayerStateResponse return { song: data, isLoading, isError: error } } function Song({as: Component = 'div', artist, title, songUrl, album, albumImageUrl}: Song) { return (
) } Song.Album = function SongAlbum({album, albumImageUrl}: Album) { return ( {album} ) } Song.Title = function SongTitle({as: Component = 'h2', title, songUrl, className}: Title) { return ( {title} ) } Song.Artist = function SongArtist({as: Component = 'p', artist}: Artist) { return ( {artist} ) } Song.Skeleton = function SongSkeleton() { return (

) } export function SpotifyPlayer(): ReactElement | null { const lastPlayed = usePlayerState('last-played') if (lastPlayed.isError) return null return (
{lastPlayed.isLoading ? : }
) }