2024-11-07 09:15:52 +08:00
|
|
|
import React, { useState } from 'react'
|
2024-11-02 14:48:55 +08:00
|
|
|
import useSWR from 'swr'
|
2024-10-12 17:08:45 +08:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2024-11-02 14:48:55 +08:00
|
|
|
import { usePluginPageContext } from '@/app/components/plugins/plugin-page/context'
|
|
|
|
import { useAppContext } from '@/context/app-context'
|
2024-10-13 10:49:55 +08:00
|
|
|
import Button from '@/app/components/base/button'
|
2024-11-02 14:48:55 +08:00
|
|
|
import Toast from '@/app/components/base/toast'
|
2024-10-13 10:49:55 +08:00
|
|
|
import Indicator from '@/app/components/header/indicator'
|
2024-11-02 14:48:55 +08:00
|
|
|
import ToolItem from '@/app/components/tools/provider/tool-item'
|
|
|
|
import ConfigCredential from '@/app/components/tools/setting/build-in/config-credentials'
|
|
|
|
import {
|
|
|
|
fetchBuiltInToolList,
|
2024-11-07 09:15:52 +08:00
|
|
|
fetchCollectionDetail,
|
2024-11-02 14:48:55 +08:00
|
|
|
removeBuiltInToolCredential,
|
|
|
|
updateBuiltInToolCredential,
|
|
|
|
} from '@/service/tools'
|
2024-10-12 16:29:46 +08:00
|
|
|
|
|
|
|
const ActionList = () => {
|
2024-10-12 17:08:45 +08:00
|
|
|
const { t } = useTranslation()
|
2024-11-02 14:48:55 +08:00
|
|
|
const { isCurrentWorkspaceManager } = useAppContext()
|
|
|
|
const currentPluginDetail = usePluginPageContext(v => v.currentPluginDetail)
|
2024-11-07 09:15:52 +08:00
|
|
|
const { data: provider } = useSWR(
|
|
|
|
`builtin/${currentPluginDetail.plugin_id}/${currentPluginDetail.name}`,
|
|
|
|
fetchCollectionDetail,
|
|
|
|
)
|
2024-11-02 14:48:55 +08:00
|
|
|
const { data } = useSWR(
|
2024-11-06 17:19:51 +08:00
|
|
|
`${currentPluginDetail.plugin_id}/${currentPluginDetail.name}`,
|
2024-11-02 14:48:55 +08:00
|
|
|
fetchBuiltInToolList,
|
|
|
|
)
|
|
|
|
|
|
|
|
const [showSettingAuth, setShowSettingAuth] = useState(false)
|
|
|
|
|
|
|
|
const handleCredentialSettingUpdate = () => {}
|
|
|
|
|
2024-11-07 09:15:52 +08:00
|
|
|
if (!data || !provider)
|
2024-11-02 14:48:55 +08:00
|
|
|
return null
|
|
|
|
|
2024-10-12 16:29:46 +08:00
|
|
|
return (
|
2024-10-13 10:49:55 +08:00
|
|
|
<div className='px-4 pt-2 pb-4'>
|
|
|
|
<div className='mb-1 py-1'>
|
|
|
|
<div className='mb-1 h-6 flex items-center justify-between text-text-secondary system-sm-semibold-uppercase'>
|
2024-11-02 14:48:55 +08:00
|
|
|
{t('plugin.detailPanel.actionNum', { num: data.length })}
|
2024-11-07 09:15:52 +08:00
|
|
|
{provider.is_team_authorization && provider.allow_delete && (
|
2024-11-02 14:48:55 +08:00
|
|
|
<Button
|
|
|
|
variant='secondary'
|
|
|
|
size='small'
|
|
|
|
onClick={() => setShowSettingAuth(true)}
|
|
|
|
disabled={!isCurrentWorkspaceManager}
|
|
|
|
>
|
|
|
|
<Indicator className='mr-2' color={'green'} />
|
|
|
|
{t('tools.auth.authorized')}
|
|
|
|
</Button>
|
|
|
|
)}
|
2024-10-13 10:49:55 +08:00
|
|
|
</div>
|
2024-11-07 09:15:52 +08:00
|
|
|
{!provider.is_team_authorization && provider.allow_delete && (
|
2024-11-02 14:48:55 +08:00
|
|
|
<Button
|
|
|
|
variant='primary'
|
|
|
|
className='w-full'
|
|
|
|
onClick={() => setShowSettingAuth(true)}
|
|
|
|
disabled={!isCurrentWorkspaceManager}
|
|
|
|
>{t('tools.auth.unauthorized')}</Button>
|
|
|
|
)}
|
2024-10-13 10:49:55 +08:00
|
|
|
</div>
|
|
|
|
<div className='flex flex-col gap-2'>
|
2024-11-02 14:48:55 +08:00
|
|
|
{data.map(tool => (
|
|
|
|
<ToolItem
|
|
|
|
key={`${currentPluginDetail.plugin_id}${tool.name}`}
|
|
|
|
disabled={false}
|
2024-11-07 09:15:52 +08:00
|
|
|
collection={provider}
|
2024-11-02 14:48:55 +08:00
|
|
|
tool={tool}
|
|
|
|
isBuiltIn={true}
|
|
|
|
isModel={false}
|
|
|
|
/>
|
|
|
|
))}
|
2024-10-12 17:08:45 +08:00
|
|
|
</div>
|
2024-11-02 14:48:55 +08:00
|
|
|
{showSettingAuth && (
|
|
|
|
<ConfigCredential
|
2024-11-07 09:15:52 +08:00
|
|
|
collection={provider}
|
2024-11-02 14:48:55 +08:00
|
|
|
onCancel={() => setShowSettingAuth(false)}
|
|
|
|
onSaved={async (value) => {
|
2024-11-07 09:15:52 +08:00
|
|
|
await updateBuiltInToolCredential(provider.name, value)
|
2024-11-02 14:48:55 +08:00
|
|
|
Toast.notify({
|
|
|
|
type: 'success',
|
|
|
|
message: t('common.api.actionSuccess'),
|
|
|
|
})
|
|
|
|
handleCredentialSettingUpdate()
|
|
|
|
setShowSettingAuth(false)
|
|
|
|
}}
|
|
|
|
onRemove={async () => {
|
2024-11-07 09:15:52 +08:00
|
|
|
await removeBuiltInToolCredential(provider.name)
|
2024-11-02 14:48:55 +08:00
|
|
|
Toast.notify({
|
|
|
|
type: 'success',
|
|
|
|
message: t('common.api.actionSuccess'),
|
|
|
|
})
|
|
|
|
handleCredentialSettingUpdate()
|
|
|
|
setShowSettingAuth(false)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2024-10-12 16:29:46 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ActionList
|