index.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { useCallback, useState } from 'react'
  2. import type { FC } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import { Portal } from '@headlessui/react'
  6. import type { FormValue, ProviderConfigModal } from '../declarations'
  7. import { ConfigurableProviders } from '../utils'
  8. import Form from './Form'
  9. import I18n from '@/context/i18n'
  10. import Button from '@/app/components/base/button'
  11. import { Lock01 } from '@/app/components/base/icons/src/vender/solid/security'
  12. import { LinkExternal02 } from '@/app/components/base/icons/src/vender/line/general'
  13. import { AlertCircle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback'
  14. import { useEventEmitterContextContext } from '@/context/event-emitter'
  15. type ModelModalProps = {
  16. isShow: boolean
  17. onCancel: () => void
  18. modelModal?: ProviderConfigModal
  19. onSave: (v?: FormValue) => void
  20. mode: string
  21. }
  22. const ModelModal: FC<ModelModalProps> = ({
  23. isShow,
  24. onCancel,
  25. modelModal,
  26. onSave,
  27. mode,
  28. }) => {
  29. const { t } = useTranslation()
  30. const { locale } = useContext(I18n)
  31. const { eventEmitter } = useEventEmitterContextContext()
  32. const [value, setValue] = useState<FormValue | undefined>()
  33. const [loading, setLoading] = useState(false)
  34. const [errorMessage, setErrorMessage] = useState('')
  35. const [cleared, setCleared] = useState(false)
  36. const [prevIsShow, setPrevIsShow] = useState(isShow)
  37. const [validating, setValidating] = useState(false)
  38. if (prevIsShow !== isShow) {
  39. setCleared(false)
  40. setPrevIsShow(isShow)
  41. }
  42. eventEmitter?.useSubscription((v) => {
  43. if (v === 'provider-save')
  44. setLoading(true)
  45. else
  46. setLoading(false)
  47. })
  48. const handleValidatedError = useCallback((newErrorMessage: string) => {
  49. setErrorMessage(newErrorMessage)
  50. }, [])
  51. const handleValidating = useCallback((newValidating: boolean) => {
  52. setValidating(newValidating)
  53. }, [])
  54. const validateRequiredValue = () => {
  55. const validateValue = value || modelModal?.defaultValue
  56. if (modelModal) {
  57. const { fields } = modelModal
  58. const requiredFields = fields.filter(field => !(typeof field.hidden === 'function' ? field.hidden(validateValue) : field.hidden) && field.required)
  59. for (let i = 0; i < requiredFields.length; i++) {
  60. const currentField = requiredFields[i]
  61. if (!validateValue?.[currentField.key]) {
  62. setErrorMessage(t('appDebug.errorMessage.valueOfVarRequired', { key: currentField.label[locale] }) || '')
  63. return false
  64. }
  65. }
  66. return true
  67. }
  68. }
  69. const handleSave = () => {
  70. if (validateRequiredValue())
  71. onSave(value || modelModal?.defaultValue)
  72. }
  73. const renderTitlePrefix = () => {
  74. let prefix
  75. if (mode === 'edit')
  76. prefix = t('common.operation.edit')
  77. else
  78. prefix = ConfigurableProviders.includes(modelModal!.key) ? t('common.operation.create') : t('common.operation.setup')
  79. return `${prefix} ${modelModal?.title[locale]}`
  80. }
  81. if (!isShow)
  82. return null
  83. return (
  84. <Portal>
  85. <div className='fixed inset-0 flex items-center justify-center bg-black/[.25]'>
  86. <div className='w-[640px] max-h-[calc(100vh-120px)] bg-white shadow-xl rounded-2xl overflow-y-auto'>
  87. <div className='px-8 pt-8'>
  88. <div className='flex justify-between items-center mb-2'>
  89. <div className='text-xl font-semibold text-gray-900'>{renderTitlePrefix()}</div>
  90. {modelModal?.icon}
  91. </div>
  92. <Form
  93. modelModal={modelModal}
  94. fields={modelModal?.fields || []}
  95. initValue={modelModal?.defaultValue}
  96. onChange={newValue => setValue(newValue)}
  97. onValidatedError={handleValidatedError}
  98. mode={mode}
  99. cleared={cleared}
  100. onClearedChange={setCleared}
  101. onValidating={handleValidating}
  102. />
  103. <div className='flex justify-between items-center py-6'>
  104. <a
  105. href={modelModal?.link.href}
  106. target='_blank'
  107. className='inline-flex items-center text-xs text-primary-600'
  108. >
  109. {modelModal?.link.label[locale]}
  110. <LinkExternal02 className='ml-1 w-3 h-3' />
  111. </a>
  112. <div>
  113. <Button className='mr-2 !h-9 !text-sm font-medium text-gray-700' onClick={onCancel}>{t('common.operation.cancel')}</Button>
  114. <Button
  115. className='!h-9 !text-sm font-medium'
  116. type='primary'
  117. onClick={handleSave}
  118. disabled={loading || (mode === 'edit' && !cleared) || validating}
  119. >
  120. {t('common.operation.save')}
  121. </Button>
  122. </div>
  123. </div>
  124. </div>
  125. <div className='border-t-[0.5px] border-t-[rgba(0,0,0,0.05)]'>
  126. {
  127. errorMessage
  128. ? (
  129. <div className='flex px-[10px] py-3 bg-[#FEF3F2] text-xs text-[#D92D20]'>
  130. <AlertCircle className='mt-[1px] mr-2 w-[14px] h-[14px]' />
  131. {errorMessage}
  132. </div>
  133. )
  134. : (
  135. <div className='flex justify-center items-center py-3 bg-gray-50 text-xs text-gray-500'>
  136. <Lock01 className='mr-1 w-3 h-3 text-gray-500' />
  137. {t('common.modelProvider.encrypted.front')}
  138. <a
  139. className='text-primary-600 mx-1'
  140. target={'_blank'}
  141. href='https://pycryptodome.readthedocs.io/en/latest/src/cipher/oaep.html'
  142. >
  143. PKCS1_OAEP
  144. </a>
  145. {t('common.modelProvider.encrypted.back')}
  146. </div>
  147. )
  148. }
  149. </div>
  150. </div>
  151. </div>
  152. </Portal>
  153. )
  154. }
  155. export default ModelModal