Files
web/apps/scandic-web/components/Countdown/index.tsx
Anton Gunnarsson 923206ee4c Merged in chore/sw-3145-move-subtitle (pull request #2516)
chore(SW-3145): Move Title and Subtitle to design-system

* Move Title and Subtitle to design-system

* Fix export


Approved-by: Linus Flood
2025-07-04 06:22:28 +00:00

36 lines
848 B
TypeScript

"use client"
import { useState } from "react"
import { useInterval } from "usehooks-ts"
import { dt } from "@scandic-hotels/common/dt"
import Title from "@scandic-hotels/design-system/Title"
import type { CountdownProps } from "@/types/components/countdown"
export default function Countdown({
minutes = 30,
seconds = 0,
onChange = () => undefined,
}: CountdownProps) {
const [time, setTime] = useState(dt.duration({ minutes, seconds }))
const timeSeconds = time.asSeconds()
useInterval(
() => {
setTime((currentTime) => {
const newTime = currentTime.asMilliseconds() - 1000
onChange(newTime)
return dt.duration(newTime)
})
},
timeSeconds > 0 ? 1000 : null
)
return (
<Title as="h1">
<time dateTime={time.toISOString()}>{time.format("m:ss")}</time>
</Title>
)
}