NodeRender.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use client';
  2. import React, { useEffect, useMemo, useState } from 'react'
  3. import Agent from './NodeType/Agent';
  4. import Form from './NodeType/Form';
  5. import Unsupport from './NodeType/Unsupport';
  6. import * as R from 'ramda'
  7. import { curNodeAtom, curStepAtom, arrowStateAtom, cardInstantAtomFamily, cardInstantAtomsAtom, instantDataAtom, stepsNodesAtom } from '../store';
  8. import { useAtom, useAtomValue } from 'jotai';
  9. import { exportFlowToDocx } from '../export';
  10. import PreviewModal from './PreviewModal';
  11. const NodeRender = () => {
  12. const [curStep, setCurStep] = useAtom(curStepAtom)
  13. const node = useAtomValue(curNodeAtom)
  14. const arrowState = useAtomValue(arrowStateAtom)
  15. const Comp = useMemo(() => {
  16. return R.cond([
  17. [R.propEq('form_card', 'type'), R.always(Form)],
  18. [R.propEq('UserTask', 'type'), R.always(Agent)],
  19. [R.T, R.always(Unsupport)],
  20. ])(node)
  21. }, [node, node?.id])
  22. const nodeName = useMemo(() => {
  23. return R.cond([
  24. [R.propEq('form_card', 'type'), R.always('表单填写')],
  25. [R.propEq('UserTask', 'type'), R.pathOr('Unknown Agent', ['properties', 'item', 'assistantName'])],
  26. [R.T, R.always('Unknown')],
  27. ])(node)
  28. }, [node, node?.id])
  29. // 动态注册当前节点的Atom,并将其放进一个atom集合的atom,方便后续格式化所有节点实例
  30. const cardInstantAtom = cardInstantAtomFamily(node)
  31. const [, dispatchCardInstantAtoms] = useAtom(cardInstantAtomsAtom)
  32. useEffect(() => {
  33. if (node?.id) {
  34. dispatchCardInstantAtoms({ [node.id]: cardInstantAtom })
  35. }
  36. }, [cardInstantAtom])
  37. const onNextStep = () => {
  38. setCurStep(prev => prev + 1)
  39. }
  40. const onPrevStep = () => {
  41. setCurStep(prev => prev - 1)
  42. }
  43. const [isPreviewModalOpen, setPreviewModalOpen] = useState(false)
  44. const onPreview = () => {
  45. setPreviewModalOpen(true)
  46. }
  47. return (
  48. <div className="card card-compact shadow-xl flex-1 overflow-hidden">
  49. <div className="card-body overflow-hidden">
  50. <div className="card-title rounded-box bg-slate-100 p-2">
  51. <h2 className="flex-1">
  52. {curStep + 1}: {nodeName}
  53. </h2>
  54. {arrowState.prev &&
  55. <button className='btn btn-sm' onClick={onPrevStep}>上一步</button>
  56. }
  57. {arrowState.next
  58. ? <button className='btn btn-sm' onClick={onNextStep}>下一步</button>
  59. : <button className='btn btn-sm btn-neutral' onClick={onPreview}>预览</button>
  60. }
  61. </div>
  62. <div className="flex-1 flex flex-col items-stretch overflow-auto">
  63. <Comp key={node?.id} node={node} cardInstantAtom={cardInstantAtom}></Comp>
  64. </div>
  65. </div>
  66. <PreviewModal open={isPreviewModalOpen} onClose={() => setPreviewModalOpen(false)} />
  67. </div>
  68. )
  69. }
  70. export default React.memo(React.forwardRef(NodeRender))