diff --git a/app/api/artist/route.tsx b/app/api/artist/route.tsx deleted file mode 100644 index 6c5072742..000000000 --- a/app/api/artist/route.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import supabase from "@/lib/supabase/serverClient"; -import { NextRequest } from "next/server"; - -export async function GET(req: NextRequest) { - const artistId = req.nextUrl.searchParams.get("artistId"); - - try { - const { data: account } = await supabase - .from("accounts") - .select("*, account_info(*), account_socials(*)") - .eq("id", artistId) - .single(); - if (!account) throw new Error("failed"); - - return Response.json( - { - artist: { - ...account.account_info[0], - ...account, - }, - }, - { status: 200 }, - ); - } catch (error) { - console.error(error); - const message = error instanceof Error ? error.message : "failed"; - return Response.json({ message }, { status: 400 }); - } -} - -export const dynamic = "force-dynamic"; -export const fetchCache = "force-no-store"; -export const revalidate = 0; diff --git a/hooks/useArtistInstruction.ts b/hooks/useArtistInstruction.ts index 2e010642d..3441bc3a7 100644 --- a/hooks/useArtistInstruction.ts +++ b/hooks/useArtistInstruction.ts @@ -1,20 +1,25 @@ import { useQuery } from "@tanstack/react-query"; +import { usePrivy } from "@privy-io/react-auth"; +import getArtist from "@/lib/getArtist"; export function useArtistInstruction(artistId?: string) { + const { getAccessToken, authenticated } = usePrivy(); + return useQuery({ queryKey: ["artist-instruction", artistId], - enabled: Boolean(artistId), + enabled: Boolean(artistId) && authenticated, queryFn: async () => { if (!artistId) return undefined; - const res = await fetch(`/api/artist?artistId=${encodeURIComponent(artistId)}`); - if (!res.ok) return undefined; - const json = await res.json(); - return json?.artist?.instruction || undefined; + const accessToken = await getAccessToken(); + if (!accessToken) { + throw new Error("Please sign in to view artist details"); + } + + const artist = await getArtist(artistId, accessToken); + return artist?.instruction || undefined; }, staleTime: 5 * 60 * 1000, }); } export default useArtistInstruction; - - diff --git a/hooks/useArtistKnowledge.ts b/hooks/useArtistKnowledge.ts index 262ab49ef..dab95bbe6 100644 --- a/hooks/useArtistKnowledge.ts +++ b/hooks/useArtistKnowledge.ts @@ -1,17 +1,23 @@ import { useQuery } from "@tanstack/react-query"; +import { usePrivy } from "@privy-io/react-auth"; +import getArtist from "@/lib/getArtist"; import type { KnowledgeBaseEntry } from "@/lib/supabase/getArtistKnowledge"; export function useArtistKnowledge(artistId?: string) { + const { getAccessToken, authenticated } = usePrivy(); + return useQuery({ queryKey: ["artist-knowledge", artistId], - enabled: Boolean(artistId), + enabled: Boolean(artistId) && authenticated, queryFn: async () => { if (!artistId) return []; - const res = await fetch(`/api/artist?artistId=${encodeURIComponent(artistId)}`); - if (!res.ok) return []; - const json = await res.json(); - const knowledges: KnowledgeBaseEntry[] = - json?.artist?.account_info?.[0]?.knowledges || json?.artist?.knowledges || []; + const accessToken = await getAccessToken(); + if (!accessToken) { + throw new Error("Please sign in to view artist details"); + } + + const artist = await getArtist(artistId, accessToken); + const knowledges: KnowledgeBaseEntry[] = artist?.knowledges || []; return Array.isArray(knowledges) ? knowledges : []; }, staleTime: 5 * 60 * 1000, @@ -19,5 +25,3 @@ export function useArtistKnowledge(artistId?: string) { } export default useArtistKnowledge; - - diff --git a/lib/getArtist.tsx b/lib/getArtist.tsx index eb984a9ed..29e05f027 100644 --- a/lib/getArtist.tsx +++ b/lib/getArtist.tsx @@ -1,13 +1,31 @@ -const getArtist = async (artistId: string) => { - try { - const response = await fetch(`/api/artist?artistId=${artistId}`); +import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; +import type { ArtistRecord } from "@/types/Artist"; - const data = await response.json(); +const getArtist = async ( + artistId: string, + accessToken: string, +) : Promise => { + const response = await fetch( + `${getClientApiBaseUrl()}/api/artists/${encodeURIComponent(artistId)}`, + { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }, + ); - return data?.artist; - } catch (error) { - return { error }; + if (!response.ok) { + const responseText = await response.text(); + throw new Error( + responseText + ? `Failed to fetch artist: ${response.status} ${responseText}` + : `Failed to fetch artist: ${response.status}`, + ); } + + const data = await response.json(); + + return data?.artist ?? null; }; export default getArtist;