주식 매매 프로그램 연동 및 페이지 개발 구체화

This commit is contained in:
2026-01-27 02:03:04 +09:00
parent 22897c3eb6
commit 9ab45b64b6
4 changed files with 394 additions and 412 deletions

View File

@@ -1,19 +1,18 @@
import React, { useEffect, useMemo, useState } from 'react';
import { Link } from 'react-router-dom';
import {
getStockHealth,
getStockIndices,
getStockNews,
triggerStockScrap,
} from '../../api';
import { getStockIndices, getStockNews } from '../../api';
import './Stock.css';
const formatDate = (value) => value ?? '-';
const formatDate = (value) => {
if (!value) return '-';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return date.toLocaleString('sv-SE');
};
const toDateValue = (value) => {
if (!value) return null;
const normalized = value.replace(' ', 'T').replace(/\./g, '-');
const date = new Date(normalized);
const date = new Date(value);
return Number.isNaN(date.getTime()) ? null : date;
};
@@ -26,36 +25,52 @@ const getLatestBy = (items, key) => {
return filtered[0]?.[key] ?? null;
};
const normalizeIndices = (data) => {
if (!data || typeof data !== 'object' || Array.isArray(data)) return [];
return Object.entries(data)
.filter(([, value]) => value && typeof value === 'object')
.map(([name, value]) => ({
name,
value: value?.value ?? '-',
change: value?.change ?? '',
percent: value?.percent ?? '',
}));
};
const getDirection = (change, percent) => {
const pick = (value) =>
value === undefined || value === null || value === '' ? null : value;
const raw = pick(change) ?? pick(percent);
if (!raw) return '';
const str = String(raw).trim();
if (str.startsWith('-')) return 'down';
if (str.startsWith('+')) return 'up';
const numeric = Number(str.replace(/[^0-9.-]/g, ''));
if (Number.isFinite(numeric)) {
if (numeric > 0) return 'up';
if (numeric < 0) return 'down';
}
return '';
};
const Stock = () => {
const [newsDomestic, setNewsDomestic] = useState([]);
const [newsOverseas, setNewsOverseas] = useState([]);
const [newsCategory, setNewsCategory] = useState('domestic');
const [limit, setLimit] = useState(20);
const [loading, setLoading] = useState(false);
const [scraping, setScraping] = useState(false);
const [error, setError] = useState('');
const [newsError, setNewsError] = useState('');
const [indicesError, setIndicesError] = useState('');
const [scrapMessage, setScrapMessage] = useState('');
const [health, setHealth] = useState({
status: 'unknown',
message: '',
});
const [indices, setIndices] = useState([]);
const [indicesLoading, setIndicesLoading] = useState(false);
const [indicesAt, setIndicesAt] = useState('');
const [autoRefreshMs] = useState(180000);
const combinedNews = useMemo(
() => [...newsDomestic, ...newsOverseas],
[newsDomestic, newsOverseas]
);
const latestCrawled = useMemo(
() => getLatestBy(combinedNews, 'crawled_at'),
[combinedNews]
);
const latestPublished = useMemo(
() => getLatestBy(combinedNews, 'pub_date'),
() => getLatestBy(combinedNews, 'published_at'),
[combinedNews]
);
@@ -76,32 +91,12 @@ const Stock = () => {
}
};
const loadHealth = async () => {
try {
const data = await getStockHealth();
setHealth({
status: data?.ok ? 'ok' : 'warn',
message: data?.message ?? '',
});
} catch (err) {
const rawMessage = err?.message ?? String(err);
const message = rawMessage.includes('404')
? '헬스 체크 엔드포인트가 아직 준비되지 않았습니다.'
: rawMessage;
setHealth({
status: 'unknown',
message,
});
}
};
const loadIndices = async () => {
setIndicesLoading(true);
setIndicesError('');
try {
const data = await getStockIndices();
setIndices(Array.isArray(data?.indices) ? data.indices : []);
setIndicesAt(data?.crawled_at ?? '');
setIndices(normalizeIndices(data));
} catch (err) {
setIndicesError(err?.message ?? String(err));
} finally {
@@ -109,46 +104,44 @@ const Stock = () => {
}
};
const onScrap = async () => {
setScraping(true);
setError('');
setScrapMessage('');
try {
const result = await triggerStockScrap();
if (!result?.ok) {
throw new Error('스크랩 요청이 실패했습니다.');
}
setScrapMessage('스크랩 요청 완료');
await loadNews();
} catch (err) {
setError(err?.message ?? String(err));
} finally {
setScraping(false);
}
};
useEffect(() => {
loadNews();
}, [limit]);
useEffect(() => {
loadHealth();
loadIndices();
const timer = window.setInterval(() => {
loadIndices();
}, autoRefreshMs);
const timer = window.setInterval(loadIndices, autoRefreshMs);
return () => window.clearInterval(timer);
}, [autoRefreshMs]);
const indexOrder = [
'KOSPI',
'KOSDAQ',
'KOSPI200',
'USD/KRW',
'NASDAQ',
'S&P500',
];
const sortedIndices = [...indices].sort((a, b) => {
const aIndex = indexOrder.indexOf(a.name);
const bIndex = indexOrder.indexOf(b.name);
if (aIndex !== -1 || bIndex !== -1) {
return (aIndex === -1 ? 999 : aIndex) - (bIndex === -1 ? 999 : bIndex);
}
return a.name.localeCompare(b.name);
});
const highlighted = new Set(['KOSPI', 'KOSDAQ', 'USD/KRW']);
const activeNews =
newsCategory === 'domestic' ? newsDomestic : newsOverseas;
return (
<div className="stock">
<header className="stock-header">
<div>
<p className="stock-kicker">Market Lab</p>
<h1>Stock Lab</h1>
<p className="stock-kicker">마켓 </p>
<h1>주식 </h1>
<p className="stock-sub">
매일 오전 8시에 트렌드 기사를 스크랩하고 요약해, 거래
빠르게 흐름을 파악할 있게 구성했습니다.
최신 시장 뉴스와 지수 흐름을 한눈에 확인하세요.
</p>
<div className="stock-actions">
<button
@@ -159,110 +152,76 @@ const Stock = () => {
뉴스 새로고침
</button>
<Link className="button ghost" to="/stock/trade">
잔고/주문 화면
거래 데스크
</Link>
<button
className="button ghost"
onClick={onScrap}
disabled={scraping}
>
{scraping ? '스크랩 중...' : '스크랩 즉시 실행'}
</button>
</div>
</div>
<div className="stock-card">
<p className="stock-card__title">오늘의 상태</p>
<p className="stock-card__title">뉴스 요약</p>
<div className="stock-status">
<div>
<span>Health</span>
<span className={`stock-pill is-${health.status}`}>
{health.status}
</span>
</div>
<div>
<span>최근 스크랩</span>
<strong>{formatDate(latestCrawled)}</strong>
</div>
<div>
<span>최근 발행</span>
<span>최신 발행</span>
<strong>{formatDate(latestPublished)}</strong>
</div>
<div>
<span>기사 </span>
<strong>{combinedNews.length}</strong>
<span>국내</span>
<strong>{newsDomestic.length}</strong>
</div>
<div>
<span>해외</span>
<strong>{newsOverseas.length}</strong>
</div>
</div>
{health.message ? (
<p className="stock-status__note">{health.message}</p>
) : null}
</div>
</header>
{error ? <p className="stock-error">{error}</p> : null}
{scrapMessage ? (
<p className="stock-success">{scrapMessage}</p>
) : null}
<section className="stock-grid">
<div className="stock-panel">
<div className="stock-panel__head">
<div>
<p className="stock-panel__eyebrow">Snapshot</p>
<h3>시장 스냅샷</h3>
<p className="stock-panel__eyebrow">스냅샷</p>
<h3>주요 지수</h3>
<p className="stock-panel__sub">
주요 표의 현재가와 등락을 빠르게 확인합니다.
주요 값과 등락을 함께 확인합니다.
</p>
</div>
<div className="stock-panel__actions">
{indicesLoading ? (
<span className="stock-chip">갱신 </span>
) : null}
{indicesAt ? (
<span className="stock-chip">{indicesAt}</span>
<span className="stock-chip">불러오는 </span>
) : null}
<button
className="button ghost small"
onClick={loadIndices}
disabled={indicesLoading}
>
지표 새로고침
새로고침
</button>
</div>
</div>
<div className="stock-snapshot">
{indicesError ? (
<p className="stock-empty">{indicesError}</p>
) : indices.length === 0 ? (
<p className="stock-empty">지표 데이터를 불러오지 못했습니다.</p>
) : sortedIndices.length === 0 ? (
<p className="stock-empty">
지수 데이터가 없습니다.
</p>
) : (
[...indices]
.sort((a, b) => {
const typeOrder = ['domestic', 'overseas'];
const order = {
domestic: ['KOSPI', 'KOSDAQ', 'KOSPI200'],
overseas: ['NASDAQ', 'NAS', 'S&P500'],
};
const aTypeIdx = typeOrder.indexOf(a.type);
const bTypeIdx = typeOrder.indexOf(b.type);
if (aTypeIdx !== bTypeIdx) {
return (
(aTypeIdx === -1 ? 99 : aTypeIdx) -
(bTypeIdx === -1 ? 99 : bTypeIdx)
);
}
const aOrder =
order[a.type]?.indexOf(a.name) ?? 999;
const bOrder =
order[b.type]?.indexOf(b.name) ?? 999;
return aOrder - bOrder;
})
.map((item) => (
sortedIndices.map((item) => {
const direction = getDirection(
item.change,
item.percent
);
const changeText = [
item.change,
item.percent,
]
.filter(Boolean)
.join(' ');
return (
<div
key={item.name}
className={`stock-snapshot__card ${
['KOSPI200', 'NASDAQ', 'NAS'].includes(
item.name?.toUpperCase?.() ?? ''
)
highlighted.has(item.name)
? 'is-highlight'
: ''
}`}
@@ -271,23 +230,18 @@ const Stock = () => {
<strong>{item.value ?? '--'}</strong>
<span
className={`stock-snapshot__change ${
item.direction === 'red'
direction === 'up'
? 'is-up'
: item.direction === 'blue'
: direction === 'down'
? 'is-down'
: ''
}`}
>
{item.direction === 'red'
? '▲'
: item.direction === 'blue'
? '▼'
: '■'}{' '}
{item.change_value ?? '--'}{' '}
{item.change_percent ?? ''}
{changeText || '--'}
</span>
</div>
))
);
})
)}
</div>
</div>
@@ -295,10 +249,10 @@ const Stock = () => {
<div className="stock-panel">
<div className="stock-panel__head">
<div>
<p className="stock-panel__eyebrow">Filter</p>
<p className="stock-panel__eyebrow">필터</p>
<h3>뉴스 필터</h3>
<p className="stock-panel__sub">
표시 개수를 조정 빠르게 훑어볼 있습니다.
표시 뉴스 개수를 조정니다.
</p>
</div>
</div>
@@ -313,13 +267,13 @@ const Stock = () => {
>
{[10, 20, 30, 40].map((value) => (
<option key={value} value={value}>
{value}
{value}
</option>
))}
</select>
</label>
<p className="stock-filter__note">
최신 기사부터 정렬됩니다.
최신 뉴스가 먼저 표시됩니다.
</p>
</div>
</div>
@@ -328,20 +282,20 @@ const Stock = () => {
<section className="stock-panel stock-panel--wide">
<div className="stock-panel__head">
<div>
<p className="stock-panel__eyebrow">Headlines</p>
<h3>트렌드 기사</h3>
<p className="stock-panel__eyebrow">헤드라인</p>
<h3>시장 뉴스</h3>
<p className="stock-panel__sub">
스크랩된 뉴스 요약을 바로 확인할 있습니다.
Stock Lab API에서 최신 뉴스를 불러옵니다.
</p>
</div>
<div className="stock-panel__actions">
{loading ? (
<span className="stock-chip">불러오는 </span>
) : null}
<span className="stock-chip">
국내 {newsDomestic.length} · 해외{' '}
{newsOverseas.length}
</span>
<span className="stock-chip">
국내 {newsDomestic.length} / 해외{' '}
{newsOverseas.length}
</span>
</div>
</div>
@@ -350,7 +304,7 @@ const Stock = () => {
) : newsError ? (
<p className="stock-empty">{newsError}</p>
) : combinedNews.length === 0 ? (
<p className="stock-empty">표시할 뉴스가 없습니다.</p>
<p className="stock-empty">뉴스가 없습니다.</p>
) : (
<>
<div className="stock-tabs">
@@ -363,7 +317,7 @@ const Stock = () => {
}`}
onClick={() => setNewsCategory('domestic')}
>
국내 뉴스
국내
</button>
<button
type="button"
@@ -374,33 +328,34 @@ const Stock = () => {
}`}
onClick={() => setNewsCategory('overseas')}
>
해외 뉴스
해외
</button>
</div>
{newsCategory === 'overseas' &&
newsOverseas.length === 0 ? (
<p className="stock-empty">해외 뉴스 </p>
{activeNews.length === 0 ? (
<p className="stock-empty">
해당 카테고리 뉴스 습니다.
</p>
) : (
<div className="stock-news">
{(newsCategory === 'domestic'
? newsDomestic
: newsOverseas
).map((item) => (
{activeNews.map((item) => (
<article
key={item.id}
key={item.id ?? item.link}
className="stock-news__item"
>
<div>
<p className="stock-news__title">
{item.title}
</p>
<p className="stock-news__summary">
{item.summary}
</p>
</div>
<div className="stock-news__meta">
<span>{item.press}</span>
<span>{item.pub_date}</span>
<span>
{formatDate(item.published_at)}
</span>
{item.sentiment ? (
<span className="stock-chip">
{item.sentiment}
</span>
) : null}
<a
href={item.link}
target="_blank"