From 86ff17a8c19eb1713424c30f2976df4a8454f43c Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 30 Jun 2026 08:12:53 -0400 Subject: [PATCH] user spend dashboard --- frontend/apps/artcraft-dashboard/src/App.tsx | 2 + .../src/api/ModerationApi.ts | 110 ++++ .../src/pages/UserProfile.tsx | 6 + .../src/pages/UserSpendSummary.tsx | 481 ++++++++++++++++++ 4 files changed, 599 insertions(+) create mode 100644 frontend/apps/artcraft-dashboard/src/pages/UserSpendSummary.tsx diff --git a/frontend/apps/artcraft-dashboard/src/App.tsx b/frontend/apps/artcraft-dashboard/src/App.tsx index 4054c2e898..d3182555de 100644 --- a/frontend/apps/artcraft-dashboard/src/App.tsx +++ b/frontend/apps/artcraft-dashboard/src/App.tsx @@ -24,6 +24,7 @@ import { JobTokenSearch } from "@/pages/JobTokenSearch"; import { DebugLogs } from "@/pages/DebugLogs"; import { DebugLogsSearch } from "@/pages/DebugLogsSearch"; import { SpendEvents } from "@/pages/SpendEvents"; +import { UserSpendSummary } from "@/pages/UserSpendSummary"; import { NotFoundPage } from "@/pages/NotFound"; import { Spinner } from "./components/ui/spinner"; import { ThemeProvider } from "@/components/theme-provider"; @@ -60,6 +61,7 @@ function AppRoutes() { } /> } /> } /> + } /> } /> } /> } /> diff --git a/frontend/apps/artcraft-dashboard/src/api/ModerationApi.ts b/frontend/apps/artcraft-dashboard/src/api/ModerationApi.ts index 098da5c11c..85834842e2 100644 --- a/frontend/apps/artcraft-dashboard/src/api/ModerationApi.ts +++ b/frontend/apps/artcraft-dashboard/src/api/ModerationApi.ts @@ -105,7 +105,117 @@ export interface UserSpendEvent { created_at: string; } +export interface UserSpendSummary { + payments_namespace: string; + user_token: string; + lifetime_gross_spend_usd_cents: number; + lifetime_subscription_spend_usd_cents: number; + lifetime_credits_spend_usd_cents: number; + lifetime_refund_usd_cents: number; + lifetime_net_spend_usd_cents: number; + lifetime_payment_count: number; + lifetime_refund_count: number; + maybe_first_payment_at: string | null; + first_spend_usd_cents: number; + maybe_last_payment_at: string | null; + last_spend_usd_cents: number; + maybe_days_since_first_payment: number | null; + maybe_days_since_last_payment: number | null; + net_spend_7d_usd_cents: number; + net_spend_prev_7d_usd_cents: number; + net_spend_14d_usd_cents: number; + net_spend_prev_14d_usd_cents: number; + net_spend_30d_usd_cents: number; + net_spend_prev_30d_usd_cents: number; + net_spend_60d_usd_cents: number; + net_spend_90d_usd_cents: number; + net_spend_this_year_usd_cents: number; + avg_weekly_net_spend_4w_usd_cents: number; + avg_weekly_net_spend_12w_usd_cents: number; + active_weeks_in_last_4: number; + active_weeks_in_last_8: number; + active_weeks_in_last_12: number; + active_weeks_in_last_24: number; + active_weeks_in_last_52: number; + consecutive_active_weeks: number; + consecutive_inactive_weeks: number; + maybe_weeks_since_last_spend: number | null; + is_active_subscriber: boolean; + maybe_subscription_interval: string | null; + maybe_reengagement_score: number | null; + created_at: string; + updated_at: string; +} + +export interface UserDailySpend { + payments_namespace: string; + spend_date: string; + subscription_spend_usd_cents: number; + credits_spend_usd_cents: number; + gross_spend_usd_cents: number; + refund_usd_cents: number; + net_spend_usd_cents: number; + payment_count: number; + credits_granted: number; + created_at: string; + updated_at: string; +} + export class ModerationApi extends ApiManager { + // Full spend summary for one user. + public async GetUserSpendSummary( + user_token: string, + payments_namespace?: string, + ): Promise> { + const endpoint = `${this.getApiSchemeAndHost()}/v1/moderation/user_spend_summaries/summary/${encodeURIComponent(user_token)}`; + return await this.get<{ + success: boolean; + maybe_summary: UserSpendSummary | null; + error_message?: string; + }>({ + endpoint, + query: { payments_namespace: payments_namespace ?? undefined }, + }) + .then((response) => ({ + success: response.success, + data: { summary: response.maybe_summary ?? null }, + errorMessage: response.error_message, + })) + .catch((err) => ({ success: false, errorMessage: err.message })); + } + + // One page of a user's daily spend rows (offset-paginated, newest date first). + public async ListUserDailySpends( + user_token: string, + offset?: number | null, + limit?: number, + payments_namespace?: string, + ): Promise> { + const endpoint = `${this.getApiSchemeAndHost()}/v1/moderation/user_daily_spends/user/${encodeURIComponent(user_token)}`; + return await this.get<{ + success: boolean; + records: UserDailySpend[]; + maybe_next_offset: number | null; + error_message?: string; + }>({ + endpoint, + query: { + offset: offset ?? undefined, + limit, + payments_namespace: payments_namespace ?? undefined, + }, + }) + .then((response) => ({ + success: response.success, + data: { + records: response.records || [], + next_offset: response.maybe_next_offset ?? null, + }, + errorMessage: response.error_message, + })) + .catch((err) => ({ success: false, errorMessage: err.message })); + } + // Spend events list (offset-paginated, newest payment first). public async ListUserSpendEvents( offset?: number | null, diff --git a/frontend/apps/artcraft-dashboard/src/pages/UserProfile.tsx b/frontend/apps/artcraft-dashboard/src/pages/UserProfile.tsx index 7f800ddc29..4782e4d489 100644 --- a/frontend/apps/artcraft-dashboard/src/pages/UserProfile.tsx +++ b/frontend/apps/artcraft-dashboard/src/pages/UserProfile.tsx @@ -604,6 +604,12 @@ export function UserProfile() { Referrals + + + + {error} + + + ); + } + + return ( +
+ {/* Header */} +
+
+ +

+ + Spend Summary +

+

+ + @{username} + + {user?.display_name && ( + {user.display_name} + )} +

+
+
+ + {/* Interactive spending-over-time graph */} + + + Spending over time + + Net spend per day ·{" "} + + {formatUsd(periodTotalCents)} + {" "} + over this period + +
+ +
+
+ + {chartData.length === 0 ? ( +
+ +

No spend in this period.

+
+ ) : ( + + + + + + + + + + + new Date(value).toLocaleDateString("en-US", { + month: "short", + day: "numeric", + }) + } + /> + formatUsdCompact(Number(value) * 100)} + /> + + new Date(value).toLocaleDateString("en-US", { + month: "short", + day: "numeric", + year: "numeric", + }) + } + formatter={(value) => formatUsd(Number(value) * 100)} + /> + } + /> + + + + )} +
+
+ + {/* Infographic HUD */} + {summary ? ( + + ) : ( +
+ +

No spend summary for this user.

+
+ )} +
+ ); +} + +// --- HUD --- + +function SpendHud({ summary }: { summary: UserSpendSummaryData }) { + return ( +
+
+ + + + +
+ +
+ + + + active + + ) : undefined + } + /> + +
+ +
+ + + + +
+ +
+ + + + +
+ +
+ + + + 0 + ? `${summary.consecutive_active_weeks}w active` + : `${summary.consecutive_inactive_weeks}w idle` + } + /> +
+
+ ); +} + +function Section({ title, children }: { title: string; children: React.ReactNode }) { + return ( +
+

+ {title} +

+
{children}
+
+ ); +} + +function StatCard({ + label, + value, + sub, + emphasis, + badge, +}: { + label: string; + value: string; + sub?: string; + emphasis?: boolean; + badge?: React.ReactNode; +}) { + return ( + + +
+ {label} + {badge} +
+ + {value} + + {sub && {sub}} +
+
+ ); +}