item.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useContext } from 'use-context-selector'
  5. import cn from 'classnames'
  6. import { useTranslation } from 'react-i18next'
  7. import type { Collection, Tool } from '../types'
  8. import Button from '../../base/button'
  9. import { CollectionType } from '../types'
  10. import TooltipPlus from '../../base/tooltip-plus'
  11. import I18n from '@/context/i18n'
  12. import SettingBuiltInTool from '@/app/components/app/configuration/config/agent/agent-tools/setting-built-in-tool'
  13. type Props = {
  14. collection: Collection
  15. icon: JSX.Element
  16. payload: Tool
  17. isInToolsPage: boolean
  18. isToolNumMax: boolean
  19. added?: boolean
  20. onAdd?: (payload: Tool) => void
  21. }
  22. const Item: FC<Props> = ({
  23. collection,
  24. icon,
  25. payload,
  26. isInToolsPage,
  27. isToolNumMax,
  28. added,
  29. onAdd,
  30. }) => {
  31. const { t } = useTranslation()
  32. const { locale } = useContext(I18n)
  33. const isBuiltIn = collection.type === CollectionType.builtIn
  34. const canShowDetail = !isBuiltIn || (isBuiltIn && isInToolsPage)
  35. const [showDetail, setShowDetail] = useState(false)
  36. const addBtn = <Button className='shrink-0 flex items-center h-7 !px-3 !text-xs !font-medium !text-gray-700' disabled={added || !collection.is_team_authorization} onClick={() => onAdd?.(payload)}>{t(`common.operation.${added ? 'added' : 'add'}`)}</Button>
  37. return (
  38. <>
  39. <div
  40. className={cn(canShowDetail && 'cursor-pointer', 'flex justify-between items-start p-4 rounded-xl border border-gray-200 bg-gray-50 shadow-xs')}
  41. onClick={() => canShowDetail && setShowDetail(true)}
  42. >
  43. <div className='flex items-start w-full'>
  44. {icon}
  45. <div className='ml-3 w-0 grow'>
  46. <div className={cn('text-base font-semibold text-gray-900 truncate')}>{payload.label[locale === 'en' ? 'en_US' : 'zh_Hans']}</div>
  47. <div className={cn('leading-[18px] text-[13px] font-normal text-gray-500')}>
  48. {payload.description[locale === 'en' ? 'en_US' : 'zh_Hans']}
  49. </div>
  50. </div>
  51. </div>
  52. <div className='shrink-0'>
  53. {!isToolNumMax && onAdd && (
  54. !collection.is_team_authorization
  55. ? <TooltipPlus popupContent={t('tools.auth.unauthorized')}>
  56. {addBtn}
  57. </TooltipPlus>
  58. : addBtn
  59. )}
  60. </div>
  61. </div>
  62. {showDetail && isBuiltIn && (
  63. <SettingBuiltInTool
  64. collection={collection}
  65. toolName={payload.name}
  66. readonly
  67. onHide={() => {
  68. setShowDetail(false)
  69. }}
  70. />
  71. )}
  72. </>
  73. )
  74. }
  75. export default React.memo(Item)