import { Spin } from 'antd' import React, { FC, useState, useRef, useEffect, useMemo } from 'react' import { useTranslation } from 'react-i18next' import { LoadingOutlined } from '@ant-design/icons' const TIME = 60 export interface SendCodeProps { beforeSend: () => Promise btnRef?: React.RefObject } const useSentCounter = () => { const [countDown, setCountDown] = useState(0) const timerRef = useRef(0) useEffect(() => { return () => clearInterval(timerRef.current) }, []) useEffect(() => { if (countDown <= 0) { clearInterval(timerRef.current) } }, [countDown]) const enabled = useMemo(() => countDown <= 0, [countDown]) const send = () => { setCountDown(TIME) timerRef.current = setInterval(() => { setCountDown((prev) => { return prev - 1 }) }, 1000) } return { enabled, send, countDown, } } export const SendCodeBtn: FC = ({ beforeSend, btnRef }) => { const { enabled, send, countDown } = useSentCounter() const [loading, setLoading] = useState(false) const { t } = useTranslation() const disabled = useMemo(() => { return !enabled || loading }, [enabled, loading]) const onClick = async (e: React.MouseEvent) => { setLoading(true) if (disabled) { return } if (!(await beforeSend())) { setLoading(false) return } setLoading(false) send() } return ( ) }