item.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import { getLanguage } from '@/i18n/language'
  14. type Props = {
  15. collection: Collection
  16. icon: JSX.Element
  17. payload: Tool
  18. isInToolsPage: boolean
  19. isToolNumMax: boolean
  20. added?: boolean
  21. onAdd?: (payload: Tool) => void
  22. }
  23. const Item: FC<Props> = ({
  24. collection,
  25. icon,
  26. payload,
  27. isInToolsPage,
  28. isToolNumMax,
  29. added,
  30. onAdd,
  31. }) => {
  32. const { t } = useTranslation()
  33. const { locale } = useContext(I18n)
  34. const language = getLanguage(locale)
  35. const isBuiltIn = collection.type === CollectionType.builtIn
  36. const isModel = collection.type === CollectionType.model
  37. const canShowDetail = isInToolsPage
  38. const [showDetail, setShowDetail] = useState(false)
  39. 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>
  40. return (
  41. <>
  42. <div
  43. className={cn(canShowDetail && 'cursor-pointer', 'flex justify-between items-start p-4 rounded-xl border border-gray-200 bg-gray-50 shadow-xs')}
  44. onClick={() => canShowDetail && setShowDetail(true)}
  45. >
  46. <div className='flex items-start w-full'>
  47. {icon}
  48. <div className='ml-3 w-0 grow'>
  49. <div className={cn('text-base font-semibold text-gray-900 truncate')}>{payload.label[language]}</div>
  50. <div className={cn('leading-[18px] text-[13px] font-normal text-gray-500')}>
  51. {payload.description[language]}
  52. </div>
  53. </div>
  54. </div>
  55. <div className='shrink-0'>
  56. {!isToolNumMax && onAdd && (
  57. !collection.is_team_authorization
  58. ? <TooltipPlus popupContent={t('tools.auth.unauthorized')}>
  59. {addBtn}
  60. </TooltipPlus>
  61. : addBtn
  62. )}
  63. </div>
  64. </div>
  65. {showDetail && (
  66. <SettingBuiltInTool
  67. collection={collection}
  68. toolName={payload.name}
  69. readonly
  70. onHide={() => {
  71. setShowDetail(false)
  72. }}
  73. isBuiltIn={isBuiltIn}
  74. isModel={isModel}
  75. />
  76. )}
  77. </>
  78. )
  79. }
  80. export default React.memo(Item)