credential-item.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {
  2. memo,
  3. useMemo,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. RiCheckLine,
  8. RiDeleteBinLine,
  9. RiEqualizer2Line,
  10. } from '@remixicon/react'
  11. import Indicator from '@/app/components/header/indicator'
  12. import ActionButton from '@/app/components/base/action-button'
  13. import Tooltip from '@/app/components/base/tooltip'
  14. import cn from '@/utils/classnames'
  15. import type { Credential } from '../../declarations'
  16. import Badge from '@/app/components/base/badge'
  17. type CredentialItemProps = {
  18. credential: Credential
  19. disabled?: boolean
  20. onDelete?: (credential: Credential) => void
  21. onEdit?: (credential?: Credential) => void
  22. onItemClick?: (credential: Credential) => void
  23. disableRename?: boolean
  24. disableEdit?: boolean
  25. disableDelete?: boolean
  26. showSelectedIcon?: boolean
  27. selectedCredentialId?: string
  28. }
  29. const CredentialItem = ({
  30. credential,
  31. disabled,
  32. onDelete,
  33. onEdit,
  34. onItemClick,
  35. disableRename,
  36. disableEdit,
  37. disableDelete,
  38. showSelectedIcon,
  39. selectedCredentialId,
  40. }: CredentialItemProps) => {
  41. const { t } = useTranslation()
  42. const showAction = useMemo(() => {
  43. return !(disableRename && disableEdit && disableDelete)
  44. }, [disableRename, disableEdit, disableDelete])
  45. const Item = (
  46. <div
  47. key={credential.credential_id}
  48. className={cn(
  49. 'group flex h-8 items-center rounded-lg p-1 hover:bg-state-base-hover',
  50. (disabled || credential.not_allowed_to_use) && 'cursor-not-allowed opacity-50',
  51. )}
  52. onClick={() => {
  53. if (disabled || credential.not_allowed_to_use)
  54. return
  55. onItemClick?.(credential)
  56. }}
  57. >
  58. <div className='flex w-0 grow items-center space-x-1.5'>
  59. {
  60. showSelectedIcon && (
  61. <div className='h-4 w-4'>
  62. {
  63. selectedCredentialId === credential.credential_id && (
  64. <RiCheckLine className='h-4 w-4 text-text-accent' />
  65. )
  66. }
  67. </div>
  68. )
  69. }
  70. <Indicator className='ml-2 mr-1.5 shrink-0' />
  71. <div
  72. className='system-md-regular truncate text-text-secondary'
  73. title={credential.credential_name}
  74. >
  75. {credential.credential_name}
  76. </div>
  77. </div>
  78. {
  79. credential.from_enterprise && (
  80. <Badge className='shrink-0'>
  81. Enterprise
  82. </Badge>
  83. )
  84. }
  85. {
  86. showAction && (
  87. <div className='ml-2 hidden shrink-0 items-center group-hover:flex'>
  88. {
  89. !disableEdit && !credential.not_allowed_to_use && !credential.from_enterprise && (
  90. <Tooltip popupContent={t('common.operation.edit')}>
  91. <ActionButton
  92. disabled={disabled}
  93. onClick={(e) => {
  94. e.stopPropagation()
  95. onEdit?.(credential)
  96. }}
  97. >
  98. <RiEqualizer2Line className='h-4 w-4 text-text-tertiary' />
  99. </ActionButton>
  100. </Tooltip>
  101. )
  102. }
  103. {
  104. !disableDelete && !credential.from_enterprise && (
  105. <Tooltip popupContent={t('common.operation.delete')}>
  106. <ActionButton
  107. className='hover:bg-transparent'
  108. disabled={disabled}
  109. onClick={(e) => {
  110. e.stopPropagation()
  111. onDelete?.(credential)
  112. }}
  113. >
  114. <RiDeleteBinLine className='h-4 w-4 text-text-tertiary hover:text-text-destructive' />
  115. </ActionButton>
  116. </Tooltip>
  117. )
  118. }
  119. </div>
  120. )
  121. }
  122. </div>
  123. )
  124. if (credential.not_allowed_to_use) {
  125. return (
  126. <Tooltip popupContent={t('plugin.auth.customCredentialUnavailable')}>
  127. {Item}
  128. </Tooltip>
  129. )
  130. }
  131. return Item
  132. }
  133. export default memo(CredentialItem)