refactor: ♻️ Rename step to block
This commit is contained in:
@ -1,82 +1,82 @@
|
||||
import {
|
||||
Block,
|
||||
Group,
|
||||
Variable,
|
||||
InputStep,
|
||||
InputBlock,
|
||||
ResultHeaderCell,
|
||||
ResultWithAnswers,
|
||||
Answer,
|
||||
VariableWithValue,
|
||||
} from 'models'
|
||||
import { isInputStep, isDefined, byId } from './utils'
|
||||
import { isInputBlock, isDefined, byId } from './utils'
|
||||
|
||||
export const parseResultHeader = ({
|
||||
blocks,
|
||||
groups,
|
||||
variables,
|
||||
}: {
|
||||
blocks: Block[]
|
||||
groups: Group[]
|
||||
variables: Variable[]
|
||||
}): ResultHeaderCell[] => {
|
||||
const parsedBlocks = parseInputsResultHeader({ blocks, variables })
|
||||
const parsedGroups = parseInputsResultHeader({ groups, variables })
|
||||
return [
|
||||
{ label: 'Submitted at' },
|
||||
...parsedBlocks,
|
||||
...parseVariablesHeaders(variables, parsedBlocks),
|
||||
...parsedGroups,
|
||||
...parseVariablesHeaders(variables, parsedGroups),
|
||||
]
|
||||
}
|
||||
|
||||
const parseInputsResultHeader = ({
|
||||
blocks,
|
||||
groups,
|
||||
variables,
|
||||
}: {
|
||||
blocks: Block[]
|
||||
groups: Group[]
|
||||
variables: Variable[]
|
||||
}): ResultHeaderCell[] =>
|
||||
(
|
||||
blocks
|
||||
.flatMap((b) =>
|
||||
b.steps.map((s) => ({
|
||||
groups
|
||||
.flatMap((g) =>
|
||||
g.blocks.map((s) => ({
|
||||
...s,
|
||||
blockTitle: b.title,
|
||||
blockTitle: g.title,
|
||||
}))
|
||||
)
|
||||
.filter((step) => isInputStep(step)) as (InputStep & {
|
||||
.filter((block) => isInputBlock(block)) as (InputBlock & {
|
||||
blockTitle: string
|
||||
})[]
|
||||
).reduce<ResultHeaderCell[]>((headers, inputStep) => {
|
||||
).reduce<ResultHeaderCell[]>((headers, inputBlock) => {
|
||||
if (
|
||||
headers.find(
|
||||
(h) =>
|
||||
isDefined(h.variableId) &&
|
||||
h.variableId ===
|
||||
variables.find(byId(inputStep.options.variableId))?.id
|
||||
variables.find(byId(inputBlock.options.variableId))?.id
|
||||
)
|
||||
)
|
||||
return headers
|
||||
const matchedVariableName =
|
||||
inputStep.options.variableId &&
|
||||
variables.find(byId(inputStep.options.variableId))?.name
|
||||
inputBlock.options.variableId &&
|
||||
variables.find(byId(inputBlock.options.variableId))?.name
|
||||
|
||||
let label = matchedVariableName ?? inputStep.blockTitle
|
||||
let label = matchedVariableName ?? inputBlock.blockTitle
|
||||
const totalPrevious = headers.filter((h) => h.label.includes(label)).length
|
||||
if (totalPrevious > 0) label = label + ` (${totalPrevious})`
|
||||
return [
|
||||
...headers,
|
||||
{
|
||||
stepType: inputStep.type,
|
||||
stepId: inputStep.id,
|
||||
variableId: inputStep.options.variableId,
|
||||
blockType: inputBlock.type,
|
||||
blockId: inputBlock.id,
|
||||
variableId: inputBlock.options.variableId,
|
||||
label,
|
||||
isLong: 'isLong' in inputStep.options && inputStep.options.isLong,
|
||||
isLong: 'isLong' in inputBlock.options && inputBlock.options.isLong,
|
||||
},
|
||||
]
|
||||
}, [])
|
||||
|
||||
const parseVariablesHeaders = (
|
||||
variables: Variable[],
|
||||
stepResultHeader: ResultHeaderCell[]
|
||||
blockResultHeader: ResultHeaderCell[]
|
||||
) =>
|
||||
variables.reduce<ResultHeaderCell[]>((headers, v) => {
|
||||
if (stepResultHeader.find((h) => h.variableId === v.id)) return headers
|
||||
if (blockResultHeader.find((h) => h.variableId === v.id)) return headers
|
||||
return [
|
||||
...headers,
|
||||
{
|
||||
@ -87,7 +87,7 @@ const parseVariablesHeaders = (
|
||||
}, [])
|
||||
|
||||
export const parseAnswers =
|
||||
({ blocks, variables }: { blocks: Block[]; variables: Variable[] }) =>
|
||||
({ groups, variables }: { groups: Group[]; variables: Variable[] }) =>
|
||||
({
|
||||
createdAt,
|
||||
answers,
|
||||
@ -95,7 +95,7 @@ export const parseAnswers =
|
||||
}: Pick<ResultWithAnswers, 'createdAt' | 'answers' | 'variables'>): {
|
||||
[key: string]: string
|
||||
} => {
|
||||
const header = parseResultHeader({ blocks, variables })
|
||||
const header = parseResultHeader({ groups, variables })
|
||||
return {
|
||||
submittedAt: createdAt,
|
||||
...[...answers, ...resultVariables].reduce<{
|
||||
@ -106,7 +106,7 @@ export const parseAnswers =
|
||||
const key = answer.variableId
|
||||
? header.find((cell) => cell.variableId === answer.variableId)
|
||||
?.label
|
||||
: header.find((cell) => cell.stepId === answer.stepId)?.label
|
||||
: header.find((cell) => cell.blockId === answer.blockId)?.label
|
||||
if (!key) return o
|
||||
return {
|
||||
...o,
|
||||
|
15
packages/utils/src/typebotConversions.ts
Normal file
15
packages/utils/src/typebotConversions.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { Typebot } from 'models'
|
||||
|
||||
export const convertTypebotToV2 = (typebot: any): Typebot => {
|
||||
const newTypebot = JSON.parse(
|
||||
JSON.stringify(typebot)
|
||||
.replace(/\"blocks\":/g, '"groups":')
|
||||
.replace(/\"steps\":/g, '"blocks":')
|
||||
.replace(/\"blockId\":/g, '"groupId":')
|
||||
.replace(/\"blockId\":/g, '"blockId":')
|
||||
)
|
||||
return {
|
||||
version: '2',
|
||||
...newTypebot,
|
||||
}
|
||||
}
|
@ -1,22 +1,22 @@
|
||||
import {
|
||||
BubbleStep,
|
||||
BubbleStepType,
|
||||
ChoiceInputStep,
|
||||
ConditionStep,
|
||||
InputStep,
|
||||
InputStepType,
|
||||
IntegrationStep,
|
||||
IntegrationStepType,
|
||||
LogicStep,
|
||||
LogicStepType,
|
||||
Step,
|
||||
TextInputStep,
|
||||
TextBubbleStep,
|
||||
WebhookStep,
|
||||
StepType,
|
||||
StepWithOptionsType,
|
||||
ImageBubbleStep,
|
||||
VideoBubbleStep,
|
||||
BubbleBlock,
|
||||
BubbleBlockType,
|
||||
ChoiceInputBlock,
|
||||
ConditionBlock,
|
||||
InputBlock,
|
||||
InputBlockType,
|
||||
IntegrationBlock,
|
||||
IntegrationBlockType,
|
||||
LogicBlock,
|
||||
LogicBlockType,
|
||||
Block,
|
||||
TextInputBlock,
|
||||
TextBubbleBlock,
|
||||
WebhookBlock,
|
||||
BlockType,
|
||||
BlockWithOptionsType,
|
||||
ImageBubbleBlock,
|
||||
VideoBubbleBlock,
|
||||
} from 'models'
|
||||
|
||||
export const sendRequest = async <ResponseData>(
|
||||
@ -67,78 +67,78 @@ export const isEmpty = (value: string | undefined | null): value is undefined =>
|
||||
export const isNotEmpty = (value: string | undefined | null): value is string =>
|
||||
value !== undefined && value !== null && value !== ''
|
||||
|
||||
export const isInputStep = (step: Step): step is InputStep =>
|
||||
(Object.values(InputStepType) as string[]).includes(step.type)
|
||||
export const isInputBlock = (block: Block): block is InputBlock =>
|
||||
(Object.values(InputBlockType) as string[]).includes(block.type)
|
||||
|
||||
export const isBubbleStep = (step: Step): step is BubbleStep =>
|
||||
(Object.values(BubbleStepType) as string[]).includes(step.type)
|
||||
export const isBubbleBlock = (block: Block): block is BubbleBlock =>
|
||||
(Object.values(BubbleBlockType) as string[]).includes(block.type)
|
||||
|
||||
export const isLogicStep = (step: Step): step is LogicStep =>
|
||||
(Object.values(LogicStepType) as string[]).includes(step.type)
|
||||
export const isLogicBlock = (block: Block): block is LogicBlock =>
|
||||
(Object.values(LogicBlockType) as string[]).includes(block.type)
|
||||
|
||||
export const isTextBubbleStep = (step: Step): step is TextBubbleStep =>
|
||||
step.type === BubbleStepType.TEXT
|
||||
export const isTextBubbleBlock = (block: Block): block is TextBubbleBlock =>
|
||||
block.type === BubbleBlockType.TEXT
|
||||
|
||||
export const isMediaBubbleStep = (
|
||||
step: Step
|
||||
): step is ImageBubbleStep | VideoBubbleStep =>
|
||||
step.type === BubbleStepType.IMAGE || step.type === BubbleStepType.VIDEO
|
||||
export const isMediaBubbleBlock = (
|
||||
block: Block
|
||||
): block is ImageBubbleBlock | VideoBubbleBlock =>
|
||||
block.type === BubbleBlockType.IMAGE || block.type === BubbleBlockType.VIDEO
|
||||
|
||||
export const isTextInputStep = (step: Step): step is TextInputStep =>
|
||||
step.type === InputStepType.TEXT
|
||||
export const isTextInputBlock = (block: Block): block is TextInputBlock =>
|
||||
block.type === InputBlockType.TEXT
|
||||
|
||||
export const isChoiceInput = (step: Step): step is ChoiceInputStep =>
|
||||
step.type === InputStepType.CHOICE
|
||||
export const isChoiceInput = (block: Block): block is ChoiceInputBlock =>
|
||||
block.type === InputBlockType.CHOICE
|
||||
|
||||
export const isSingleChoiceInput = (step: Step): step is ChoiceInputStep =>
|
||||
step.type === InputStepType.CHOICE &&
|
||||
'options' in step &&
|
||||
!step.options.isMultipleChoice
|
||||
export const isSingleChoiceInput = (block: Block): block is ChoiceInputBlock =>
|
||||
block.type === InputBlockType.CHOICE &&
|
||||
'options' in block &&
|
||||
!block.options.isMultipleChoice
|
||||
|
||||
export const isConditionStep = (step: Step): step is ConditionStep =>
|
||||
step.type === LogicStepType.CONDITION
|
||||
export const isConditionBlock = (block: Block): block is ConditionBlock =>
|
||||
block.type === LogicBlockType.CONDITION
|
||||
|
||||
export const isIntegrationStep = (step: Step): step is IntegrationStep =>
|
||||
(Object.values(IntegrationStepType) as string[]).includes(step.type)
|
||||
export const isIntegrationBlock = (block: Block): block is IntegrationBlock =>
|
||||
(Object.values(IntegrationBlockType) as string[]).includes(block.type)
|
||||
|
||||
export const isWebhookStep = (step: Step): step is WebhookStep =>
|
||||
export const isWebhookBlock = (block: Block): block is WebhookBlock =>
|
||||
[
|
||||
IntegrationStepType.WEBHOOK,
|
||||
IntegrationStepType.PABBLY_CONNECT,
|
||||
IntegrationStepType.ZAPIER,
|
||||
IntegrationStepType.MAKE_COM,
|
||||
].includes(step.type as IntegrationStepType)
|
||||
IntegrationBlockType.WEBHOOK,
|
||||
IntegrationBlockType.PABBLY_CONNECT,
|
||||
IntegrationBlockType.ZAPIER,
|
||||
IntegrationBlockType.MAKE_COM,
|
||||
].includes(block.type as IntegrationBlockType)
|
||||
|
||||
export const isBubbleStepType = (type: StepType): type is BubbleStepType =>
|
||||
(Object.values(BubbleStepType) as string[]).includes(type)
|
||||
export const isBubbleBlockType = (type: BlockType): type is BubbleBlockType =>
|
||||
(Object.values(BubbleBlockType) as string[]).includes(type)
|
||||
|
||||
export const stepTypeHasOption = (
|
||||
type: StepType
|
||||
): type is StepWithOptionsType =>
|
||||
(Object.values(InputStepType) as string[])
|
||||
.concat(Object.values(LogicStepType))
|
||||
.concat(Object.values(IntegrationStepType))
|
||||
export const blockTypeHasOption = (
|
||||
type: BlockType
|
||||
): type is BlockWithOptionsType =>
|
||||
(Object.values(InputBlockType) as string[])
|
||||
.concat(Object.values(LogicBlockType))
|
||||
.concat(Object.values(IntegrationBlockType))
|
||||
.includes(type)
|
||||
|
||||
export const stepTypeHasWebhook = (
|
||||
type: StepType
|
||||
): type is IntegrationStepType.WEBHOOK =>
|
||||
export const blockTypeHasWebhook = (
|
||||
type: BlockType
|
||||
): type is IntegrationBlockType.WEBHOOK =>
|
||||
Object.values([
|
||||
IntegrationStepType.WEBHOOK,
|
||||
IntegrationStepType.ZAPIER,
|
||||
IntegrationStepType.MAKE_COM,
|
||||
IntegrationStepType.PABBLY_CONNECT,
|
||||
IntegrationBlockType.WEBHOOK,
|
||||
IntegrationBlockType.ZAPIER,
|
||||
IntegrationBlockType.MAKE_COM,
|
||||
IntegrationBlockType.PABBLY_CONNECT,
|
||||
] as string[]).includes(type)
|
||||
|
||||
export const stepTypeHasItems = (
|
||||
type: StepType
|
||||
): type is LogicStepType.CONDITION | InputStepType.CHOICE =>
|
||||
type === LogicStepType.CONDITION || type === InputStepType.CHOICE
|
||||
export const blockTypeHasItems = (
|
||||
type: BlockType
|
||||
): type is LogicBlockType.CONDITION | InputBlockType.CHOICE =>
|
||||
type === LogicBlockType.CONDITION || type === InputBlockType.CHOICE
|
||||
|
||||
export const stepHasItems = (
|
||||
step: Step
|
||||
): step is ConditionStep | ChoiceInputStep =>
|
||||
'items' in step && isDefined(step.items)
|
||||
export const blockHasItems = (
|
||||
block: Block
|
||||
): block is ConditionBlock | ChoiceInputBlock =>
|
||||
'items' in block && isDefined(block.items)
|
||||
|
||||
export const byId = (id?: string) => (obj: { id: string }) => obj.id === id
|
||||
|
||||
|
Reference in New Issue
Block a user