modal.tsx 5.1 KB

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