index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import produce from 'immer'
  6. import { useDebounce, useGetState } from 'ahooks'
  7. import { clone } from 'lodash-es'
  8. import cn from 'classnames'
  9. import { LinkExternal02, Settings01 } from '../../base/icons/src/vender/line/general'
  10. import type { Credential, CustomCollectionBackend, CustomParamSchema, Emoji } from '../types'
  11. import { AuthType } from '../types'
  12. import GetSchema from './get-schema'
  13. import ConfigCredentials from './config-credentials'
  14. import TestApi from './test-api'
  15. import Drawer from '@/app/components/base/drawer-plus'
  16. import Button from '@/app/components/base/button'
  17. import EmojiPicker from '@/app/components/base/emoji-picker'
  18. import AppIcon from '@/app/components/base/app-icon'
  19. import { parseParamsSchema } from '@/service/tools'
  20. const fieldNameClassNames = 'py-2 leading-5 text-sm font-medium text-gray-900'
  21. type Props = {
  22. payload: any
  23. onHide: () => void
  24. onAdd?: (payload: CustomCollectionBackend) => void
  25. onRemove?: () => void
  26. onEdit?: (payload: CustomCollectionBackend) => void
  27. }
  28. // Add and Edit
  29. const EditCustomCollectionModal: FC<Props> = ({
  30. payload,
  31. onHide,
  32. onAdd,
  33. onEdit,
  34. onRemove,
  35. }) => {
  36. const { t } = useTranslation()
  37. const isAdd = !payload
  38. const isEdit = !!payload
  39. const [editFirst, setEditFirst] = useState(!isAdd)
  40. const [paramsSchemas, setParamsSchemas] = useState<CustomParamSchema[]>(payload?.tools || [])
  41. const [customCollection, setCustomCollection, getCustomCollection] = useGetState<CustomCollectionBackend>(isAdd
  42. ? {
  43. provider: '',
  44. credentials: {
  45. auth_type: AuthType.none,
  46. },
  47. icon: {
  48. content: '🕵️',
  49. background: '#FEF7C3',
  50. },
  51. schema_type: '',
  52. schema: '',
  53. }
  54. : payload)
  55. const originalProvider = isEdit ? payload.provider : ''
  56. const [showEmojiPicker, setShowEmojiPicker] = useState(false)
  57. const emoji = customCollection.icon
  58. const setEmoji = (emoji: Emoji) => {
  59. const newCollection = produce(customCollection, (draft) => {
  60. draft.icon = emoji
  61. })
  62. setCustomCollection(newCollection)
  63. }
  64. const schema = customCollection.schema
  65. const debouncedSchema = useDebounce(schema, { wait: 500 })
  66. const setSchema = (schema: string) => {
  67. const newCollection = produce(customCollection, (draft) => {
  68. draft.schema = schema
  69. })
  70. setCustomCollection(newCollection)
  71. }
  72. useEffect(() => {
  73. if (!debouncedSchema)
  74. return
  75. if (isEdit && editFirst) {
  76. setEditFirst(false)
  77. return
  78. }
  79. (async () => {
  80. const customCollection = getCustomCollection()
  81. try {
  82. const { parameters_schema, schema_type } = await parseParamsSchema(debouncedSchema) as any
  83. const newCollection = produce(customCollection, (draft) => {
  84. draft.schema_type = schema_type
  85. })
  86. setCustomCollection(newCollection)
  87. setParamsSchemas(parameters_schema)
  88. }
  89. catch (e) {
  90. const newCollection = produce(customCollection, (draft) => {
  91. draft.schema_type = ''
  92. })
  93. setCustomCollection(newCollection)
  94. setParamsSchemas([])
  95. }
  96. })()
  97. }, [debouncedSchema])
  98. const [credentialsModalShow, setCredentialsModalShow] = useState(false)
  99. const credential = customCollection.credentials
  100. const setCredential = (credential: Credential) => {
  101. const newCollection = produce(customCollection, (draft) => {
  102. draft.credentials = credential
  103. })
  104. setCustomCollection(newCollection)
  105. }
  106. const [currTool, setCurrTool] = useState<CustomParamSchema | null>(null)
  107. const [isShowTestApi, setIsShowTestApi] = useState(false)
  108. const handleSave = () => {
  109. const postData = clone(customCollection)
  110. delete postData.tools
  111. if (isAdd) {
  112. onAdd?.(postData)
  113. return
  114. }
  115. onEdit?.({
  116. ...postData,
  117. original_provider: originalProvider,
  118. })
  119. }
  120. return (
  121. <>
  122. <Drawer
  123. isShow
  124. onHide={onHide}
  125. title={t(`tools.createTool.${isAdd ? 'title' : 'editTitle'}`)!}
  126. panelClassName='mt-2 !w-[640px]'
  127. maxWidthClassName='!max-w-[640px]'
  128. height='calc(100vh - 16px)'
  129. headerClassName='!border-b-black/5'
  130. body={
  131. <div className='flex flex-col h-full'>
  132. <div className='grow h-0 overflow-y-auto px-6 py-3 space-y-4'>
  133. <div>
  134. <div className={fieldNameClassNames}>{t('tools.createTool.name')}</div>
  135. <div className='flex items-center justify-between gap-3'>
  136. <AppIcon size='large' onClick={() => { setShowEmojiPicker(true) }} className='cursor-pointer' icon={emoji.content} background={emoji.background} />
  137. <input
  138. className='h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('tools.createTool.toolNamePlaceHolder')!}
  139. value={customCollection.provider}
  140. onChange={(e) => {
  141. const newCollection = produce(customCollection, (draft) => {
  142. draft.provider = e.target.value
  143. })
  144. setCustomCollection(newCollection)
  145. }}
  146. />
  147. </div>
  148. </div>
  149. {/* Schema */}
  150. <div className='select-none'>
  151. <div className='flex justify-between items-center'>
  152. <div className='flex items-center'>
  153. <div className={fieldNameClassNames}>{t('tools.createTool.schema')}</div>
  154. <div className='mx-2 w-px h-3 bg-black/5'></div>
  155. <a
  156. href="https://swagger.io/specification/"
  157. target='_blank'
  158. className='flex items-center h-[18px] space-x-1 text-[#155EEF]'
  159. >
  160. <div className='text-xs font-normal'>{t('tools.createTool.viewSchemaSpec')}</div>
  161. <LinkExternal02 className='w-3 h-3' />
  162. </a>
  163. </div>
  164. <GetSchema onChange={setSchema} />
  165. </div>
  166. <textarea
  167. value={schema}
  168. onChange={e => setSchema(e.target.value)}
  169. className='w-full h-[240px] px-3 py-2 leading-4 text-xs font-normal text-gray-900 bg-gray-100 rounded-lg overflow-y-auto'
  170. placeholder={t('tools.createTool.schemaPlaceHolder')!}
  171. ></textarea>
  172. </div>
  173. {/* Available Tools */}
  174. <div>
  175. <div className={fieldNameClassNames}>{t('tools.createTool.availableTools.title')}</div>
  176. <div className='rounded-lg border border-gray-200 w-full overflow-x-auto'>
  177. <table className='w-full leading-[18px] text-xs text-gray-700 font-normal'>
  178. <thead className='text-gray-500 uppercase'>
  179. <tr className={cn(paramsSchemas.length > 0 && 'border-b', 'border-gray-200')}>
  180. <th className="p-2 pl-3 font-medium">{t('tools.createTool.availableTools.name')}</th>
  181. <th className="p-2 pl-3 font-medium w-[236px]">{t('tools.createTool.availableTools.description')}</th>
  182. <th className="p-2 pl-3 font-medium">{t('tools.createTool.availableTools.method')}</th>
  183. <th className="p-2 pl-3 font-medium">{t('tools.createTool.availableTools.path')}</th>
  184. <th className="p-2 pl-3 font-medium w-[54px]">{t('tools.createTool.availableTools.action')}</th>
  185. </tr>
  186. </thead>
  187. <tbody>
  188. {paramsSchemas.map((item, index) => (
  189. <tr key={index} className='border-b last:border-0 border-gray-200'>
  190. <td className="p-2 pl-3">{item.operation_id}</td>
  191. <td className="p-2 pl-3 text-gray-500 w-[236px]">{item.summary}</td>
  192. <td className="p-2 pl-3">{item.method}</td>
  193. <td className="p-2 pl-3">{item.server_url ? new URL(item.server_url).pathname : ''}</td>
  194. <td className="p-2 pl-3 w-[62px]">
  195. <Button
  196. className='!h-6 !px-2 text-xs font-medium text-gray-700'
  197. onClick={() => {
  198. setCurrTool(item)
  199. setIsShowTestApi(true)
  200. }}
  201. >
  202. {t('tools.createTool.availableTools.test')}
  203. </Button>
  204. </td>
  205. </tr>
  206. ))}
  207. </tbody>
  208. </table>
  209. </div>
  210. </div>
  211. {/* Authorization method */}
  212. <div>
  213. <div className={fieldNameClassNames}>{t('tools.createTool.authMethod.title')}</div>
  214. <div className='flex items-center h-9 justify-between px-2.5 bg-gray-100 rounded-lg cursor-pointer' onClick={() => setCredentialsModalShow(true)}>
  215. <div className='text-sm font-normal text-gray-900'>{t(`tools.createTool.authMethod.types.${credential.auth_type}`)}</div>
  216. <Settings01 className='w-4 h-4 text-gray-700 opacity-60' />
  217. </div>
  218. </div>
  219. <div>
  220. <div className={fieldNameClassNames}>{t('tools.createTool.privacyPolicy')}</div>
  221. <input
  222. value={customCollection.privacy_policy}
  223. onChange={(e) => {
  224. const newCollection = produce(customCollection, (draft) => {
  225. draft.privacy_policy = e.target.value
  226. })
  227. setCustomCollection(newCollection)
  228. }}
  229. className='w-full h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('tools.createTool.privacyPolicyPlaceholder') || ''} />
  230. </div>
  231. </div>
  232. <div className={cn(isEdit ? 'justify-between' : 'justify-end', 'mt-2 shrink-0 flex py-4 px-6 rounded-b-[10px] bg-gray-50 border-t border-black/5')} >
  233. {
  234. isEdit && (
  235. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onRemove}>{t('common.operation.remove')}</Button>
  236. )
  237. }
  238. <div className='flex space-x-2 '>
  239. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onHide}>{t('common.operation.cancel')}</Button>
  240. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium' type='primary' onClick={handleSave}>{t('common.operation.save')}</Button>
  241. </div>
  242. </div>
  243. </div>
  244. }
  245. isShowMask={true}
  246. clickOutsideNotOpen={true}
  247. />
  248. {showEmojiPicker && <EmojiPicker
  249. onSelect={(icon, icon_background) => {
  250. setEmoji({ content: icon, background: icon_background })
  251. setShowEmojiPicker(false)
  252. }}
  253. onClose={() => {
  254. setShowEmojiPicker(false)
  255. }}
  256. />}
  257. {credentialsModalShow && (
  258. <ConfigCredentials
  259. credential={credential}
  260. onChange={setCredential}
  261. onHide={() => setCredentialsModalShow(false)}
  262. />)
  263. }
  264. {isShowTestApi && (
  265. <TestApi
  266. tool={currTool as CustomParamSchema}
  267. customCollection={customCollection}
  268. onHide={() => setIsShowTestApi(false)}
  269. />
  270. )}
  271. </>
  272. )
  273. }
  274. export default React.memo(EditCustomCollectionModal)