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

100% Statements 25/25
77.14% Branches 27/35
100% Functions 9/9
100% Lines 25/25

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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 2101x                                         1x 1x                                     10x 4x     16x           6x 10x                                                             10x     24x                                                                     10x         10x         10x         10x 5x         10x         10x 1x 1x 1x 1x     10x                                 16x     20x                                               1x                      
"use client";
 
import {useQuery} from "@tanstack/react-query";
import {
  ArrowRight,
  BarChart3,
  ListChecks,
  ShieldCheck,
  UsersRound,
} from "lucide-react";
import Link from "next/link";
import {DashboardShell} from "@/components/dashboard/dashboard-shell/dashboard-shell";
import {KpiCard} from "@/components/dashboard/kpi-card/kpi-card";
import {OpsSummary} from "@/components/dashboard/ops-summary/ops-summary";
import {RevenueChart} from "@/components/dashboard/revenue-chart/revenue-chart";
import {Skeleton} from "@/components/ui/skeleton/skeleton";
import {uiStyles} from "@/components/ui/style-primitives";
import {fetchCustomers, fetchDelivery, fetchMetrics, fetchRevenue} from "@/lib/api/api";
import {cn, formatCurrency, formatNumber, formatPercent} from "@/lib/utils/utils";
import type {CustomersResponse, JiraDeliveryResponse, RevenuePoint} from "@/types/dashboard";
 
const pageSize = 8;
const overviewCustomerFilters = {
  page: 1,
  pageSize,
  plan: "all" as const,
  query: "",
  status: "all" as const,
};
 
function OverviewFocusPanel({
                              customers,
                              delivery,
                              isLoading,
                              revenue,
                            }: {
  customers?: CustomersResponse;
  delivery?: JiraDeliveryResponse;
  isLoading: boolean;
  revenue?: RevenuePoint[];
}) {
  if (isLoading) {
    return (
        <section aria-label="Management focus" className="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
          {Array.from({length: 4}).map((_, index) => (
              <Skeleton className="h-32" key={index}/>
          ))}
        </section>
    );
  }
 
  const latestRevenue = revenue?.at(-1);
  const focusCards = [
    {
      caption: `${formatCurrency(latestRevenue?.newBusiness ?? 0, true)} new business this month`,
      href: "/revenue",
      icon: BarChart3,
      label: "Revenue",
      value: formatCurrency(latestRevenue?.revenue ?? 0, true),
    },
    {
      caption: `${formatNumber(customers?.summary.atRisk ?? 0)} accounts need attention`,
      href: "/customers",
      icon: UsersRound,
      label: "Customers",
      value: formatNumber(customers?.total ?? 0),
    },
    {
      caption: `${formatNumber(delivery?.summary.blockerCount ?? 0)} blockers before release`,
      href: "/delivery",
      icon: ListChecks,
      label: "Delivery",
      value: formatPercent((delivery?.summary.completionRate ?? 0) / 100),
    },
    {
      caption: `${formatNumber(customers?.summary.trial ?? 0)} trials in conversion window`,
      href: "/health",
      icon: ShieldCheck,
      label: "Health",
      value: `${formatNumber(customers?.summary.averageHealth ?? 0)}/100`,
    },
  ];
 
  return (
      <section aria-label="Management focus" className="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        {focusCards.map((card) => (
            <Link
                className={cn(
                    "group flex min-h-32 flex-col justify-between p-4 transition-colors hover:border-cyan-300 hover:bg-cyan-50/60 dark:hover:border-cyan-800 dark:hover:bg-cyan-950/20",
                    uiStyles.surface,
                )}
                href={card.href}
                key={card.label}
            >
          <span className="flex items-center justify-between gap-3">
            <span className="flex items-center gap-2 text-sm font-medium text-slate-500 dark:text-slate-400">
              <card.icon aria-hidden="true" size={16}/>
              {card.label}
            </span>
            <ArrowRight
                aria-hidden="true"
                className="text-slate-400 transition-transform group-hover:translate-x-0.5 group-hover:text-cyan-700 dark:group-hover:text-cyan-300"
                size={16}
            />
          </span>
              <span>
            <span className="block text-2xl font-semibold text-slate-950 dark:text-white">
              {card.value}
            </span>
            <span className="mt-1 block text-sm text-slate-500 dark:text-slate-400">
              {card.caption}
            </span>
          </span>
            </Link>
        ))}
      </section>
  );
}
 
/** Executive overview route that coordinates summary queries and cross-page refreshes. */
export function DashboardClient() {
  const metricsQuery = useQuery({
    queryFn: fetchMetrics,
    queryKey: ["metrics"],
  });
 
  const revenueQuery = useQuery({
    queryFn: fetchRevenue,
    queryKey: ["revenue"],
  });
 
  const deliveryQuery = useQuery({
    queryFn: fetchDelivery,
    queryKey: ["delivery"],
  });
 
  const customersQuery = useQuery({
    queryFn: () => fetchCustomers(overviewCustomerFilters),
    queryKey: ["customers", overviewCustomerFilters],
  });
 
  const isRefreshing =
      metricsQuery.isFetching ||
      revenueQuery.isFetching ||
      deliveryQuery.isFetching ||
      customersQuery.isFetching;
 
  const refreshDashboard = () => {
    void metricsQuery.refetch();
    void revenueQuery.refetch();
    void deliveryQuery.refetch();
    void customersQuery.refetch();
  };
 
  return (
      <DashboardShell
          activeSection="overview"
          description="Scan revenue, customer health, delivery risk, and operating movement before opening a focused view."
          eyebrow="Executive overview"
          isRefreshing={isRefreshing}
          onRefresh={refreshDashboard}
          title="SaaS Analytics Dashboard"
      >
        <section
            aria-busy={metricsQuery.isLoading || metricsQuery.isFetching}
            aria-label="Executive metrics"
            aria-live="polite"
            className="grid gap-4 md:grid-cols-2 xl:grid-cols-4"
        >
          {metricsQuery.isLoading
              ? Array.from({length: 4}).map((_, index) => (
                  <Skeleton className="h-40" key={index}/>
              ))
              : metricsQuery.data?.data.map((metric) => (
                  <KpiCard key={metric.id} metric={metric}/>
              ))}
        </section>
 
        {metricsQuery.isError ? (
            <div
                role="alert"
                className="rounded-lg border border-rose-200 bg-rose-50 p-4 text-sm text-rose-900 dark:border-rose-900/70 dark:bg-rose-950/20 dark:text-rose-100">
              Metrics failed to load. The rest of the dashboard remains usable.
            </div>
        ) : null}
 
        <OverviewFocusPanel
            customers={customersQuery.data}
            delivery={deliveryQuery.data?.data}
            isLoading={customersQuery.isLoading || deliveryQuery.isLoading || revenueQuery.isLoading}
            revenue={revenueQuery.data?.data}
        />
 
        <div className="grid gap-4 xl:grid-cols-[minmax(0,1fr)_360px]">
          <RevenueChart
              data={revenueQuery.data?.data ?? []}
              isError={revenueQuery.isError}
              isLoading={revenueQuery.isLoading}
              onRetry={() => void revenueQuery.refetch()}
          />
          <OpsSummary
              customers={customersQuery.data}
              isLoading={customersQuery.isLoading || revenueQuery.isLoading}
              revenue={revenueQuery.data?.data}
          />
        </div>
      </DashboardShell>
  );
}