summaryrefslogtreecommitdiff
path: root/frontend/components/prism/RatiosCard.tsx
blob: 1a008299b0a089d6140fd0244f17094ead98c4d3 (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
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
210
211
212
"use client";

import type { RatioPoint, RatiosResponse } from "@/types/api";
import { fmtNumber, fmtPct } from "@/lib/format";

const BRASS = "#C2AA7A";
const GAIN = "#4F8C5E";

type Props = {
  data: RatiosResponse;
};

type ValueKind = "multiple" | "percent" | "coverage";

function buildLine(values: (number | null)[], width: number, height: number): string {
  const numeric = values.filter((value): value is number => value != null && Number.isFinite(value));
  if (numeric.length === 0) return "";
  if (numeric.length === 1) {
    const y = height / 2;
    return `0,${y} ${width},${y}`;
  }

  const min = Math.min(...numeric);
  const max = Math.max(...numeric);
  const range = max - min || 1;

  return numeric
    .map((value, index) => {
      const x = (index / (numeric.length - 1)) * width;
      const y = max === min ? height / 2 : height - ((value - min) / range) * height;
      return `${x.toFixed(1)},${y.toFixed(1)}`;
    })
    .join(" ");
}

function fmtMultiple(value?: number | null): string {
  if (value == null || Number.isNaN(value)) return "—";
  return `${fmtNumber(value)}x`;
}

function fmtCoverage(value?: number | null): string {
  if (value == null || Number.isNaN(value)) return "—";
  return `${fmtNumber(value)}x`;
}

function formatValue(value: number | null, kind: ValueKind): string {
  if (kind === "percent") return fmtPct(value);
  if (kind === "coverage") return fmtCoverage(value);
  return fmtMultiple(value);
}

function MiniSpark({ values, color }: { values: (number | null)[]; color: string }) {
  const points = buildLine(values, 88, 24);
  if (!points) {
    return <span className="psm-ratio-spark-empty">—</span>;
  }

  return (
    <svg className="psm-ratio-mini-spark" viewBox="0 0 88 24" aria-hidden="true">
      <polyline
        points={points}
        fill="none"
        stroke={color}
        strokeWidth="1.8"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
}

function HeroSpark({ values, color }: { values: (number | null)[]; color: string }) {
  const points = buildLine(values, 196, 52);
  if (!points) {
    return <span className="psm-ratio-spark-empty">No trend</span>;
  }

  return (
    <svg className="psm-ratio-hero-spark" viewBox="0 0 196 52" aria-hidden="true">
      <polyline
        points={points}
        fill="none"
        stroke={color}
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
}

function HeroCard({
  label,
  point,
  kind,
  color,
}: {
  label: string;
  point: RatioPoint;
  kind: ValueKind;
  color: string;
}) {
  return (
    <article className="psm-ratio-hero">
      <div className="psm-ratio-hero-head">
        <span className="psm-ratio-hero-label">{label}</span>
        <span className="psm-ratio-hero-sector">
          Sector {formatValue(point.vs_sector, kind)}
        </span>
      </div>
      <div className="psm-ratio-hero-value" style={{ color }}>
        {formatValue(point.value, kind)}
      </div>
      <HeroSpark values={point.spark} color={color} />
    </article>
  );
}

function DetailRow({
  label,
  point,
  kind,
  color,
}: {
  label: string;
  point: RatioPoint;
  kind: ValueKind;
  color: string;
}) {
  return (
    <div className="psm-ratio-row">
      <span className="psm-ratio-row-label">{label}</span>
      <span className="psm-ratio-row-value">{formatValue(point.value, kind)}</span>
      <span className="psm-ratio-row-sector">{formatValue(point.vs_sector, kind)}</span>
      <MiniSpark values={point.spark} color={color} />
    </div>
  );
}

function GroupHeader({ label }: { label: string }) {
  return (
    <div className="psm-ratio-group-label">
      <span>{label}</span>
      <span>Current</span>
      <span>Sector</span>
      <span>Trend</span>
    </div>
  );
}

export function RatiosCard({ data }: Props) {
  const showDividends = data.dividend_yield.value != null || data.dividend_payout.value != null;

  return (
    <section className="psm-card psm-ratio-card">
      <div className="psm-card-head">
        <div>
          <div className="psm-eyebrow">Key Ratios</div>
          <h2 className="psm-card-title">Key Ratios</h2>
        </div>
      </div>

      <div className="psm-ratio-heroes">
        <HeroCard label="P / E TTM" point={data.pe_ttm} kind="multiple" color={BRASS} />
        <HeroCard label="EV / EBITDA" point={data.ev_ebitda} kind="multiple" color={BRASS} />
        <HeroCard label="Gross Margin" point={data.gross_margin} kind="percent" color={GAIN} />
        <HeroCard label="Net Margin" point={data.net_margin} kind="percent" color={GAIN} />
      </div>

      <div className="psm-ratio-detail">
        <section>
          <GroupHeader label="Valuation" />
          <DetailRow label="P / Book" point={data.price_to_book} kind="multiple" color={BRASS} />
          <DetailRow label="P / Sales" point={data.price_to_sales} kind="multiple" color={BRASS} />
          <DetailRow label="EV / Sales" point={data.ev_to_sales} kind="multiple" color={BRASS} />
          <DetailRow label="P / FCF" point={data.p_fcf} kind="multiple" color={BRASS} />
          <DetailRow label="Forward P / E" point={data.forward_pe} kind="multiple" color={BRASS} />
        </section>

        <section>
          <GroupHeader label="Profitability" />
          <DetailRow label="Operating Margin" point={data.operating_margin} kind="percent" color={GAIN} />
          <DetailRow label="EBITDA Margin" point={data.ebitda_margin} kind="percent" color={GAIN} />
          <DetailRow label="FCF Margin" point={data.fcf_margin} kind="percent" color={GAIN} />
        </section>

        <section>
          <GroupHeader label="Returns" />
          <DetailRow label="ROE" point={data.roe} kind="percent" color={GAIN} />
          <DetailRow label="ROA" point={data.roa} kind="percent" color={GAIN} />
          <DetailRow label="ROIC" point={data.roic} kind="percent" color={GAIN} />
        </section>

        <section>
          <GroupHeader label="Leverage / Liquidity" />
          <DetailRow label="Debt / Equity" point={data.debt_to_equity} kind="multiple" color={BRASS} />
          <DetailRow label="Current Ratio" point={data.current_ratio} kind="multiple" color={BRASS} />
          <DetailRow label="Quick Ratio" point={data.quick_ratio} kind="multiple" color={BRASS} />
          <DetailRow label="Interest Coverage" point={data.interest_coverage} kind="coverage" color={BRASS} />
        </section>

        {showDividends && (
          <section>
            <GroupHeader label="Dividends" />
            <DetailRow label="Dividend Yield" point={data.dividend_yield} kind="percent" color={GAIN} />
            <DetailRow label="Payout Ratio" point={data.dividend_payout} kind="percent" color={GAIN} />
          </section>
        )}
      </div>
    </section>
  );
}