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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. memo,
  3. useCallback,
  4. useEffect,
  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. useEdgesInteractions,
  13. useNodesInteractions,
  14. useWorkflowInteractions,
  15. } from '../../hooks'
  16. import { BlockEnum } from '../../types'
  17. import type { StartNodeType } from '../../nodes/start/types'
  18. import ChatWrapper from './chat-wrapper'
  19. import cn from '@/utils/classnames'
  20. import { RefreshCcw01 } from '@/app/components/base/icons/src/vender/line/arrows'
  21. import { BubbleX } from '@/app/components/base/icons/src/vender/line/others'
  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 { 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 } = useNodesInteractions()
  34. const { handleEdgeCancelRunningStatus } = useEdgesInteractions()
  35. const varList = useStore(s => s.conversationVariables)
  36. const [expanded, setExpanded] = useState(true)
  37. const nodes = useNodes<StartNodeType>()
  38. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  39. const variables = startNode?.data.variables || []
  40. const [showConversationVariableModal, setShowConversationVariableModal] = useState(false)
  41. const handleRestartChat = () => {
  42. handleNodeCancelRunningStatus()
  43. handleEdgeCancelRunningStatus()
  44. chatRef.current.handleRestart()
  45. }
  46. const [panelWidth, setPanelWidth] = useState(420)
  47. const [isResizing, setIsResizing] = useState(false)
  48. const startResizing = useCallback((e: React.MouseEvent) => {
  49. e.preventDefault()
  50. setIsResizing(true)
  51. }, [])
  52. const stopResizing = useCallback(() => {
  53. setIsResizing(false)
  54. }, [])
  55. const resize = useCallback((e: MouseEvent) => {
  56. if (isResizing) {
  57. const newWidth = window.innerWidth - e.clientX
  58. if (newWidth > 420 && newWidth < 1024)
  59. setPanelWidth(newWidth)
  60. }
  61. }, [isResizing])
  62. useEffect(() => {
  63. window.addEventListener('mousemove', resize)
  64. window.addEventListener('mouseup', stopResizing)
  65. return () => {
  66. window.removeEventListener('mousemove', resize)
  67. window.removeEventListener('mouseup', stopResizing)
  68. }
  69. }, [resize, stopResizing])
  70. return (
  71. <div
  72. className={cn(
  73. 'relative flex h-full flex-col rounded-l-2xl border border-r-0 border-components-panel-border bg-chatbot-bg shadow-xl',
  74. )}
  75. style={{ width: `${panelWidth}px` }}
  76. >
  77. <div
  78. className="absolute bottom-0 left-[3px] top-1/2 z-50 h-6 w-[3px] cursor-col-resize rounded bg-gray-300"
  79. onMouseDown={startResizing}
  80. />
  81. <div className='system-xl-semibold flex shrink-0 items-center justify-between px-4 pb-2 pt-3 text-text-primary'>
  82. <div className='h-8'>{t('workflow.common.debugAndPreview').toLocaleUpperCase()}</div>
  83. <div className='flex items-center gap-1'>
  84. <Tooltip
  85. popupContent={t('common.operation.refresh')}
  86. >
  87. <ActionButton onClick={() => handleRestartChat()}>
  88. <RefreshCcw01 className='h-4 w-4' />
  89. </ActionButton>
  90. </Tooltip>
  91. {varList.length > 0 && (
  92. <Tooltip
  93. popupContent={t('workflow.chatVariable.panelTitle')}
  94. >
  95. <ActionButton onClick={() => setShowConversationVariableModal(true)}>
  96. <BubbleX className='h-4 w-4' />
  97. </ActionButton>
  98. </Tooltip>
  99. )}
  100. {variables.length > 0 && (
  101. <div className='relative'>
  102. <Tooltip
  103. popupContent={t('workflow.panel.userInputField')}
  104. >
  105. <ActionButton state={expanded ? ActionButtonState.Active : undefined} onClick={() => setExpanded(!expanded)}>
  106. <RiEqualizer2Line className='h-4 w-4' />
  107. </ActionButton>
  108. </Tooltip>
  109. {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'/>}
  110. </div>
  111. )}
  112. <div className='mx-3 h-3.5 w-[1px] bg-divider-regular'></div>
  113. <div
  114. className='flex h-6 w-6 cursor-pointer items-center justify-center'
  115. onClick={handleCancelDebugAndPreviewPanel}
  116. >
  117. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  118. </div>
  119. </div>
  120. </div>
  121. <div className='grow overflow-y-auto rounded-b-2xl'>
  122. <ChatWrapper
  123. ref={chatRef}
  124. showConversationVariableModal={showConversationVariableModal}
  125. onConversationModalHide={() => setShowConversationVariableModal(false)}
  126. showInputsFieldsPanel={expanded}
  127. onHide={() => setExpanded(false)}
  128. />
  129. </div>
  130. </div>
  131. )
  132. }
  133. export default memo(DebugAndPreview)