item.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 { getModelRuntimeSupported } from '@/utils/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 = getModelRuntimeSupported(locale)
  35. const isBuiltIn = collection.type === CollectionType.builtIn
  36. const canShowDetail = !isBuiltIn || (isBuiltIn && isInToolsPage)
  37. const [showDetail, setShowDetail] = useState(false)
  38. 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>
  39. return (
  40. <>
  41. <div
  42. className={cn(canShowDetail && 'cursor-pointer', 'flex justify-between items-start p-4 rounded-xl border border-gray-200 bg-gray-50 shadow-xs')}
  43. onClick={() => canShowDetail && setShowDetail(true)}
  44. >
  45. <div className='flex items-start w-full'>
  46. {icon}
  47. <div className='ml-3 w-0 grow'>
  48. <div className={cn('text-base font-semibold text-gray-900 truncate')}>{payload.label[language]}</div>
  49. <div className={cn('leading-[18px] text-[13px] font-normal text-gray-500')}>
  50. {payload.description[language]}
  51. </div>
  52. </div>
  53. </div>
  54. <div className='shrink-0'>
  55. {!isToolNumMax && onAdd && (
  56. !collection.is_team_authorization
  57. ? <TooltipPlus popupContent={t('tools.auth.unauthorized')}>
  58. {addBtn}
  59. </TooltipPlus>
  60. : addBtn
  61. )}
  62. </div>
  63. </div>
  64. {showDetail && isBuiltIn && (
  65. <SettingBuiltInTool
  66. collection={collection}
  67. toolName={payload.name}
  68. readonly
  69. onHide={() => {
  70. setShowDetail(false)
  71. }}
  72. />
  73. )}
  74. </>
  75. )
  76. }
  77. export default React.memo(Item)