NodeRender.tsx 2.8 KB

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