NodeRender.tsx 2.5 KB

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