modal.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Modal from '@/app/components/base/modal'
  5. import Button from '@/app/components/base/button'
  6. import { BookOpen01 } from '@/app/components/base/icons/src/vender/line/education'
  7. import type { ApiBasedExtension } from '@/models/common'
  8. import {
  9. addApiBasedExtension,
  10. updateApiBasedExtension,
  11. } from '@/service/common'
  12. import { useToastContext } from '@/app/components/base/toast'
  13. export type ApiBasedExtensionData = {
  14. name?: string
  15. apiEndpoint?: string
  16. apiKey?: string
  17. }
  18. type ApiBasedExtensionModalProps = {
  19. data: ApiBasedExtension
  20. onCancel: () => void
  21. onSave?: (newData: ApiBasedExtension) => void
  22. }
  23. const ApiBasedExtensionModal: FC<ApiBasedExtensionModalProps> = ({
  24. data,
  25. onCancel,
  26. onSave,
  27. }) => {
  28. const { t } = useTranslation()
  29. const [localeData, setLocaleData] = useState(data)
  30. const [loading, setLoading] = useState(false)
  31. const { notify } = useToastContext()
  32. const handleDataChange = (type: string, value: string) => {
  33. setLocaleData({ ...localeData, [type]: value })
  34. }
  35. const handleSave = async () => {
  36. setLoading(true)
  37. if (localeData && localeData.api_key && localeData.api_key?.length < 5) {
  38. notify({ type: 'error', message: t('common.apiBasedExtension.modal.apiKey.lengthError') })
  39. setLoading(false)
  40. return
  41. }
  42. try {
  43. let res: ApiBasedExtension = {}
  44. if (!data.id) {
  45. res = await addApiBasedExtension({
  46. url: '/api-based-extension',
  47. body: localeData,
  48. })
  49. }
  50. else {
  51. res = await updateApiBasedExtension({
  52. url: `/api-based-extension/${data.id}`,
  53. body: {
  54. ...localeData,
  55. api_key: data.api_key === localeData.api_key ? '[__HIDDEN__]' : localeData.api_key,
  56. },
  57. })
  58. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  59. }
  60. if (onSave)
  61. onSave(res)
  62. }
  63. finally {
  64. setLoading(false)
  65. }
  66. }
  67. return (
  68. <Modal
  69. isShow
  70. onClose={() => {}}
  71. wrapperClassName='!z-[103]'
  72. className='!p-8 !pb-6 !max-w-none !w-[640px]'
  73. >
  74. <div className='mb-2 text-xl font-semibold text-gray-900'>
  75. {
  76. data.name
  77. ? t('common.apiBasedExtension.modal.editTitle')
  78. : t('common.apiBasedExtension.modal.title')
  79. }
  80. </div>
  81. <div className='py-2'>
  82. <div className='leading-9 text-sm font-medium text-gray-900'>
  83. {t('common.apiBasedExtension.modal.name.title')}
  84. </div>
  85. <input
  86. value={localeData.name || ''}
  87. onChange={e => handleDataChange('name', e.target.value)}
  88. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  89. placeholder={t('common.apiBasedExtension.modal.name.placeholder') || ''}
  90. />
  91. </div>
  92. <div className='py-2'>
  93. <div className='flex justify-between items-center h-9 text-sm font-medium text-gray-900'>
  94. {t('common.apiBasedExtension.modal.apiEndpoint.title')}
  95. <a
  96. href={t('common.apiBasedExtension.linkUrl') || '/'}
  97. target='_blank'
  98. className='group flex items-center text-xs text-gray-500 font-normal hover:text-primary-600'
  99. >
  100. <BookOpen01 className='mr-1 w-3 h-3 text-gray-500 group-hover:text-primary-600' />
  101. {t('common.apiBasedExtension.link')}
  102. </a>
  103. </div>
  104. <input
  105. value={localeData.api_endpoint || ''}
  106. onChange={e => handleDataChange('api_endpoint', e.target.value)}
  107. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  108. placeholder={t('common.apiBasedExtension.modal.apiEndpoint.placeholder') || ''}
  109. />
  110. </div>
  111. <div className='py-2'>
  112. <div className='leading-9 text-sm font-medium text-gray-900'>
  113. {t('common.apiBasedExtension.modal.apiKey.title')}
  114. </div>
  115. <div className='flex items-center'>
  116. <input
  117. value={localeData.api_key || ''}
  118. onChange={e => handleDataChange('api_key', e.target.value)}
  119. className='block grow mr-2 px-3 h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  120. placeholder={t('common.apiBasedExtension.modal.apiKey.placeholder') || ''}
  121. />
  122. </div>
  123. </div>
  124. <div className='flex items-center justify-end mt-6'>
  125. <Button
  126. onClick={onCancel}
  127. className='mr-2 text-sm font-medium'
  128. >
  129. {t('common.operation.cancel')}
  130. </Button>
  131. <Button
  132. type='primary'
  133. className='text-sm font-medium'
  134. disabled={!localeData.name || !localeData.api_endpoint || !localeData.api_key || loading}
  135. onClick={handleSave}
  136. >
  137. {t('common.operation.save')}
  138. </Button>
  139. </div>
  140. </Modal>
  141. )
  142. }
  143. export default ApiBasedExtensionModal