All files / components/dashboard/revenue-page-client revenue-page-client.tsx

100% Statements 17/17
88.46% Branches 23/26
100% Functions 9/9
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 1931x                         5x 5x 5x   5x                                                 8x 3x     12x           5x     20x                     4x                                                         48x                                                       8x         8x   8x           1x                 1x                                                                                              
"use client";
 
import {useQuery} from "@tanstack/react-query";
import {TrendingDown, TrendingUp, UsersRound} from "lucide-react";
import {DashboardShell} from "../dashboard-shell/dashboard-shell";
import {RevenueChart} from "../revenue-chart/revenue-chart";
import {Skeleton} from "@/components/ui/skeleton/skeleton";
import {uiStyles} from "@/components/ui/style-primitives";
import {fetchRevenue} from "@/lib/api/api";
import {cn, formatCurrency, formatNumber, formatPercent} from "@/lib/utils/utils";
import type {RevenuePoint} from "@/types/dashboard";
 
function latestRevenueCards(data: RevenuePoint[]) {
  const current = data.at(-1);
  const previous = data.at(-2);
  const growth = current && previous ? (current.revenue - previous.revenue) / previous.revenue : 0;
 
  return [
    {
      caption: "Current monthly recurring revenue",
      label: "MRR",
      value: formatCurrency(current?.revenue ?? 0),
    },
    {
      caption: "New business booked this month",
      label: "New business",
      value: formatCurrency(current?.newBusiness ?? 0, true),
    },
    {
      caption: "Expansion movement from existing accounts",
      label: "Expansion",
      value: formatCurrency(current?.expansion ?? 0, true),
    },
    {
      caption: `${formatNumber(current?.customers ?? 0)} active accounts`,
      label: "Month growth",
      value: formatPercent(growth),
    },
  ];
}
 
function RevenueSummaryCards({data, isLoading}: { data: RevenuePoint[]; isLoading: boolean }) {
  if (isLoading) {
    return (
        <section aria-label="Revenue summary" className="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
          {Array.from({length: 4}).map((_, index) => (
              <Skeleton className="h-28" key={index}/>
          ))}
        </section>
    );
  }
 
  return (
      <section aria-label="Revenue summary" className="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        {latestRevenueCards(data).map((card) => (
            <article className={cn("p-4", uiStyles.surface)} key={card.label}>
              <p className={uiStyles.subtleText}>{card.label}</p>
              <p className="mt-3 text-2xl font-semibold text-slate-950 dark:text-white">{card.value}</p>
              <p className="mt-2 text-sm text-slate-500 dark:text-slate-400">{card.caption}</p>
            </article>
        ))}
      </section>
  );
}
 
function RevenueTable({data}: { data: RevenuePoint[] }) {
  return (
      <section aria-labelledby="revenue-history-title" className={cn("p-5", uiStyles.surface)}>
        <div className="flex items-center justify-between gap-3">
          <div>
            <h2 className={uiStyles.sectionHeading} id="revenue-history-title">Revenue history</h2>
            <p className={cn("mt-1", uiStyles.subtleText)}>
              Monthly movement across acquisition, expansion, churn, and customers.
            </p>
          </div>
          <TrendingUp aria-hidden="true" className="text-emerald-600 dark:text-emerald-300" size={20}/>
        </div>
 
        <div className="mt-4 overflow-x-auto">
          <table className="w-full min-w-175 text-left text-sm">
            <caption className="sr-only">
              Monthly revenue history with MRR, new business, expansion, churn, and customer count.
            </caption>
            <thead className="text-xs font-semibold uppercase text-slate-500 dark:text-slate-400">
            <tr>
              <th className="py-2 pr-4">Month</th>
              <th className="px-4 py-2 text-right">MRR</th>
              <th className="px-4 py-2 text-right">New</th>
              <th className="px-4 py-2 text-right">Expansion</th>
              <th className="px-4 py-2 text-right">Churn</th>
              <th className="px-4 py-2 text-right">Customers</th>
            </tr>
            </thead>
            <tbody className="divide-y divide-slate-200 dark:divide-slate-800">
            {data.map((point) => (
                <tr key={point.month}>
                  <td className="py-3 pr-4 font-medium text-slate-950 dark:text-white">{point.month}</td>
                  <td className="px-4 py-3 text-right text-slate-700 dark:text-slate-200">
                    {formatCurrency(point.revenue, true)}
                  </td>
                  <td className="px-4 py-3 text-right text-emerald-700 dark:text-emerald-300">
                    {formatCurrency(point.newBusiness, true)}
                  </td>
                  <td className="px-4 py-3 text-right text-amber-700 dark:text-amber-200">
                    {formatCurrency(point.expansion, true)}
                  </td>
                  <td className="px-4 py-3 text-right text-rose-700 dark:text-rose-300">
                    {formatCurrency(point.churn, true)}
                  </td>
                  <td className="px-4 py-3 text-right text-slate-700 dark:text-slate-200">
                    {formatNumber(point.customers)}
                  </td>
                </tr>
            ))}
            </tbody>
          </table>
        </div>
      </section>
  );
}
 
