diff --git a/src/lib/analytics.ts b/src/lib/analytics.ts index 75fb866..e7eb5e7 100644 --- a/src/lib/analytics.ts +++ b/src/lib/analytics.ts @@ -5,6 +5,21 @@ import type { LabelCount, } from "../types/analytics.types" +/** Canonical subscription row statuses shown in analytics breakdowns. */ +export const SUBSCRIPTION_STATUS_BREAKDOWN = [ + "ACTIVE", + "PENDING", + "EXPIRED", + "CANCELLED", +] as const + +const SUBSCRIPTION_STATUS_COLORS: Record = { + ACTIVE: "#9E2891", + PENDING: "#FFD23F", + EXPIRED: "#F97316", + CANCELLED: "#EF4444", +} + const INACTIVE_SUBSCRIPTION_STATUSES = new Set([ "INACTIVE", "CANCELLED", @@ -20,6 +35,51 @@ export interface SubscriptionMetrics { inactive: number } +export interface SubscriptionStatusPieSlice { + name: string + value: number + color: string +} + +/** + * Ensures every canonical subscription status appears in the pie/legend, + * including statuses with a count of 0. + */ +export function buildSubscriptionStatusPie( + byStatus: LabelCount[] | null | undefined, + fallbackColors: string[] = ["#9E2891", "#FFD23F", "#1DE9B6", "#C26FC0"], +): SubscriptionStatusPieSlice[] { + const counts = new Map() + for (const row of byStatus ?? []) { + const key = row.label.trim().toUpperCase() + if (!key) continue + counts.set(key, (counts.get(key) ?? 0) + row.count) + } + + const known = new Set(SUBSCRIPTION_STATUS_BREAKDOWN) + const slices: SubscriptionStatusPieSlice[] = SUBSCRIPTION_STATUS_BREAKDOWN.map( + (status, index) => ({ + name: status, + value: counts.get(status) ?? 0, + color: + SUBSCRIPTION_STATUS_COLORS[status] ?? + fallbackColors[index % fallbackColors.length], + }), + ) + + // Preserve any unexpected statuses from the API after the canonical set. + for (const [status, value] of counts) { + if (known.has(status)) continue + slices.push({ + name: status, + value, + color: fallbackColors[slices.length % fallbackColors.length], + }) + } + + return slices +} + /** Derives inactive count from by_status when present, else total − active. */ export function getSubscriptionMetrics( subscriptions: DashboardSubscriptions, diff --git a/src/pages/DashboardPage.tsx b/src/pages/DashboardPage.tsx index eb76571..d44449b 100644 --- a/src/pages/DashboardPage.tsx +++ b/src/pages/DashboardPage.tsx @@ -47,6 +47,7 @@ import { getSeriesPeriodLabel, getSubscriptionMetrics, getVideoLessonsSummary, + buildSubscriptionStatusPie, } from "../lib/analytics" import type { DashboardData, DashboardFilters } from "../types/analytics.types" import { formatAverageStars } from "../lib/ratingsDisplay" @@ -156,13 +157,10 @@ export function DashboardPage() { count: d.count, })) ?? [] - const subscriptionStatusData = - dashboard?.subscriptions.by_status.map((s, i) => ({ - name: s.label, - value: s.count, - color: PIE_COLORS[i % PIE_COLORS.length], - })) ?? [] - + const subscriptionStatusData = buildSubscriptionStatusPie( + dashboard?.subscriptions.by_status, + PIE_COLORS, + ) const issueStatusData = dashboard?.issues.by_status.map((s, i) => ({ name: s.label, diff --git a/src/pages/analytics/AnalyticsPage.tsx b/src/pages/analytics/AnalyticsPage.tsx index 215f895..c85dbfa 100644 --- a/src/pages/analytics/AnalyticsPage.tsx +++ b/src/pages/analytics/AnalyticsPage.tsx @@ -56,6 +56,7 @@ import { getSubscriptionMetrics, getVideoLessonsSummary, formatPercentRate, + buildSubscriptionStatusPie, } from "../../lib/analytics" import type { DashboardData, DashboardFilters, LabelCount } from "../../types/analytics.types" @@ -400,12 +401,10 @@ export function AnalyticsPage() { color: PIE_COLORS[i % PIE_COLORS.length], })) - const subscriptionStatusPie = subscriptions.by_status.map((s, i) => ({ - name: s.label, - value: s.count, - color: PIE_COLORS[i % PIE_COLORS.length], - })) - + const subscriptionStatusPie = buildSubscriptionStatusPie( + subscriptions.by_status, + PIE_COLORS, + ) const notifByTypePie = notifications.by_type.slice(0, 8).map((s, i) => ({ name: s.label, value: s.count, diff --git a/src/pages/payments/PaymentsPage.tsx b/src/pages/payments/PaymentsPage.tsx index dedfe08..80a7022 100644 --- a/src/pages/payments/PaymentsPage.tsx +++ b/src/pages/payments/PaymentsPage.tsx @@ -59,10 +59,7 @@ import { DEFAULT_TABLE_PAGE_SIZE, TABLE_PAGE_SIZE_OPTIONS } from "../../lib/tabl const STATUS_FILTERS: { value: PaymentStatus; label: string }[] = [ { value: "PENDING", label: "Pending" }, - { value: "PROCESSING", label: "Processing" }, { value: "SUCCESS", label: "Success" }, - { value: "FAILED", label: "Failed" }, - { value: "CANCELLED", label: "Cancelled" }, { value: "EXPIRED", label: "Expired" }, ]