Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions app/api/room/artist/route.tsx

This file was deleted.

67 changes: 32 additions & 35 deletions hooks/useArtistFromRoom.ts
Original file line number Diff line number Diff line change
@@ -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]);
}
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]);
}
31 changes: 31 additions & 0 deletions lib/chats/getChatArtist.ts
Original file line number Diff line number Diff line change
@@ -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<ChatArtistResponse> {
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();
}
Loading