(typebotLink) Better typebot link with merge option

Closes #675
This commit is contained in:
Baptiste Arnaud
2023-08-24 07:48:30 +02:00
parent 0acede92ef
commit ee3b94c35d
59 changed files with 1147 additions and 696 deletions

View File

@@ -1,11 +1,17 @@
import { useToast } from '@/hooks/useToast'
import { ResultHeaderCell, ResultWithAnswers } from '@typebot.io/schemas'
import {
LogicBlockType,
ResultHeaderCell,
ResultWithAnswers,
} from '@typebot.io/schemas'
import { createContext, ReactNode, useContext, useMemo } from 'react'
import { parseResultHeader } from '@typebot.io/lib/results'
import { useTypebot } from '../editor/providers/TypebotProvider'
import { useResultsQuery } from './hooks/useResultsQuery'
import { TableData } from './types'
import { convertResultsToTableData } from './helpers/convertResultsToTableData'
import { trpc } from '@/lib/trpc'
import { isDefined } from '@typebot.io/lib/utils'
const resultsContext = createContext<{
resultsList: { results: ResultWithAnswers[] }[] | undefined
@@ -32,7 +38,7 @@ export const ResultsProvider = ({
totalResults: number
onDeleteResults: (totalResultsDeleted: number) => void
}) => {
const { publishedTypebot, linkedTypebots } = useTypebot()
const { publishedTypebot } = useTypebot()
const { showToast } = useToast()
const { data, fetchNextPage, hasNextPage, refetch } = useResultsQuery({
typebotId,
@@ -41,6 +47,29 @@ export const ResultsProvider = ({
},
})
const linkedTypebotIds =
publishedTypebot?.groups
.flatMap((group) => group.blocks)
.reduce<string[]>(
(typebotIds, block) =>
block.type === LogicBlockType.TYPEBOT_LINK &&
isDefined(block.options.typebotId) &&
!typebotIds.includes(block.options.typebotId) &&
block.options.mergeResults !== false
? [...typebotIds, block.options.typebotId]
: typebotIds,
[]
) ?? []
const { data: linkedTypebotsData } = trpc.getLinkedTypebots.useQuery(
{
typebotId,
},
{
enabled: linkedTypebotIds.length > 0,
}
)
const flatResults = useMemo(
() => data?.flatMap((d) => d.results) ?? [],
[data]
@@ -49,9 +78,9 @@ export const ResultsProvider = ({
const resultHeader = useMemo(
() =>
publishedTypebot
? parseResultHeader(publishedTypebot, linkedTypebots)
? parseResultHeader(publishedTypebot, linkedTypebotsData?.typebots)
: [],
[linkedTypebots, publishedTypebot]
[linkedTypebotsData?.typebots, publishedTypebot]
)
const tableData = useMemo(

View File

@@ -23,6 +23,7 @@ import { useResults } from '../../ResultsProvider'
import { parseColumnOrder } from '../../helpers/parseColumnsOrder'
import { convertResultsToTableData } from '../../helpers/convertResultsToTableData'
import { parseAccessor } from '../../helpers/parseAccessor'
import { isDefined } from '@typebot.io/lib'
type Props = {
isOpen: boolean
@@ -30,7 +31,7 @@ type Props = {
}
export const ExportAllResultsModal = ({ isOpen, onClose }: Props) => {
const { typebot, publishedTypebot, linkedTypebots } = useTypebot()
const { typebot, publishedTypebot } = useTypebot()
const workspaceId = typebot?.workspaceId
const typebotId = typebot?.id
const { showToast } = useToast()
@@ -41,6 +42,15 @@ export const ExportAllResultsModal = ({ isOpen, onClose }: Props) => {
const [areDeletedBlocksIncluded, setAreDeletedBlocksIncluded] =
useState(false)
const { data: linkedTypebotsData } = trpc.getLinkedTypebots.useQuery(
{
typebotId: typebotId as string,
},
{
enabled: isDefined(typebotId),
}
)
const getAllResults = async () => {
if (!workspaceId || !typebotId) return []
const allResults = []
@@ -71,7 +81,11 @@ export const ExportAllResultsModal = ({ isOpen, onClose }: Props) => {
const results = await getAllResults()
const resultHeader = areDeletedBlocksIncluded
? parseResultHeader(publishedTypebot, linkedTypebots, results)
? parseResultHeader(
publishedTypebot,
linkedTypebotsData?.typebots,
results
)
: existingResultHeader
const dataToUnparse = convertResultsToTableData(results, resultHeader)