panel.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 HelpLink from './components/help-link'
  16. import {
  17. DescriptionInput,
  18. TitleInput,
  19. } from './components/title-description-input'
  20. import { useResizePanel } from './hooks/use-resize-panel'
  21. import {
  22. XClose,
  23. } from '@/app/components/base/icons/src/vender/line/general'
  24. import BlockIcon from '@/app/components/workflow/block-icon'
  25. import {
  26. useAvailableBlocks,
  27. useNodeDataUpdate,
  28. useNodesInteractions,
  29. useNodesReadOnly,
  30. useNodesSyncDraft,
  31. useToolIcon,
  32. useWorkflow,
  33. } from '@/app/components/workflow/hooks'
  34. import { canRunBySingle } from '@/app/components/workflow/utils'
  35. import { Play } from '@/app/components/base/icons/src/vender/line/mediaAndDevices'
  36. import TooltipPlus from '@/app/components/base/tooltip-plus'
  37. import type { Node } from '@/app/components/workflow/types'
  38. import { useStore as useAppStore } from '@/app/components/app/store'
  39. type BasePanelProps = {
  40. children: ReactElement
  41. } & Node
  42. const BasePanel: FC<BasePanelProps> = ({
  43. id,
  44. data,
  45. children,
  46. }) => {
  47. const { t } = useTranslation()
  48. const { showMessageLogModal } = useAppStore(useShallow(state => ({
  49. showMessageLogModal: state.showMessageLogModal,
  50. })))
  51. const panelWidth = localStorage.getItem('workflow-node-panel-width') ? parseFloat(localStorage.getItem('workflow-node-panel-width')!) : 420
  52. const {
  53. setPanelWidth,
  54. } = useWorkflow()
  55. const { handleNodeSelect } = useNodesInteractions()
  56. const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  57. const { nodesReadOnly } = useNodesReadOnly()
  58. const { availableNextBlocks } = useAvailableBlocks(data.type, data.isInIteration)
  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. <HelpLink nodeType={data.type} />
  131. <PanelOperator id={id} data={data} showHelpLink={false} />
  132. <div className='mx-3 w-[1px] h-3.5 bg-gray-200' />
  133. <div
  134. className='flex items-center justify-center w-6 h-6 cursor-pointer'
  135. onClick={() => handleNodeSelect(id, true)}
  136. >
  137. <XClose className='w-4 h-4' />
  138. </div>
  139. </div>
  140. </div>
  141. <div className='p-2'>
  142. <DescriptionInput
  143. value={data.desc || ''}
  144. onChange={handleDescriptionChange}
  145. />
  146. </div>
  147. </div>
  148. <div className='py-2'>
  149. {cloneElement(children, { id, data })}
  150. </div>
  151. {
  152. !!availableNextBlocks.length && (
  153. <div className='p-4 border-t-[0.5px] border-t-black/5'>
  154. <div className='flex items-center mb-1 text-gray-700 text-[13px] font-semibold'>
  155. {t('workflow.panel.nextStep').toLocaleUpperCase()}
  156. </div>
  157. <div className='mb-2 text-xs text-gray-400'>
  158. {t('workflow.panel.addNextStep')}
  159. </div>
  160. <NextStep selectedNode={{ id, data } as Node} />
  161. </div>
  162. )
  163. }
  164. </div>
  165. </div>
  166. )
  167. }
  168. export default memo(BasePanel)