Skip to content
Closed
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
24 changes: 0 additions & 24 deletions app/api/segments/create/route.ts

This file was deleted.

24 changes: 12 additions & 12 deletions hooks/useCreateSegments.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions lib/artists/createArtistSegments.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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");
}
}
Loading