Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.tsx 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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 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 } = useNodesInteractionsWithoutSync()
  34. const { handleEdgeCancelRunningStatus } = useEdgesInteractionsWithoutSync()
  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 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 [panelWidth, setPanelWidth] = useState(420)
  48. const [isResizing, setIsResizing] = useState(false)
  49. const startResizing = useCallback((e: React.MouseEvent) => {
  50. e.preventDefault()
  51. setIsResizing(true)
  52. }, [])
  53. const stopResizing = useCallback(() => {
  54. setIsResizing(false)
  55. }, [])
  56. const resize = useCallback((e: MouseEvent) => {
  57. if (isResizing) {
  58. const newWidth = window.innerWidth - e.clientX
  59. if (newWidth > 420 && newWidth < 1024)
  60. setPanelWidth(newWidth)
  61. }
  62. }, [isResizing])
  63. useEffect(() => {
  64. window.addEventListener('mousemove', resize)
  65. window.addEventListener('mouseup', stopResizing)
  66. return () => {
  67. window.removeEventListener('mousemove', resize)
  68. window.removeEventListener('mouseup', stopResizing)
  69. }
  70. }, [resize, stopResizing])
  71. return (
  72. <div
  73. className={cn(
  74. 'relative flex h-full flex-col rounded-l-2xl border border-r-0 border-components-panel-border bg-chatbot-bg shadow-xl',
  75. )}
  76. style={{ width: `${panelWidth}px` }}
  77. >
  78. <div
  79. className="absolute bottom-0 left-[3px] top-1/2 z-50 h-6 w-[3px] cursor-col-resize rounded bg-gray-300"
  80. onMouseDown={startResizing}
  81. />
  82. <div className='system-xl-semibold flex shrink-0 items-center justify-between px-4 pb-2 pt-3 text-text-primary'>
  83. <div className='h-8'>{t('workflow.common.debugAndPreview').toLocaleUpperCase()}</div>
  84. <div className='flex items-center gap-1'>
  85. <Tooltip
  86. popupContent={t('common.operation.refresh')}
  87. >
  88. <ActionButton onClick={() => handleRestartChat()}>
  89. <RefreshCcw01 className='h-4 w-4' />
  90. </ActionButton>
  91. </Tooltip>
  92. {varList.length > 0 && (
  93. <Tooltip
  94. popupContent={t('workflow.chatVariable.panelTitle')}
  95. >
  96. <ActionButton onClick={() => setShowConversationVariableModal(true)}>
  97. <BubbleX className='h-4 w-4' />
  98. </ActionButton>
  99. </Tooltip>
  100. )}
  101. {visibleVariables.length > 0 && (
  102. <div className='relative'>
  103. <Tooltip
  104. popupContent={t('workflow.panel.userInputField')}
  105. >
  106. <ActionButton state={expanded ? ActionButtonState.Active : undefined} onClick={() => setExpanded(!expanded)}>
  107. <RiEqualizer2Line className='h-4 w-4' />
  108. </ActionButton>
  109. </Tooltip>
  110. {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' />}
  111. </div>
  112. )}
  113. <div className='mx-3 h-3.5 w-[1px] bg-divider-regular'></div>
  114. <div
  115. className='flex h-6 w-6 cursor-pointer items-center justify-center'
  116. onClick={handleCancelDebugAndPreviewPanel}
  117. >
  118. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  119. </div>
  120. </div>
  121. </div>
  122. <div className='grow overflow-y-auto rounded-b-2xl'>
  123. <ChatWrapper
  124. ref={chatRef}
  125. showConversationVariableModal={showConversationVariableModal}
  126. onConversationModalHide={() => setShowConversationVariableModal(false)}
  127. showInputsFieldsPanel={expanded}
  128. onHide={() => setExpanded(false)}
  129. />
  130. </div>
  131. </div>
  132. )
  133. }
  134. export default memo(DebugAndPreview)