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 (