2023-04-01 09:00:59 +02:00
|
|
|
import { Stack, Text } from '@chakra-ui/react'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { isDefined } from '@typebot.io/lib'
|
2022-11-15 09:35:48 +01:00
|
|
|
import {
|
|
|
|
ResultWithAnswers,
|
|
|
|
ResultHeaderCell,
|
2023-03-15 11:51:30 +01:00
|
|
|
VariableWithValue,
|
2022-11-15 09:35:48 +01:00
|
|
|
InputBlockType,
|
2023-04-01 09:00:59 +02:00
|
|
|
Answer,
|
2023-03-15 08:35:16 +01:00
|
|
|
} from '@typebot.io/schemas'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { FileLinks } from '../components/FileLinks'
|
|
|
|
import { TableData } from '../types'
|
|
|
|
import { convertDateToReadable } from './convertDateToReadable'
|
2022-11-15 09:35:48 +01:00
|
|
|
|
|
|
|
export const convertResultsToTableData = (
|
|
|
|
results: ResultWithAnswers[] | undefined,
|
|
|
|
headerCells: ResultHeaderCell[]
|
|
|
|
): TableData[] =>
|
|
|
|
(results ?? []).map((result) => ({
|
|
|
|
id: { plainText: result.id },
|
2023-09-18 18:34:53 +02:00
|
|
|
date: {
|
2023-03-15 11:51:30 +01:00
|
|
|
plainText: convertDateToReadable(result.createdAt),
|
2022-11-15 09:35:48 +01:00
|
|
|
},
|
|
|
|
...[...result.answers, ...result.variables].reduce<{
|
|
|
|
[key: string]: { element?: JSX.Element; plainText: string }
|
2023-04-01 09:00:59 +02:00
|
|
|
}>((tableData, answerOrVariable) => {
|
2022-11-15 09:35:48 +01:00
|
|
|
if ('groupId' in answerOrVariable) {
|
2023-04-01 09:00:59 +02:00
|
|
|
const answer = answerOrVariable satisfies Answer
|
2022-11-15 09:35:48 +01:00
|
|
|
const header = answer.variableId
|
|
|
|
? headerCells.find((headerCell) =>
|
|
|
|
headerCell.variableIds?.includes(answer.variableId as string)
|
|
|
|
)
|
|
|
|
: headerCells.find((headerCell) =>
|
|
|
|
headerCell.blocks?.some((block) => block.id === answer.blockId)
|
|
|
|
)
|
2023-04-01 09:00:59 +02:00
|
|
|
if (!header || !header.blocks || !header.blockType) return tableData
|
|
|
|
const variableValue = result.variables.find(
|
|
|
|
(variable) => variable.id === answer.variableId
|
|
|
|
)?.value
|
|
|
|
const content = variableValue ?? answer.content
|
2022-11-15 09:35:48 +01:00
|
|
|
return {
|
2023-04-01 09:00:59 +02:00
|
|
|
...tableData,
|
2023-09-18 18:34:53 +02:00
|
|
|
[header.id]: parseCellContent(content, header.blockType),
|
2022-11-15 09:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-01 09:00:59 +02:00
|
|
|
const variable = answerOrVariable satisfies VariableWithValue
|
|
|
|
if (variable.value === null) return tableData
|
2023-09-18 18:34:53 +02:00
|
|
|
const headerId = headerCells.find((headerCell) =>
|
2022-11-15 09:35:48 +01:00
|
|
|
headerCell.variableIds?.includes(variable.id)
|
2023-09-18 18:34:53 +02:00
|
|
|
)?.id
|
|
|
|
if (!headerId) return tableData
|
|
|
|
if (isDefined(tableData[headerId])) return tableData
|
2022-11-15 09:35:48 +01:00
|
|
|
return {
|
2023-04-01 09:00:59 +02:00
|
|
|
...tableData,
|
2023-09-18 18:34:53 +02:00
|
|
|
[headerId]: parseCellContent(variable.value),
|
2022-11-15 09:35:48 +01:00
|
|
|
}
|
|
|
|
}, {}),
|
|
|
|
}))
|
|
|
|
|
2023-04-01 09:00:59 +02:00
|
|
|
const parseCellContent = (
|
|
|
|
content: VariableWithValue['value'],
|
|
|
|
blockType?: InputBlockType
|
|
|
|
): { element?: JSX.Element; plainText: string } => {
|
|
|
|
if (!content) return { element: undefined, plainText: '' }
|
|
|
|
if (Array.isArray(content))
|
|
|
|
return {
|
|
|
|
element: (
|
|
|
|
<Stack spacing={2}>
|
|
|
|
{content.map((item, idx) => (
|
|
|
|
<Text key={idx}>
|
|
|
|
{idx + 1}. {item}
|
|
|
|
</Text>
|
|
|
|
))}
|
|
|
|
</Stack>
|
|
|
|
),
|
|
|
|
plainText: content.join(', '),
|
|
|
|
}
|
|
|
|
return blockType === InputBlockType.FILE
|
|
|
|
? { element: <FileLinks fileNamesStr={content} />, plainText: content }
|
|
|
|
: { plainText: content.toString() }
|
|
|
|
}
|