Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.4 KB
Raw Blame
'use client'
import { useCountDown } from 'ahooks'
import { Suspense, use, useEffect, useState } from 'react'
import { browser } from 'react-dom'
import { useTranslation } from 'react-i18next'
import { COUNT_DOWN_TIME_MS, useCountdownLeftTimeValue, useSetCountdownLeftTime } from './storage'

type CountdownProps = {
  onResend?: () => void
  resendDisabled?: boolean
  restartOnResend?: boolean
}

export default function Countdown({
  onResend,
  resendDisabled,
  restartOnResend = true,
}: CountdownProps) {
  return (
    <Suspense fallback={<CountdownFallback />}>
      <CountdownContent
        onResend={onResend}
        resendDisabled={resendDisabled}
        restartOnResend={restartOnResend}
      />
    </Suspense>
  )
}

function CountdownFallback() {
  const { t } = useTranslation(['login'])

  return (
    <p className="system-xs-regular text-text-tertiary">
      <span>{t(($) => $['checkCode.didNotReceiveCode'], { ns: 'login' })}</span>
    </p>
  )
}

function CountdownContent({ onResend, resendDisabled, restartOnResend }: CountdownProps) {
  use(browser())

  const { t } = useTranslation(['login'])
  const storedLeftTime = useCountdownLeftTimeValue()
  const setStoredLeftTime = useSetCountdownLeftTime()
  const [leftTime, setLeftTime] = useState(() => Number(storedLeftTime || COUNT_DOWN_TIME_MS))
  const [time] = useCountDown({
    leftTime,
    onEnd: () => {
      setLeftTime(0)
      setStoredLeftTime(null)
    },
  })

  const resend = function () {
    if (restartOnResend) {
      setLeftTime(COUNT_DOWN_TIME_MS)
      setStoredLeftTime(`${COUNT_DOWN_TIME_MS}`)
    }
    onResend?.()
  }

  useEffect(() => {
    setStoredLeftTime(`${time}`)
  }, [setStoredLeftTime, time])

  return (
    <p className="system-xs-regular text-text-tertiary">
      <span>{t(($) => $['checkCode.didNotReceiveCode'], { ns: 'login' })}</span>
      {time > 0 && <span>{Math.round(time / 1000)}s</span>}
      {time <= 0 && (
        <button
          type="button"
          className="cursor-pointer border-none bg-transparent p-0 text-left system-xs-medium text-text-accent-secondary focus-visible:ring-1 focus-visible:ring-components-input-border-active focus-visible:outline-hidden disabled:cursor-not-allowed disabled:text-text-disabled"
          disabled={resendDisabled}
          onClick={resend}
        >
          {t(($) => $['checkCode.resend'], { ns: 'login' })}
        </button>
      )}
    </p>
  )
}