2
0
Files
bot/apps/builder/components/board/graph/BlockNode/StepNode/SettingsPopoverContent/SettingsPopoverContent.tsx
2022-01-20 16:14:47 +01:00

184 lines
4.5 KiB
TypeScript

import {
PopoverContent,
PopoverArrow,
PopoverBody,
useEventListener,
Portal,
IconButton,
} from '@chakra-ui/react'
import { ExpandIcon } from 'assets/icons'
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
import {
InputStep,
InputStepType,
IntegrationStepType,
LogicStepType,
Step,
StepOptions,
TextBubbleStep,
} from 'models'
import { useRef } from 'react'
import {
TextInputSettingsBody,
NumberInputSettingsBody,
EmailInputSettingsBody,
UrlInputSettingsBody,
DateInputSettingsBody,
} from './bodies'
import { ChoiceInputSettingsBody } from './bodies/ChoiceInputSettingsBody'
import { ConditionSettingsBody } from './bodies/ConditionSettingsBody'
import { GoogleAnalyticsSettings } from './bodies/GoogleAnalyticsSettings'
import { GoogleSheetsSettingsBody } from './bodies/GoogleSheetsSettingsBody'
import { PhoneNumberSettingsBody } from './bodies/PhoneNumberSettingsBody'
import { RedirectSettings } from './bodies/RedirectSettings'
import { SetVariableSettingsBody } from './bodies/SetVariableSettingsBody'
type Props = {
step: Exclude<Step, TextBubbleStep>
onExpandClick: () => void
}
export const SettingsPopoverContent = ({ step, onExpandClick }: Props) => {
const ref = useRef<HTMLDivElement | null>(null)
const handleMouseDown = (e: React.MouseEvent) => e.stopPropagation()
const handleMouseWheel = (e: WheelEvent) => {
e.stopPropagation()
}
useEventListener('wheel', handleMouseWheel, ref.current)
return (
<Portal>
<PopoverContent onMouseDown={handleMouseDown} pos="relative">
<PopoverArrow />
<PopoverBody
p="6"
overflowY="scroll"
maxH="400px"
ref={ref}
shadow="lg"
>
<StepSettings step={step} />
</PopoverBody>
<IconButton
pos="absolute"
top="5px"
right="5px"
aria-label="expand"
icon={<ExpandIcon />}
size="xs"
onClick={onExpandClick}
/>
</PopoverContent>
</Portal>
)
}
export const StepSettings = ({ step }: { step: Step }) => {
const { updateStep } = useTypebot()
const handleOptionsChange = (options: StepOptions) =>
updateStep(step.id, { options } as Partial<InputStep>)
switch (step.type) {
case InputStepType.TEXT: {
return (
<TextInputSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case InputStepType.NUMBER: {
return (
<NumberInputSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case InputStepType.EMAIL: {
return (
<EmailInputSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case InputStepType.URL: {
return (
<UrlInputSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case InputStepType.DATE: {
return (
<DateInputSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case InputStepType.PHONE: {
return (
<PhoneNumberSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case InputStepType.CHOICE: {
return (
<ChoiceInputSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case LogicStepType.SET_VARIABLE: {
return (
<SetVariableSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case LogicStepType.CONDITION: {
return (
<ConditionSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case LogicStepType.REDIRECT: {
return (
<RedirectSettings
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
case IntegrationStepType.GOOGLE_SHEETS: {
return (
<GoogleSheetsSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
stepId={step.id}
/>
)
}
case IntegrationStepType.GOOGLE_ANALYTICS: {
return (
<GoogleAnalyticsSettings
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
default: {
return <></>
}
}
}