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.

4.5 KB
Raw Blame
import { Alert, AlertActions, AlertTitle } from '@langgenius/dify-ui/alert'
import { Button } from '@langgenius/dify-ui/button'
import { useCallback, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Script from '@/next/script'

const TURNSTILE_SCRIPT_SRC = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'

type TurnstileRenderOptions = {
  sitekey: string
  action: string
  appearance: 'always'
  size: 'flexible'
  theme: 'auto'
  callback: (token: string) => void
  'error-callback': (errorCode: string) => boolean
  'expired-callback': () => void
  'timeout-callback': () => void
  'unsupported-callback': () => void
}

type TurnstileApi = {
  render: (container: HTMLElement, options: TurnstileRenderOptions) => string
  remove: (widgetId: string) => void
}

const getTurnstileApi = () => (window as Window & { turnstile?: TurnstileApi }).turnstile

type TurnstileProps = {
  action: 'signin_code' | 'signin_code_verify'
  resetKey?: number
  siteKey: string
  onVerify: (token: string) => void
  onInvalidate: () => void
  onError?: () => void
}

export default function Turnstile({
  action,
  resetKey = 0,
  siteKey,
  onVerify,
  onInvalidate,
  onError,
}: TurnstileProps) {
  const { t } = useTranslation(['common', 'login'])
  const containerRef = useRef<HTMLDivElement>(null)
  const onVerifyRef = useRef(onVerify)
  const onInvalidateRef = useRef(onInvalidate)
  const onErrorRef = useRef(onError)
  const [hasError, setHasError] = useState(false)
  const [isScriptReady, setIsScriptReady] = useState(false)
  const [scriptGeneration, setScriptGeneration] = useState(0)

  useEffect(() => {
    onVerifyRef.current = onVerify
    onInvalidateRef.current = onInvalidate
    onErrorRef.current = onError
  }, [onError, onInvalidate, onVerify])

  const invalidate = useCallback(() => {
    onInvalidateRef.current()
  }, [])

  const handleChallengeError = useCallback(() => {
    onErrorRef.current?.()
    setHasError(true)
  }, [])

  useEffect(() => {
    if (!isScriptReady || hasError) return

    const turnstile = getTurnstileApi()
    const container = containerRef.current
    if (!container || !turnstile) return

    let widgetId: string | undefined
    try {
      widgetId = turnstile.render(container, {
        sitekey: siteKey,
        action,
        appearance: 'always',
        size: 'flexible',
        theme: 'auto',
        callback: (token) => {
          onVerifyRef.current(token)
        },
        'error-callback': () => {
          handleChallengeError()
          return true
        },
        'expired-callback': invalidate,
        'timeout-callback': invalidate,
        'unsupported-callback': handleChallengeError,
      })
    } catch {
      queueMicrotask(handleChallengeError)
    }

    return () => {
      if (!widgetId) return
      turnstile.remove(widgetId)
    }
  }, [action, handleChallengeError, hasError, invalidate, isScriptReady, resetKey, siteKey])

  const handleScriptReady = () => {
    if (getTurnstileApi()) {
      setIsScriptReady(true)
      return
    }

    setIsScriptReady(false)
    handleChallengeError()
  }

  const handleScriptError = () => {
    setIsScriptReady(false)
    handleChallengeError()
  }

  const handleRetry = () => {
    const canReuseLoadedScript = Boolean(getTurnstileApi())

    setHasError(false)

    if (canReuseLoadedScript) {
      setIsScriptReady(true)
      return
    }

    setIsScriptReady(false)
    setScriptGeneration((current) => current + 1)
  }

  const scriptId = scriptGeneration
    ? `cloudflare-turnstile-${scriptGeneration}`
    : 'cloudflare-turnstile'
  const scriptSrc = scriptGeneration
    ? `${TURNSTILE_SCRIPT_SRC}#retry-${scriptGeneration}`
    : TURNSTILE_SCRIPT_SRC

  return (
    <>
      <div className="mt-3 grid min-h-18.25 items-center">
        {hasError && (
          <Alert type="error" className="w-full">
            <AlertTitle>{t(($) => $['turnstile.loadError'], { ns: 'login' })}</AlertTitle>
            <AlertActions>
              <Button type="button" size="small" onClick={handleRetry}>
                {t(($) => $['operation.retry'], { ns: 'common' })}
              </Button>
            </AlertActions>
          </Alert>
        )}
        <div ref={containerRef} className={`h-16.25 w-full ${hasError ? 'hidden' : ''}`} />
      </div>
      <Script
        key={scriptGeneration}
        id={scriptId}
        src={scriptSrc}
        strategy="afterInteractive"
        onReady={handleScriptReady}
        onError={handleScriptError}
      />
    </>
  )
}