You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

verify-state-modal.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { useEffect, useRef, useState } from 'react'
  2. import { createPortal } from 'react-dom'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiExternalLinkLine,
  6. } from '@remixicon/react'
  7. import Button from '@/app/components/base/button'
  8. import { useDocLink } from '@/context/i18n'
  9. export type IConfirm = {
  10. className?: string
  11. isShow: boolean
  12. title: string
  13. content?: React.ReactNode
  14. onConfirm: () => void
  15. onCancel: () => void
  16. maskClosable?: boolean
  17. email?: string
  18. showLink?: boolean
  19. }
  20. function Confirm({
  21. isShow,
  22. title,
  23. content,
  24. onConfirm,
  25. onCancel,
  26. maskClosable = true,
  27. showLink,
  28. email,
  29. }: IConfirm) {
  30. const { t } = useTranslation()
  31. const docLink = useDocLink()
  32. const dialogRef = useRef<HTMLDivElement>(null)
  33. const [isVisible, setIsVisible] = useState(isShow)
  34. const eduDocLink = docLink('/getting-started/dify-for-education')
  35. const handleClick = () => {
  36. window.open(eduDocLink, '_blank', 'noopener,noreferrer')
  37. }
  38. useEffect(() => {
  39. const handleKeyDown = (event: KeyboardEvent) => {
  40. if (event.key === 'Escape')
  41. onCancel()
  42. }
  43. document.addEventListener('keydown', handleKeyDown)
  44. return () => {
  45. document.removeEventListener('keydown', handleKeyDown)
  46. }
  47. }, [onCancel])
  48. const handleClickOutside = (event: MouseEvent) => {
  49. if (maskClosable && dialogRef.current && !dialogRef.current.contains(event.target as Node))
  50. onCancel()
  51. }
  52. useEffect(() => {
  53. document.addEventListener('mousedown', handleClickOutside)
  54. return () => {
  55. document.removeEventListener('mousedown', handleClickOutside)
  56. }
  57. }, [maskClosable])
  58. useEffect(() => {
  59. if (isShow) {
  60. setIsVisible(true)
  61. }
  62. else {
  63. const timer = setTimeout(() => setIsVisible(false), 200)
  64. return () => clearTimeout(timer)
  65. }
  66. }, [isShow])
  67. if (!isVisible)
  68. return null
  69. return createPortal(
  70. <div className={'fixed inset-0 z-[10000000] flex items-center justify-center bg-background-overlay'}
  71. onClick={(e) => {
  72. e.preventDefault()
  73. e.stopPropagation()
  74. }}
  75. >
  76. <div ref={dialogRef} className={'relative w-full max-w-[481px] overflow-hidden'}>
  77. <div className='shadows-shadow-lg flex max-w-full flex-col items-start rounded-2xl border-[0.5px] border-solid border-components-panel-border bg-components-panel-bg'>
  78. <div className='flex flex-col items-start gap-2 self-stretch pb-4 pl-6 pr-6 pt-6'>
  79. <div className='title-2xl-semi-bold text-text-primary'>{title}</div>
  80. <div className='system-md-regular w-full text-text-tertiary'>{content}</div>
  81. </div>
  82. {email && (
  83. <div className='w-full space-y-1 px-6 py-3'>
  84. <div className='system-sm-semibold py-1 text-text-secondary'>{t('education.emailLabel')}</div>
  85. <div className='system-sm-regular rounded-lg bg-components-input-bg-disabled px-3 py-2 text-components-input-text-filled-disabled'>{email}</div>
  86. </div>
  87. )}
  88. <div className='flex items-center justify-between gap-2 self-stretch p-6'>
  89. <div className='flex items-center gap-1'>
  90. {showLink && (
  91. <>
  92. <a onClick={handleClick} href={eduDocLink} target='_blank' className='system-xs-regular cursor-pointer text-text-accent'>{t('education.learn')}</a>
  93. <RiExternalLinkLine className='h-3 w-3 text-text-accent' />
  94. </>
  95. )}
  96. </div>
  97. <Button variant='primary' className='!w-20' onClick={onConfirm}>{t('common.operation.ok')}</Button>
  98. </div>
  99. </div>
  100. </div>
  101. </div>, document.body,
  102. )
  103. }
  104. export default React.memo(Confirm)