Files
stock_chart_site/web/components/MarketsOverview.tsx
claude 21db09f65f feat(markets): /api/markets/movers + /related/{code} + 홈/종목 페이지 연동
backend:
  - 신규 라우터 app/api/markets.py
  - GET /api/markets/movers?market=ALL|KOSPI|KOSDAQ&limit=10
    → gainers / losers / by_volume 3 카테고리. ohlcv_daily 최신 2 거래일 비교.
  - GET /api/markets/related/{code}?limit=8
    → 같은 market 내 거래량 상위 (sector 컬럼은 시드 단계 NULL 다수라 제외).
  - main.py 에 라우터 include.

frontend:
  - lib/api.ts 에 Mover/MoversResponse/RelatedResponse 타입 + .movers/.related 추가.
  - components/MarketsOverview.tsx: 등락 상위 / 등락 하위 / 거래량 상위 3 컬럼.
  - components/RelatedStocks.tsx: 같은 시장 내 관련 종목 8 개.
  - 홈 페이지에 MarketsOverview 배치 (SeedTiles 아래).
  - 종목 페이지 우측 사이드바 컬럼에 RelatedStocks 추가 (SymbolSidebar 아래).

verify: py_compile clean, tsc --noEmit clean, next lint clean.
2026-05-28 01:36:13 +09:00

148 lines
4.1 KiB
TypeScript

"use client";
// 시장 overview: 등락 상위 / 등락 하위 / 거래량 상위 3 컬럼.
// /api/markets/movers 응답을 그대로 표 형태로 렌더.
import Link from "next/link";
import { useEffect, useState } from "react";
import { api, type Mover, type MoversResponse } from "../lib/api";
export function MarketsOverview() {
const [data, setData] = useState<MoversResponse | null>(null);
const [err, setErr] = useState<string | null>(null);
useEffect(() => {
let alive = true;
api
.movers("ALL", 10)
.then((r) => {
if (alive) setData(r);
})
.catch((e) => {
if (alive) setErr(e instanceof Error ? e.message : String(e));
});
return () => {
alive = false;
};
}, []);
if (err)
return (
<div className="rounded-lg border border-zinc-800 bg-zinc-900/30 p-4 text-xs text-zinc-500">
overview : {err}
</div>
);
if (!data)
return (
<div className="rounded-lg border border-zinc-800 bg-zinc-900/30 p-4 text-xs text-zinc-500">
overview
</div>
);
// 아직 ohlcv_daily 가 비어 있으면 모든 리스트가 빈 배열로 옴 — 안내만.
const empty =
!data.gainers.length && !data.losers.length && !data.by_volume.length;
if (empty)
return (
<div className="rounded-lg border border-zinc-800 bg-zinc-900/30 p-4 text-xs text-zinc-500">
. ohlcv_daily .
</div>
);
return (
<div className="grid gap-3 md:grid-cols-3">
<MoverList title="등락 상위" items={data.gainers} />
<MoverList title="등락 하위" items={data.losers} />
<MoverList title="거래량 상위" items={data.by_volume} showVolume />
</div>
);
}
function MoverList({
title,
items,
showVolume = false,
}: {
title: string;
items: Mover[];
showVolume?: boolean;
}) {
return (
<div className="rounded-lg border border-zinc-800 bg-zinc-900/30 p-3">
<div className="mb-2 text-xs font-medium text-zinc-300">{title}</div>
{items.length === 0 ? (
<div className="text-[11px] text-zinc-500"> </div>
) : (
<ul className="divide-y divide-zinc-800">
{items.map((m, i) => (
<MoverRow key={m.code} rank={i + 1} m={m} showVolume={showVolume} />
))}
</ul>
)}
</div>
);
}
function MoverRow({
rank,
m,
showVolume,
}: {
rank: number;
m: Mover;
showVolume: boolean;
}) {
const pct = m.pct_change;
const tone =
pct == null || pct === 0
? "text-zinc-400"
: pct > 0
? "text-rose-400"
: "text-sky-400";
return (
<li>
<Link
href={`/${m.code}`}
className="flex items-center justify-between gap-2 py-1.5 text-xs hover:bg-zinc-800/40"
>
<div className="flex min-w-0 items-center gap-2">
<span className="w-4 shrink-0 text-zinc-500">{rank}</span>
<span className="min-w-0 truncate text-zinc-100">{m.name}</span>
</div>
<div className="shrink-0 text-right tabular-nums">
{showVolume ? (
<>
<div className="text-zinc-100">{fmtVol(m.volume)}</div>
<div className={`text-[10px] ${tone}`}>
{pct != null
? `${pct >= 0 ? "+" : ""}${pct.toFixed(2)}%`
: ""}
</div>
</>
) : (
<>
<div className="text-zinc-100">
{m.close != null
? m.close.toLocaleString(undefined, { maximumFractionDigits: 0 })
: "—"}
</div>
<div className={`text-[10px] ${tone}`}>
{pct != null
? `${pct >= 0 ? "+" : ""}${pct.toFixed(2)}%`
: ""}
</div>
</>
)}
</div>
</Link>
</li>
);
}
function fmtVol(n: number | null): string {
if (n == null) return "—";
if (n >= 1e8) return `${(n / 1e8).toFixed(1)}`;
if (n >= 1e4) return `${(n / 1e4).toFixed(1)}`;
return n.toLocaleString();
}