modal.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. className='!p-8 !pb-6 !max-w-none !w-[640px]'
  72. >
  73. <div className='mb-2 text-xl font-semibold text-gray-900'>
  74. {
  75. data.name
  76. ? t('common.apiBasedExtension.modal.editTitle')
  77. : t('common.apiBasedExtension.modal.title')
  78. }
  79. </div>
  80. <div className='py-2'>
  81. <div className='leading-9 text-sm font-medium text-gray-900'>
  82. {t('common.apiBasedExtension.modal.name.title')}
  83. </div>
  84. <input
  85. value={localeData.name || ''}
  86. onChange={e => handleDataChange('name', e.target.value)}
  87. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  88. placeholder={t('common.apiBasedExtension.modal.name.placeholder') || ''}
  89. />
  90. </div>
  91. <div className='py-2'>
  92. <div className='flex justify-between items-center h-9 text-sm font-medium text-gray-900'>
  93. {t('common.apiBasedExtension.modal.apiEndpoint.title')}
  94. <a
  95. href={t('common.apiBasedExtension.linkUrl') || '/'}
  96. target='_blank' rel='noopener noreferrer'
  97. className='group flex items-center text-xs text-gray-500 font-normal hover:text-primary-600'
  98. >
  99. <BookOpen01 className='mr-1 w-3 h-3 text-gray-500 group-hover:text-primary-600' />
  100. {t('common.apiBasedExtension.link')}
  101. </a>
  102. </div>
  103. <input
  104. value={localeData.api_endpoint || ''}
  105. onChange={e => handleDataChange('api_endpoint', e.target.value)}
  106. className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  107. placeholder={t('common.apiBasedExtension.modal.apiEndpoint.placeholder') || ''}
  108. />
  109. </div>
  110. <div className='py-2'>
  111. <div className='leading-9 text-sm font-medium text-gray-900'>
  112. {t('common.apiBasedExtension.modal.apiKey.title')}
  113. </div>
  114. <div className='flex items-center'>
  115. <input
  116. value={localeData.api_key || ''}
  117. onChange={e => handleDataChange('api_key', e.target.value)}
  118. className='block grow mr-2 px-3 h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none'
  119. placeholder={t('common.apiBasedExtension.modal.apiKey.placeholder') || ''}
  120. />
  121. </div>
  122. </div>
  123. <div className='flex items-center justify-end mt-6'>
  124. <Button
  125. onClick={onCancel}
  126. className='mr-2'
  127. >
  128. {t('common.operation.cancel')}
  129. </Button>
  130. <Button
  131. variant='primary'
  132. disabled={!localeData.name || !localeData.api_endpoint || !localeData.api_key || loading}
  133. onClick={handleSave}
  134. >
  135. {t('common.operation.save')}
  136. </Button>
  137. </div>
  138. </Modal>
  139. )
  140. }
  141. export default ApiBasedExtensionModal