index.tsx 6.2 KB

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