2
0

🚸 (chatwoot) Add close widget task

This commit is contained in:
Baptiste Arnaud
2023-03-10 11:56:27 +01:00
parent 26c80f064f
commit 9785a0df5c
4 changed files with 118 additions and 81 deletions

View File

@ -6,7 +6,9 @@ type Props = {
} }
export const ChatwootBlockNodeLabel = ({ block }: Props) => export const ChatwootBlockNodeLabel = ({ block }: Props) =>
block.options.websiteToken.length === 0 ? ( block.options.task === 'Close widget' ? (
<Text>Close Chatwoot</Text>
) : block.options.websiteToken.length === 0 ? (
<Text color="gray.500">Configure...</Text> <Text color="gray.500">Configure...</Text>
) : ( ) : (
<Text>Open Chatwoot</Text> <Text>Open Chatwoot</Text>

View File

@ -1,3 +1,4 @@
import { DropdownList } from '@/components/DropdownList'
import { TextInput } from '@/components/inputs' import { TextInput } from '@/components/inputs'
import { import {
Accordion, Accordion,
@ -7,7 +8,7 @@ import {
AccordionPanel, AccordionPanel,
Stack, Stack,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { ChatwootOptions } from 'models' import { ChatwootOptions, chatwootTasks } from 'models'
import React from 'react' import React from 'react'
type Props = { type Props = {
@ -16,8 +17,20 @@ type Props = {
} }
export const ChatwootSettingsForm = ({ options, onOptionsChange }: Props) => { export const ChatwootSettingsForm = ({ options, onOptionsChange }: Props) => {
const updateTask = (task: (typeof chatwootTasks)[number]) => {
onOptionsChange({ ...options, task })
}
return ( return (
<Stack spacing={4}> <Stack spacing={4}>
<DropdownList
currentItem={options.task ?? 'Show widget'}
onItemSelect={updateTask}
items={chatwootTasks}
/>
{!options.task ||
(options.task === 'Show widget' && (
<>
<TextInput <TextInput
isRequired isRequired
label="Base URL" label="Base URL"
@ -47,7 +60,10 @@ export const ChatwootSettingsForm = ({ options, onOptionsChange }: Props) => {
label="ID" label="ID"
defaultValue={options.user?.id} defaultValue={options.user?.id}
onChange={(id: string) => { onChange={(id: string) => {
onOptionsChange({ ...options, user: { ...options.user, id } }) onOptionsChange({
...options,
user: { ...options.user, id },
})
}} }}
/> />
<TextInput <TextInput
@ -93,6 +109,8 @@ export const ChatwootSettingsForm = ({ options, onOptionsChange }: Props) => {
</AccordionPanel> </AccordionPanel>
</AccordionItem> </AccordionItem>
</Accordion> </Accordion>
</>
))}
</Stack> </Stack>
) )
} }

View File

@ -49,13 +49,25 @@ if (window.$chatwoot) {
})(document, "script"); })(document, "script");
}` }`
const chatwootCloseCode = `
if (window.$chatwoot) {
window.$chatwoot.toggle("close");
window.$chatwoot.toggleBubbleVisibility("hide");
}
`
export const executeChatwootBlock = ( export const executeChatwootBlock = (
{ typebot: { variables }, result }: SessionState, { typebot: { variables }, result }: SessionState,
block: ChatwootBlock, block: ChatwootBlock,
lastBubbleBlockId?: string lastBubbleBlockId?: string
): ExecuteIntegrationResponse => { ): ExecuteIntegrationResponse => {
const isPreview = !result.id const isPreview = !result.id
const chatwootCode = parseChatwootOpenCode(block.options) const chatwootCode =
block.options.task === 'Close widget'
? chatwootCloseCode
: isPreview
? ''
: parseChatwootOpenCode(block.options)
return { return {
outgoingEdgeId: block.outgoingEdgeId, outgoingEdgeId: block.outgoingEdgeId,
clientSideActions: [ clientSideActions: [
@ -76,7 +88,8 @@ export const executeChatwootBlock = (
}, },
}, },
], ],
logs: isPreview logs:
chatwootCode === ''
? [ ? [
{ {
status: 'info', status: 'info',

View File

@ -2,7 +2,10 @@ import { z } from 'zod'
import { blockBaseSchema } from '../baseSchemas' import { blockBaseSchema } from '../baseSchemas'
import { IntegrationBlockType } from './enums' import { IntegrationBlockType } from './enums'
export const chatwootTasks = ['Show widget', 'Close widget'] as const
export const chatwootOptionsSchema = z.object({ export const chatwootOptionsSchema = z.object({
task: z.enum(chatwootTasks).optional(),
baseUrl: z.string(), baseUrl: z.string(),
websiteToken: z.string(), websiteToken: z.string(),
user: z user: z
@ -24,6 +27,7 @@ export const chatwootBlockSchema = blockBaseSchema.and(
) )
export const defaultChatwootOptions: ChatwootOptions = { export const defaultChatwootOptions: ChatwootOptions = {
task: 'Show widget',
baseUrl: 'https://app.chatwoot.com', baseUrl: 'https://app.chatwoot.com',
websiteToken: '', websiteToken: '',
} }