11'use client'
22
3- import React , { useCallback , useEffect , useState } from 'react' ;
3+ import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
44import { Bar , BarChart , Cell , ResponsiveContainer , Tooltip , XAxis , YAxis } from 'recharts' ;
55import { useTranslations } from 'next-intl' ;
66import useOrderStore from "@/app/zustand/order" ;
77import { AggregatedSlotData } from "@/model/timeslot" ;
8- import { ORDER_AMOUNT_THRESHOLDS } from "@/config" ;
9-
10- interface TimelineProps {
11- startDate : Date ;
12- stopDate : Date ;
13- every_x_seconds : number ;
14- }
8+ import { ORDER_AMOUNT_THRESHOLDS , TIME_SLOT_CONFIG } from "@/config" ;
9+ import { timeslotToLocalTime } from "@/lib/time" ;
1510
1611const generateAllTimeslots = ( startDate : Date , stopDate : Date ) : AggregatedSlotData [ ] => {
1712 const timeSlots : AggregatedSlotData [ ] = [ ] ;
@@ -28,16 +23,24 @@ const generateAllTimeslots = (startDate: Date, stopDate: Date): AggregatedSlotDa
2823 return timeSlots ;
2924} ;
3025
31- const Timeline : React . FC < TimelineProps > = ( {
32- startDate,
33- stopDate,
34- every_x_seconds
35- } ) => {
26+ const Timeline = ( ) => {
3627 const [ timeslots , setTimeslots ] = useState < AggregatedSlotData [ ] > ( [ ] ) ;
3728 const [ isLoading , setIsLoading ] = useState ( true ) ;
3829 const [ error , setError ] = useState < string | null > ( null ) ;
3930 const t = useTranslations ( ) ;
4031
32+ // Timeline date range calculation (can be done outside component if static)
33+ const { startDate, stopDate } = useMemo ( ( ) => {
34+ const startDate = new Date ( )
35+ startDate . setHours ( startDate . getHours ( ) - 1 ) ;
36+ startDate . setMinutes ( 0 , 0 , 0 ) ;
37+
38+ const stopDate = new Date ( ) ;
39+ stopDate . setHours ( stopDate . getHours ( ) + 1 ) ;
40+ stopDate . setMinutes ( 59 , 59 , 999 ) ;
41+ return { startDate, stopDate } ;
42+ } , [ ] ) ;
43+
4144 const { setTimeslot } = useOrderStore ( ) ;
4245 const fetchTimeline = useCallback ( async ( ) => {
4346 setError ( null ) ;
@@ -47,18 +50,22 @@ const Timeline: React.FC<TimelineProps> = ({
4750 setError ( 'Failed to fetch timeline data' ) ;
4851 apiSlots = generateAllTimeslots ( startDate , stopDate ) ;
4952 } else {
50- apiSlots = await response . json ( ) ;
53+ apiSlots = await response . json ( )
54+ apiSlots = apiSlots . map ( ( item : AggregatedSlotData ) => ( {
55+ ...item ,
56+ time : timeslotToLocalTime ( item . time ) ,
57+ } ) )
5158 }
5259
5360 setTimeslots ( apiSlots ) ;
5461 setIsLoading ( false ) ;
55- } , [ startDate , stopDate , every_x_seconds ] ) ;
62+ } , [ startDate , stopDate , TIME_SLOT_CONFIG . UPDATE_EVERY_SECONDS ] ) ;
5663
5764 useEffect ( ( ) => {
5865 fetchTimeline ( ) ;
59- const interval = setInterval ( fetchTimeline , every_x_seconds * 1000 ) ;
66+ const interval = setInterval ( fetchTimeline , TIME_SLOT_CONFIG . UPDATE_EVERY_SECONDS * 1000 ) ;
6067 return ( ) => clearInterval ( interval ) ;
61- } , [ fetchTimeline , every_x_seconds ] ) ;
68+ } , [ fetchTimeline , TIME_SLOT_CONFIG . UPDATE_EVERY_SECONDS ] ) ;
6269
6370 const handleBarClick = useCallback ( ( event : any ) => {
6471 if ( event ?. activeLabel ) {
0 commit comments