panel.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. useNodeDataUpdate,
  27. useNodesExtraData,
  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 nodesExtraData = useNodesExtraData()
  59. const availableNextNodes = nodesExtraData[data.type].availableNextNodes
  60. const toolIcon = useToolIcon(data)
  61. const handleResize = useCallback((width: number) => {
  62. setPanelWidth(width)
  63. }, [setPanelWidth])
  64. const {
  65. triggerRef,
  66. containerRef,
  67. } = useResizePanel({
  68. direction: 'horizontal',
  69. triggerDirection: 'left',
  70. minWidth: 420,
  71. maxWidth: 720,
  72. onResize: handleResize,
  73. })
  74. const {
  75. handleNodeDataUpdate,
  76. handleNodeDataUpdateWithSyncDraft,
  77. } = useNodeDataUpdate()
  78. const handleTitleBlur = useCallback((title: string) => {
  79. handleNodeDataUpdateWithSyncDraft({ id, data: { title } })
  80. }, [handleNodeDataUpdateWithSyncDraft, id])
  81. const handleDescriptionChange = useCallback((desc: string) => {
  82. handleNodeDataUpdateWithSyncDraft({ id, data: { desc } })
  83. }, [handleNodeDataUpdateWithSyncDraft, id])
  84. return (
  85. <div className={cn(
  86. 'relative mr-2 h-full',
  87. 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',
  88. )}>
  89. <div
  90. ref={triggerRef}
  91. className='absolute top-1/2 -translate-y-1/2 -left-2 w-3 h-6 cursor-col-resize resize-x'>
  92. <div className='w-1 h-6 bg-gray-300 rounded-sm'></div>
  93. </div>
  94. <div
  95. ref={containerRef}
  96. className='relative h-full bg-white shadow-lg border-[0.5px] border-gray-200 rounded-2xl overflow-y-auto'
  97. style={{
  98. width: `${panelWidth}px`,
  99. }}
  100. >
  101. <div className='sticky top-0 bg-white border-b-[0.5px] border-black/5 z-10'>
  102. <div className='flex items-center px-4 pt-4 pb-1'>
  103. <BlockIcon
  104. className='shrink-0 mr-1'
  105. type={data.type}
  106. toolIcon={toolIcon}
  107. size='md'
  108. />
  109. <TitleInput
  110. value={data.title || ''}
  111. onBlur={handleTitleBlur}
  112. />
  113. <div className='shrink-0 flex items-center text-gray-500'>
  114. {
  115. canRunBySingle(data.type) && !nodesReadOnly && (
  116. <TooltipPlus
  117. popupContent={t('workflow.panel.runThisStep')}
  118. >
  119. <div
  120. className='flex items-center justify-center mr-1 w-6 h-6 rounded-md hover:bg-black/5 cursor-pointer'
  121. onClick={() => {
  122. handleNodeDataUpdate({ id, data: { _isSingleRun: true } })
  123. handleSyncWorkflowDraft(true)
  124. }}
  125. >
  126. <Play className='w-4 h-4 text-gray-500' />
  127. </div>
  128. </TooltipPlus>
  129. )
  130. }
  131. <HelpLink nodeType={data.type} />
  132. <PanelOperator id={id} data={data} showHelpLink={false} />
  133. <div className='mx-3 w-[1px] h-3.5 bg-gray-200' />
  134. <div
  135. className='flex items-center justify-center w-6 h-6 cursor-pointer'
  136. onClick={() => handleNodeSelect(id, true)}
  137. >
  138. <XClose className='w-4 h-4' />
  139. </div>
  140. </div>
  141. </div>
  142. <div className='p-2'>
  143. <DescriptionInput
  144. value={data.desc || ''}
  145. onChange={handleDescriptionChange}
  146. />
  147. </div>
  148. </div>
  149. <div className='py-2'>
  150. {cloneElement(children, { id, data })}
  151. </div>
  152. {
  153. !!availableNextNodes.length && (
  154. <div className='p-4 border-t-[0.5px] border-t-black/5'>
  155. <div className='flex items-center mb-1 text-gray-700 text-[13px] font-semibold'>
  156. {t('workflow.panel.nextStep').toLocaleUpperCase()}
  157. </div>
  158. <div className='mb-2 text-xs text-gray-400'>
  159. {t('workflow.panel.addNextStep')}
  160. </div>
  161. <NextStep selectedNode={{ id, data } as Node} />
  162. </div>
  163. )
  164. }
  165. </div>
  166. </div>
  167. )
  168. }
  169. export default memo(BasePanel)