var-item.tsx 3.3 KB

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