index.tsx 12 KB

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