From 05b9525c4f25d0495c133b6d7dd9494fc435dbd1 Mon Sep 17 00:00:00 2001 From: Francisco Pessano Date: Thu, 10 Oct 2024 23:20:02 -0300 Subject: [PATCH] Album showcase --- src/app/_components/album-showcase.tsx | 30 +++++++ src/app/_components/spotify-data.tsx | 116 ++++++++++++++++--------- src/app/page.tsx | 39 +-------- 3 files changed, 108 insertions(+), 77 deletions(-) create mode 100644 src/app/_components/album-showcase.tsx diff --git a/src/app/_components/album-showcase.tsx b/src/app/_components/album-showcase.tsx new file mode 100644 index 0000000..29fae99 --- /dev/null +++ b/src/app/_components/album-showcase.tsx @@ -0,0 +1,30 @@ +import { TrackByAlbum } from "./spotify-data"; + +export default function AlbumShowcase({ + album, + position, + tracks, +}: TrackByAlbum) { + const imageSource = album.images[0] + ? album.images[0].url + : "https://via.placeholder.com/150"; + + return ( +
+ {album.name} +
+

+ {album.name} +

+
    + {tracks.map((track) => ( +
  • {track.name}
  • + ))} +
+
+
+ ); +} diff --git a/src/app/_components/spotify-data.tsx b/src/app/_components/spotify-data.tsx index 045170c..ae69b00 100644 --- a/src/app/_components/spotify-data.tsx +++ b/src/app/_components/spotify-data.tsx @@ -1,7 +1,21 @@ import SpotifyWebApi from "spotify-web-api-node"; +import AlbumShowcase from "./album-showcase"; const TRACKS_LIMIT = 20; +export type TrackByAlbum = { + album: { + id: string; + name: string; + images: { url: string }[]; + }; + position: number; + tracks: { + id: string; + name: string; + }[]; +}; + export default async function SpotifyData({ accessToken, refreshToken, @@ -33,25 +47,66 @@ export default async function SpotifyData({ ...track, position: i + 1, })); - const tracksByAlbum = tracksData.body.items.reduce((acc, track) => { - if (!acc[track.album.id]) { - const tracksWithAlbum = tracks.filter( - (t) => t.album.id === track.album.id, - ); - acc[track.album.id] = { - album: track.album, - position: - tracksWithAlbum.reduce( - (acc, _track) => TRACKS_LIMIT / _track.position + acc, - 0, - ) / tracksWithAlbum.length, - tracks: [], - }; - } - acc[track.album.id].tracks.push(track); - return acc; - }, {}); + const tracksByAlbum = tracksData.body.items.reduce( + (acc: Record, track) => { + if (!acc[track.album.id]) { + const tracksWithAlbum = tracks.filter( + (t) => t.album.id === track.album.id, + ); + acc[track.album.id] = { + album: track.album, + position: + tracksWithAlbum.reduce( + (acc, _track) => TRACKS_LIMIT / _track.position + acc, + 0, + ) / tracksWithAlbum.length, + tracks: [], + }; + } + (acc[track.album.id] || ({ tracks: [] } as any)).tracks.push(track); + return acc; + }, + {}, + ); + // type Track = { + // id: string; + // name: string; + // album: { + // id: string; + // name: string; + // images: { url: string }[]; + // }; + // position: number; + // }; + + // type Album = { + // album: { + // id: string; + // name: string; + // images: { url: string }[]; + // }; + // position: number; + // tracks: Track[]; + // }; + + // // type Artist = { + // // id: string; + // // name: string; + // // images: { url: string }[]; + // // }; + + // // type SpotifyDataProps = { + // // accessToken: string; + // // refreshToken: string; + // // }; + + // // type SpotifyDataResponse = { + // // albums: Album[]; + // // artists: Artist[]; + // // tracks: Track[]; + // // tracksByAlbum: { [key: string]: Album }; + // // }; console.log("Albums:", albums); console.log("Artists:", artists); console.log("Tracks:", tracks); @@ -90,29 +145,10 @@ export default async function SpotifyData({ {Object.values(tracksByAlbum) .sort((a, b) => b.position - a.position) .map((album) => ( -
-

{album.album.name}

-
- {album.album.name} -
    - {album.tracks.map((track) => ( -
  • {track.name}
  • - ))} -
-
-
+ ))} -

Albums images

+ {/*

Albums images

); })} -
+ */}

Artists images

-
-
-

- Create T3 App -

-
- -

First Steps →

-
- Just the basics - Everything you need to know to set up your - database and authentication. -
- - -

Documentation →

-
- Learn more about Create T3 App, the libraries it uses, and how - to deploy it. -
- -
-
-

- {hello ? hello.greeting : "Loading tRPC query..."} -

-
+
+
{userIsLogged ? ( )} -