summaryrefslogtreecommitdiff
path: root/frontend/components/prism/FinancialsPage.tsx
blob: fcd27636e70c9f935c23aa7cf298ca6fe626230f (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
"use client";
import { useEffect, useState } from "react";
import { api } from "@/lib/api";
import { buildKpis } from "@/lib/overview";
import { FinancialsCard } from "@/components/prism/FinancialsCard";
import { KPIStrip } from "@/components/prism/KPIStrip";
import { TickerHeader } from "@/components/prism/TickerHeader";
import type { FinancialsResponse, TickerOverview } from "@/types/api";

type StatementKey = "income" | "balance" | "cash_flow";
type PeriodKey = "annual" | "quarterly";
type FinState = "loading" | "ready" | "error";

type Props = {
  ticker: string;
  overview: TickerOverview;
  isSaved: boolean;
  onToggleWatchlist: () => void;
};

export function FinancialsPage({ ticker, overview, isSaved, onToggleWatchlist }: Props) {
  const [statement, setStatement] = useState<StatementKey>("income");
  const [period, setPeriod] = useState<PeriodKey>("annual");
  const [data, setData] = useState<FinancialsResponse | null>(null);
  const [finState, setFinState] = useState<FinState>("loading");
  const kpis = buildKpis(overview);

  useEffect(() => {
    let cancelled = false;
    setFinState("loading");
    setData(null);

    api
      .financials(ticker, period)
      .then((res) => {
        if (!cancelled) {
          setData(res);
          setFinState("ready");
        }
      })
      .catch(() => {
        if (!cancelled) setFinState("error");
      });

    return () => {
      cancelled = true;
    };
  }, [ticker, period]);

  return (
    <>
      <TickerHeader overview={overview} isSaved={isSaved} onToggleWatchlist={onToggleWatchlist} />
      <KPIStrip items={kpis} />
      {finState === "loading" && (
        <section className="psm-card psm-skeleton" style={{ minHeight: 320 }} />
      )}
      {finState === "error" && (
        <section className="psm-card">
          <p className="psm-muted-copy">Financial statements unavailable for {ticker}.</p>
        </section>
      )}
      {finState === "ready" && data && (
        <FinancialsCard
          data={data}
          statement={statement}
          period={period}
          onChangeStatement={setStatement}
          onChangePeriod={setPeriod}
        />
      )}
    </>
  );
}