-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathTimer.jsx
More file actions
1103 lines (1013 loc) · 35.9 KB
/
Timer.jsx
File metadata and controls
1103 lines (1013 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable jsx-a11y/media-has-caption */
import moment from 'moment';
import { useState, useCallback, useEffect, useRef, useMemo } from 'react';
import PropTypes from 'prop-types';
import { Modal, ModalHeader, ModalBody, ModalFooter, Button, Progress } from 'reactstrap';
import useWebSocket, { ReadyState } from 'react-use-websocket';
import { BsAlarmFill } from 'react-icons/bs';
import {
FaPlusCircle,
FaMinusCircle,
FaPlayCircle,
FaPauseCircle,
FaStopCircle,
FaUndoAlt,
} from 'react-icons/fa';
import { toast } from 'react-toastify';
import cs from 'classnames';
import { connect, useDispatch } from 'react-redux';
import css from './Timer.module.css';
import '../Header/index.css';
import { ENDPOINTS } from '~/utils/URL';
import config from '../../config.json';
import TimeEntryForm from '../Timelog/TimeEntryForm';
import Countdown from './Countdown';
import TimerStatus from './TimerStatus';
import TimerPopout from './TimerPopout';
import { postTimeEntry, editTimeEntry } from '../../actions/timeEntries';
function Timer({ authUser, darkMode, isPopout }) {
const dispatch = useDispatch();
const realIsPopout = typeof isPopout === 'boolean' ? isPopout : !!window.opener;
/**
* Because the websocket can not be closed when internet is cut off (lost server connection),
* the readyState will be stuck at OPEN, so here we need to use a custom readyState to
* mimic the real readyState, and when internet is cut off, the custom readyState will be set
* to CLOSED, and the user will be notified to refresh the page to reconnect to the server.
* */
const [customReadyState, setCustomReadyState] = useState(ReadyState.CONNECTING);
const WSoptions = {
share: false,
protocols: localStorage.getItem(config.tokenKey),
onOpen: () => setCustomReadyState(ReadyState.OPEN),
onClose: () => setCustomReadyState(ReadyState.CLOSED),
onError: error => {
throw new Error('WebSocket Error:', error);
},
};
/**
* Expected message format: {
* userId: string,
* time: number,
* paused: boolean,
* forcedPause: boolean,
* started: boolean,
* goal: number,
* startAt: Date,
* weekEndPause: boolean
* }
*/
const { sendMessage, sendJsonMessage, lastJsonMessage, getWebSocket } = useWebSocket(
ENDPOINTS.TIMER_SERVICE,
WSoptions,
);
// This is the contract between server and client
const action = {
START_TIMER: 'START_TIMER',
PAUSE_TIMER: 'PAUSE_TIMER',
STOP_TIMER: 'STOP_TIMER',
CLEAR_TIMER: 'CLEAR_TIMER',
GET_TIMER: 'GET_TIMER',
SET_GOAL: 'SET_GOAL',
ADD_GOAL: 'ADD_TO_GOAL',
REMOVE_GOAL: 'REMOVE_FROM_GOAL',
ACK_FORCED: 'ACK_FORCED',
START_CHIME: 'START_CHIME',
HEARTBEAT: 'ping',
};
const defaultMessage = {
time: 900000,
paused: false,
forcedPause: false,
started: false,
goal: 900000,
initialGoal: 900000,
chiming: false,
startAt: Date.now(),
weekEndPause: false,
};
const MAX_HOURS = 5;
const MIN_MINS = 1;
const ALLOWED_ROLES_TO_INTERACT = useMemo(() => ['Owner', 'Administrator'], []);
const [message, setMessage] = useState(defaultMessage);
const { time, paused, started, goal, startAt } = message;
const [running, setRunning] = useState(false);
const [confirmationResetModal, setConfirmationResetModal] = useState(false);
const [confirmSubmitModalOpen, setConfirmSubmitModalOpen] = useState(false);
const [pendingSubmitTime, setPendingSubmitTime] = useState({ hours: 0, minutes: 0 });
const [logTimeEntryModal, setLogTimeEntryModal] = useState(false);
const [inacModal, setInacModal] = useState(false);
const [showTimer, setShowTimer] = useState(false);
const [timeIsOverModalOpen, setTimeIsOverModalIsOpen] = useState(false);
const [remaining, setRemaining] = useState(time);
const [logTimer, setLogTimer] = useState({ hours: 0, minutes: 0 });
const [lastSubmittedTime, setLastSubmittedTime] = useState(null); // Add this state to track submitted time
const [submissionHistory, setSubmissionHistory] = useState([]); // Track submission history for debugging
const [timerState, setTimerState] = useState('idle'); // Track timer state: 'idle', 'running', 'paused', 'completed'
const [sessionId, setSessionId] = useState(null); // Unique session identifier
const [viewingUserId, setViewingUserId] = useState(null);
const [weekEndModal, setWeekEndModal] = useState(false);
const isWSOpenRef = useRef(0);
const timeIsOverAudioRef = useRef(null);
const forcedPausedAudioRef = useRef(null);
const timeToLog = moment.duration(goal - remaining);
const logHours = timeToLog.hours();
const logMinutes = timeToLog.minutes();
const sendJsonMessageNoQueue = useCallback(msg => sendJsonMessage(msg, false), [sendMessage]);
// Enhanced function to clear submitted time with better logging
const clearSubmittedTime = useCallback(() => {
console.log(' Clearing submitted time - Timer reset or user change detected');
setLastSubmittedTime(null);
setLogTimer({ hours: 0, minutes: 0 });
setTimerState('idle');
}, []);
// Enhanced function to track time submission with detailed logging
const trackTimeSubmission = useCallback(
submittedTime => {
const timeKey = `${submittedTime.hours}_${submittedTime.minutes}_${goal}_${remaining}`;
const submissionRecord = {
id: Date.now(),
time: submittedTime,
timestamp: new Date().toISOString(),
sessionId,
userId: viewingUserId || authUser?.userid,
goal,
remaining,
};
console.log('✅ Time submitted successfully:', submissionRecord);
setLastSubmittedTime(timeKey);
setSubmissionHistory(prev => [...prev, submissionRecord]);
setLogTimer({ hours: 0, minutes: 0 });
setTimerState('completed');
// Store in localStorage for debugging (optional)
try {
const existingHistory = JSON.parse(localStorage.getItem('timerSubmissionHistory') || '[]');
const updatedHistory = [...existingHistory, submissionRecord].slice(-10); // Keep last 10 entries
localStorage.setItem('timerSubmissionHistory', JSON.stringify(updatedHistory));
} catch (error) {
console.warn('Could not save submission history to localStorage:', error);
}
},
[goal, remaining, sessionId, viewingUserId, authUser?.userid],
);
// Enhanced function to validate time before submission
const validateTimeForSubmission = useCallback(
timeToSubmit => {
const { hours, minutes } = timeToSubmit;
const totalMinutes = hours * 60 + minutes;
// Check minimum time requirement
if (totalMinutes < 1) {
toast.error('❌ You need at least 1 minute to log time!');
return false;
}
// Check maximum time limit (5 hours)
if (totalMinutes > 300) {
toast.error('❌ Maximum time limit is 5 hours per session!');
return false;
}
// Check if this exact time was already submitted
const timeKey = `${hours}_${minutes}_${goal}_${remaining}`;
if (lastSubmittedTime === timeKey) {
toast.info('ℹ️ This time has already been submitted in this session.');
return false;
}
return true;
},
[goal, remaining, lastSubmittedTime],
);
// Enhanced function to get timer statistics
const getTimerStats = useCallback(() => {
const today = new Date().toDateString();
const todaySubmissions = submissionHistory.filter(
record => new Date(record.timestamp).toDateString() === today,
);
const totalTimeToday = todaySubmissions.reduce((total, record) => {
return total + (record.time.hours * 60 + record.time.minutes);
}, 0);
return {
totalSubmissions: submissionHistory.length,
todaySubmissions: todaySubmissions.length,
totalTimeToday: {
hours: Math.floor(totalTimeToday / 60),
minutes: totalTimeToday % 60,
},
lastSubmission: submissionHistory[submissionHistory.length - 1],
};
}, [submissionHistory]);
// Enhanced function to handle timer state changes
const updateTimerState = useCallback(
newState => {
console.log(`🔄 Timer state changed: ${timerState} → ${newState}`);
setTimerState(newState);
},
[timerState],
);
// Initialize session ID on component mount
useEffect(() => {
// Use cryptographically secure random values for session ID generation
// This is safe for client-side session tracking (not used for authentication)
let randomPart = '';
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
const randomBytes = new Uint8Array(9);
crypto.getRandomValues(randomBytes);
randomPart = Array.from(randomBytes)
.map(b => b.toString(36))
.join('')
.substring(0, 9);
} else {
// Fallback for environments without crypto API (should not occur in modern browsers)
// Use timestamp as fallback (not cryptographically secure but acceptable for non-security use)
randomPart = Date.now()
.toString(36)
.substring(2, 11);
}
const newSessionId = `session_${Date.now()}_${randomPart}`;
setSessionId(newSessionId);
console.log('🚀 Timer session initialized:', newSessionId);
}, []);
// Enhanced useEffect for timer state management
useEffect(() => {
if (running && !paused) {
updateTimerState('running');
} else if (paused && started) {
updateTimerState('paused');
} else if (!started) {
updateTimerState('idle');
}
}, [running, paused, started, updateTimerState]);
useEffect(() => {
const handleStorageEvent = () => {
const sessionStorageData = JSON.parse(window.sessionStorage.getItem('viewingUser'));
if (sessionStorageData) {
setViewingUserId(sessionStorageData.userId);
} else {
setViewingUserId(null);
}
};
// Set the initial state when the component mounts
handleStorageEvent();
// Add the event listener
window.addEventListener('storage', handleStorageEvent);
// Clean up the event listener when the component unmounts
return () => {
window.removeEventListener('storage', handleStorageEvent);
};
}, []);
// control whether buttons should be clickable
const isButtonDisabled = useMemo(
() => viewingUserId && !ALLOWED_ROLES_TO_INTERACT.includes(authUser?.role),
[viewingUserId, authUser],
);
// control whether to send GET_TIMER message to avoid message overriding
const isInitialJsonMessageReceived = useMemo(() => !!lastJsonMessage, [lastJsonMessage]);
// Modify the sendStop function to track submitted time
const wsJsonMessageHandler = useMemo(() => {
if (viewingUserId == null) {
return {
sendStart: () => sendJsonMessageNoQueue({ action: action.START_TIMER }),
sendPause: () => sendJsonMessageNoQueue({ action: action.PAUSE_TIMER }),
sendClear: () => sendJsonMessageNoQueue({ action: action.CLEAR_TIMER }),
sendStop: () => {
sendJsonMessageNoQueue({ action: action.STOP_TIMER });
// Clear the submitted time when stopping
clearSubmittedTime();
},
sendAckForced: () => sendJsonMessageNoQueue({ action: action.ACK_FORCED }),
sendGetTimer: () => sendJsonMessageNoQueue({ action: action.GET_TIMER }),
sendStartChime: state =>
sendJsonMessageNoQueue({ action: action.START_CHIME, value: state }),
sendSetGoal: timerGoal =>
sendJsonMessageNoQueue({ action: action.SET_GOAL, value: timerGoal }),
sendAddGoal: duration =>
sendJsonMessageNoQueue({ action: action.ADD_GOAL, value: duration }),
sendRemoveGoal: duration =>
sendJsonMessageNoQueue({ action: action.REMOVE_GOAL, value: duration }),
sendHeartbeat: () => sendJsonMessageNoQueue({ action: action.HEARTBEAT }),
};
}
return {
sendStart: () =>
sendJsonMessageNoQueue({ action: action.START_TIMER, userId: viewingUserId }),
sendPause: () =>
sendJsonMessageNoQueue({ action: action.PAUSE_TIMER, userId: viewingUserId }),
sendClear: () =>
sendJsonMessageNoQueue({ action: action.CLEAR_TIMER, userId: viewingUserId }),
sendStop: () => {
sendJsonMessageNoQueue({ action: action.STOP_TIMER, userId: viewingUserId });
// Clear the submitted time when stopping
clearSubmittedTime();
},
sendAckForced: () =>
sendJsonMessageNoQueue({ action: action.ACK_FORCED, userId: viewingUserId }),
sendGetTimer: () =>
sendJsonMessageNoQueue({ action: action.GET_TIMER, userId: viewingUserId }),
sendStartChime: state =>
sendJsonMessageNoQueue({ action: action.START_CHIME, userId: viewingUserId, value: state }),
sendSetGoal: timerGoal =>
sendJsonMessageNoQueue({
action: action.SET_GOAL,
userId: viewingUserId,
value: timerGoal,
}),
sendAddGoal: duration =>
sendJsonMessageNoQueue({ action: action.ADD_GOAL, userId: viewingUserId, value: duration }),
sendRemoveGoal: duration =>
sendJsonMessageNoQueue({
action: action.REMOVE_GOAL,
userId: viewingUserId,
value: duration,
}),
sendHeartbeat: () =>
sendJsonMessageNoQueue({ action: action.HEARTBEAT, userId: viewingUserId }),
};
}, [sendJsonMessageNoQueue, viewingUserId, clearSubmittedTime]);
const {
sendStart,
sendPause,
sendClear,
sendStop,
sendAckForced,
sendGetTimer,
sendStartChime,
sendAddGoal,
sendRemoveGoal,
sendHeartbeat,
} = wsJsonMessageHandler;
const toggleLogTimeModal = () => {
setLogTimeEntryModal(modal => !modal);
};
// Enhanced TimeEntryForm callback
const handleTimeSubmitted = useCallback(
submittedTime => {
trackTimeSubmission(submittedTime);
// Show success message
toast.success(`✅ Time logged successfully!`);
},
[trackTimeSubmission],
);
const toggleTimer = () => setShowTimer(timer => !timer);
const toggleTimeIsOver = () => {
setTimeIsOverModalIsOpen(!timeIsOverModalOpen);
sendStartChime(!timeIsOverModalOpen);
};
const toggleWeekEndModal = () => {
setWeekEndModal(!weekEndModal);
if (weekEndModal) {
// When closing the modal, stop chiming
sendStartChime(false);
}
};
const checkBtnAvail = useCallback(
addition => {
const remainingDuration = moment.duration(remaining);
const goalDuration = moment.duration(goal);
return (
remainingDuration.asMinutes() + addition > 0 &&
goalDuration.asMinutes() + addition >= MIN_MINS &&
goalDuration.asHours() < MAX_HOURS
);
},
[remaining],
);
const handleStartButton = useCallback(() => {
if (remaining === 0) {
toast.error('There is no more Remaining time, please add more or log your passed time');
} else {
sendStart();
}
}, [remaining]);
const handleAddButton = useCallback(
duration => {
if (goal >= MAX_HOURS * 3600000) {
toast.error(`Goal time cannot be set over ${MAX_HOURS} hours!`);
return;
}
const goalAfterAdditionAsHours = moment
.duration(goal)
.add(duration, 'minutes')
.asHours();
if (goalAfterAdditionAsHours > MAX_HOURS) {
toast.info(`Goal time cannot be set over ${MAX_HOURS} hours, Goal time is set to 5 hours`);
}
sendAddGoal(moment.duration(duration, 'minutes').asMilliseconds());
},
[remaining],
);
const handleSubtractButton = useCallback(
duration => {
const remainingtimeAfterRemoval = moment
.duration(remaining)
.subtract(duration, 'minutes')
.asMinutes();
const goalAfterRemovalAsMinutes = moment
.duration(goal)
.subtract(duration, 'minutes')
.asMinutes();
if (started && remainingtimeAfterRemoval <= 0) {
toast.error(`Remaining time is already less than ${duration} minutes!`);
} else if (goalAfterRemovalAsMinutes < MIN_MINS) {
toast.error(`Goal time cannot be set less than ${MIN_MINS} minutes!`);
} else {
sendRemoveGoal(moment.duration(duration, 'minutes').asMilliseconds());
}
},
[remaining],
);
// Enhanced handleStopButton with better validation and user feedback
const handleStopButton = useCallback(() => {
const timeToSubmit = { hours: logHours, minutes: logMinutes };
if (!validateTimeForSubmission(timeToSubmit)) return;
// Show confirmation popup for longer sessions
if (logHours >= 2) {
setPendingSubmitTime(timeToSubmit);
setConfirmSubmitModalOpen(true);
return;
}
console.log('🛑 Stop button clicked - preparing to log time:', timeToSubmit);
toggleLogTimeModal();
}, [logHours, logMinutes, validateTimeForSubmission, toggleLogTimeModal]);
const updateRemaining = () => {
if (!running) return;
const now = moment.utc();
const timePassed = moment.duration(now.diff(startAt)).asMilliseconds();
setRemaining(time > timePassed ? time - timePassed : 0);
};
const checkRemainingTime = () => {
if (remaining === 0) {
sendPause();
}
};
const handleWeekEndPause = () => {
if (goal - remaining < 60000) {
return; // Don't show modal if less than 1 minute
}
// Prevent duplicate calls if modal is already open
if (weekEndModal) {
return;
}
setWeekEndModal(true);
sendPause(); // Pause timer
toast.info('The week is close to ending! Please log your time.');
sendStartChime(true); // Start chiming
};
/**
* This useEffect is to make sure that all the states will be updated before taking effects,
* so that message state and other states like running, inacMoal ... will be updated together
* at the same time.
*/
useEffect(() => {
// Exclude heartbeat message and timelog event messages
if (lastJsonMessage && lastJsonMessage.heartbeat === 'pong') {
isWSOpenRef.current = 0;
return;
}
// Ignore TIMELOG_EVENT messages - they're for the timestamps tab, not the timer
if (lastJsonMessage && lastJsonMessage.type === 'TIMELOG_EVENT') {
return;
}
// Handle explicit week close pause action messages
if (lastJsonMessage && lastJsonMessage.action === 'WEEK_CLOSE_PAUSE') {
if (running) {
handleWeekEndPause();
}
return; // Exit early to prevent other modal logic
}
const {
paused: pausedLJM,
forcedPause: forcedPauseLJM,
started: startedLJM,
chiming: chimingLJM,
weekEndPause: weekEndPauseLJM,
} = lastJsonMessage || defaultMessage;
setMessage(lastJsonMessage || defaultMessage);
setRunning(startedLJM && !pausedLJM);
// Show inactivity or time-over modals based on message state
setInacModal(forcedPauseLJM);
setTimeIsOverModalIsOpen(chimingLJM && (customReadyState === ReadyState.OPEN || !weekEndModal));
}, [lastJsonMessage, customReadyState, running, message, weekEndModal]);
// This useEffect is to make sure that the WS connection is maintained by sending a heartbeat every 60 seconds
useEffect(() => {
const interval = setInterval(() => {
if (running) {
isWSOpenRef.current += 1;
sendHeartbeat();
setTimeout(() => {
// make sure to notify the user if the heartbeat is not responded for 3 times
if (isWSOpenRef.current > 3) {
setRunning(false);
setInacModal(true);
getWebSocket().close(); // try to close the WS connection, but it might not work when internet is cut off
setCustomReadyState(ReadyState.CLOSED);
}
}, 10000); // close the WS if no response after 10 seconds
}
}, 60000);
return () => clearInterval(interval);
}, [running]);
useEffect(() => {
/**
* This useEffect will run upon message change,
* and message is a state as a copy of the lastJsonMessage,
* here message works as a buffer, for details see above
*/
let interval;
if (running) {
updateRemaining();
interval = setInterval(() => {
updateRemaining();
}, 1000);
} else {
setRemaining(time);
clearInterval(interval);
}
return () => clearInterval(interval);
}, [running, message]);
// Enhanced useEffect that updates logTimer with better validation
useEffect(() => {
checkRemainingTime();
const currentTimeToLog = { hours: logHours, minutes: logMinutes };
const timeKey = `${logHours}_${logMinutes}_${goal}_${remaining}`;
// Only update logTimer if we haven't submitted this time yet and it's valid
if (lastSubmittedTime !== timeKey && (logHours > 0 || logMinutes > 0)) {
if (validateTimeForSubmission(currentTimeToLog)) {
setLogTimer(currentTimeToLog);
console.log('⏱️ Time to log updated:', currentTimeToLog);
}
}
}, [remaining, logHours, logMinutes, goal, lastSubmittedTime, validateTimeForSubmission]);
// Enhanced useEffect for timer state changes
useEffect(() => {
if (!started || goal !== lastSubmittedTime?.goal) {
clearSubmittedTime();
console.log('🔄 Timer reset detected - clearing submitted time');
}
}, [started, goal, clearSubmittedTime]);
useEffect(() => {
if (timeIsOverModalOpen) {
window.focus();
timeIsOverAudioRef.current.play();
} else {
window.focus();
timeIsOverAudioRef.current.pause();
timeIsOverAudioRef.current.currentTime = 0;
}
}, [timeIsOverModalOpen]);
// Close time over modal if connection is lost
useEffect(() => {
if (customReadyState !== ReadyState.OPEN && timeIsOverModalOpen) {
setTimeIsOverModalIsOpen(false);
}
}, [customReadyState, timeIsOverModalOpen]);
useEffect(() => {
if (inacModal) {
window.focus();
forcedPausedAudioRef.current.play();
} else {
window.focus();
forcedPausedAudioRef.current.pause();
forcedPausedAudioRef.current.currentTime = 0;
}
}, [inacModal]);
useEffect(() => {
if (weekEndModal) {
window.focus();
timeIsOverAudioRef.current.play();
} else {
window.focus();
timeIsOverAudioRef.current.pause();
timeIsOverAudioRef.current.currentTime = 0;
}
}, [weekEndModal]);
useEffect(() => {
if (!isInitialJsonMessageReceived) return;
sendGetTimer();
}, [isInitialJsonMessageReceived, viewingUserId]);
// Enhanced cleanup when viewing user changes
useEffect(() => {
clearSubmittedTime();
console.log('👤 User changed - clearing timer state');
}, [viewingUserId, clearSubmittedTime]);
// Enhanced cleanup on component unmount
useEffect(() => {
return () => {
clearSubmittedTime();
console.log('🔚 Timer component unmounting - cleanup complete');
};
}, [clearSubmittedTime]);
const fontColor = darkMode ? 'text-light' : '';
const headerBg = darkMode ? 'bg-space-cadet' : '';
const bodyBg = darkMode ? 'bg-yinmn-blue' : '';
const renderTimeEntryForm = () =>
logTimeEntryModal && (
<TimeEntryForm
from="Timer"
edit={false}
toggle={toggleLogTimeModal}
isOpen={logTimeEntryModal}
data={logTimer}
sendStop={sendStop}
timerConnected={customReadyState === ReadyState.OPEN}
onTimeSubmitted={handleTimeSubmitted}
sessionId={sessionId}
timerStats={getTimerStats()}
/>
);
const renderAudioElements = () => (
<>
<audio
ref={timeIsOverAudioRef}
key="timeIsOverAudio"
loop
preload="auto"
src="https://bigsoundbank.com/UPLOAD/mp3/2554.mp3"
/>
<audio
ref={forcedPausedAudioRef}
key="forcedPausedAudio"
loop
preload="auto"
src="https://bigsoundbank.com/UPLOAD/mp3/1102.mp3"
/>
</>
);
const renderConfirmSubmitModal = () => (
<Modal
isOpen={confirmSubmitModalOpen}
toggle={() => setConfirmSubmitModalOpen(false)}
centered
size="md"
className={cs(fontColor, darkMode ? 'dark-mode' : '')}
>
<ModalHeader
className={darkMode ? 'bg-space-cadet' : ''}
toggle={() => setConfirmSubmitModalOpen(false)}
>
Confirm submission
</ModalHeader>
<ModalBody className={darkMode ? 'bg-yinmn-blue' : ''}>
<div style={{ fontSize: '1rem', lineHeight: 1.4 }}>
<div style={{ fontWeight: 700, marginBottom: 8 }}>
Are you sure you want to submit{' '}
{pendingSubmitTime.hours ? `${pendingSubmitTime.hours} hour(s)` : ''}
{pendingSubmitTime.minutes ? ` ${pendingSubmitTime.minutes} minute(s)` : ''}?
</div>
<div style={{ opacity: 0.9 }}>This action cannot be undone.</div>
</div>
</ModalBody>
<ModalFooter className={darkMode ? 'bg-yinmn-blue' : ''}>
<Button color="secondary" onClick={() => setConfirmSubmitModalOpen(false)}>
Cancel
</Button>
<Button
color="primary"
onClick={() => {
setConfirmSubmitModalOpen(false);
toggleLogTimeModal();
}}
>
Yes, submit
</Button>
</ModalFooter>
</Modal>
);
const renderConfirmationResetModal = () => (
<Modal
isOpen={confirmationResetModal}
toggle={() => setConfirmationResetModal(!confirmationResetModal)}
centered
size="md"
className={cs(fontColor, darkMode ? 'dark-mode' : '')}
>
<ModalHeader
className={darkMode ? 'bg-space-cadet' : ''}
toggle={() => setConfirmationResetModal(false)}
>
Reset Time
</ModalHeader>
<ModalBody className={darkMode ? 'bg-yinmn-blue' : ''}>
Are you sure you want to reset your time?
</ModalBody>
<ModalFooter className={darkMode ? 'bg-yinmn-blue' : ''}>
<Button
color="primary"
onClick={() => {
sendClear();
setConfirmationResetModal(false);
}}
>
Yes, reset time!
</Button>{' '}
</ModalFooter>
</Modal>
);
const renderInactivityModal = () => (
<Modal
className={cs(fontColor, darkMode ? 'dark-mode' : '')}
size="md"
isOpen={inacModal}
toggle={() => setInacModal(!inacModal)}
centered
>
<ModalHeader className={headerBg} toggle={() => setInacModal(!inacModal)}>
Timer Paused
</ModalHeader>
<ModalBody className={bodyBg}>
The user timer has been paused due to inactivity or a lost in connection to the server.
Please check your internet connection and refresh the page to continue. This is to ensure
that our resources are being used efficiently and to improve performance for all of our
users.
</ModalBody>
<ModalFooter className={bodyBg}>
<Button
color="primary"
onClick={() => {
setInacModal(!inacModal);
sendAckForced();
}}
>
I understand
</Button>
</ModalFooter>
</Modal>
);
const renderTimeCompleteModal = () => (
<Modal
className={cs(fontColor, darkMode ? 'dark-mode' : '')}
isOpen={timeIsOverModalOpen}
toggle={toggleTimeIsOver}
centered
size="md"
>
<ModalHeader className={headerBg} toggle={toggleTimeIsOver}>
Time Complete!
</ModalHeader>
<ModalBody className={bodyBg}>{`You have worked for ${logHours ? `${logHours} hours` : ''}${
logMinutes ? ` ${logMinutes} minutes` : ''
}. Click below if you'd like to add time or Log Time.`}</ModalBody>
<ModalFooter className={bodyBg}>
<Button
color="primary"
onClick={() => {
toggleTimeIsOver();
toggleLogTimeModal();
}}
>
Log Time
</Button>{' '}
<Button
color="secondary"
onClick={() => {
toggleTimeIsOver();
handleAddButton(15);
sendStart();
}}
>
Add More Time
</Button>{' '}
</ModalFooter>
</Modal>
);
if (realIsPopout) {
return (
<div className={cs(css.timer, darkMode ? 'dark-mode' : '')}>
<div className={css.timerContent}>
{customReadyState === ReadyState.OPEN && (
<Countdown
message={message}
timerRange={{ MAX_HOURS, MIN_MINS }}
running={running}
wsMessageHandler={wsJsonMessageHandler}
remaining={remaining}
setConfirmationResetModal={setConfirmationResetModal}
checkBtnAvail={checkBtnAvail}
handleStartButton={handleStartButton}
handleAddButton={handleAddButton}
handleSubtractButton={handleSubtractButton}
handleStopButton={handleStopButton}
toggleTimer={() => window.close()}
/>
)}
{customReadyState !== ReadyState.OPEN && (
<TimerStatus
readyState={customReadyState}
message={message}
toggleTimer={() => window.close()}
/>
)}
</div>
{renderTimeEntryForm()}
{renderAudioElements()}
{renderConfirmationResetModal()}
{renderInactivityModal()}
{renderTimeCompleteModal()}
{renderConfirmSubmitModal()}
</div>
);
}
return (
<div className={cs(css.timerContainer)}>
<button
type="button"
disabled={isButtonDisabled}
onClick={toggleTimer}
className={css.btnDiv}
aria-label="Open timer dropdown"
>
<div className={cs(css.iconWrapper, isButtonDisabled ? css.btnDisabled : css.btn)}>
<BsAlarmFill fontSize="2rem" title="Open timer dropdown" />
</div>
</button>
<div className={css.previewContainer} title="Open timer dropdown">
<Progress multi style={{ height: '6px' }}>
<Progress bar value={100 * (1 - remaining / goal)} color="success" animated={running} />
<Progress bar value={2} color="light" />
<Progress bar value={100 * (remaining / goal)} color="primary" animated={running} />
</Progress>
{customReadyState === ReadyState.OPEN ? (
<button
type="button"
disabled={isButtonDisabled}
className={css.preview}
onClick={toggleTimer}
>
{moment.utc(remaining).format('HH:mm:ss')}
</button>
) : (
<div className={css.disconnected}>Disconnected</div>
)}
</div>
{customReadyState === ReadyState.OPEN && (
<div className={css.btns}>
<button
type="button"
disabled={isButtonDisabled}
onClick={() => {
handleAddButton(15);
}}
title="Add 15min"
aria-label="Add 15min"
style={{ background: 'none', border: 'none' }}
>
<div
className={cs(
css.iconWrapper,
isButtonDisabled ? css.btnDisabled : css.transitionColor,
)}
>
<FaPlusCircle
className={checkBtnAvail(15) ? css.btn : css.btnDisabled}
fontSize="1.5rem"
/>
</div>
</button>
<button
type="button"
disabled={isButtonDisabled}
onClick={() => handleSubtractButton(15)}
title="Subtract 15min"
aria-label="Subtract 15min"
style={{ background: 'none', border: 'none' }}
>
<div
className={cs(
css.iconWrapper,
isButtonDisabled ? css.btnDisabled : css.transitionColor,
)}
>
<FaMinusCircle
className={checkBtnAvail(-15) ? css.btn : css.btnDisabled}
fontSize="1.5rem"
/>
</div>
</button>
{!started || paused ? (
<button
type="button"
disabled={isButtonDisabled}
onClick={handleStartButton}
aria-label="Start timer"
style={{ background: 'none', border: 'none' }}
>
<div
className={cs(
css.iconWrapper,
isButtonDisabled ? css.btnDisabled : css.transitionColor,
)}
>
<FaPlayCircle
className={remaining !== 0 ? css.btn : css.btnDisabled}
fontSize="1.5rem"
title="Start timer"
/>
</div>
</button>
) : (
<button
type="button"
disabled={isButtonDisabled}
onClick={sendPause}
aria-label="Pause timer"
style={{ background: 'none', border: 'none' }}
>
<div
className={cs(
css.iconWrapper,
isButtonDisabled ? css.btnDisabled : css.transitionColor,