-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
WebhookAdvancedConfigForm.tsx
229 lines (215 loc) 路 7.68 KB
/
WebhookAdvancedConfigForm.tsx
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { DropdownList } from '@/components/DropdownList'
import { CodeEditor } from '@/components/inputs/CodeEditor'
import { SwitchWithLabel } from '@/components/inputs/SwitchWithLabel'
import { TableList, TableListItemProps } from '@/components/TableList'
import { useTypebot } from '@/features/editor/providers/TypebotProvider'
import { useToast } from '@/hooks/useToast'
import {
Stack,
HStack,
Accordion,
AccordionItem,
AccordionButton,
AccordionIcon,
AccordionPanel,
Button,
Text,
} from '@chakra-ui/react'
import {
HttpMethod,
KeyValue,
VariableForTest,
ResponseVariableMapping,
WebhookOptions,
Webhook,
} from '@typebot.io/schemas'
import { useState, useMemo } from 'react'
import { executeWebhook } from '../queries/executeWebhookQuery'
import { convertVariablesForTestToVariables } from '../helpers/convertVariablesForTestToVariables'
import { getDeepKeys } from '../helpers/getDeepKeys'
import { QueryParamsInputs, HeadersInputs } from './KeyValueInputs'
import { DataVariableInputs } from './ResponseMappingInputs'
import { VariableForTestInputs } from './VariableForTestInputs'
import { SwitchWithRelatedSettings } from '@/components/SwitchWithRelatedSettings'
type Props = {
blockId: string
webhook: Webhook
options: WebhookOptions
onWebhookChange: (webhook: Webhook) => void
onOptionsChange: (options: WebhookOptions) => void
}
export const WebhookAdvancedConfigForm = ({
blockId,
webhook,
options,
onWebhookChange,
onOptionsChange,
}: Props) => {
const { typebot, save, updateWebhook } = useTypebot()
const [isTestResponseLoading, setIsTestResponseLoading] = useState(false)
const [testResponse, setTestResponse] = useState<string>()
const [responseKeys, setResponseKeys] = useState<string[]>([])
const { showToast } = useToast()
const updateMethod = (method: HttpMethod) =>
onWebhookChange({ ...webhook, method })
const updateQueryParams = (queryParams: KeyValue[]) =>
onWebhookChange({ ...webhook, queryParams })
const updateHeaders = (headers: KeyValue[]) =>
onWebhookChange({ ...webhook, headers })
const updateBody = (body: string) => onWebhookChange({ ...webhook, body })
const updateVariablesForTest = (variablesForTest: VariableForTest[]) =>
onOptionsChange({ ...options, variablesForTest })
const updateResponseVariableMapping = (
responseVariableMapping: ResponseVariableMapping[]
) => onOptionsChange({ ...options, responseVariableMapping })
const updateAdvancedConfig = (isAdvancedConfig: boolean) =>
onOptionsChange({ ...options, isAdvancedConfig })
const updateIsCustomBody = (isCustomBody: boolean) =>
onOptionsChange({ ...options, isCustomBody })
const executeTestRequest = async () => {
if (!typebot || !webhook) return
setIsTestResponseLoading(true)
await Promise.all([updateWebhook(webhook.id, webhook), save()])
const { data, error } = await executeWebhook(
typebot.id,
convertVariablesForTestToVariables(
options.variablesForTest,
typebot.variables
),
{ blockId }
)
if (error)
return showToast({ title: error.name, description: error.message })
setTestResponse(JSON.stringify(data, undefined, 2))
setResponseKeys(getDeepKeys(data))
setIsTestResponseLoading(false)
}
const updateIsExecutedOnClient = (isExecutedOnClient: boolean) =>
onOptionsChange({ ...options, isExecutedOnClient })
const ResponseMappingInputs = useMemo(
() =>
function Component(props: TableListItemProps<ResponseVariableMapping>) {
return <DataVariableInputs {...props} dataItems={responseKeys} />
},
[responseKeys]
)
return (
<>
<SwitchWithRelatedSettings
label="Advanced configuration"
initialValue={options.isAdvancedConfig ?? true}
onCheckChange={updateAdvancedConfig}
>
<SwitchWithLabel
label="Execute on client"
moreInfoContent="If enabled, the webhook will be executed on the client. It means it will be executed in the browser of your visitor. Make sure to enable CORS and do not expose sensitive data."
initialValue={options.isExecutedOnClient ?? false}
onCheckChange={updateIsExecutedOnClient}
/>
<HStack justify="space-between">
<Text>Method:</Text>
<DropdownList
currentItem={webhook.method as HttpMethod}
onItemSelect={updateMethod}
items={Object.values(HttpMethod)}
/>
</HStack>
<Accordion allowMultiple>
<AccordionItem>
<AccordionButton justifyContent="space-between">
Query params
<AccordionIcon />
</AccordionButton>
<AccordionPanel pt="4">
<TableList<KeyValue>
initialItems={webhook.queryParams}
onItemsChange={updateQueryParams}
Item={QueryParamsInputs}
addLabel="Add a param"
/>
</AccordionPanel>
</AccordionItem>
<AccordionItem>
<AccordionButton justifyContent="space-between">
Headers
<AccordionIcon />
</AccordionButton>
<AccordionPanel pt="4">
<TableList<KeyValue>
initialItems={webhook.headers}
onItemsChange={updateHeaders}
Item={HeadersInputs}
addLabel="Add a value"
/>
</AccordionPanel>
</AccordionItem>
<AccordionItem>
<AccordionButton justifyContent="space-between">
Body
<AccordionIcon />
</AccordionButton>
<AccordionPanel py={4} as={Stack} spacing="6">
<SwitchWithLabel
label="Custom body"
initialValue={options.isCustomBody ?? true}
onCheckChange={updateIsCustomBody}
/>
{(options.isCustomBody ?? true) && (
<CodeEditor
defaultValue={webhook.body ?? ''}
lang="json"
onChange={updateBody}
debounceTimeout={0}
/>
)}
</AccordionPanel>
</AccordionItem>
<AccordionItem>
<AccordionButton justifyContent="space-between">
Variable values for test
<AccordionIcon />
</AccordionButton>
<AccordionPanel pt="4">
<TableList<VariableForTest>
initialItems={
options?.variablesForTest ?? { byId: {}, allIds: [] }
}
onItemsChange={updateVariablesForTest}
Item={VariableForTestInputs}
addLabel="Add an entry"
/>
</AccordionPanel>
</AccordionItem>
</Accordion>
</SwitchWithRelatedSettings>
{webhook.url && (
<Button
onClick={executeTestRequest}
colorScheme="blue"
isLoading={isTestResponseLoading}
>
Test the request
</Button>
)}
{testResponse && (
<CodeEditor isReadOnly lang="json" value={testResponse} />
)}
<Accordion allowMultiple>
<AccordionItem>
<AccordionButton justifyContent="space-between">
Save in variables
<AccordionIcon />
</AccordionButton>
<AccordionPanel pt="4">
<TableList<ResponseVariableMapping>
initialItems={options.responseVariableMapping}
onItemsChange={updateResponseVariableMapping}
Item={ResponseMappingInputs}
addLabel="Add an entry"
/>
</AccordionPanel>
</AccordionItem>
</Accordion>
</>
)
}