1- import ChartFilterBar from "@/_MarketDetailPage/components/ChartFilterBar" ;
21import ReactECharts from "echarts-for-react" ;
3- import {
4- type ChartType ,
5- type Period ,
6- type PriceHistory ,
7- } from "@/_MarketDetailPage/types/stockDataType" ;
8- import { useState , useRef } from "react" ;
2+ import { type ChartType , type Period } from "@/_MarketDetailPage/types/stockDataType" ;
3+ import { useRef , useEffect , useMemo } from "react" ;
4+ import { useGetStockDetail } from "@/lib/hooks/useGetStockDetail" ;
5+ import { Spinner } from "@/components/ui/spinner" ;
96
107interface StockChartProps {
11- stockData : PriceHistory [ ] ;
8+ stockCode : string ;
9+ period : Period ;
10+ chartType : ChartType ;
1211}
1312
14- const StockChart = ( { stockData } : StockChartProps ) => {
15- const [ period , setPeriod ] = useState < Period > ( "1M" ) ;
16- const [ chartType , setChartType ] = useState < ChartType > ( "candlestick" ) ;
13+ const StockChart = ( { stockCode, period, chartType } : StockChartProps ) => {
1714 const isFirstRender = useRef ( true ) ;
1815
19- const handleChangePeriod = ( value : string ) => {
20- setPeriod ( value as Period ) ;
21- isFirstRender . current = false ;
22- } ;
16+ // period에 해당하는 API 요청
17+ const { data : stockData , isLoading } = useGetStockDetail ( stockCode , period ) ;
2318
24- const handleChangeChartType = ( value : string ) => {
25- setChartType ( value as ChartType ) ;
19+ useEffect ( ( ) => {
2620 isFirstRender . current = false ;
27- } ;
21+ } , [ chartType , period ] ) ;
2822
2923 const formatDate = ( dateString : string ) => {
30- const year = dateString . substring ( 0 , 4 ) ;
31- const month = dateString . substring ( 4 , 6 ) ;
32- const day = dateString . substring ( 6 , 8 ) ;
24+ const parts = dateString . split ( "-" ) ;
25+ const year = parts [ 0 ] ;
26+ const month = parts [ 1 ] ;
27+ const day = parts [ 2 ] ;
3328
3429 if ( period === "10Y" ) {
3530 return `${ year } .${ month } ` ;
@@ -38,15 +33,34 @@ const StockChart = ({ stockData }: StockChartProps) => {
3833 }
3934 } ;
4035
41- const dates = stockData . map ( ( item ) => formatDate ( item . baseDate ) ) ;
42- const candleData = stockData . map ( ( item ) => [
36+ // period에 따라 데이터 필터링 (useMemo는 early return 전에 호출)
37+ const filteredDataByPeriod = useMemo ( ( ) => {
38+ if ( ! stockData || ! stockData . stockPriceList ) {
39+ return [ ] ;
40+ }
41+
42+ // 모든 데이터를 역순으로 반환 (10년 데이터도 모두 렌더링)
43+ return [ ...stockData . stockPriceList ] . reverse ( ) ;
44+ } , [ stockData , period ] ) ;
45+
46+ // API 데이터가 없으면 로딩 표시
47+ if ( isLoading || ! stockData || ! stockData . stockPriceList || filteredDataByPeriod . length === 0 ) {
48+ return (
49+ < div className = "flex justify-center items-center h-500" >
50+ < Spinner className = "size-12" />
51+ </ div >
52+ ) ;
53+ }
54+
55+ const dates = filteredDataByPeriod . map ( ( item ) => formatDate ( item . baseDate ) ) ;
56+ const candleData = filteredDataByPeriod . map ( ( item ) => [
4357 item . openPrice ,
4458 item . closePrice ,
4559 item . lowPrice ,
4660 item . highPrice ,
4761 ] ) ;
48- const lows = stockData . map ( ( d ) => d . lowPrice ) ;
49- const highs = stockData . map ( ( d ) => d . highPrice ) ;
62+ const lows = filteredDataByPeriod . map ( ( d ) => d . lowPrice ) ;
63+ const highs = filteredDataByPeriod . map ( ( d ) => d . highPrice ) ;
5064
5165 const option = {
5266 animation : isFirstRender . current ,
@@ -99,7 +113,10 @@ const StockChart = ({ stockData }: StockChartProps) => {
99113 } ,
100114 series : {
101115 type : chartType === "candlestick" ? "candlestick" : "line" ,
102- data : chartType === "candlestick" ? candleData : stockData . map ( ( item ) => item . closePrice ) ,
116+ data :
117+ chartType === "candlestick"
118+ ? candleData
119+ : filteredDataByPeriod . map ( ( item ) => item . closePrice ) ,
103120 smooth : false ,
104121 itemStyle :
105122 chartType === "candlestick"
@@ -138,10 +155,6 @@ const StockChart = ({ stockData }: StockChartProps) => {
138155 } ;
139156 return (
140157 < div className = "flex-col gap-20 m-10 w-full" >
141- < ChartFilterBar
142- onChangePeriod = { handleChangePeriod }
143- onChangeChartType = { handleChangeChartType }
144- />
145158 < ReactECharts option = { option } style = { { height : 500 } } />
146159 </ div >
147160 ) ;
0 commit comments