-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #945 from MirzaHanan/feature/bounty-elapsed-timer
Implement Real-Time Elapsed Time Tracker for Assigned Bounties in Modal
- Loading branch information
Showing
4 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { bountyReviewStore } from 'store/bountyReviewStore'; | ||
import styled from 'styled-components'; | ||
import { formatElapsedTime } from '../../helpers/timeFormatting'; | ||
|
||
interface ElapsedTimerProps { | ||
bountyId: string; | ||
} | ||
|
||
const TimerContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 3px; | ||
`; | ||
|
||
const Label = styled.span` | ||
font-size: 16px; | ||
font-weight: 700; | ||
color: #8f8f8f; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
font-family: 'Barlow'; | ||
`; | ||
|
||
const Time = styled.span` | ||
font-size: 20px; | ||
font-weight: 600; | ||
color: #000000; | ||
text-align: center; | ||
`; | ||
|
||
const ElapsedTimerBase: React.FC<ElapsedTimerProps> = ({ bountyId }: ElapsedTimerProps) => { | ||
const [elapsedTime, setElapsedTime] = useState<string>('00h 00m'); | ||
const [firstAssignedAt, setFirstAssignedAt] = useState<string | null>(null); | ||
const [error, setError] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const fetchTiming = async () => { | ||
if (bountyId === '0') { | ||
setError(false); | ||
return; | ||
} | ||
|
||
try { | ||
await bountyReviewStore.getTiming(bountyId); | ||
const timing = bountyReviewStore.timings[bountyId]; | ||
|
||
if (timing?.firstAssignedAt) { | ||
setFirstAssignedAt(timing.firstAssignedAt); | ||
setElapsedTime(formatElapsedTime(timing.firstAssignedAt)); | ||
setError(false); | ||
} else { | ||
setFirstAssignedAt(null); | ||
setElapsedTime('00h 00m'); | ||
setError(false); | ||
} | ||
} catch (error) { | ||
console.error('Error fetching timing:', error); | ||
setError(true); | ||
setFirstAssignedAt(null); | ||
setElapsedTime('00h 00m'); | ||
} | ||
}; | ||
|
||
fetchTiming(); | ||
}, [bountyId]); | ||
|
||
useEffect(() => { | ||
if (!firstAssignedAt) return; | ||
|
||
const updateTimer = () => { | ||
setElapsedTime(formatElapsedTime(firstAssignedAt)); | ||
}; | ||
|
||
updateTimer(); | ||
const interval = setInterval(updateTimer, 60000); | ||
|
||
return () => clearInterval(interval); | ||
}, [firstAssignedAt]); | ||
|
||
if (error) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<TimerContainer> | ||
<Label>Elapsed</Label> | ||
<Time>{elapsedTime}</Time> | ||
</TimerContainer> | ||
); | ||
}; | ||
|
||
export const ElapsedTimer = observer(ElapsedTimerBase); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const formatElapsedTime = (firstAssignedAt: string): string => { | ||
const startTime = new Date(firstAssignedAt); | ||
const currentTime = new Date(); | ||
|
||
const elapsed = currentTime.getTime() - startTime.getTime(); | ||
|
||
const hours = Math.floor(elapsed / (1000 * 60 * 60)); | ||
const minutes = Math.floor((elapsed % (1000 * 60 * 60)) / (1000 * 60)); | ||
|
||
return `${hours.toString().padStart(2, '0')}h ${minutes.toString().padStart(2, '0')}m`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters