summaryrefslogtreecommitdiff
path: root/frontend/components/prism/FinancialsCard.tsx
blob: 43a2dc2799f5eca531c80c09d5e99321ed69dfa2 (plain)
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
"use client";
import type { FinancialRow, FinancialsResponse } from "@/types/api";
import { fmtLarge } from "@/lib/format";

type StatementKey = "income" | "balance" | "cash_flow";
type PeriodKey = "annual" | "quarterly";

type Props = {
  data: FinancialsResponse;
  statement: StatementKey;
  period: PeriodKey;
  onChangePeriod: (p: PeriodKey) => void;
};

function fmtFinVal(val: number | null | undefined, isMargin: boolean): string {
  if (val === null || val === undefined) return "—";
  if (isMargin) return `${(val * 100).toFixed(1)}%`;
  return fmtLarge(val);
}

function FinRow({ row, lastColIdx }: { row: FinancialRow; lastColIdx: number }) {
  if (row.is_section) {
    return (
      <tr className="psm-fin-section-row">
        <td className="psm-fin-section-label" colSpan={lastColIdx + 2}>
          {row.label}
        </td>
      </tr>
    );
  }

  const cls = [
    "psm-fin-row",
    row.is_total ? "is-total" : "",
    row.is_margin ? "is-margin" : "",
    row.indent === 1 ? "is-indent" : "",
  ]
    .filter(Boolean)
    .join(" ");

  return (
    <tr className={cls}>
      <td className="psm-fin-label">{row.label}</td>
      {row.values.map((val, i) => (
        <td
          key={i}
          className={[
            "psm-fin-val",
            i === lastColIdx ? "accent" : "",
            val !== null && val < 0 && !row.is_margin ? "neg" : "",
          ]
            .filter(Boolean)
            .join(" ")}
        >
          {fmtFinVal(val, row.is_margin)}
        </td>
      ))}
    </tr>
  );
}

export function FinancialsCard({
  data,
  statement,
  period,
  onChangePeriod,
}: Props) {
  const stmt = data[statement];
  const lastColIdx = stmt.columns.length - 1;

  return (
    <section className="psm-card psm-financials-card">
      <div className="psm-fin-header">
        <div className="psm-fin-period">
          {(["annual", "quarterly"] as PeriodKey[]).map((p) => (
            <button
              key={p}
              type="button"
              className={`psm-fin-period-btn${period === p ? " active" : ""}`}
              onClick={() => onChangePeriod(p)}
            >
              {p === "annual" ? "ANNUAL" : "QUARTERLY"}
            </button>
          ))}
        </div>
      </div>

      {stmt.columns.length === 0 ? (
        <p className="psm-muted-copy psm-fin-empty">Statement data unavailable.</p>
      ) : (
        <div className="psm-fin-table-wrap">
          <table className="psm-fin-table">
            <thead>
              <tr>
                <th className="psm-fin-label-col">USD (millions)</th>
                {stmt.columns.map((col, i) => (
                  <th
                    key={i}
                    className={`psm-fin-val-col${i === lastColIdx ? " accent" : ""}`}
                  >
                    {col}
                  </th>
                ))}
              </tr>
            </thead>
            <tbody>
              {stmt.rows.map((row, i) => (
                <FinRow key={i} row={row} lastColIdx={lastColIdx} />
              ))}
            </tbody>
          </table>
        </div>
      )}
    </section>
  );
}