136 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-06-27 15:01:33 +08:00
import {
2025-06-30 16:16:16 +08:00
memo,
2025-06-27 15:01:33 +08:00
useCallback,
useMemo,
} from 'react'
2025-07-21 17:40:19 +08:00
import { useTranslation } from 'react-i18next'
2025-06-27 15:01:33 +08:00
import {
RiDeleteBinLine,
2025-07-21 17:40:19 +08:00
RiEditLine,
RiEqualizer2Line,
RiHome9Line,
2025-06-27 15:01:33 +08:00
RiStickyNoteAddLine,
} from '@remixicon/react'
import Dropdown from '@/app/components/base/dropdown'
import type { Item } from '@/app/components/base/dropdown'
2025-07-21 17:40:19 +08:00
import type {
DataSourceCredential,
} from './types'
import { CredentialTypeEnum } from '@/app/components/plugins/plugin-auth/types'
2025-06-27 15:01:33 +08:00
2025-07-21 17:40:19 +08:00
type OperatorProps = {
credentialItem: DataSourceCredential
onAction: (action: string, credentialItem: DataSourceCredential) => void
onRename?: () => void
}
const Operator = ({
credentialItem,
onAction,
onRename,
}: OperatorProps) => {
const { t } = useTranslation()
const {
type,
} = credentialItem
2025-06-27 15:01:33 +08:00
const items = useMemo(() => {
2025-07-21 17:40:19 +08:00
const commonItems = [
2025-06-27 15:01:33 +08:00
{
2025-07-21 17:40:19 +08:00
value: 'setDefault',
2025-06-27 15:01:33 +08:00
text: (
2025-07-21 17:40:19 +08:00
<div className='flex items-center'>
<RiHome9Line className='mr-2 h-4 w-4 text-text-tertiary' />
<div className='system-sm-semibold text-text-secondary'>{t('plugin.auth.setDefault')}</div>
2025-06-27 15:01:33 +08:00
</div>
),
},
2025-07-22 10:39:19 +08:00
...(
type === CredentialTypeEnum.OAUTH2
? [
{
value: 'rename',
text: (
<div className='flex items-center'>
<RiEditLine className='mr-2 h-4 w-4 text-text-tertiary' />
<div className='system-sm-semibold text-text-secondary'>{t('common.operation.rename')}</div>
</div>
),
},
]
: []
),
...(
type === CredentialTypeEnum.API_KEY
? [
{
value: 'edit',
text: (
<div className='flex items-center'>
<RiEqualizer2Line className='mr-2 h-4 w-4 text-text-tertiary' />
<div className='system-sm-semibold text-text-secondary'>{t('common.operation.edit')}</div>
</div>
),
},
]
: []
),
2025-06-27 15:01:33 +08:00
]
2025-07-21 17:40:19 +08:00
if (type === CredentialTypeEnum.OAUTH2) {
const oAuthItems = [
{
value: 'change',
text: (
<div className='flex items-center'>
2025-07-22 10:39:19 +08:00
<RiStickyNoteAddLine className='mr-2 h-4 w-4 text-text-tertiary' />
<div className='system-sm-semibold mb-1 text-text-secondary'>{t('common.dataSource.notion.changeAuthorizedPages')}</div>
2025-07-21 17:40:19 +08:00
</div>
),
},
]
commonItems.push(...oAuthItems)
}
return commonItems
}, [t, type])
2025-06-27 15:01:33 +08:00
const secondItems = useMemo(() => {
return [
{
value: 'delete',
text: (
<div className='flex items-center'>
<RiDeleteBinLine className='mr-2 h-4 w-4 text-text-tertiary' />
2025-07-22 10:39:19 +08:00
<div className='system-sm-semibold text-text-secondary'>
{t('common.operation.remove')}
</div>
2025-06-27 15:01:33 +08:00
</div>
),
},
]
}, [])
const handleSelect = useCallback((item: Item) => {
2025-07-21 17:40:19 +08:00
if (item.value === 'rename') {
onRename?.()
return
}
onAction(
item.value as string,
credentialItem,
)
}, [onAction, credentialItem, onRename])
2025-06-27 15:01:33 +08:00
return (
2025-07-21 17:40:19 +08:00
<Dropdown
items={items}
secondItems={secondItems}
onSelect={handleSelect}
popupClassName='z-[61]'
triggerProps={{
size: 'l',
}}
itemClassName='py-2 h-auto hover:bg-state-base-hover'
secondItemClassName='py-2 h-auto hover:bg-state-base-hover'
/>
2025-06-27 15:01:33 +08:00
)
}
2025-06-30 16:16:16 +08:00
export default memo(Operator)