@@ -21,29 +21,24 @@ import Series from './Series';
2121import ToolTip from './ToolTip' ;
2222
2323import api from '../../api/tradeview.endpoints' ;
24- import { Periods } from '../../const/chart.const' ;
24+ import { Intervals } from '../../const/chart.const' ;
2525import usePastTimeData from '../../hooks/usePastTimeData' ;
2626import useRealTimeData from '../../hooks/useRealTimeData' ;
2727import { extractCandlestickData , timestampToISOString } from '../../utils' ;
28- import PeriodSelector from '../PeriodSelector ' ;
28+ import IntervalSelector from '../IntervalSelector ' ;
2929
3030type ChartProps = {
3131 ticker ?: string ;
32- interval ?: number ;
3332 count ?: number ;
3433} ;
3534
36- export default function Chart ( {
37- ticker = 'BTC' ,
38- interval = 1 ,
39- count = 10 ,
40- } : ChartProps ) {
41- const [ selectedPeriod , setSelectedPeriod ] = useState ( 1 ) ;
35+ export default function Chart ( { ticker = 'BTC' , count = 30 } : ChartProps ) {
36+ const [ selectedInterval , setSelectedInterval ] = useState ( 1 ) ;
4237 const chartRef = useRef < IChartApi > ( null ) ;
4338 const seriesRef = useRef < ISeriesApi < 'Candlestick' > > ( null ) ;
4439 const [ isChartReady , setIsChartReady ] = useState ( false ) ;
4540 const realTimeData = useRealTimeData ( ticker ) ;
46- const pastTimeData = usePastTimeData ( ticker , interval , count ) ;
41+ const pastTimeData = usePastTimeData ( ticker , selectedInterval , count ) ;
4742
4843 const chartOption : DeepPartial < TimeChartOptions > = useMemo ( ( ) => {
4944 return {
@@ -99,7 +94,7 @@ export default function Chart({
9994
10095 const response = await api . getPastData (
10196 ticker ,
102- interval ,
97+ selectedInterval ,
10398 count ,
10499 firstDate ,
105100 ) ;
@@ -123,7 +118,7 @@ export default function Chart({
123118 ?. timeScale ( )
124119 . unsubscribeVisibleLogicalRangeChange ( handleVisibleRangeChange ) ;
125120 } ;
126- } , [ isChartReady , count , interval , ticker ] ) ;
121+ } , [ isChartReady , count , selectedInterval , ticker ] ) ;
127122
128123 useLayoutEffect ( ( ) => {
129124 if ( ! realTimeData || ! realTimeData . time ) return ;
@@ -136,26 +131,26 @@ export default function Chart({
136131
137132 const timeDiff = + realTimeData . time - + latestTime . time ;
138133
139- if ( timeDiff < 60 * interval ) {
134+ if ( timeDiff < 60 * selectedInterval ) {
140135 seriesRef . current ?. update ( { ...realTimeData , time : latestTime . time } ) ;
141136 } else {
142137 seriesRef . current ?. update ( {
143138 ...realTimeData ,
144- time : ( + latestTime . time + 60 * interval ) as Time ,
139+ time : ( + latestTime . time + 60 * selectedInterval ) as Time ,
145140 } ) ;
146141 }
147- } , [ realTimeData , interval ] ) ;
142+ } , [ realTimeData , selectedInterval ] ) ;
148143
149- const handleSelectPeriod = ( e : MouseEvent < HTMLButtonElement > ) => {
150- setSelectedPeriod ( Number ( e . currentTarget . value ) ) ;
144+ const handleSelectInterval = ( e : MouseEvent < HTMLButtonElement > ) => {
145+ setSelectedInterval ( Number ( e . currentTarget . value ) ) ;
151146 } ;
152147
153148 return (
154149 < ChartRoot >
155- < PeriodSelector
156- periods = { Periods }
157- onSelectPeriod = { handleSelectPeriod }
158- selectedPeriod = { selectedPeriod }
150+ < IntervalSelector
151+ intervals = { Intervals }
152+ onSelectInterval = { handleSelectInterval }
153+ selectedInterval = { selectedInterval }
159154 />
160155 < ChartContainer
161156 ref = { chartRef }
0 commit comments