123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 'use client';
- import { atom } from "jotai";
- import { atomFamily, atomWithReducer } from 'jotai/utils'
- import * as R from 'ramda'
- export const flowModelRecordAtom = atom()
- flowModelRecordAtom.debugLabel = 'flowModelRecordAtom'
- // LF nodes model
- export const flowModelAtom = atom(get => {
- const record = get(flowModelRecordAtom)
- const { nodes, edges } = record?.content ?? {}
- const chainNodes = []
- let curNode = R.find(R.propEq('node_id_1', 'id'), nodes ?? [])
- if (curNode) {
- chainNodes.push(curNode)
- const cached = new Set()
- while (true) {
- const nextNode = R.find(
- R.propEq(
- R.prop(
- 'targetNodeId',
- R.find(R.propEq(curNode.id, 'sourceNodeId'), edges)
- ),
- 'id'
- ),
- nodes
- )
- if (!nextNode || nextNode.id === 'node_id_2') {
- break;
- }
- if (cached.has(nextNode.id)) {
- break;
- }
- cached.add(nextNode.id)
- curNode = nextNode
- chainNodes.push(nextNode)
- }
- }
- return chainNodes
- })
- flowModelAtom.debugLabel = 'flowModelAtom'
- // react flow nodes model
- export const flowModelTranslatedAtom = atom(get => {
- // TODO translated to react flow model
- return get(flowModelAtom)
- })
- flowModelTranslatedAtom.debugLabel = 'flowModelTranslatedAtom'
- // filtered for steps nodes model
- export const stepsNodesAtom = atom(get => {
- const nodes = get(flowModelTranslatedAtom)
- return R.filter(
- R.propSatisfies(
- R.includes(
- R.__,
- ['form_card', 'UserTask']
- ),
- 'type'
- ),
- nodes
- )
- })
- stepsNodesAtom.debugLabel = 'stepsNodesAtom'
- export const curStepAtom = atom(0)
- curStepAtom.debugLabel = 'curStepAtom'
- export const viewedStepAtom = atom(0)
- viewedStepAtom.debugLabel = 'viewedStepAtom'
- export const curNodeAtom = atom(get => {
- const stepsNodes = get(stepsNodesAtom)
- const curStep = get(curStepAtom)
- return R.prop(curStep, stepsNodes)
- })
- curNodeAtom.debugLabel = 'curNodeAtom'
- export const viewedNodeAtom = atom(get => {
- const stepsNodes = get(stepsNodesAtom)
- const viewedStep = get(viewedStepAtom)
- return R.prop(viewedStep, stepsNodes)
- })
- viewedNodeAtom.debugLabel = 'viewedNodeAtom'
- export const arrowStateAtom = atom(get => {
- const stepsNodes = get(stepsNodesAtom)
- const curStep = get(curStepAtom)
- return {
- prev: stepsNodes && curStep > 0,
- next: stepsNodes && curStep < stepsNodes?.length - 1
- }
- })
- arrowStateAtom.debugLabel = 'arrowStateAtom'
- // node render下相关的状态
- export const cardInstantAtomFamily = atomFamily(
- (node) => {
- const defaults = { id: node?.id, type: node?.type }
- switch (node?.type) {
- case 'form_card':
- defaults['fieldsDefinition'] = node?.properties?.fields
- break;
- case 'UserTask':
- defaults['assistantName'] = R.path(['properties', 'item', 'assistantName'], node)
- break;
- default:
- break;
- }
- return atom(defaults)
- },
- (a, b) => a?.id === b?.id
- )
- export const cardInstantAtomsAtom = atomWithReducer(
- {},
- (prev, action: { payload: { [K in any]?: any }, type: 'replace' | 'update' } = { payload: {}, type: 'update' }) => {
- switch (action.type) {
- case 'replace':
- return action.payload
- case 'update':
- return { ...prev, ...action.payload }
- default:
- return prev
- }
- }
- )
- cardInstantAtomsAtom.debugLabel = 'cardInstantAtomsAtom'
- // Readonly
- export const instantDataAtom = atom(get => {
- const cardInstantAtoms = get(cardInstantAtomsAtom)
- const cardInstant = R.map((at) => get(at), cardInstantAtoms)
- return cardInstant
- })
- instantDataAtom.debugLabel = 'instantDataAtom'
- // aside下相关的状态
- export const asideInstantAtomFamily = atomFamily(
- (node) => atom({ id: node?.id }),
- (a, b) => a?.id === b?.id
- )
- export const asideInstantAtomsAtom = atomWithReducer(
- {},
- (prev, action: { payload: { [K in any]?: any }, type: 'replace' | 'update' } = { payload: {}, type: 'update' }) => {
- switch (action.type) {
- case 'replace':
- return action.payload
- case 'update':
- return { ...prev, ...action.payload }
- default:
- return prev
- }
- }
- )
- asideInstantAtomsAtom.debugLabel = 'asideInstantAtomsAtom'
|