import { memo, useMemo, useState, } from 'react' import { useTranslation } from 'react-i18next' import { RiCheckLine, RiDeleteBinLine, RiEditLine, RiEqualizer2Line, } from '@remixicon/react' import Indicator from '@/app/components/header/indicator' import Badge from '@/app/components/base/badge' import ActionButton from '@/app/components/base/action-button' import Tooltip from '@/app/components/base/tooltip' import Button from '@/app/components/base/button' import Input from '@/app/components/base/input' import cn from '@/utils/classnames' import type { Credential } from '../types' import { CredentialTypeEnum } from '../types' type ItemProps = { credential: Credential disabled?: boolean onDelete?: (id: string) => void onEdit?: (id: string, values: Record) => void onSetDefault?: (id: string) => void onRename?: (payload: { credential_id: string name: string }) => void disableRename?: boolean disableEdit?: boolean disableDelete?: boolean disableSetDefault?: boolean onItemClick?: (id: string) => void showSelectedIcon?: boolean selectedCredentialId?: string } const Item = ({ credential, disabled, onDelete, onEdit, onSetDefault, onRename, disableRename, disableEdit, disableDelete, disableSetDefault, onItemClick, showSelectedIcon, selectedCredentialId, }: ItemProps) => { const { t } = useTranslation() const [renaming, setRenaming] = useState(false) const [renameValue, setRenameValue] = useState(credential.name) const isOAuth = credential.credential_type === CredentialTypeEnum.OAUTH2 const showAction = useMemo(() => { return !(disableRename && disableEdit && disableDelete && disableSetDefault) }, [disableRename, disableEdit, disableDelete, disableSetDefault]) return (
onItemClick?.(credential.id === '__workspace_default__' ? '' : credential.id)} > { renaming && (
setRenameValue(e.target.value)} placeholder={t('common.placeholder.input')} onClick={e => e.stopPropagation()} />
) } { !renaming && (
{ showSelectedIcon && (
{ selectedCredentialId === credential.id && ( ) }
) }
{credential.name}
{ credential.is_default && ( {t('plugin.auth.default')} ) }
) } { showAction && !renaming && (
{ !credential.is_default && !disableSetDefault && ( ) } { !disableRename && ( { e.stopPropagation() setRenaming(true) setRenameValue(credential.name) }} > ) } { !isOAuth && !disableEdit && ( { e.stopPropagation() onEdit?.( credential.id, { ...credential.credentials, __name__: credential.name, __credential_id__: credential.id, }, ) }} > ) } { !disableDelete && ( { e.stopPropagation() onDelete?.(credential.id) }} > ) }
) }
) } export default memo(Item)