diff --git a/src/components/SpotifyCard.tsx b/src/components/SpotifyCard.tsx index d6b3529..a784f51 100644 --- a/src/components/SpotifyCard.tsx +++ b/src/components/SpotifyCard.tsx @@ -4,17 +4,7 @@ import { useEffect, useState } from 'react' import io from 'socket.io-client' import Image from 'next/image' import Link from 'next/link' - -interface SpotifyData { - song?: { - artist: string[] - title: string - url: string - image: string - progress: number - length: number - } -} +import type { SpotifyData } from '@/utils/interface' export const NowPlayingCard = () => { const [spotify, setSpotify] = useState({}) @@ -31,15 +21,6 @@ export const NowPlayingCard = () => { } }, []) - const Progress = () => { - if (spotify.song && spotify.song.length > 0) { - const progressPercentage = - (spotify.song.progress / spotify.song.length) * 100 - return `${progressPercentage}%` - } - return '0%' - } - return (
{spotify.song ? ( @@ -70,7 +51,10 @@ export const NowPlayingCard = () => {
diff --git a/src/pages/api/spotify.ts b/src/pages/api/spotify.ts index b9cdf5e..ad2d4ac 100644 --- a/src/pages/api/spotify.ts +++ b/src/pages/api/spotify.ts @@ -21,21 +21,26 @@ export default async function handler( ) { try { const song = await spotify.getCurrentSong() - if (!song || !song.isPlaying) { + if (!song.is_playing) { return res.status(200).json({ - nowplaying: false, + is_playing: false, }) } res.status(200).json({ - nowplaying: true, - song: { - artist: song.artists.name, - title: song.title, - url: song.url, + is_playing: true, + name: song.name, + album: { + name: song.album.name, image: song.album.image, - progress: song.progress, - length: song.length, + release: song.album.release_date, }, + artists: { + name: song.artists.name, + url: song.artists.url, + }, + url: song.url, + progress: song.progress, + duration: song.duration, }) } catch (error) { res diff --git a/src/utils/interface.ts b/src/utils/interface.ts index a48736a..5296a55 100644 --- a/src/utils/interface.ts +++ b/src/utils/interface.ts @@ -3,7 +3,7 @@ interface Album { images: { url: string }[] - release: string[] + release_date: string } export interface Artist { @@ -18,3 +18,14 @@ export interface Item { external_urls: { spotify: string } duration_ms: number } + +export interface SpotifyData { + song?: { + title: string + artist: string[] + url: string + image: string + progress: number + duration: number + } +} diff --git a/src/utils/result.ts b/src/utils/result.ts index c3c45e8..362b235 100644 --- a/src/utils/result.ts +++ b/src/utils/result.ts @@ -10,20 +10,20 @@ export class SongResultMap { const { item } = result return { - progress: result.progress_ms, - title: item.name, + name: item.name, album: { name: item.album.name, image: item.album.images[0]?.url, - release: item.album.release, + release_date: item.album.release_date, }, artists: { name: item.artists.map((x: Artist) => x.name), url: item.artists.map((x: Artist) => x.external_urls.spotify), }, url: item.external_urls.spotify, - length: item.duration_ms, - isPlaying: result.is_playing, + progress: result.progress_ms, + duration: item.duration_ms, + is_playing: result.is_playing, } } } diff --git a/src/utils/type.ts b/src/utils/type.ts index fbd8c91..1907c3b 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -1,16 +1,16 @@ export type SongResult = { - progress: number + is_playing: boolean + name: string album: { name: string image?: string - release: string[] + release_date: string } artists: { name: string[] url: string[] } url: string - title: string - length: number - isPlaying: boolean + progress: number + duration: number }