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.

modal.tsx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. import { noop } from 'lodash-es'
  14. export type ApiBasedExtensionData = {
  15. name?: string
  16. apiEndpoint?: string
  17. apiKey?: string
  18. }
  19. type ApiBasedExtensionModalProps = {
  20. data: ApiBasedExtension
  21. onCancel: () => void
  22. onSave?: (newData: ApiBasedExtension) => void
  23. }
  24. const ApiBasedExtensionModal: FC<ApiBasedExtensionModalProps> = ({
  25. data,
  26. onCancel,
  27. onSave,
  28. }) => {
  29. const { t } = useTranslation()
  30. const [localeData, setLocaleData] = useState(data)
  31. const [loading, setLoading] = useState(false)
  32. const { notify } = useToastContext()
  33. const handleDataChange = (type: string, value: string) => {
  34. setLocaleData({ ...localeData, [type]: value })
  35. }
  36. const handleSave = async () => {
  37. setLoading(true)
  38. if (localeData && localeData.api_key && localeData.api_key?.length < 5) {
  39. notify({ type: 'error', message: t('common.apiBasedExtension.modal.apiKey.lengthError') })
  40. setLoading(false)
  41. return
  42. }
  43. try {
  44. let res: ApiBasedExtension = {}
  45. if (!data.id) {
  46. res = await addApiBasedExtension({
  47. url: '/api-based-extension',
  48. body: localeData,
  49. })
  50. }
  51. else {
  52. res = await updateApiBasedExtension({
  53. url: `/api-based-extension/${data.id}`,
  54. body: {
  55. ...localeData,
  56. api_key: data.api_key === localeData.api_key ? '[__HIDDEN__]' : localeData.api_key,
  57. },
  58. })
  59. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  60. }
  61. if (onSave)
  62. onSave(res)
  63. }
  64. finally {
  65. setLoading(false)
  66. }
  67. }
  68. return (
  69. <Modal
  70. isShow
  71. onClose={noop}
  72. className='!w-[640px] !max-w-none !p-8 !pb-6'
  73. >
  74. <div className='mb-2 text-xl font-semibold text-text-primary'>
  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='text-sm font-medium leading-9 text-text-primary'>
  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 h-9 w-full appearance-none rounded-lg bg-components-input-bg-normal px-3 text-sm text-text-primary outline-none'
  89. placeholder={t('common.apiBasedExtension.modal.name.placeholder') || ''}
  90. />
  91. </div>
  92. <div className='py-2'>
  93. <div className='flex h-9 items-center justify-between text-sm font-medium text-text-primary'>
  94. {t('common.apiBasedExtension.modal.apiEndpoint.title')}
  95. <a
  96. href={t('common.apiBasedExtension.linkUrl') || '/'}
  97. target='_blank' rel='noopener noreferrer'
  98. className='group flex items-center text-xs font-normal text-text-accent'
  99. >
  100. <BookOpen01 className='mr-1 h-3 w-3' />
  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 h-9 w-full appearance-none rounded-lg bg-components-input-bg-normal px-3 text-sm text-text-primary outline-none'
  108. placeholder={t('common.apiBasedExtension.modal.apiEndpoint.placeholder') || ''}
  109. />
  110. </div>
  111. <div className='py-2'>
  112. <div className='text-sm font-medium leading-9 text-text-primary'>
  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='mr-2 block h-9 grow appearance-none rounded-lg bg-components-input-bg-normal px-3 text-sm text-text-primary outline-none'
  120. placeholder={t('common.apiBasedExtension.modal.apiKey.placeholder') || ''}
  121. />
  122. </div>
  123. </div>
  124. <div className='mt-6 flex items-center justify-end'>
  125. <Button
  126. onClick={onCancel}
  127. className='mr-2'
  128. >
  129. {t('common.operation.cancel')}
  130. </Button>
  131. <Button
  132. variant='primary'
  133. disabled={!localeData.name || !localeData.api_endpoint || !localeData.api_key || loading}
  134. onClick={handleSave}
  135. >
  136. {t('common.operation.save')}
  137. </Button>
  138. </div>
  139. </Modal>
  140. )
  141. }
  142. export default ApiBasedExtensionModal