action-list.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useAppContext } from '@/context/app-context'
  4. import Button from '@/app/components/base/button'
  5. import Toast from '@/app/components/base/toast'
  6. import Indicator from '@/app/components/header/indicator'
  7. import ToolItem from '@/app/components/tools/provider/tool-item'
  8. import ConfigCredential from '@/app/components/tools/setting/build-in/config-credentials'
  9. import {
  10. useAllToolProviders,
  11. useBuiltinTools,
  12. useInvalidateAllToolProviders,
  13. useRemoveProviderCredentials,
  14. useUpdateProviderCredentials,
  15. } from '@/service/use-tools'
  16. import type { PluginDetail } from '@/app/components/plugins/types'
  17. type Props = {
  18. detail: PluginDetail
  19. }
  20. const ActionList = ({
  21. detail,
  22. }: Props) => {
  23. const { t } = useTranslation()
  24. const { isCurrentWorkspaceManager } = useAppContext()
  25. const providerBriefInfo = detail.declaration.tool.identity
  26. const providerKey = `${detail.plugin_id}/${providerBriefInfo.name}`
  27. const { data: collectionList = [] } = useAllToolProviders()
  28. const invalidateAllToolProviders = useInvalidateAllToolProviders()
  29. const provider = useMemo(() => {
  30. return collectionList.find(collection => collection.name === providerKey)
  31. }, [collectionList, providerKey])
  32. const { data } = useBuiltinTools(providerKey)
  33. const [showSettingAuth, setShowSettingAuth] = useState(false)
  34. const handleCredentialSettingUpdate = () => {
  35. invalidateAllToolProviders()
  36. Toast.notify({
  37. type: 'success',
  38. message: t('common.api.actionSuccess'),
  39. })
  40. setShowSettingAuth(false)
  41. }
  42. const { mutate: updatePermission, isPending } = useUpdateProviderCredentials({
  43. onSuccess: handleCredentialSettingUpdate,
  44. })
  45. const { mutate: removePermission } = useRemoveProviderCredentials({
  46. onSuccess: handleCredentialSettingUpdate,
  47. })
  48. if (!data || !provider)
  49. return null
  50. return (
  51. <div className='px-4 pt-2 pb-4'>
  52. <div className='mb-1 py-1'>
  53. <div className='mb-1 h-6 flex items-center justify-between text-text-secondary system-sm-semibold-uppercase'>
  54. {t('plugin.detailPanel.actionNum', { num: data.length, action: data.length > 1 ? 'actions' : 'action' })}
  55. {provider.is_team_authorization && provider.allow_delete && (
  56. <Button
  57. variant='secondary'
  58. size='small'
  59. onClick={() => setShowSettingAuth(true)}
  60. disabled={!isCurrentWorkspaceManager}
  61. >
  62. <Indicator className='mr-2' color={'green'} />
  63. {t('tools.auth.authorized')}
  64. </Button>
  65. )}
  66. </div>
  67. {!provider.is_team_authorization && provider.allow_delete && (
  68. <Button
  69. variant='primary'
  70. className='w-full'
  71. onClick={() => setShowSettingAuth(true)}
  72. disabled={!isCurrentWorkspaceManager}
  73. >{t('tools.auth.unauthorized')}</Button>
  74. )}
  75. </div>
  76. <div className='flex flex-col gap-2'>
  77. {data.map(tool => (
  78. <ToolItem
  79. key={`${detail.plugin_id}${tool.name}`}
  80. disabled={false}
  81. collection={provider}
  82. tool={tool}
  83. isBuiltIn={true}
  84. isModel={false}
  85. />
  86. ))}
  87. </div>
  88. {showSettingAuth && (
  89. <ConfigCredential
  90. collection={provider}
  91. onCancel={() => setShowSettingAuth(false)}
  92. onSaved={async value => updatePermission({
  93. providerName: provider.name,
  94. credentials: value,
  95. })}
  96. onRemove={async () => removePermission(provider.name)}
  97. isSaving={isPending}
  98. />
  99. )}
  100. </div>
  101. )
  102. }
  103. export default ActionList