summaryrefslogtreecommitdiff
path: root/frontend/components/prism/RatiosPage.tsx
blob: 26868f8c0eb1c67ba705115e08209741166758e8 (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
"use client";

import { useEffect, useState } from "react";
import { api } from "@/lib/api";
import { RatiosCard } from "@/components/prism/RatiosCard";
import type { RatiosResponse } from "@/types/api";

type RatiosState = "loading" | "ready" | "error";

type Props = {
  ticker: string;
};

export function RatiosPage({ ticker }: Props) {
  const [data, setData] = useState<RatiosResponse | null>(null);
  const [ratiosState, setRatiosState] = useState<RatiosState>("loading");

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

    api
      .ratios(ticker)
      .then((res) => {
        if (!cancelled) {
          setData(res);
          setRatiosState("ready");
        }
      })
      .catch(() => {
        if (!cancelled) setRatiosState("error");
      });

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

  if (ratiosState === "loading") {
    return <section className="psm-card psm-skeleton" style={{ minHeight: 320 }} />;
  }

  if (ratiosState === "error") {
    return (
      <section className="psm-card">
        <p className="psm-muted-copy">Ratio data unavailable for {ticker}.</p>
      </section>
    );
  }

  if (ratiosState === "ready" && data) {
    return <RatiosCard data={data} />;
  }

  return null;
}