You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. useRef,
  6. useState,
  7. } from 'react'
  8. import { RiCloseLine, RiEqualizer2Line } from '@remixicon/react'
  9. import { useTranslation } from 'react-i18next'
  10. import { useNodes } from 'reactflow'
  11. import {
  12. useWorkflowInteractions,
  13. } from '../../hooks'
  14. import { useEdgesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-edges-interactions-without-sync'
  15. import { useNodesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-nodes-interactions-without-sync'
  16. import { BlockEnum } from '../../types'
  17. import type { StartNodeType } from '../../nodes/start/types'
  18. import { useResizePanel } from '../../nodes/_base/hooks/use-resize-panel'
  19. import ChatWrapper from './chat-wrapper'
  20. import cn from '@/utils/classnames'
  21. import { RefreshCcw01 } from '@/app/components/base/icons/src/vender/line/arrows'
  22. import Tooltip from '@/app/components/base/tooltip'
  23. import ActionButton, { ActionButtonState } from '@/app/components/base/action-button'
  24. import { useStore } from '@/app/components/workflow/store'
  25. import { debounce, noop } from 'lodash-es'
  26. export type ChatWrapperRefType = {
  27. handleRestart: () => void
  28. }
  29. const DebugAndPreview = () => {
  30. const { t } = useTranslation()
  31. const chatRef = useRef({ handleRestart: noop })
  32. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  33. const { handleNodeCancelRunningStatus } = useNodesInteractionsWithoutSync()
  34. const { handleEdgeCancelRunningStatus } = useEdgesInteractionsWithoutSync()
  35. const [expanded, setExpanded] = useState(true)
  36. const nodes = useNodes<StartNodeType>()
  37. const selectedNode = nodes.find(node => node.data.selected)
  38. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  39. const variables = startNode?.data.variables || []
  40. const visibleVariables = variables.filter(v => v.hide !== true)
  41. const [showConversationVariableModal, setShowConversationVariableModal] = useState(false)
  42. const handleRestartChat = () => {
  43. handleNodeCancelRunningStatus()
  44. handleEdgeCancelRunningStatus()
  45. chatRef.current.handleRestart()
  46. }
  47. const workflowCanvasWidth = useStore(s => s.workflowCanvasWidth)
  48. const nodePanelWidth = useStore(s => s.nodePanelWidth)
  49. const panelWidth = useStore(s => s.previewPanelWidth)
  50. const setPanelWidth = useStore(s => s.setPreviewPanelWidth)
  51. const handleResize = useCallback((width: number, source: 'user' | 'system' = 'user') => {
  52. if (source === 'user')
  53. localStorage.setItem('debug-and-preview-panel-width', `${width}`)
  54. setPanelWidth(width)
  55. }, [setPanelWidth])
  56. const maxPanelWidth = useMemo(() => {
  57. if (!workflowCanvasWidth)
  58. return 720
  59. if (!selectedNode)
  60. return workflowCanvasWidth - 400
  61. return workflowCanvasWidth - 400 - 400
  62. }, [workflowCanvasWidth, selectedNode, nodePanelWidth])
  63. const {
  64. triggerRef,
  65. containerRef,
  66. } = useResizePanel({
  67. direction: 'horizontal',
  68. triggerDirection: 'left',
  69. minWidth: 400,
  70. maxWidth: maxPanelWidth,
  71. onResize: debounce((width: number) => {
  72. handleResize(width, 'user')
  73. }),
  74. })
  75. return (
  76. <div className='relative h-full'>
  77. <div
  78. ref={triggerRef}
  79. className='absolute -left-1 top-0 flex h-full w-1 cursor-col-resize resize-x items-center justify-center'>
  80. <div className='h-10 w-0.5 rounded-sm bg-state-base-handle hover:h-full hover:bg-state-accent-solid active:h-full active:bg-state-accent-solid'></div>
  81. </div>
  82. <div
  83. ref={containerRef}
  84. className={cn(
  85. 'relative flex h-full flex-col rounded-l-2xl border border-r-0 border-components-panel-border bg-chatbot-bg shadow-xl',
  86. )}
  87. style={{ width: `${panelWidth}px` }}
  88. >
  89. <div className='system-xl-semibold flex shrink-0 items-center justify-between px-4 pb-2 pt-3 text-text-primary'>
  90. <div className='h-8'>{t('workflow.common.debugAndPreview').toLocaleUpperCase()}</div>
  91. <div className='flex items-center gap-1'>
  92. <Tooltip
  93. popupContent={t('common.operation.refresh')}
  94. >
  95. <ActionButton onClick={() => handleRestartChat()}>
  96. <RefreshCcw01 className='h-4 w-4' />
  97. </ActionButton>
  98. </Tooltip>
  99. {visibleVariables.length > 0 && (
  100. <div className='relative'>
  101. <Tooltip
  102. popupContent={t('workflow.panel.userInputField')}
  103. >
  104. <ActionButton state={expanded ? ActionButtonState.Active : undefined} onClick={() => setExpanded(!expanded)}>
  105. <RiEqualizer2Line className='h-4 w-4' />
  106. </ActionButton>
  107. </Tooltip>
  108. {expanded && <div className='absolute bottom-[-17px] right-[5px] z-10 h-3 w-3 rotate-45 border-l-[0.5px] border-t-[0.5px] border-components-panel-border-subtle bg-components-panel-on-panel-item-bg' />}
  109. </div>
  110. )}
  111. <div className='mx-3 h-3.5 w-[1px] bg-divider-regular'></div>
  112. <div
  113. className='flex h-6 w-6 cursor-pointer items-center justify-center'
  114. onClick={handleCancelDebugAndPreviewPanel}
  115. >
  116. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  117. </div>
  118. </div>
  119. </div>
  120. <div className='grow overflow-y-auto rounded-b-2xl'>
  121. <ChatWrapper
  122. ref={chatRef}
  123. showConversationVariableModal={showConversationVariableModal}
  124. onConversationModalHide={() => setShowConversationVariableModal(false)}
  125. showInputsFieldsPanel={expanded}
  126. onHide={() => setExpanded(false)}
  127. />
  128. </div>
  129. </div>
  130. </div>
  131. )
  132. }
  133. export default memo(DebugAndPreview)