Skip to content

Commit

Permalink
fix(clock): made clock work on older browsers (#1141)
Browse files Browse the repository at this point in the history
* 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
vildeopp and lindtvedtsebastian authored Jul 20, 2023
1 parent 17f7ef0 commit 195b7cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useNonNullContext } from 'hooks/useNonNullContext'
import { formatTime, getRelativeTimeString } from 'Board/utils/time'
import { formatDateString, getRelativeTimeString } from 'utils/time'
import { DepartureContext } from '../../contexts'

import classes from './styles.module.css'
Expand All @@ -18,7 +18,7 @@ function Time() {
{getRelativeTimeString(departure.expectedDepartureTime)}
</div>
<div className={classes.aimedDepartureTime}>
{formatTime(departure.aimedDepartureTime)}
{formatDateString(departure.aimedDepartureTime)}
</div>
</td>
)
Expand Down
14 changes: 0 additions & 14 deletions next-tavla/src/Board/utils/time.ts

This file was deleted.

14 changes: 4 additions & 10 deletions next-tavla/src/Shared/components/Clock/index.tsx
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 }
22 changes: 22 additions & 0 deletions next-tavla/src/Shared/utils/time.ts
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)
}

0 comments on commit 195b7cf

Please sign in to comment.