diff --git a/app/api/roomSegment/route.ts b/app/api/roomSegment/route.ts deleted file mode 100644 index c7302e598..000000000 --- a/app/api/roomSegment/route.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { getSegmentIdForRoomId } from "@/lib/supabase/getSegmentIdForRoomId"; - -export async function GET(req: Request) { - try { - const roomId = new URL(req.url).searchParams.get("roomId"); - if (!roomId) { - return new Response( - JSON.stringify({ - error: "Room ID is required", - }), - { - status: 400, - headers: { - "Content-Type": "application/json", - }, - } - ); - } - - const segmentId = await getSegmentIdForRoomId(roomId); - return Response.json({ segmentId }); - } catch (error) { - console.error("[/api/roomSegment] Error:", { - error: error instanceof Error ? error.message : "Unknown error", - stack: error instanceof Error ? error.stack : undefined, - }); - - return new Response( - JSON.stringify({ - error: "Failed to get segment ID", - details: error instanceof Error ? error.message : "Unknown error", - }), - { - status: 500, - headers: { - "Content-Type": "application/json", - }, - } - ); - } -} - -export const dynamic = "force-dynamic"; -export const revalidate = 0; -export const fetchCache = "force-no-store"; diff --git a/hooks/useChatSegment.ts b/hooks/useChatSegment.ts index c45871e12..158e2fb6d 100644 --- a/hooks/useChatSegment.ts +++ b/hooks/useChatSegment.ts @@ -1,33 +1,29 @@ import { useQuery } from "@tanstack/react-query"; +import { usePrivy } from "@privy-io/react-auth"; +import { useApiOverride } from "@/hooks/useApiOverride"; +import { getChatSegment } from "@/lib/chats/getChatSegment"; interface RoomSegmentResponse { segmentId: string | null; - error?: string; } export const useChatSegment = (roomId?: string) => { + const { getAccessToken } = usePrivy(); + const apiOverride = useApiOverride(); + return useQuery({ queryKey: ["roomSegment", roomId], queryFn: async (): Promise => { - if (!roomId) { - return { segmentId: null }; - } + if (!roomId) return { segmentId: null }; - const response = await fetch(`/api/roomSegment?roomId=${roomId}`); - if (!response.ok) { - const error = await response.json(); - console.error("[useChatSegment] API error:", { - status: response.status, - error, - }); - throw new Error(error.error || "Failed to fetch segment ID"); - } + const accessToken = await getAccessToken(); + if (!accessToken) throw new Error("No access token"); - const data = await response.json(); - return data; + const data = await getChatSegment(roomId, accessToken, apiOverride ?? undefined); + return { segmentId: data.segment_id ?? null }; }, enabled: !!roomId, - staleTime: 1000 * 60 * 5, // Cache for 5 minutes + staleTime: 1000 * 60 * 5, retry: 2, }); }; diff --git a/lib/chats/getChatSegment.ts b/lib/chats/getChatSegment.ts new file mode 100644 index 000000000..06b53d716 --- /dev/null +++ b/lib/chats/getChatSegment.ts @@ -0,0 +1,32 @@ +import { NEW_API_BASE_URL } from "@/lib/consts"; + +interface ChatSegmentResponse { + status: string; + room_id: string; + segment_id: string | null; + segment_exists: boolean; +} + +/** + * Fetches the segment associated with a chat room. + */ +export async function getChatSegment( + roomId: string, + accessToken: string, + baseUrl?: string, +): Promise { + const url = baseUrl || NEW_API_BASE_URL; + + const response = await fetch(`${url}/api/chats/${encodeURIComponent(roomId)}/segment`, { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }); + + if (!response.ok) { + const error = await response.json(); + throw new Error(error.error || "Failed to fetch segment ID"); + } + + return response.json(); +}