/** Focused revenue route with chart, month cards, and historical detail. */
export function RevenuePageClient() {
  const revenueQuery = useQuery({
    queryFn: fetchRevenue,
    queryKey: ["revenue"],
  });
 
  const data = revenueQuery.data?.data ?? [];
 
  return (
      <DashboardShell
          activeSection="revenue"
          description="Understand recurring revenue movement, acquisition, expansion, churn, and account growth from one focused view."
          eyebrow="Revenue"
          isRefreshing={revenueQuery.isFetching}
          onRefresh={() => void revenueQuery.refetch()}
          title="Revenue"
      >
        <RevenueSummaryCards data={data} isLoading={revenueQuery.isLoading}/>
 
        <RevenueChart
            data={data}
            isError={revenueQuery.isError}
            isLoading={revenueQuery.isLoading}
            onRetry={() => void revenueQuery.refetch()}
        />
 
        {!revenueQuery.isLoading && !revenueQuery.isError ? (
            <>
              <RevenueTable data={data}/>
 
              <section aria-label="Revenue watch items" className="grid gap-3 md:grid-cols-2">
                <article className={cn("p-4", uiStyles.surface)}>
                  <div
                      className="flex items-center gap-2 text-sm font-semibold text-emerald-700 dark:text-emerald-300">
                    <TrendingUp aria-hidden="true" size={16}/>
                    Expansion motion
                  </div>
                  <p className={cn("mt-2", uiStyles.bodyText)}>
                    Expansion is tracking above churn, with the latest month
                    adding {formatCurrency(data.at(-1)?.expansion ?? 0, true)}.
                  </p>
                </article>
                <article className={cn("p-4", uiStyles.surface)}>
                  <div
                      className="flex items-center gap-2 text-sm font-semibold text-rose-700 dark:text-rose-300">
                    <TrendingDown aria-hidden="true" size={16}/>
                    Churn pressure
                  </div>
                  <p className={cn("mt-2", uiStyles.bodyText)}>
                    Current churn impact is {formatCurrency(Math.abs(data.at(-1)?.churn ?? 0), true)} and
                    should stay below new business velocity.
                  </p>
                </article>
                <article className={cn("p-4 md:col-span-2", uiStyles.surface)}>
                  <div
                      className="flex items-center gap-2 text-sm font-semibold text-slate-700 dark:text-slate-200">
                    <UsersRound aria-hidden="true" size={16}/>
                    Account base
                  </div>
                  <p className={cn("mt-2", uiStyles.bodyText)}>
                    The customer base closed at {formatNumber(data.at(-1)?.customers ?? 0)} accounts in the
                    latest reporting month.
                  </p>
                </article>
              </section>
            </>
        ) : null}
      </DashboardShell>
  );
}