Skip to content

Commit 0e64688

Browse files
committed
Use now state to drive resend cooldown timer
Introduce a now state that is updated on reset and when bumping the cooldown, and start a 1s interval while a resend cooldown is active to update now. Replace direct Date.now() reads with now for computing cooldownRemaining so the UI re-renders each second and shows an accurate remaining seconds count. Ensure the interval is cleared when conditions end.
1 parent 2744319 commit 0e64688

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

ui/src/components/auth/LoginModal.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function LoginModal({
5555
emailType: MagicLinkEmailType;
5656
} | null>(null);
5757
const [cooldownUntil, setCooldownUntil] = useState(0);
58+
const [now, setNow] = useState(() => Date.now());
5859

5960
const emailInputRef = useRef<HTMLInputElement>(null);
6061

@@ -68,6 +69,7 @@ export function LoginModal({
6869
setSuccess(null);
6970
setIsSubmitting(false);
7071
setCooldownUntil(0);
72+
setNow(Date.now());
7173
}, [open, launchKey, initialEmail]);
7274

7375
useEffect(() => {
@@ -80,9 +82,21 @@ export function LoginModal({
8082
}, [open, success, launchKey]);
8183

8284
const bumpCooldown = useCallback(() => {
83-
setCooldownUntil(Date.now() + RESEND_COOLDOWN_MS);
85+
const nextNow = Date.now();
86+
setNow(nextNow);
87+
setCooldownUntil(nextNow + RESEND_COOLDOWN_MS);
8488
}, []);
8589

90+
useEffect(() => {
91+
if (!open || !success || cooldownUntil <= now) {
92+
return;
93+
}
94+
const id = window.setInterval(() => {
95+
setNow(Date.now());
96+
}, 1_000);
97+
return () => window.clearInterval(id);
98+
}, [cooldownUntil, now, open, success]);
99+
86100
const validateEmail = (): boolean => {
87101
const emailResult = emailSchema.safeParse(email);
88102
if (!emailResult.success) {
@@ -142,7 +156,7 @@ export function LoginModal({
142156

143157
const cooldownRemaining = Math.max(
144158
0,
145-
Math.ceil((cooldownUntil - Date.now()) / 1000)
159+
Math.ceil((cooldownUntil - now) / 1000)
146160
);
147161

148162
return (

0 commit comments

Comments
 (0)