var-item.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useRef } from 'react'
  4. import { useBoolean, useHover } from 'ahooks'
  5. import { useTranslation } from 'react-i18next'
  6. import InputVarTypeIcon from '../../_base/components/input-var-type-icon'
  7. import type { InputVar, MoreInfo } from '@/app/components/workflow/types'
  8. import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
  9. import { Edit03 } from '@/app/components/base/icons/src/vender/solid/general'
  10. import { Trash03 } from '@/app/components/base/icons/src/vender/line/general'
  11. import ConfigVarModal from '@/app/components/app/configuration/config-var/config-modal'
  12. type Props = {
  13. readonly: boolean
  14. payload: InputVar
  15. onChange?: (item: InputVar, moreInfo?: MoreInfo) => void
  16. onRemove?: () => void
  17. rightContent?: JSX.Element
  18. varKeys?: string[]
  19. }
  20. const VarItem: FC<Props> = ({
  21. readonly,
  22. payload,
  23. onChange = () => { },
  24. onRemove = () => { },
  25. rightContent,
  26. varKeys = [],
  27. }) => {
  28. const { t } = useTranslation()
  29. const ref = useRef(null)
  30. const isHovering = useHover(ref)
  31. const [isShowEditVarModal, {
  32. setTrue: showEditVarModal,
  33. setFalse: hideEditVarModal,
  34. }] = useBoolean(false)
  35. const handlePayloadChange = useCallback((payload: InputVar, moreInfo?: MoreInfo) => {
  36. onChange(payload, moreInfo)
  37. hideEditVarModal()
  38. }, [onChange, hideEditVarModal])
  39. return (
  40. <div ref={ref} className='flex items-center h-8 justify-between px-2.5 bg-white rounded-lg border border-gray-200 shadow-xs cursor-pointer hover:shadow-md'>
  41. <div className='flex items-center space-x-1 grow w-0'>
  42. <Variable02 className='w-3.5 h-3.5 text-primary-500' />
  43. <div title={payload.variable} className='shrink-0 max-w-[130px] truncate text-[13px] font-medium text-gray-700'>{payload.variable}</div>
  44. {payload.label && (<><div className='shrink-0 text-xs font-medium text-gray-400'>·</div>
  45. <div title={payload.label as string} className='max-w-[130px] truncate text-[13px] font-medium text-gray-500'>{payload.label as string}</div>
  46. </>)}
  47. </div>
  48. <div className='shrink-0 ml-2 flex items-center'>
  49. {rightContent || (<>
  50. {(!isHovering || readonly)
  51. ? (
  52. <>
  53. {payload.required && (
  54. <div className='mr-2 text-xs font-normal text-gray-500'>{t('workflow.nodes.start.required')}</div>
  55. )}
  56. <InputVarTypeIcon type={payload.type} className='w-3.5 h-3.5 text-gray-500' />
  57. </>
  58. )
  59. : (!readonly && (
  60. <>
  61. <div onClick={showEditVarModal} className='mr-1 p-1 rounded-md cursor-pointer hover:bg-black/5'>
  62. <Edit03 className='w-4 h-4 text-gray-500' />
  63. </div>
  64. <div onClick={onRemove} className='p-1 rounded-md cursor-pointer hover:bg-black/5'>
  65. <Trash03 className='w-4 h-4 text-gray-500' />
  66. </div>
  67. </>
  68. ))}
  69. </>)}
  70. </div>
  71. {
  72. isShowEditVarModal && (
  73. <ConfigVarModal
  74. isShow
  75. payload={payload}
  76. onClose={hideEditVarModal}
  77. onConfirm={handlePayloadChange}
  78. varKeys={varKeys}
  79. />
  80. )
  81. }
  82. </div>
  83. )
  84. }
  85. export default React.memo(VarItem)