2022-11-15 09:35:48 +01:00
|
|
|
import {
|
|
|
|
ResultWithAnswers,
|
|
|
|
ResultHeaderCell,
|
2023-03-15 11:51:30 +01:00
|
|
|
VariableWithValue,
|
2023-04-01 09:00:59 +02:00
|
|
|
Answer,
|
2024-01-12 10:16:01 +01:00
|
|
|
TableData,
|
2023-03-15 08:35:16 +01:00
|
|
|
} from '@typebot.io/schemas'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
|
2024-03-15 16:32:29 +01:00
|
|
|
import { isDefined } from '../lib/utils'
|
2024-01-12 10:16:01 +01:00
|
|
|
|
|
|
|
type CellParser = (
|
|
|
|
content: VariableWithValue['value'],
|
|
|
|
blockType?: InputBlockType
|
|
|
|
) => { element?: React.JSX.Element; plainText: string }
|
|
|
|
|
|
|
|
const defaultCellParser: CellParser = (content, blockType) => {
|
|
|
|
if (!content) return { plainText: '' }
|
|
|
|
if (Array.isArray(content))
|
|
|
|
return {
|
|
|
|
plainText: content.join(', '),
|
|
|
|
}
|
|
|
|
return blockType === InputBlockType.FILE
|
|
|
|
? { plainText: content }
|
|
|
|
: { plainText: content.toString() }
|
|
|
|
}
|
2022-11-15 09:35:48 +01:00
|
|
|
|
|
|
|
export const convertResultsToTableData = (
|
|
|
|
results: ResultWithAnswers[] | undefined,
|
2024-01-12 10:16:01 +01:00
|
|
|
headerCells: ResultHeaderCell[],
|
|
|
|
cellParser: CellParser = defaultCellParser
|
2022-11-15 09:35:48 +01:00
|
|
|
): 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,
|
2024-01-12 10:16:01 +01:00
|
|
|
[header.id]: cellParser(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,
|
2024-01-12 10:16:01 +01:00
|
|
|
[headerId]: cellParser(variable.value),
|
2022-11-15 09:35:48 +01:00
|
|
|
}
|
|
|
|
}, {}),
|
|
|
|
}))
|
|
|
|
|
2024-03-05 10:06:42 +01:00
|
|
|
const convertDateToReadable = (date: Date): string => {
|
|
|
|
const isThisYear = new Date().getFullYear() === date.getFullYear()
|
|
|
|
|
|
|
|
const dateString = date.toLocaleDateString('default', {
|
|
|
|
month: 'short',
|
|
|
|
day: 'numeric',
|
|
|
|
year: isThisYear ? undefined : 'numeric', // Only show the year if it's not the current year
|
|
|
|
})
|
|
|
|
|
|
|
|
const timeString = date.toLocaleTimeString('default', {
|
2024-01-12 10:16:01 +01:00
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
|
|
|
})
|
2024-03-05 10:06:42 +01:00
|
|
|
|
|
|
|
return `${dateString}, ${timeString}`
|
|
|
|
}
|