diff --git a/src/app/_components/spotify-data.tsx b/src/app/_components/spotify-data.tsx index 08d930e..08d76fc 100644 --- a/src/app/_components/spotify-data.tsx +++ b/src/app/_components/spotify-data.tsx @@ -1,6 +1,5 @@ import SpotifyWebApi from "spotify-web-api-node"; import Showcase from "./showcase"; -import { FETCH_ARTISTS_LIMIT, FETCH_TRACKS_LIMIT } from "../utils/contants"; import { getSpotifyData } from "../utils/getSpotifyData"; import { api } from "@/trpc/server"; @@ -21,10 +20,14 @@ export default async function SpotifyData({ accessToken, refreshToken, placeholderData, + tracksLimit, + artistsLimit, }: { accessToken: string; refreshToken: string; placeholderData: boolean; + tracksLimit?: number; + artistsLimit?: number; }) { const fetchData = async () => { if (placeholderData) { @@ -48,6 +51,8 @@ export default async function SpotifyData({ } else { return getSpotifyData({ accessToken, + tracksLimit, + artistsLimit, }); } }; diff --git a/src/app/page.tsx b/src/app/page.tsx index a4260ea..12d3fe9 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -28,6 +28,16 @@ export default async function Home({ redirectUri: process.env.SPOTIFY_REDIRECT_URI, }); + const [tracksLimit, artistsLimit] = [ + searchParams?.["tracks-limit"], + searchParams?.["artists-limit"], + ].map((value) => { + if (value && typeof value === "string") { + return parseInt(value, 10); + } + return undefined; + }); + if (userIsLogged) try { spotifyApi.setAccessToken(access_token as string); @@ -47,6 +57,8 @@ export default async function Home({ accessToken={access_token as string} refreshToken={refresh_token as string} placeholderData={placeholderDataSelected} + tracksLimit={tracksLimit} + artistsLimit={artistsLimit} /> ) : ( diff --git a/src/app/utils/getSpotifyData.ts b/src/app/utils/getSpotifyData.ts index 2511e86..b40ec85 100644 --- a/src/app/utils/getSpotifyData.ts +++ b/src/app/utils/getSpotifyData.ts @@ -4,8 +4,12 @@ import { TrackByAlbum } from "../_components/spotify-data"; export const getSpotifyData = async ({ accessToken, + tracksLimit, + artistsLimit, }: { accessToken: string; + tracksLimit?: number; + artistsLimit?: number; }) => { const spotifyApi = new SpotifyWebApi({ clientId: process.env.SPOTIFY_CLIENT_ID, @@ -16,11 +20,19 @@ export const getSpotifyData = async ({ const [artistsData, tracksData, userData] = await Promise.all([ spotifyApi.getMyTopArtists({ - limit: FETCH_ARTISTS_LIMIT, + limit: Math.min( + ...[FETCH_ARTISTS_LIMIT, artistsLimit].filter( + (v) => typeof v === "number", + ), + ), time_range: "short_term", }), spotifyApi.getMyTopTracks({ - limit: FETCH_TRACKS_LIMIT, + limit: Math.min( + ...[FETCH_TRACKS_LIMIT, tracksLimit].filter( + (v) => typeof v === "number", + ), + ), time_range: "short_term", }), spotifyApi.getMe(),