var-item.tsx 3.6 KB

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