panel.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import type {
  2. FC,
  3. ReactElement,
  4. } from 'react'
  5. import {
  6. cloneElement,
  7. memo,
  8. useCallback,
  9. } from 'react'
  10. import cn from 'classnames'
  11. import { useShallow } from 'zustand/react/shallow'
  12. import { useTranslation } from 'react-i18next'
  13. import NextStep from './components/next-step'
  14. import PanelOperator from './components/panel-operator'
  15. import {
  16. DescriptionInput,
  17. TitleInput,
  18. } from './components/title-description-input'
  19. import { useResizePanel } from './hooks/use-resize-panel'
  20. import {
  21. XClose,
  22. } from '@/app/components/base/icons/src/vender/line/general'
  23. import BlockIcon from '@/app/components/workflow/block-icon'
  24. import {
  25. useNodeDataUpdate,
  26. useNodesExtraData,
  27. useNodesInteractions,
  28. useNodesReadOnly,
  29. useNodesSyncDraft,
  30. useToolIcon,
  31. useWorkflow,
  32. } from '@/app/components/workflow/hooks'
  33. import { canRunBySingle } from '@/app/components/workflow/utils'
  34. import { Play } from '@/app/components/base/icons/src/vender/line/mediaAndDevices'
  35. import TooltipPlus from '@/app/components/base/tooltip-plus'
  36. import type { Node } from '@/app/components/workflow/types'
  37. import { useStore as useAppStore } from '@/app/components/app/store'
  38. type BasePanelProps = {
  39. children: ReactElement
  40. } & Node
  41. const BasePanel: FC<BasePanelProps> = ({
  42. id,
  43. data,
  44. children,
  45. }) => {
  46. const { t } = useTranslation()
  47. const { showMessageLogModal } = useAppStore(useShallow(state => ({
  48. showMessageLogModal: state.showMessageLogModal,
  49. })))
  50. const panelWidth = localStorage.getItem('workflow-node-panel-width') ? parseFloat(localStorage.getItem('workflow-node-panel-width')!) : 420
  51. const {
  52. setPanelWidth,
  53. } = useWorkflow()
  54. const { handleNodeSelect } = useNodesInteractions()
  55. const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  56. const { nodesReadOnly } = useNodesReadOnly()
  57. const nodesExtraData = useNodesExtraData()
  58. const availableNextNodes = nodesExtraData[data.type].availableNextNodes
  59. const toolIcon = useToolIcon(data)
  60. const handleResize = useCallback((width: number) => {
  61. setPanelWidth(width)
  62. }, [setPanelWidth])
  63. const {
  64. triggerRef,
  65. containerRef,
  66. } = useResizePanel({
  67. direction: 'horizontal',
  68. triggerDirection: 'left',
  69. minWidth: 420,
  70. maxWidth: 720,
  71. onResize: handleResize,
  72. })
  73. const {
  74. handleNodeDataUpdate,
  75. handleNodeDataUpdateWithSyncDraft,
  76. } = useNodeDataUpdate()
  77. const handleTitleBlur = useCallback((title: string) => {
  78. handleNodeDataUpdateWithSyncDraft({ id, data: { title } })
  79. }, [handleNodeDataUpdateWithSyncDraft, id])
  80. const handleDescriptionChange = useCallback((desc: string) => {
  81. handleNodeDataUpdateWithSyncDraft({ id, data: { desc } })
  82. }, [handleNodeDataUpdateWithSyncDraft, id])
  83. return (
  84. <div className={cn(
  85. 'relative mr-2 h-full',
  86. showMessageLogModal && '!absolute !mr-0 w-[384px] overflow-hidden -top-[5px] right-[416px] z-0 shadow-lg border-[0.5px] border-gray-200 rounded-2xl transition-all',
  87. )}>
  88. <div
  89. ref={triggerRef}
  90. className='absolute top-1/2 -translate-y-1/2 -left-2 w-3 h-6 cursor-col-resize resize-x'>
  91. <div className='w-1 h-6 bg-gray-300 rounded-sm'></div>
  92. </div>
  93. <div
  94. ref={containerRef}
  95. className='relative h-full bg-white shadow-lg border-[0.5px] border-gray-200 rounded-2xl overflow-y-auto'
  96. style={{
  97. width: `${panelWidth}px`,
  98. }}
  99. >
  100. <div className='sticky top-0 bg-white border-b-[0.5px] border-black/5 z-10'>
  101. <div className='flex items-center px-4 pt-4 pb-1'>
  102. <BlockIcon
  103. className='shrink-0 mr-1'
  104. type={data.type}
  105. toolIcon={toolIcon}
  106. size='md'
  107. />
  108. <TitleInput
  109. value={data.title || ''}
  110. onBlur={handleTitleBlur}
  111. />
  112. <div className='shrink-0 flex items-center text-gray-500'>
  113. {
  114. canRunBySingle(data.type) && !nodesReadOnly && (
  115. <TooltipPlus
  116. popupContent={t('workflow.panel.runThisStep')}
  117. >
  118. <div
  119. className='flex items-center justify-center mr-1 w-6 h-6 rounded-md hover:bg-black/5 cursor-pointer'
  120. onClick={() => {
  121. handleNodeDataUpdate({ id, data: { _isSingleRun: true } })
  122. handleSyncWorkflowDraft(true)
  123. }}
  124. >
  125. <Play className='w-4 h-4 text-gray-500' />
  126. </div>
  127. </TooltipPlus>
  128. )
  129. }
  130. <PanelOperator id={id} data={data} />
  131. <div className='mx-3 w-[1px] h-3.5 bg-gray-200' />
  132. <div
  133. className='flex items-center justify-center w-6 h-6 cursor-pointer'
  134. onClick={() => handleNodeSelect(id, true)}
  135. >
  136. <XClose className='w-4 h-4' />
  137. </div>
  138. </div>
  139. </div>
  140. <div className='p-2'>
  141. <DescriptionInput
  142. value={data.desc || ''}
  143. onChange={handleDescriptionChange}
  144. />
  145. </div>
  146. </div>
  147. <div className='py-2'>
  148. {cloneElement(children, { id, data })}
  149. </div>
  150. {
  151. !!availableNextNodes.length && (
  152. <div className='p-4 border-t-[0.5px] border-t-black/5'>
  153. <div className='flex items-center mb-1 text-gray-700 text-[13px] font-semibold'>
  154. {t('workflow.panel.nextStep').toLocaleUpperCase()}
  155. </div>
  156. <div className='mb-2 text-xs text-gray-400'>
  157. {t('workflow.panel.addNextStep')}
  158. </div>
  159. <NextStep selectedNode={{ id, data } as Node} />
  160. </div>
  161. )
  162. }
  163. </div>
  164. </div>
  165. )
  166. }
  167. export default memo(BasePanel)