From f83c96bb7737489a3f646c466f2a927f04eec3bd Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 30 Jul 2026 07:27:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(BindContactModal):=20=E6=8A=BD?= =?UTF-8?q?=E5=8F=96=20useCountdown=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindContactModal/hooks/useCountdown.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 apps/web/src/components/auth/BindContactModal/hooks/useCountdown.ts diff --git a/apps/web/src/components/auth/BindContactModal/hooks/useCountdown.ts b/apps/web/src/components/auth/BindContactModal/hooks/useCountdown.ts new file mode 100644 index 000000000..e21c96f35 --- /dev/null +++ b/apps/web/src/components/auth/BindContactModal/hooks/useCountdown.ts @@ -0,0 +1,40 @@ +import { useState, useEffect, useRef, useCallback } from "react" + +/** + * 倒计时 Hook + * 用于验证码发送等场景的倒计时控制 + */ +export const useCountdown = (initialSeconds = 60) => { + const [countdown, setCountdown] = useState(0) + const timerRef = useRef | null>(null) + + useEffect(() => { + if (countdown > 0) { + timerRef.current = setInterval(() => { + setCountdown((prev) => prev - 1) + }, 1000) + } else if (timerRef.current) { + clearInterval(timerRef.current) + timerRef.current = null + } + return () => { + if (timerRef.current) clearInterval(timerRef.current) + } + }, [countdown]) + + const start = useCallback(() => { + setCountdown(initialSeconds) + }, [initialSeconds]) + + const reset = useCallback(() => { + setCountdown(0) + }, []) + + return { + countdown, + isRunning: countdown > 0, + start, + reset, + } +} +