diff --git a/app/api/segments/create/route.ts b/app/api/segments/create/route.ts deleted file mode 100644 index 7540fae5c..000000000 --- a/app/api/segments/create/route.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { createSegments } from "@/lib/segments/createSegments"; - -export async function POST(req: NextRequest) { - try { - const { artist_account_id, prompt } = await req.json(); - if (!artist_account_id || !prompt) { - return NextResponse.json( - { error: "artist_account_id and prompt are required" }, - { status: 400 } - ); - } - const result = await createSegments({ artist_account_id, prompt }); - return NextResponse.json(result); - } catch (error) { - return NextResponse.json( - { - error: - error instanceof Error ? error.message : "Failed to create segments", - }, - { status: 500 } - ); - } -} diff --git a/hooks/useCreateSegments.ts b/hooks/useCreateSegments.ts index 80ebb5d33..6717c6112 100644 --- a/hooks/useCreateSegments.ts +++ b/hooks/useCreateSegments.ts @@ -1,8 +1,11 @@ import { useState } from "react"; import { toast } from "react-toastify"; +import { usePrivy } from "@privy-io/react-auth"; import { useArtistProvider } from "@/providers/ArtistProvider"; +import { createArtistSegments } from "@/lib/artists/createArtistSegments"; export function useCreateSegments() { + const { getAccessToken } = usePrivy(); const { selectedArtist } = useArtistProvider(); const artist_account_id = selectedArtist?.account_id; const [loading, setLoading] = useState(false); @@ -11,23 +14,20 @@ export function useCreateSegments() { if (!artist_account_id) return; setLoading(true); try { - const response = await fetch("/api/segments/create", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - artist_account_id, - prompt: "Segment my fans to help me fund my next project.", - }), - }); - const data = await response.json(); - if (!response.ok || data.error) { - throw new Error(data.error || "Failed to generate segments"); + const accessToken = await getAccessToken(); + if (!accessToken) { + throw new Error("Please sign in to generate segments"); } + await createArtistSegments( + accessToken, + artist_account_id, + "Segment my fans to help me fund my next project.", + ); toast.success("Segments generated successfully!"); if (onSuccess) onSuccess(); } catch (error) { toast.error( - error instanceof Error ? error.message : "Failed to generate segments" + error instanceof Error ? error.message : "Failed to generate segments", ); console.error(error); } finally { diff --git a/lib/artists/createArtistSegments.ts b/lib/artists/createArtistSegments.ts new file mode 100644 index 000000000..5f741a9f8 --- /dev/null +++ b/lib/artists/createArtistSegments.ts @@ -0,0 +1,39 @@ +import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; + +interface CreateArtistSegmentsResponse { + status: "success" | "error"; + segments_created?: number; + message?: string; + error?: string; +} + +/** + * Creates segments for an artist via the dedicated API. + * + * @param accessToken - Privy access token for Bearer auth + * @param artistId - Artist account ID (path-encoded) + * @param prompt - Segmentation prompt + */ +export async function createArtistSegments( + accessToken: string, + artistId: string, + prompt: string, +): Promise { + const response = await fetch( + `${getClientApiBaseUrl()}/api/artists/${artistId}/segments`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + }, + body: JSON.stringify({ prompt }), + }, + ); + + const data: CreateArtistSegmentsResponse = await response.json(); + + if (!response.ok || data.status !== "success") { + throw new Error(data.error || "Failed to generate segments"); + } +}