From 0c433e5dbf04025340a37cf92436b1943facf83e Mon Sep 17 00:00:00 2001 From: Francisco Pessano Date: Fri, 11 Oct 2024 01:31:36 -0300 Subject: [PATCH] Add spooky image generation and saving to album showcase --- src/app/_components/album-showcase.tsx | 74 +++++++++++++++++++++++++- src/app/_components/spotify-data.tsx | 18 +++---- 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/app/_components/album-showcase.tsx b/src/app/_components/album-showcase.tsx index 46af408..d3dcf18 100644 --- a/src/app/_components/album-showcase.tsx +++ b/src/app/_components/album-showcase.tsx @@ -1,4 +1,8 @@ +"use client"; + +import { useEffect, useState } from "react"; import { TrackByAlbum } from "./spotify-data"; +import { api } from "@/trpc/react"; export default function AlbumShowcase({ album, @@ -9,12 +13,79 @@ export default function AlbumShowcase({ ? album.images[0].url : "https://via.placeholder.com/150"; + const [spookyImageLoaded, setSpookyImageLoaded] = useState(false); + + const entry = api.entry.get.useQuery({ + type: "album", + name: album.name, + image: imageSource, + }); + + const generateSpookyImage = api.entry.generate.useMutation(); + const saveImage = api.entry.save.useMutation(); + + const handleGenerateSpookyImage = async () => { + if (!entry.data && !entry.isLoading) { + generateSpookyImage.mutate({ + entry: { + type: "album", + name: album.name, + image: imageSource, + }, + }); + } + }; + + const handleSaveImage = async () => { + if (!entry.data && !entry.isLoading) { + saveImage.mutate({ + entry: { + type: "album", + image: imageSource, + name: album.name, + }, + }); + } + }; + + useEffect(() => { + handleGenerateSpookyImage(); + }, [entry.data]); + + useEffect(() => { + if (spookyImageLoaded) { + handleSaveImage(); + } + }, [spookyImageLoaded]); + + //TODO ERROR HANDLING + + const spookyImageMatch = ( + (entry.data?.value || generateSpookyImage.data) as null | string + )?.match(/https:\/\/res.cloudinary.com\/[^"]+/); + + const spookyImageSource = spookyImageMatch ? spookyImageMatch[0] : null; + return (
- {album.name} +
+ {album.name} + {!!spookyImageSource && ( + {album.name} { + if (!spookyImageLoaded && generateSpookyImage.data) { + setSpookyImageLoaded(true); + } + }} + /> + )} +

{album.name} @@ -23,6 +94,7 @@ export default function AlbumShowcase({ {tracks.map((track) => (
  • {track.name}
  • ))} + {JSON.stringify(generateSpookyImage.data)}

    diff --git a/src/app/_components/spotify-data.tsx b/src/app/_components/spotify-data.tsx index d0735b8..c8dd7e5 100644 --- a/src/app/_components/spotify-data.tsx +++ b/src/app/_components/spotify-data.tsx @@ -1,6 +1,7 @@ import SpotifyWebApi from "spotify-web-api-node"; import AlbumShowcase from "./album-showcase"; import ArtistShowcase from "./artist-showcase"; +import { TRPCReactProvider } from "@/trpc/react"; const TRACKS_LIMIT = 20; const ARTISTS_LIMIT = 21; @@ -109,10 +110,6 @@ export default async function SpotifyData({ // // tracks: Track[]; // // tracksByAlbum: { [key: string]: Album }; // // }; - console.log("Albums:", albums); - console.log("Artists:", artists); - console.log("Tracks:", tracks); - console.log("Tracks by album:", tracksByAlbum); return ( <> @@ -143,12 +140,13 @@ export default async function SpotifyData({ })} */} -

    Tracks by album

    - {Object.values(tracksByAlbum) - .sort((a, b) => b.position - a.position) - .map((album) => ( - - ))} + {/* TODO CHECK IF NEEDED */} + +

    Tracks by album

    + {Object.values(tracksByAlbum) + .sort((a, b) => b.position - a.position) + .map((album, index) => !index && )} +
    {/*

    Albums images