-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clock): made clock work on older browsers (#1141)
* fix(clock): made clock work on older browsers * chore(shared): relocated time.ts to shared utils * fix(clock): remove hydration errors --------- Co-authored-by: lindtvedtsebastian <[email protected]>
- Loading branch information
1 parent
17f7ef0
commit 195b7cf
Showing
4 changed files
with
28 additions
and
26 deletions.
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,24 +1,18 @@ | ||
import { useEffect, useState } from 'react' | ||
import classes from './styles.module.css' | ||
import { formatTimeStamp } from 'utils/time' | ||
|
||
function Clock() { | ||
const [currentTime, setCurrentTime] = useState(Date.now()) | ||
const [currentTime, setCurrentTime] = useState(0) | ||
|
||
useEffect(() => { | ||
const intervalId = setInterval(() => setCurrentTime(Date.now()), 1000) | ||
return () => clearInterval(intervalId) | ||
}, []) | ||
|
||
const time = new Intl.DateTimeFormat('no-NB', { | ||
timeStyle: 'short', | ||
timeZone: 'Europe/Oslo', | ||
}).format(currentTime) | ||
const time = formatTimeStamp(currentTime) | ||
|
||
return ( | ||
<span suppressHydrationWarning className={classes.clock}> | ||
{time} | ||
</span> | ||
) | ||
return <span className={classes.clock}>{time}</span> | ||
} | ||
|
||
export { Clock } |
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,22 @@ | ||
export function getRelativeTimeString(dateString: string) { | ||
const timeDiff = Date.parse(dateString) - Date.now() | ||
if (timeDiff < 60_000) return 'Nå' | ||
else if (timeDiff < 900_000) return Math.floor(timeDiff / 60_000) + ' min' | ||
else return formatDateString(dateString) | ||
} | ||
|
||
export function formatDateString(dateString: string) { | ||
return Intl.DateTimeFormat('no-NB', { | ||
hour12: false, | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
}).format(Date.parse(dateString)) | ||
} | ||
|
||
export function formatTimeStamp(timestamp: number) { | ||
return Intl.DateTimeFormat('no-NB', { | ||
hour12: false, | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
}).format(timestamp) | ||
} |