You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

content.tsx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. 'use client'
  2. import React, { useCallback, useEffect } from 'react'
  3. import type { FC } from 'react'
  4. import { useBoolean } from 'ahooks'
  5. import copy from 'copy-to-clipboard'
  6. import { useTranslation } from 'react-i18next'
  7. import { useAppContext } from '@/context/app-context'
  8. import {
  9. RiCloseLine,
  10. RiLoader2Line,
  11. RiLoopLeftLine,
  12. } from '@remixicon/react'
  13. import type { ToolWithProvider } from '../../../workflow/types'
  14. import Icon from '@/app/components/plugins/card/base/card-icon'
  15. import ActionButton from '@/app/components/base/action-button'
  16. import Button from '@/app/components/base/button'
  17. import Confirm from '@/app/components/base/confirm'
  18. import Indicator from '@/app/components/header/indicator'
  19. import Tooltip from '@/app/components/base/tooltip'
  20. import MCPModal from '../modal'
  21. import OperationDropdown from './operation-dropdown'
  22. import ListLoading from './list-loading'
  23. import ToolItem from './tool-item'
  24. import {
  25. useAuthorizeMCP,
  26. useDeleteMCP,
  27. useInvalidateMCPTools,
  28. useMCPTools,
  29. useUpdateMCP,
  30. useUpdateMCPTools,
  31. } from '@/service/use-tools'
  32. import { openOAuthPopup } from '@/hooks/use-oauth'
  33. import cn from '@/utils/classnames'
  34. type Props = {
  35. detail: ToolWithProvider
  36. onUpdate: (isDelete?: boolean) => void
  37. onHide: () => void
  38. isTriggerAuthorize: boolean
  39. onFirstCreate: () => void
  40. }
  41. const MCPDetailContent: FC<Props> = ({
  42. detail,
  43. onUpdate,
  44. onHide,
  45. isTriggerAuthorize,
  46. onFirstCreate,
  47. }) => {
  48. const { t } = useTranslation()
  49. const { isCurrentWorkspaceManager } = useAppContext()
  50. const { data, isFetching: isGettingTools } = useMCPTools(detail.is_team_authorization ? detail.id : '')
  51. const invalidateMCPTools = useInvalidateMCPTools()
  52. const { mutateAsync: updateTools, isPending: isUpdating } = useUpdateMCPTools()
  53. const { mutateAsync: authorizeMcp, isPending: isAuthorizing } = useAuthorizeMCP()
  54. const toolList = data?.tools || []
  55. const [isShowUpdateConfirm, {
  56. setTrue: showUpdateConfirm,
  57. setFalse: hideUpdateConfirm,
  58. }] = useBoolean(false)
  59. const handleUpdateTools = useCallback(async () => {
  60. hideUpdateConfirm()
  61. if (!detail)
  62. return
  63. await updateTools(detail.id)
  64. invalidateMCPTools(detail.id)
  65. onUpdate()
  66. }, [detail, hideUpdateConfirm, invalidateMCPTools, onUpdate, updateTools])
  67. const { mutateAsync: updateMCP } = useUpdateMCP({})
  68. const { mutateAsync: deleteMCP } = useDeleteMCP({})
  69. const [isShowUpdateModal, {
  70. setTrue: showUpdateModal,
  71. setFalse: hideUpdateModal,
  72. }] = useBoolean(false)
  73. const [isShowDeleteConfirm, {
  74. setTrue: showDeleteConfirm,
  75. setFalse: hideDeleteConfirm,
  76. }] = useBoolean(false)
  77. const [deleting, {
  78. setTrue: showDeleting,
  79. setFalse: hideDeleting,
  80. }] = useBoolean(false)
  81. const handleOAuthCallback = useCallback(() => {
  82. if (!isCurrentWorkspaceManager)
  83. return
  84. if (!detail.id)
  85. return
  86. handleUpdateTools()
  87. }, [detail.id, handleUpdateTools, isCurrentWorkspaceManager])
  88. const handleAuthorize = useCallback(async () => {
  89. onFirstCreate()
  90. if (!isCurrentWorkspaceManager)
  91. return
  92. if (!detail)
  93. return
  94. const res = await authorizeMcp({
  95. provider_id: detail.id,
  96. })
  97. if (res.result === 'success')
  98. handleUpdateTools()
  99. else if (res.authorization_url)
  100. openOAuthPopup(res.authorization_url, handleOAuthCallback)
  101. }, [onFirstCreate, isCurrentWorkspaceManager, detail, authorizeMcp, handleUpdateTools, handleOAuthCallback])
  102. const handleUpdate = useCallback(async (data: any) => {
  103. if (!detail)
  104. return
  105. const res = await updateMCP({
  106. ...data,
  107. provider_id: detail.id,
  108. })
  109. if ((res as any)?.result === 'success') {
  110. hideUpdateModal()
  111. onUpdate()
  112. handleAuthorize()
  113. }
  114. }, [detail, updateMCP, hideUpdateModal, onUpdate, handleAuthorize])
  115. const handleDelete = useCallback(async () => {
  116. if (!detail)
  117. return
  118. showDeleting()
  119. const res = await deleteMCP(detail.id)
  120. hideDeleting()
  121. if ((res as any)?.result === 'success') {
  122. hideDeleteConfirm()
  123. onUpdate(true)
  124. }
  125. }, [detail, showDeleting, deleteMCP, hideDeleting, hideDeleteConfirm, onUpdate])
  126. useEffect(() => {
  127. if (isTriggerAuthorize)
  128. handleAuthorize()
  129. }, [])
  130. if (!detail)
  131. return null
  132. return (
  133. <>
  134. <div className={cn('shrink-0 border-b border-divider-subtle bg-components-panel-bg p-4 pb-3')}>
  135. <div className='flex'>
  136. <div className='shrink-0 overflow-hidden rounded-xl border border-components-panel-border-subtle'>
  137. <Icon src={detail.icon} />
  138. </div>
  139. <div className='ml-3 w-0 grow'>
  140. <div className='flex h-5 items-center'>
  141. <div className='system-md-semibold truncate text-text-primary' title={detail.name}>{detail.name}</div>
  142. </div>
  143. <div className='mt-0.5 flex items-center gap-1'>
  144. <Tooltip popupContent={t('tools.mcp.identifier')}>
  145. <div className='system-xs-regular shrink-0 cursor-pointer text-text-secondary' onClick={() => copy(detail.server_identifier || '')}>{detail.server_identifier}</div>
  146. </Tooltip>
  147. <div className='system-xs-regular shrink-0 text-text-quaternary'>·</div>
  148. <Tooltip popupContent={t('tools.mcp.modal.serverUrl')}>
  149. <div className='system-xs-regular truncate text-text-secondary'>{detail.server_url}</div>
  150. </Tooltip>
  151. </div>
  152. </div>
  153. <div className='flex gap-1'>
  154. <OperationDropdown
  155. onEdit={showUpdateModal}
  156. onRemove={showDeleteConfirm}
  157. />
  158. <ActionButton onClick={onHide}>
  159. <RiCloseLine className='h-4 w-4' />
  160. </ActionButton>
  161. </div>
  162. </div>
  163. <div className='mt-5'>
  164. {!isAuthorizing && detail.is_team_authorization && (
  165. <Button
  166. variant='secondary'
  167. className='w-full'
  168. onClick={handleAuthorize}
  169. disabled={!isCurrentWorkspaceManager}
  170. >
  171. <Indicator className='mr-2' color={'green'} />
  172. {t('tools.auth.authorized')}
  173. </Button>
  174. )}
  175. {!detail.is_team_authorization && !isAuthorizing && (
  176. <Button
  177. variant='primary'
  178. className='w-full'
  179. onClick={handleAuthorize}
  180. disabled={!isCurrentWorkspaceManager}
  181. >
  182. {t('tools.mcp.authorize')}
  183. </Button>
  184. )}
  185. {isAuthorizing && (
  186. <Button
  187. variant='primary'
  188. className='w-full'
  189. disabled
  190. >
  191. <RiLoader2Line className={cn('mr-1 h-4 w-4 animate-spin')} />
  192. {t('tools.mcp.authorizing')}
  193. </Button>
  194. )}
  195. </div>
  196. </div>
  197. <div className='flex grow flex-col'>
  198. {((detail.is_team_authorization && isGettingTools) || isUpdating) && (
  199. <>
  200. <div className='flex shrink-0 justify-between gap-2 px-4 pb-1 pt-2'>
  201. <div className='flex h-6 items-center'>
  202. {!isUpdating && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.gettingTools')}</div>}
  203. {isUpdating && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.updateTools')}</div>}
  204. </div>
  205. <div></div>
  206. </div>
  207. <div className='flex h-full w-full grow flex-col overflow-hidden px-4 pb-4'>
  208. <ListLoading />
  209. </div>
  210. </>
  211. )}
  212. {!isUpdating && detail.is_team_authorization && !isGettingTools && !toolList.length && (
  213. <div className='flex h-full w-full flex-col items-center justify-center'>
  214. <div className='system-sm-regular mb-3 text-text-tertiary'>{t('tools.mcp.toolsEmpty')}</div>
  215. <Button
  216. variant='primary'
  217. onClick={handleUpdateTools}
  218. >{t('tools.mcp.getTools')}</Button>
  219. </div>
  220. )}
  221. {!isUpdating && !isGettingTools && toolList.length > 0 && (
  222. <>
  223. <div className='flex shrink-0 justify-between gap-2 px-4 pb-1 pt-2'>
  224. <div className='flex h-6 items-center'>
  225. {toolList.length > 1 && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.toolsNum', { count: toolList.length })}</div>}
  226. {toolList.length === 1 && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.onlyTool')}</div>}
  227. </div>
  228. <div>
  229. <Button size='small' onClick={showUpdateConfirm}>
  230. <RiLoopLeftLine className='mr-1 h-3.5 w-3.5' />
  231. {t('tools.mcp.update')}
  232. </Button>
  233. </div>
  234. </div>
  235. <div className='flex h-0 w-full grow flex-col gap-2 overflow-y-auto px-4 pb-4'>
  236. {toolList.map(tool => (
  237. <ToolItem
  238. key={`${detail.id}${tool.name}`}
  239. tool={tool}
  240. />
  241. ))}
  242. </div>
  243. </>
  244. )}
  245. {!isUpdating && !detail.is_team_authorization && (
  246. <div className='flex h-full w-full flex-col items-center justify-center'>
  247. {!isAuthorizing && <div className='system-md-medium mb-1 text-text-secondary'>{t('tools.mcp.authorizingRequired')}</div>}
  248. {isAuthorizing && <div className='system-md-medium mb-1 text-text-secondary'>{t('tools.mcp.authorizing')}</div>}
  249. <div className='system-sm-regular text-text-tertiary'>{t('tools.mcp.authorizeTip')}</div>
  250. </div>
  251. )}
  252. </div>
  253. {isShowUpdateModal && (
  254. <MCPModal
  255. data={detail}
  256. show={isShowUpdateModal}
  257. onConfirm={handleUpdate}
  258. onHide={hideUpdateModal}
  259. />
  260. )}
  261. {isShowDeleteConfirm && (
  262. <Confirm
  263. isShow
  264. title={t('tools.mcp.delete')}
  265. content={
  266. <div>
  267. {t('tools.mcp.deleteConfirmTitle', { mcp: detail.name })}
  268. </div>
  269. }
  270. onCancel={hideDeleteConfirm}
  271. onConfirm={handleDelete}
  272. isLoading={deleting}
  273. isDisabled={deleting}
  274. />
  275. )}
  276. {isShowUpdateConfirm && (
  277. <Confirm
  278. isShow
  279. title={t('tools.mcp.toolUpdateConfirmTitle')}
  280. content={t('tools.mcp.toolUpdateConfirmContent')}
  281. onCancel={hideUpdateConfirm}
  282. onConfirm={handleUpdateTools}
  283. />
  284. )}
  285. </>
  286. )
  287. }
  288. export default MCPDetailContent