diff --git a/app/api/room/artist/route.tsx b/app/api/room/artist/route.tsx deleted file mode 100644 index 0afd8d99e..000000000 --- a/app/api/room/artist/route.tsx +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextRequest } from "next/server"; -import { getRoomArtistId } from "@/lib/supabase/getRoomArtistId"; -import { ensureRoomAccess } from "@/lib/supabase/ensureRoomAccess"; -import { ensureArtistAccess } from "@/lib/supabase/ensureArtistAccess"; - -/** - * GET endpoint to get the artist ID for a room and handle sharing - */ -export async function GET(req: NextRequest) { - const searchParams = req.nextUrl.searchParams; - const roomId = searchParams.get("roomId"); - const accountId = searchParams.get("accountId"); - - if (!roomId) { - return Response.json({ error: "Missing roomId parameter" }, { status: 400 }); - } - - try { - const artistId = await getRoomArtistId(roomId); - const response = { - artist_id: artistId, - artist_exists: !!artistId, - room_id: roomId - }; - - if (!artistId || !accountId) { - return Response.json(response); - } - - const [newRoomId, artistAccessGranted] = await Promise.all([ - ensureRoomAccess(roomId, accountId), - ensureArtistAccess(artistId, accountId) - ]); - - return Response.json({ - ...response, - room_access_granted: !!newRoomId, - artist_added: artistAccessGranted, - new_room_id: newRoomId, - original_room_id: roomId - }); - } catch (error) { - console.error("Error processing room artist request:", error); - return Response.json({ error: "Failed to process request" }, { status: 500 }); - } -} - -// Ensure this API route is never cached -export const dynamic = "force-dynamic"; -export const revalidate = 0; \ No newline at end of file diff --git a/hooks/useArtistFromRoom.ts b/hooks/useArtistFromRoom.ts index 01e6563b3..d8faa06d0 100644 --- a/hooks/useArtistFromRoom.ts +++ b/hooks/useArtistFromRoom.ts @@ -1,47 +1,44 @@ -import { useEffect, useRef } from "react"; +import { useEffect } from "react"; +import { useQuery } from "@tanstack/react-query"; +import { usePrivy } from "@privy-io/react-auth"; import { useArtistProvider } from "@/providers/ArtistProvider"; import { useUserProvider } from "@/providers/UserProvder"; import type { ArtistRecord } from "@/types/Artist"; +import { useApiOverride } from "@/hooks/useApiOverride"; +import { getChatArtist } from "@/lib/chats/getChatArtist"; /** * A hook that automatically selects the artist associated with a room. * @param roomId The ID of the room to get the artist for */ export function useArtistFromRoom(roomId: string) { + const { getAccessToken } = usePrivy(); const { userData } = useUserProvider(); const { selectedArtist, artists, setSelectedArtist, getArtists } = useArtistProvider(); - const hasRun = useRef(false); - + const apiOverride = useApiOverride(); + + const { data } = useQuery({ + queryKey: ["chatArtist", roomId], + queryFn: async () => { + const accessToken = await getAccessToken(); + if (!accessToken) throw new Error("No access token"); + return getChatArtist(roomId, accessToken, apiOverride ?? undefined); + }, + enabled: !!roomId && !!userData?.id, + staleTime: Infinity, + retry: 2, + }); + useEffect(() => { - if (hasRun.current || !roomId || !userData?.id) return; - hasRun.current = true; - - (async () => { - try { - const response = await fetch( - `/api/room/artist?roomId=${encodeURIComponent(roomId)}&accountId=${encodeURIComponent(userData.id)}` - ); - - if (!response.ok) return; - const data = await response.json(); - - if (data.new_room_id && data.new_room_id !== roomId) { - window.history.replaceState({}, '', `/chat/${data.new_room_id}`); - } - - if (!data.artist_id || selectedArtist?.account_id === data.artist_id) return; - - const artistList = artists as ArtistRecord[]; - const artist = artistList.find(a => a.account_id === data.artist_id); - - if (artist) { - setSelectedArtist(artist); - } else { - await getArtists(data.artist_id); - } - } catch (error) { - console.error("Error selecting artist for room:", error); - } - })(); - }, [roomId, userData, selectedArtist, artists, setSelectedArtist, getArtists]); -} \ No newline at end of file + if (!data?.artist_id || selectedArtist?.account_id === data.artist_id) return; + + const artistList = artists as ArtistRecord[]; + const artist = artistList.find(a => a.account_id === data.artist_id); + + if (artist) { + setSelectedArtist(artist); + } else { + getArtists(data.artist_id); + } + }, [data, selectedArtist, artists, setSelectedArtist, getArtists]); +} diff --git a/lib/chats/getChatArtist.ts b/lib/chats/getChatArtist.ts new file mode 100644 index 000000000..ba56b1727 --- /dev/null +++ b/lib/chats/getChatArtist.ts @@ -0,0 +1,31 @@ +import { NEW_API_BASE_URL } from "@/lib/consts"; + +interface ChatArtistResponse { + status: string; + room_id: string; + artist_id: string | null; + artist_exists: boolean; +} + +/** + * Fetches the artist associated with a chat room. + */ +export async function getChatArtist( + roomId: string, + accessToken: string, + baseUrl?: string, +): Promise { + const url = baseUrl || NEW_API_BASE_URL; + + const response = await fetch(`${url}/api/chats/${encodeURIComponent(roomId)}/artist`, { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }); + + if (!response.ok) { + throw new Error("Failed to fetch chat artist"); + } + + return response.json(); +}