-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathActionConfigurationInput.tsx
More file actions
111 lines (101 loc) · 3.03 KB
/
ActionConfigurationInput.tsx
File metadata and controls
111 lines (101 loc) · 3.03 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
import React, { useMemo } from 'react'
import FormSelect from '../../../../design/components/molecules/Form/atoms/FormSelect'
import FormRowItem from '../../../../design/components/molecules/Form/templates/FormRowItem'
import { pickBy } from 'ramda'
import { BoostAST, BoostPrimitives, BoostType } from '../../../lib/automations'
import { LiteralNode, RefNode } from '../../../lib/automations/ast'
import { StdPrimitives } from '../../../lib/automations/types'
const CONFIG_TYPES = [
{ label: 'Event', value: 'event' },
{ label: 'Custom', value: 'custom' },
]
interface ActionConfigurationInputProps {
onChange: (value: BoostAST | undefined) => void
value: BoostAST
customInput: (
onChange: ActionConfigurationInputProps['onChange'],
value: Extract<BoostAST, { type: 'literal' }> | null
) => React.ReactNode
eventDataOptions: Record<string, BoostType>
type: BoostPrimitives | StdPrimitives
defaultValue: any
}
const ActionConfigurationInput = ({
value,
eventDataOptions,
onChange,
customInput,
type: dataType,
defaultValue,
}: ActionConfigurationInputProps) => {
const type = useMemo(() => {
if (value == null) {
return CONFIG_TYPES[1]
}
if (value.type === 'reference') {
if (value.identifier.startsWith('$event')) {
return CONFIG_TYPES[0]
}
if (value.identifier.startsWith('$env')) {
return CONFIG_TYPES[1]
}
}
return CONFIG_TYPES[1]
}, [value])
const options = useMemo(() => {
return Object.keys(
pickBy(
(val) => val.type === 'primitive' && val.def === dataType,
eventDataOptions
)
).map((key) => ({ label: key, value: key }))
}, [eventDataOptions, dataType])
const normalized = useMemo(() => {
if (value == null) {
return ''
}
if (value.type === 'reference') {
if (value.identifier.startsWith('$event')) {
return value.identifier.substr('$event.'.length)
}
if (value.identifier.startsWith('$env')) {
return value.identifier.substr('$env.'.length)
}
}
return value.type === 'literal' ? value.value?.toString() || '' : ''
}, [value])
return (
<>
<FormRowItem>
<FormSelect
options={CONFIG_TYPES}
value={type}
onChange={(val) => {
if (val.value === 'event') {
onChange(RefNode('$event.'))
} else {
onChange(LiteralNode(dataType, defaultValue))
}
}}
/>
</FormRowItem>
<FormRowItem>
{type.value === 'event' && (
<FormSelect
value={{ label: normalized, value: normalized }}
options={options}
onChange={({ value }) => onChange(RefNode(`$event.${value}`))}
/>
)}
{type.value === 'custom' &&
customInput(
onChange,
value == null || value.type !== 'literal'
? LiteralNode(dataType, defaultValue)
: value
)}
</FormRowItem>
</>
)
}
export default ActionConfigurationInput