index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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)
  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. const getPath = (url: string) => {
  121. if (!url)
  122. return ''
  123. try {
  124. const path = new URL(url).pathname
  125. return path || ''
  126. }
  127. catch (e) {
  128. return url
  129. }
  130. }
  131. return (
  132. <>
  133. <Drawer
  134. isShow
  135. onHide={onHide}
  136. title={t(`tools.createTool.${isAdd ? 'title' : 'editTitle'}`)!}
  137. panelClassName='mt-2 !w-[640px]'
  138. maxWidthClassName='!max-w-[640px]'
  139. height='calc(100vh - 16px)'
  140. headerClassName='!border-b-black/5'
  141. body={
  142. <div className='flex flex-col h-full'>
  143. <div className='grow h-0 overflow-y-auto px-6 py-3 space-y-4'>
  144. <div>
  145. <div className={fieldNameClassNames}>{t('tools.createTool.name')}</div>
  146. <div className='flex items-center justify-between gap-3'>
  147. <AppIcon size='large' onClick={() => { setShowEmojiPicker(true) }} className='cursor-pointer' icon={emoji.content} background={emoji.background} />
  148. <input
  149. className='h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('tools.createTool.toolNamePlaceHolder')!}
  150. value={customCollection.provider}
  151. onChange={(e) => {
  152. const newCollection = produce(customCollection, (draft) => {
  153. draft.provider = e.target.value
  154. })
  155. setCustomCollection(newCollection)
  156. }}
  157. />
  158. </div>
  159. </div>
  160. {/* Schema */}
  161. <div className='select-none'>
  162. <div className='flex justify-between items-center'>
  163. <div className='flex items-center'>
  164. <div className={fieldNameClassNames}>{t('tools.createTool.schema')}</div>
  165. <div className='mx-2 w-px h-3 bg-black/5'></div>
  166. <a
  167. href="https://swagger.io/specification/"
  168. target='_blank'
  169. className='flex items-center h-[18px] space-x-1 text-[#155EEF]'
  170. >
  171. <div className='text-xs font-normal'>{t('tools.createTool.viewSchemaSpec')}</div>
  172. <LinkExternal02 className='w-3 h-3' />
  173. </a>
  174. </div>
  175. <GetSchema onChange={setSchema} />
  176. </div>
  177. <textarea
  178. value={schema}
  179. onChange={e => setSchema(e.target.value)}
  180. 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'
  181. placeholder={t('tools.createTool.schemaPlaceHolder')!}
  182. ></textarea>
  183. </div>
  184. {/* Available Tools */}
  185. <div>
  186. <div className={fieldNameClassNames}>{t('tools.createTool.availableTools.title')}</div>
  187. <div className='rounded-lg border border-gray-200 w-full overflow-x-auto'>
  188. <table className='w-full leading-[18px] text-xs text-gray-700 font-normal'>
  189. <thead className='text-gray-500 uppercase'>
  190. <tr className={cn(paramsSchemas.length > 0 && 'border-b', 'border-gray-200')}>
  191. <th className="p-2 pl-3 font-medium">{t('tools.createTool.availableTools.name')}</th>
  192. <th className="p-2 pl-3 font-medium w-[236px]">{t('tools.createTool.availableTools.description')}</th>
  193. <th className="p-2 pl-3 font-medium">{t('tools.createTool.availableTools.method')}</th>
  194. <th className="p-2 pl-3 font-medium">{t('tools.createTool.availableTools.path')}</th>
  195. <th className="p-2 pl-3 font-medium w-[54px]">{t('tools.createTool.availableTools.action')}</th>
  196. </tr>
  197. </thead>
  198. <tbody>
  199. {paramsSchemas.map((item, index) => (
  200. <tr key={index} className='border-b last:border-0 border-gray-200'>
  201. <td className="p-2 pl-3">{item.operation_id}</td>
  202. <td className="p-2 pl-3 text-gray-500 w-[236px]">{item.summary}</td>
  203. <td className="p-2 pl-3">{item.method}</td>
  204. <td className="p-2 pl-3">{getPath(item.server_url)}</td>
  205. <td className="p-2 pl-3 w-[62px]">
  206. <Button
  207. className='!h-6 !px-2 text-xs font-medium text-gray-700 whitespace-nowrap'
  208. onClick={() => {
  209. setCurrTool(item)
  210. setIsShowTestApi(true)
  211. }}
  212. >
  213. {t('tools.createTool.availableTools.test')}
  214. </Button>
  215. </td>
  216. </tr>
  217. ))}
  218. </tbody>
  219. </table>
  220. </div>
  221. </div>
  222. {/* Authorization method */}
  223. <div>
  224. <div className={fieldNameClassNames}>{t('tools.createTool.authMethod.title')}</div>
  225. <div className='flex items-center h-9 justify-between px-2.5 bg-gray-100 rounded-lg cursor-pointer' onClick={() => setCredentialsModalShow(true)}>
  226. <div className='text-sm font-normal text-gray-900'>{t(`tools.createTool.authMethod.types.${credential.auth_type}`)}</div>
  227. <Settings01 className='w-4 h-4 text-gray-700 opacity-60' />
  228. </div>
  229. </div>
  230. <div>
  231. <div className={fieldNameClassNames}>{t('tools.createTool.privacyPolicy')}</div>
  232. <input
  233. value={customCollection.privacy_policy}
  234. onChange={(e) => {
  235. const newCollection = produce(customCollection, (draft) => {
  236. draft.privacy_policy = e.target.value
  237. })
  238. setCustomCollection(newCollection)
  239. }}
  240. className='w-full h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('tools.createTool.privacyPolicyPlaceholder') || ''} />
  241. </div>
  242. </div>
  243. <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')} >
  244. {
  245. isEdit && (
  246. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onRemove}>{t('common.operation.remove')}</Button>
  247. )
  248. }
  249. <div className='flex space-x-2 '>
  250. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onHide}>{t('common.operation.cancel')}</Button>
  251. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium' type='primary' onClick={handleSave}>{t('common.operation.save')}</Button>
  252. </div>
  253. </div>
  254. </div>
  255. }
  256. isShowMask={true}
  257. clickOutsideNotOpen={true}
  258. />
  259. {showEmojiPicker && <EmojiPicker
  260. onSelect={(icon, icon_background) => {
  261. setEmoji({ content: icon, background: icon_background })
  262. setShowEmojiPicker(false)
  263. }}
  264. onClose={() => {
  265. setShowEmojiPicker(false)
  266. }}
  267. />}
  268. {credentialsModalShow && (
  269. <ConfigCredentials
  270. credential={credential}
  271. onChange={setCredential}
  272. onHide={() => setCredentialsModalShow(false)}
  273. />)
  274. }
  275. {isShowTestApi && (
  276. <TestApi
  277. tool={currTool as CustomParamSchema}
  278. customCollection={customCollection}
  279. onHide={() => setIsShowTestApi(false)}
  280. />
  281. )}
  282. </>
  283. )
  284. }
  285. export default React.memo(EditCustomCollectionModal)