page.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import '../style.scss';
  2. import '@/app/styles/data-table.scss';
  3. import { Metadata } from 'next';
  4. import Link from 'next/link';
  5. import MarketNav from '../_components/MarketNav';
  6. import MarketFilterTabs from '../_components/MarketFilterTabs';
  7. import FuturesTable from '../_components/FuturesTable';
  8. import OptionsTable from '../_components/OptionsTable';
  9. import Pager from '../_components/Pager';
  10. import { fetchMarketFutures, fetchMarketOptions } from '@/lib/api/market';
  11. import { FuturesKindName, OptionsKindName, MarketSession, MarketSessionName } from '@/types/market';
  12. import { formatNumber } from '@/lib/utils/stock';
  13. import { tradeDateLabel } from '@/lib/utils/market';
  14. export const metadata: Metadata = {
  15. title: '파생 (선물·옵션) — 개미투자',
  16. description: '국내 파생상품 시세 — 선물·옵션 현재가·정산가·미결제약정·거래량 (정규/야간 세션, 전일 종가 기준)'
  17. };
  18. const SECTION_TABS: { value: 'futures'|'options'; label: string }[] = [
  19. { value: 'futures', label: '선물' },
  20. { value: 'options', label: '옵션' }
  21. ];
  22. const KIND_TABS: { value: FuturesKindName&OptionsKindName; label: string }[] = [
  23. { value: 'General', label: '일반' },
  24. { value: 'StockKospi', label: '주식(코스피)' },
  25. { value: 'StockKosdaq', label: '주식(코스닥)' }
  26. ];
  27. // 세션 토글 (정규/야간) — API 로는 숫자 1/2 전송 (MarketSession)
  28. const SESSION_TABS: { value: MarketSessionName; label: string }[] = [
  29. { value: 'Regular', label: '정규' },
  30. { value: 'Night', label: '야간' }
  31. ];
  32. const PER_PAGE = 20;
  33. type Props = {
  34. searchParams: Promise<{
  35. section?: string;
  36. kind?: string;
  37. session?: string;
  38. page?: string;
  39. }>;
  40. };
  41. export default async function MarketDerivativesPage({ searchParams }: Props)
  42. {
  43. const query = await searchParams;
  44. const section: 'futures'|'options' = SECTION_TABS.some(c => c.value === query.section) ? (query.section as 'futures'|'options') : 'futures';
  45. const kind = KIND_TABS.some(c => c.value === query.kind) ? (query.kind as FuturesKindName&OptionsKindName) : 'General';
  46. const session: MarketSessionName = query.session === 'Night' ? 'Night' : 'Regular';
  47. const isNight = session === 'Night';
  48. const page = Math.max(Number(query.page) || 1, 1);
  49. const futuresRes = section === 'futures' ? await fetchMarketFutures({ kind, page, session: MarketSession[session] }) : null;
  50. const optionsRes = section === 'options' ? await fetchMarketOptions({ kind, page, session: MarketSession[session] }) : null;
  51. const res = section === 'futures' ? futuresRes! : optionsRes!;
  52. const data = res.success ? res.data : null;
  53. const tradeDate = section === 'futures' ? futuresRes?.data?.tradeDate : optionsRes?.data?.tradeDate;
  54. const sectionLabel = section === 'futures' ? '선물' : '옵션';
  55. return (
  56. <div className='market'>
  57. <div className='market__head'>
  58. <h1 className='market__title'>파생</h1>
  59. <span className='market__basis'>{tradeDateLabel(tradeDate)} · 선물/옵션{isNight ? ' · 야간 세션' : ''}</span>
  60. </div>
  61. <MarketNav />
  62. {/* 선물/옵션 섹션 탭 (kind 는 섹션 변경 시 초기화, 세션은 유지) */}
  63. <nav className='market__filters' aria-label='파생 구분'>
  64. <div className='market__filter-group' role='group'>
  65. <span className='market__filter-label'>구분</span>
  66. {SECTION_TABS.map(tab => (
  67. <Link
  68. key={tab.value}
  69. href={`/market/derivatives?section=${tab.value}${isNight ? '&session=Night' : ''}`}
  70. className={`market__tab${section === tab.value ? ' market__tab--active' : ''}`}
  71. {...(section === tab.value ? { 'aria-current': 'page' as const } : {})}
  72. >
  73. {tab.label}
  74. </Link>
  75. ))}
  76. </div>
  77. <MarketFilterTabs
  78. label='종류'
  79. paramKey='kind'
  80. tabs={KIND_TABS}
  81. active={kind}
  82. basePath='/market/derivatives'
  83. preserve={isNight ? { section, session } : { section }}
  84. />
  85. <MarketFilterTabs
  86. label='세션'
  87. paramKey='session'
  88. tabs={SESSION_TABS}
  89. active={session}
  90. basePath='/market/derivatives'
  91. preserve={{ section, kind }}
  92. />
  93. </nav>
  94. {!res.success ? (
  95. <div className='market__error' role='alert'>
  96. <strong>{sectionLabel} 목록을 불러오지 못했습니다.</strong>
  97. 잠시 후 다시 시도해 주세요.
  98. </div>
  99. ) : !data || data.list.length === 0 ? (
  100. <div className='market__empty'>
  101. {isNight
  102. ? `야간 세션 ${sectionLabel} 데이터가 없습니다. 야간(글로벌) 시세 수집 전이거나 휴장일 수 있습니다.`
  103. : `표시할 ${sectionLabel}이 없습니다.`}
  104. </div>
  105. ) : (
  106. <>
  107. <p className='market__total'>전체 {formatNumber(data.total)}종목</p>
  108. <div className='market__table-wrap'>
  109. {section === 'futures' ? (
  110. <FuturesTable rows={futuresRes!.data!.list} showSession={isNight} />
  111. ) : (
  112. <OptionsTable rows={optionsRes!.data!.list} showSession={isNight} />
  113. )}
  114. </div>
  115. <Pager
  116. total={data.total}
  117. page={page}
  118. perPage={PER_PAGE}
  119. basePath='/market/derivatives'
  120. query={isNight ? { section, kind, session } : { section, kind }}
  121. />
  122. </>
  123. )}
  124. </div>
  125. );
  126. }