This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: new app banner with redirect (#4108)
- Loading branch information
Showing
3 changed files
with
71 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ReactElement, useEffect, useState } from 'react' | ||
|
||
const Countdown = ({ | ||
children, | ||
seconds, | ||
onEnd, | ||
}: { | ||
children: (count: ReactElement) => ReactElement | ||
seconds: number | ||
onEnd: () => void | ||
}): ReactElement | null => { | ||
const [countdown, setCountdown] = useState<number>(seconds) | ||
const [cancel, setCancel] = useState<boolean>(false) | ||
|
||
useEffect(() => { | ||
if (cancel) return | ||
|
||
const interval = setInterval(() => { | ||
if (cancel) { | ||
clearInterval(interval) | ||
return | ||
} | ||
|
||
setCountdown((prevCountdown) => { | ||
if (prevCountdown === 1) { | ||
clearInterval(interval) | ||
onEnd() | ||
} | ||
|
||
return prevCountdown - 1 | ||
}) | ||
}, 1000) | ||
|
||
return () => clearInterval(interval) | ||
}, [onEnd, cancel]) | ||
|
||
return cancel ? null : ( | ||
<> | ||
{children(<span style={{ display: 'inline-block', width: '1em' }}>{countdown}</span>)}{' '} | ||
<a onClick={() => setCancel(true)}>Cancel</a> | ||
</> | ||
) | ||
} | ||
|
||
export default Countdown |
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