Spaces:
Running

File size: 3,397 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { BanknoteIcon } from "lucide-react";

import { isPackMissing, OrdersTable } from "@/components/commerce/orders-view";
import { EmptyState, ErrorState, PageHeader, StatTile } from "@/components/shell/page";
import { Skeleton } from "@/components/ui/skeleton";
import { formatMoney, formatNumber, formatPct } from "@/lib/format";
import { useI18n } from "@/lib/i18n";
import type { CodSummary, RiskBand } from "@/lib/types";
import { useApi } from "@/lib/use-api";
import { cn } from "@/lib/utils";

const BAND_BAR: Record<RiskBand, string> = {
  low: "bg-success",
  medium: "bg-warning",
  high: "bg-destructive",
};

export function CodView() {
  const { t, m, locale } = useI18n();
  const s = useApi<CodSummary>("/packs/ecommerce/cod/summary", { pollMs: 30_000 });

  let body;
  if (s.error && isPackMissing(s.error)) {
    body = <EmptyState icon={BanknoteIcon} title={t("common.notAvailable")} hint={t("cod.unavailable")} />;
  } else if (s.error && !s.data) {
    body = <ErrorState error={s.error} onRetry={s.reload} />;
  } else if (!s.data) {
    body = <Skeleton className="h-40 w-full" />;
  } else {
    const d = s.data;
    const bandTotal = d.by_band.low + d.by_band.medium + d.by_band.high;
    body = (
      <div className="space-y-5">

        <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4" data-testid="cod-summary">

          <StatTile label={t("cod.pending")} value={formatNumber(d.pending_confirmation)} />

          <StatTile label={t("cod.confirmed")} value={formatNumber(d.confirmed)} />

          <StatTile label={t("cod.rto")} value={formatPct(d.rto_rate, 1)} />

          <StatTile label={t("cod.outstanding")} value={formatMoney(d.cod_outstanding, locale)} />

        </div>

        <section className="rounded-lg border bg-card p-4">

          <h2 className="mb-3 text-sm font-semibold">{t("cod.byBand")}</h2>

          <div className="flex h-3 overflow-hidden rounded-full bg-muted" role="img" aria-label={t("cod.byBand")}>

            {(["low", "medium", "high"] as const).map((b) =>

              d.by_band[b] ? (

                <div

                  key={b}

                  className={cn("h-full", BAND_BAR[b])}

                  style={{ width: `${(d.by_band[b] / Math.max(1, bandTotal)) * 100}%` }}

                />

              ) : null,

            )}

          </div>

          <dl className="mt-3 flex flex-wrap gap-6 text-sm">

            {(["low", "medium", "high"] as const).map((b) => (

              <div key={b} className="flex items-center gap-2">

                <span className={cn("size-2 rounded-full", BAND_BAR[b])} aria-hidden />

                <dt className="text-muted-foreground">{m.orders.bands[b]}</dt>

                <dd className="tabular font-medium">{formatNumber(d.by_band[b])}</dd>

              </div>

            ))}

          </dl>

        </section>

        <section className="space-y-2">

          <h2 className="text-sm font-semibold">{t("cod.queue")}</h2>

          <OrdersTable

            testId="cod-queue"

            query={{ payment_method: "cod", status: "pending", limit: 50, offset: 0 }}

          />

        </section>

      </div>
    );
  }
  return (
    <div className="space-y-5">

      <PageHeader title={t("cod.title")} subtitle={t("cod.subtitle")} />

      {body}

    </div>
  );
}