"use client"; import Link from "next/link"; import { useEffect, useState } from "react"; import { MetricsPanel } from "../../components/MetricsPanel"; import { NewsList } from "../../components/NewsList"; import { PredictionPanel } from "../../components/PredictionPanel"; import { StockChart } from "../../components/StockChart"; import { api, type ChartInterval, type ChartPayload, type LatestPredictionResponse, } from "../../lib/api"; const INTERVALS: { label: string; value: ChartInterval; defaultDays: number }[] = [ { label: "10분", value: "10m", defaultDays: 1 }, { label: "일", value: "1d", defaultDays: 180 }, { label: "주", value: "1w", defaultDays: 365 * 2 }, { label: "월", value: "1mo", defaultDays: 365 * 5 }, ]; export default function CodePage({ params }: { params: { code: string } }) { const { code } = params; const [chart, setChart] = useState(null); const [prediction, setPrediction] = useState(null); const [err, setErr] = useState(null); const [interval, setIntervalKind] = useState("1d"); const [days, setDays] = useState(180); // interval 바꾸면 days 도 그 interval 에 맞는 기본값으로 (사용자가 명시적으로 다시 고를 수 있게). function pickInterval(v: ChartInterval) { const meta = INTERVALS.find((i) => i.value === v)!; setIntervalKind(v); setDays(meta.defaultDays); } // 초기/주기적 차트 로드. 10분봉이면 60초마다 폴링 — 백엔드가 캐시-then-fetch 로 // 10분 이내면 DB 만 읽고, 넘었으면 KIS 호출. 폴링 부담은 낮음. useEffect(() => { let alive = true; setErr(null); setChart(null); const load = () => { api .getChart(code, days, interval) .then((c) => { if (alive) setChart(c); }) .catch((e) => { if (alive) setErr(e instanceof Error ? e.message : String(e)); }); }; load(); if (interval === "10m") { const h = window.setInterval(load, 60_000); return () => { alive = false; window.clearInterval(h); }; } return () => { alive = false; }; }, [code, days, interval]); useEffect(() => { let alive = true; api .latestPrediction(code) .then((r) => { if (alive && r.found) setPrediction(r); }) .catch(() => { // 예측 이력 없는 경우는 무시. }); return () => { alive = false; }; }, [code]); return (
← 검색으로
{INTERVALS.map((it) => ( ))}
{interval !== "10m" && ( )}
{chart && (

{chart.name}{" "} {chart.code} · {chart.market}

{interval === "10m" && (
실시간 10분봉 · 60초마다 갱신 {chart.intraday_status && ( [{chart.intraday_status}] )}
)}
)} {err &&
차트 로딩 실패: {err}
} {chart && ( <>
)}
); }