modal.tsx 5.2 KB

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