您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.tsx 5.3KB

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