NodeRender.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use client';
  2. import React, { useEffect, useMemo } from 'react'
  3. import { BsArrowLeft, BsArrowRight } from "react-icons/bs";
  4. import Agent from './NodeType/Agent';
  5. import Form from './NodeType/Form';
  6. import Unsupport from './NodeType/Unsupport';
  7. import * as R from 'ramda'
  8. import { curNodeAtom, curStepAtom, arrowStateAtom, cardInstantAtomFamily, cardInstantAtomsAtom } from '../store';
  9. import { useAtom, useAtomValue } from 'jotai';
  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?.id)
  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. return (
  43. <div className="card card-compact shadow-xl flex-1 overflow-hidden">
  44. <div className="card-body overflow-hidden">
  45. <div className="card-title rounded-box bg-slate-200 p-2">
  46. <h2 className="flex-1">
  47. {curStep + 1}: {nodeName}
  48. </h2>
  49. <button className='btn btn-circle btn-sm' disabled={!arrowState.prev} onClick={onPrevStep}><BsArrowLeft /></button>
  50. <button className='btn btn-circle btn-sm' disabled={!arrowState.next} onClick={onNextStep}><BsArrowRight /></button>
  51. </div>
  52. <div className="flex-1 flex flex-col items-stretch overflow-auto">
  53. <Comp key={node?.id} node={node} cardInstantAtom={cardInstantAtom}></Comp>
  54. </div>
  55. {/* <div className="card-actions justify-end">
  56. <button className="btn btn-primary">确认,下一步</button>
  57. </div> */}
  58. </div>
  59. </div>
  60. )
  61. }
  62. export default React.memo(React.forwardRef(NodeRender))