Repository navigation
Expand file tree
/
Copy pathdocs-block-node.tsx
More file actions
150 lines (137 loc) · 4.91 KB
/
Copy pathdocs-block-node.tsx
File metadata and controls
150 lines (137 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use client'
import { type ComponentType, memo } from 'react'
import { SubBlockRowView, WorkflowBlockView } from '@sim/workflow-renderer'
import type { Node, NodeProps } from '@xyflow/react'
import { m } from 'framer-motion'
import { resolveIcon } from '@/components/workflow-preview/block-icons'
import {
BLOCK_STAGGER,
EASE_OUT,
type PreviewTool,
} from '@/components/workflow-preview/workflow-data'
/** Renders the colored square with no glyph when a block type has no registered icon. */
const EMPTY_ICON: ComponentType<{ className?: string }> = () => null
const RING_STYLES = 'ring-[1.75px] ring-[var(--brand-secondary)]'
export interface DocsBlockData extends Record<string, unknown> {
name: string
blockType: string
bgColor: string
rows: Array<{ title: string; value: string }>
branches?: Array<{ id: string; label: string; value?: string }>
tools?: PreviewTool[]
hideTargetHandle?: boolean
index?: number
animate?: boolean
isHighlighted?: boolean
isDimmed?: boolean
}
export type DocsBlockNodeType = Node<DocsBlockData, 'previewBlock'>
/**
* Docs adapter for workflow block nodes: maps the static preview data to the
* shared {@link WorkflowBlockView}'s props. Carries no stores, hooks, or
* queries — it only reshapes data into View props and wraps the result in the
* dim/stagger motion used by the rest of the diagram (the parent
* `WorkflowPreview` provides the `LazyMotion` feature set). The block's ring is
* driven by `hasRing`/`ringStyles` inside the View.
*/
export const DocsBlockNode = memo(function DocsBlockNode({
id,
data,
}: NodeProps<DocsBlockNodeType>) {
const {
name,
blockType,
bgColor,
rows: dataRows,
branches,
tools,
hideTargetHandle = false,
index = 0,
animate = false,
isHighlighted = false,
isDimmed = false,
} = data
/** The View gates router handle topology on `type === 'router_v2'`. */
const type = blockType === 'router' ? 'router_v2' : blockType
const Icon = resolveIcon(blockType) ?? EMPTY_ICON
const delay = animate ? index * BLOCK_STAGGER : 0
const hasBranches = Boolean(branches && branches.length > 0)
const hasTools = Boolean(tools && tools.length > 0)
/** The View renders the default target/source/error handles (and the error row) for non-trigger blocks; mirror that gate. */
const shouldShowDefaultHandles = !hideTargetHandle
const hasContentBelowHeader =
dataRows.length > 0 || hasBranches || hasTools || shouldShowDefaultHandles
/**
* Strip the app's `condition-`/`router-` handle prefixes — the View
* regenerates them, so passing them through would double-prefix the handle id.
* Branch + router-context values render through the editor's `getDisplayValue`,
* which shows `-` for a blank value (e.g. an `else` branch); mirror that.
*/
const conditionRows =
type === 'condition'
? (branches ?? []).map((branch) => ({
id: branch.id.replace(/^condition-/, ''),
title: branch.label,
value: branch.value || '-',
}))
: []
const routerRows =
type === 'router_v2'
? (branches ?? []).map((branch) => ({
id: branch.id.replace(/^router-/, ''),
value: branch.value || '-',
}))
: []
/** The View renders the router's leading Context row from this prop, not `rows`. */
const routerContextValue =
type === 'router_v2' ? dataRows.find((row) => row.title === 'Context')?.value || '-' : undefined
/**
* Non-branch content only — the View renders condition/router/error rows from
* the conditionRows/routerRows it receives, so their order stays locked to its
* handle geometry in one place.
*/
const rows =
type === 'condition' || type === 'router_v2' ? null : (
<>
{dataRows.map((row) => (
<SubBlockRowView key={row.title} title={row.title} displayValue={row.value} />
))}
{hasTools && (
<SubBlockRowView
title='Tools'
displayValue={tools?.map((tool) => tool.name).join(', ')}
/>
)}
</>
)
return (
<m.div
className='relative transition-opacity duration-300'
style={{ opacity: isDimmed ? 0.35 : 1 }}
initial={animate ? { opacity: 0 } : false}
animate={{ opacity: isDimmed ? 0.35 : 1 }}
transition={{ duration: 0.45, delay, ease: EASE_OUT }}
>
<WorkflowBlockView
id={id}
type={type}
name={name}
isEnabled
isLocked={false}
hasRing={Boolean(isHighlighted)}
ringStyles={RING_STYLES}
Icon={Icon}
iconBgColor={bgColor}
horizontalHandles
shouldShowDefaultHandles={shouldShowDefaultHandles}
hasContentBelowHeader={hasContentBelowHeader}
conditionRows={conditionRows}
routerRows={routerRows}
routerContextValue={routerContextValue}
wouldCreateConnectionCycle={() => false}
onSelect={() => {}}
rows={rows}
/>
</m.div>
)
})