mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-21 14:36:09 +05:00
30 lines
899 B
JavaScript
30 lines
899 B
JavaScript
import { Button } from '@material-ui/core'
|
|
import { useEffect, useState } from 'react'
|
|
|
|
export default function UnsafeButton({ timeout, children, disabled, ...props }) {
|
|
const [timeLeft, setTimeLeft] = useState(timeout || 7)
|
|
const [buttonDisabled, setButtonDisabled] = useState(disabled || timeLeft > 0)
|
|
const handleTimerTick = () => {
|
|
const newTimeLeft = timeLeft - 1
|
|
setTimeLeft(newTimeLeft)
|
|
if (newTimeLeft <= 0) {
|
|
setButtonDisabled(disabled)
|
|
}
|
|
}
|
|
const getTimerText = () => (!disabled && timeLeft > 0 ? ` (${timeLeft})` : '')
|
|
useEffect(() => {
|
|
if (disabled || !timeLeft) {
|
|
return
|
|
}
|
|
const intervalId = setInterval(handleTimerTick, 1000)
|
|
return () => clearInterval(intervalId)
|
|
// eslint-disable-next-line
|
|
}, [timeLeft])
|
|
|
|
return (
|
|
<Button disabled={buttonDisabled} {...props}>
|
|
{children} {getTimerText()}
|
|
</Button>
|
|
)
|
|
}
|