1+ import { useRouter } from "next/router" ;
12import React , { useEffect , useState } from "react" ;
23
34import CurrentTaskTitle from "@/components/ui/focus/currentTaskTitle" ;
45import TimeDisplay from "@/components/ui/focus/timeDisplay" ;
56import UpcomingTasks from "@/components/ui/focus/upcomingTasks" ;
6- import { useRouter } from "next/router" ;
77
88interface Time {
99 id : number ;
@@ -77,7 +77,6 @@ const CountdownTimer = () => {
7777 const result = await tasksFetch . json ( ) ;
7878 console . log ( "tasks: " , result ) ;
7979 setTasks ( result ) ;
80-
8180 } catch ( error ) {
8281 console . error ( error ) ;
8382 }
@@ -87,20 +86,7 @@ const CountdownTimer = () => {
8786 } , [ router ] ) ;
8887
8988 useEffect ( ( ) => {
90- var task_times : Time [ ] = [ ] ;
91- for ( let index = 0 ; index < tasks . length ; index ++ ) {
92- const task = tasks [ index ] ;
93- for ( let i = 0 ; i < task . times . length ; i ++ ) {
94- task_times = task_times . concat ( task . times [ i ] )
95- console . log ( "new time: " , task . times [ i ] ) ;
96- }
97- }
98- setTimes ( task_times ) ;
99- console . log ( "times: " , task_times ) ;
100- getCurrentTask ( task_times ) ;
101- } , [ tasks ] )
102-
103- function getCurrentTask ( ts : Time [ ] ) {
89+ function getCurrentTask ( ts : Time [ ] ) {
10490 let upcomingTimes : Time [ ] ;
10591
10692 const d = new Date ( ) ;
@@ -110,15 +96,18 @@ const CountdownTimer = () => {
11096
11197 // filter out finished tasks
11298 upcomingTimes = ts . filter ( ( time ) => {
113- return ! ( cur_d == time . day && serializeTime ( time . end_time ) <= cur_time + 10 ) ;
99+ return ! (
100+ cur_d == time . day && serializeTime ( time . end_time ) <= cur_time + 10
101+ ) ;
114102 } ) ;
115103
116104 // sort by start time
117105 upcomingTimes = upcomingTimes . sort ( ( a , b ) => {
118106 return (
119107 serializeTime ( a . start_time ) +
120108 ( ( a . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 -
121- ( serializeTime ( b . start_time ) + ( ( b . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 )
109+ ( serializeTime ( b . start_time ) +
110+ ( ( b . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 )
122111 ) ;
123112 } ) ;
124113
@@ -129,9 +118,11 @@ const CountdownTimer = () => {
129118 upcomingTimes [ 0 ] . day == cur_d
130119 ) {
131120 setCurrentTime ( upcomingTimes [ 0 ] ) ;
132-
133- const cur_task = tasks . filter ( ( task ) => task . id === upcomingTimes [ 0 ] ?. task ) ;
134- console . log ( tasks , cur_task )
121+
122+ const cur_task = tasks . filter (
123+ ( task ) => task . id === upcomingTimes [ 0 ] ?. task ,
124+ ) ;
125+ console . log ( tasks , cur_task ) ;
135126 setCurrentTask ( cur_task [ 0 ] ) ;
136127 } else {
137128 setCurrentTime ( null ) ;
@@ -144,6 +135,67 @@ const CountdownTimer = () => {
144135 }
145136 }
146137
138+ let task_times : Time [ ] = [ ] ;
139+ for ( let index = 0 ; index < tasks . length ; index ++ ) {
140+ const task = tasks [ index ] ;
141+ for ( let i = 0 ; i < task . times . length ; i ++ ) {
142+ task_times = task_times . concat ( task . times [ i ] ) ;
143+ console . log ( "new time: " , task . times [ i ] ) ;
144+ }
145+ }
146+ setTimes ( task_times ) ;
147+ console . log ( "times: " , task_times ) ;
148+ getCurrentTask ( task_times ) ;
149+ } , [ tasks ] ) ;
150+
151+ function getCurrentTask ( ts : Time [ ] ) {
152+ let upcomingTimes : Time [ ] ;
153+
154+ const d = new Date ( ) ;
155+ const cur_time =
156+ d . getHours ( ) * 60 * 60 + d . getMinutes ( ) * 60 + d . getSeconds ( ) ;
157+ const cur_d = d . getDay ( ) ;
158+
159+ // filter out finished tasks
160+ upcomingTimes = ts . filter ( ( time ) => {
161+ return ! (
162+ cur_d == time . day && serializeTime ( time . end_time ) <= cur_time + 10
163+ ) ;
164+ } ) ;
165+
166+ // sort by start time
167+ upcomingTimes = upcomingTimes . sort ( ( a , b ) => {
168+ return (
169+ serializeTime ( a . start_time ) +
170+ ( ( a . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 -
171+ ( serializeTime ( b . start_time ) + ( ( b . day + 8 - cur_d ) % 8 ) * 24 * 60 * 60 )
172+ ) ;
173+ } ) ;
174+
175+ // logic for if there are no tasks currently or at all
176+ if ( upcomingTimes . length > 0 ) {
177+ if (
178+ serializeTime ( upcomingTimes [ 0 ] . start_time ) < cur_time &&
179+ upcomingTimes [ 0 ] . day == cur_d
180+ ) {
181+ setCurrentTime ( upcomingTimes [ 0 ] ) ;
182+
183+ const cur_task = tasks . filter (
184+ ( task ) => task . id === upcomingTimes [ 0 ] ?. task ,
185+ ) ;
186+ console . log ( tasks , cur_task ) ;
187+ setCurrentTask ( cur_task [ 0 ] ) ;
188+ } else {
189+ setCurrentTime ( null ) ;
190+ setCurrentTask ( null ) ;
191+ setNextTime ( upcomingTimes [ 0 ] ) ;
192+ }
193+ } else {
194+ setCurrentTime ( null ) ;
195+ setNextTime ( null ) ;
196+ }
197+ }
198+
147199 function onTaskEnd ( status : string ) {
148200 if ( status == "finished" ) {
149201 setRefresh ( ! refresh ) ;
@@ -152,7 +204,7 @@ const CountdownTimer = () => {
152204 }
153205
154206 return (
155- < div className = "h-[calc(100vh-64px)] bg-slate-800 font-inter flex w-full flex-col items-center justify-center" >
207+ < div className = "flex h-[calc(100vh-64px)] w-full flex-col items-center justify-center bg-slate-800 font-inter " >
156208 < div className = "flex flex-col items-center justify-start rounded-xl bg-slate-800 p-8 font-mono text-white" >
157209 < p className = "p-4 font-inter text-4xl font-semibold" > Focus</ p >
158210 < div className = "flex flex-row justify-center" >
@@ -194,16 +246,14 @@ const CountdownTimer = () => {
194246 < div className = "p-4 font-inter text-2xl font-semibold" >
195247 tasks
196248 </ div >
197- < UpcomingTasks tasks = { tasks } times = { times } refresh = { refresh } />
249+ < UpcomingTasks tasks = { tasks } times = { times } refresh = { refresh } />
198250 </ div >
199251 ) : (
200252 < > </ >
201253 ) }
202254 </ div >
203255 </ div >
204- < div className = "p-8" >
205-
206- </ div >
256+ < div className = "p-8" > </ div >
207257 </ div >
208258 </ div >
209259 ) ;
0 commit comments