From 9054cd09905a966eb4d5926b0b5c89b1dbc67f02 Mon Sep 17 00:00:00 2001 From: Arpit Gupta Date: Tue, 31 Mar 2026 01:04:54 +0530 Subject: [PATCH 1/2] feat: migrate room segment lookup to dedicated chats api --- app/api/roomSegment/route.ts | 45 ------------------------------------ hooks/useChatSegment.ts | 19 +++++++++++---- 2 files changed, 15 insertions(+), 49 deletions(-) delete mode 100644 app/api/roomSegment/route.ts 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..a6696c01c 100644 --- a/hooks/useChatSegment.ts +++ b/hooks/useChatSegment.ts @@ -1,4 +1,7 @@ import { useQuery } from "@tanstack/react-query"; +import { useAccessToken } from "@/hooks/useAccessToken"; +import { useApiOverride } from "@/hooks/useApiOverride"; +import { NEW_API_BASE_URL } from "@/lib/consts"; interface RoomSegmentResponse { segmentId: string | null; @@ -6,14 +9,22 @@ interface RoomSegmentResponse { } export const useChatSegment = (roomId?: string) => { + const accessToken = useAccessToken(); + const apiOverride = useApiOverride(); + const baseUrl = apiOverride || NEW_API_BASE_URL; + return useQuery({ queryKey: ["roomSegment", roomId], queryFn: async (): Promise => { - if (!roomId) { + if (!roomId || !accessToken) { return { segmentId: null }; } - const response = await fetch(`/api/roomSegment?roomId=${roomId}`); + const response = await fetch(`${baseUrl}/api/chats/${encodeURIComponent(roomId)}/segment`, { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }); if (!response.ok) { const error = await response.json(); console.error("[useChatSegment] API error:", { @@ -24,9 +35,9 @@ export const useChatSegment = (roomId?: string) => { } const data = await response.json(); - return data; + return { segmentId: data.segment_id ?? null }; }, - enabled: !!roomId, + enabled: !!roomId && !!accessToken, staleTime: 1000 * 60 * 5, // Cache for 5 minutes retry: 2, }); From ad56dc7017521948ba88734f63e4047531fc0445 Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Mon, 30 Mar 2026 23:09:07 -0500 Subject: [PATCH 2/2] refactor: use Privy getAccessToken directly + extract getChatSegment - Extract fetch to lib/chats/getChatSegment.ts (SRP) - Use getAccessToken from Privy SDK instead of useAccessToken hook (KISS) - Follows same pattern as useDeleteChat and useArtistFromRoom Co-Authored-By: Claude Opus 4.6 (1M context) --- hooks/useChatSegment.ts | 33 +++++++++------------------------ lib/chats/getChatSegment.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 lib/chats/getChatSegment.ts diff --git a/hooks/useChatSegment.ts b/hooks/useChatSegment.ts index a6696c01c..158e2fb6d 100644 --- a/hooks/useChatSegment.ts +++ b/hooks/useChatSegment.ts @@ -1,44 +1,29 @@ import { useQuery } from "@tanstack/react-query"; -import { useAccessToken } from "@/hooks/useAccessToken"; +import { usePrivy } from "@privy-io/react-auth"; import { useApiOverride } from "@/hooks/useApiOverride"; -import { NEW_API_BASE_URL } from "@/lib/consts"; +import { getChatSegment } from "@/lib/chats/getChatSegment"; interface RoomSegmentResponse { segmentId: string | null; - error?: string; } export const useChatSegment = (roomId?: string) => { - const accessToken = useAccessToken(); + const { getAccessToken } = usePrivy(); const apiOverride = useApiOverride(); - const baseUrl = apiOverride || NEW_API_BASE_URL; return useQuery({ queryKey: ["roomSegment", roomId], queryFn: async (): Promise => { - if (!roomId || !accessToken) { - return { segmentId: null }; - } + if (!roomId) return { segmentId: null }; - const response = await fetch(`${baseUrl}/api/chats/${encodeURIComponent(roomId)}/segment`, { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }); - 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(); + const data = await getChatSegment(roomId, accessToken, apiOverride ?? undefined); return { segmentId: data.segment_id ?? null }; }, - enabled: !!roomId && !!accessToken, - staleTime: 1000 * 60 * 5, // Cache for 5 minutes + enabled: !!roomId, + 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(); +}