feat(symbol-page): intraday short-term returns + hline storage-failure guard

- IntradayReturns: 10분/30분/1시간/3시간/시가대비 mini cards for 1D mode
  (PeriodReturns labels lie when interval is 10m; swap them out by isIntraday)
- addLine/removeLine/clearLines now return previous state when localStorage
  write fails — UI no longer diverges from storage in locked-storage env

addresses reviewer 318eae6 non-blocking caveat about write-failure divergence
This commit is contained in:
claude-owner
2026-05-29 00:35:21 +09:00
parent 318eae67df
commit d6fc9d8a0b
3 changed files with 95 additions and 4 deletions

View File

@@ -0,0 +1,82 @@
"use client";
// 1D (10분봉) 모드 전용 단기 수익률 미니 카드.
// PeriodReturns 는 거래일 단위 (1주/1개월/...) 계산이라 10m 데이터에선 라벨이 거짓이 됨
// (1주=5봉=50분). 그래서 intraday 일 때는 이 컴포넌트로 갈아끼움.
//
// 정의:
// - "현재가" = 마지막 유효 close (null 제외 후 가장 뒤). 폴링되면 자동 갱신.
// - 10분/30분/1시간/3시간 = 마지막에서 N봉 전 close 와 비교.
// - 시가대비 = 가장 첫 봉의 open(없으면 close) 과 비교 — 당일 손익률.
// - 데이터 부족하면 "—" 표시.
//
// 추가 백엔드 호출 없음 — chart.ohlcv 슬라이싱만.
import type { OhlcvPoint } from "../lib/api";
const BUCKETS: { label: string; bars: number }[] = [
{ label: "10분", bars: 1 },
{ label: "30분", bars: 3 },
{ label: "1시간", bars: 6 },
{ label: "3시간", bars: 18 },
];
export function IntradayReturns({ ohlcv }: { ohlcv: OhlcvPoint[] }) {
// close == null 인 휴장/결측 봉은 비교에서 제외.
const valid = ohlcv.filter((p): p is OhlcvPoint & { close: number } => p.close != null);
if (valid.length < 2) {
return (
<div className="rounded-md border border-zinc-800 bg-zinc-900/40 p-4 text-xs text-zinc-500">
</div>
);
}
const last = valid[valid.length - 1].close;
const buckets = BUCKETS.map((b) => {
const idx = valid.length - 1 - b.bars;
if (idx < 0) return { label: b.label, pct: null as number | null };
const base = valid[idx].close;
return { label: b.label, pct: base ? ((last - base) / base) * 100 : null };
});
// 시가대비 — 당일 첫 봉의 open 우선, 없으면 close.
const first = valid[0];
const openBase =
first.open != null && first.open > 0 ? first.open : first.close;
const todayPct =
openBase != null && openBase > 0 ? ((last - openBase) / openBase) * 100 : null;
return (
<div className="rounded-md border border-zinc-800 bg-zinc-900/40 p-4">
<div className="mb-3 flex items-baseline justify-between">
<div className="text-sm font-medium text-zinc-200"> </div>
<div className="text-[11px] text-zinc-500">10 · 60 </div>
</div>
<div className="grid grid-cols-3 gap-2 sm:grid-cols-5">
{buckets.map((b) => (
<Bucket key={b.label} label={b.label} pct={b.pct} />
))}
<Bucket label="시가대비" pct={todayPct} />
</div>
</div>
);
}
function Bucket({ label, pct }: { label: string; pct: number | null }) {
// 한국식 컬러 — 상승=rose, 하락=sky, 0/null=zinc.
const tone =
pct == null || pct === 0
? "text-zinc-400"
: pct > 0
? "text-rose-400"
: "text-sky-400";
return (
<div className="rounded-md border border-zinc-800 bg-zinc-900/30 px-2 py-2 text-center">
<div className="text-[11px] text-zinc-500">{label}</div>
<div className={`mt-1 text-sm font-medium tabular-nums ${tone}`}>
{pct == null ? "—" : `${pct >= 0 ? "+" : ""}${pct.toFixed(2)}%`}
</div>
</div>
);
}