2022-01-03 17:39:59 +01:00
|
|
|
import { Button, Flex, HStack, Tag, useToast, Text } from '@chakra-ui/react'
|
2021-12-24 10:08:41 +01:00
|
|
|
import { NextChakraLink } from 'components/nextChakra/NextChakraLink'
|
2022-01-06 09:40:56 +01:00
|
|
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
2022-02-12 12:54:16 +01:00
|
|
|
import { useUser } from 'contexts/UserContext'
|
2021-12-24 10:08:41 +01:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import React, { useMemo } from 'react'
|
2022-01-03 17:39:59 +01:00
|
|
|
import { useStats } from 'services/analytics'
|
2022-02-24 11:13:19 +01:00
|
|
|
import { isFreePlan } from 'services/user/user'
|
2021-12-24 10:08:41 +01:00
|
|
|
import { AnalyticsContent } from './AnalyticsContent'
|
|
|
|
import { SubmissionsContent } from './SubmissionContent'
|
|
|
|
|
|
|
|
export const ResultsContent = () => {
|
|
|
|
const router = useRouter()
|
2022-02-12 12:54:16 +01:00
|
|
|
const { user } = useUser()
|
2022-02-17 10:46:04 +01:00
|
|
|
const { typebot, publishedTypebot } = useTypebot()
|
2021-12-24 10:08:41 +01:00
|
|
|
const isAnalytics = useMemo(
|
|
|
|
() => router.pathname.endsWith('analytics'),
|
|
|
|
[router.pathname]
|
|
|
|
)
|
2021-12-30 10:24:16 +01:00
|
|
|
const toast = useToast({
|
|
|
|
position: 'top-right',
|
|
|
|
status: 'error',
|
|
|
|
})
|
|
|
|
|
2022-01-04 12:25:48 +01:00
|
|
|
const { stats, mutate } = useStats({
|
2022-02-17 10:46:04 +01:00
|
|
|
typebotId: publishedTypebot?.typebotId,
|
2021-12-30 10:24:16 +01:00
|
|
|
onError: (err) => toast({ title: err.name, description: err.message }),
|
|
|
|
})
|
2022-01-04 12:25:48 +01:00
|
|
|
|
|
|
|
const handleDeletedResults = (total: number) => {
|
|
|
|
if (!stats) return
|
|
|
|
mutate({
|
|
|
|
stats: { ...stats, totalStarts: stats.totalStarts - total },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-24 10:08:41 +01:00
|
|
|
return (
|
2022-01-03 17:39:59 +01:00
|
|
|
<Flex h="full" w="full">
|
|
|
|
<Flex
|
|
|
|
pos="absolute"
|
|
|
|
zIndex={2}
|
|
|
|
bgColor="white"
|
|
|
|
w="full"
|
|
|
|
justifyContent="center"
|
|
|
|
h="60px"
|
2022-03-24 11:44:34 +01:00
|
|
|
display={['none', 'flex']}
|
2022-01-03 17:39:59 +01:00
|
|
|
>
|
|
|
|
<HStack maxW="1200px" w="full">
|
2021-12-24 10:08:41 +01:00
|
|
|
<Button
|
|
|
|
as={NextChakraLink}
|
|
|
|
colorScheme={!isAnalytics ? 'blue' : 'gray'}
|
|
|
|
variant={!isAnalytics ? 'outline' : 'ghost'}
|
|
|
|
size="sm"
|
|
|
|
href={`/typebots/${typebot?.id}/results`}
|
|
|
|
>
|
2021-12-30 10:24:16 +01:00
|
|
|
<Text>Submissions</Text>
|
2022-01-03 17:39:59 +01:00
|
|
|
{(stats?.totalStarts ?? 0) > 0 && (
|
2021-12-30 10:24:16 +01:00
|
|
|
<Tag size="sm" colorScheme="blue" ml="1">
|
2022-01-03 17:39:59 +01:00
|
|
|
{stats?.totalStarts}
|
2021-12-30 10:24:16 +01:00
|
|
|
</Tag>
|
|
|
|
)}
|
2021-12-24 10:08:41 +01:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
as={NextChakraLink}
|
|
|
|
colorScheme={isAnalytics ? 'blue' : 'gray'}
|
|
|
|
variant={isAnalytics ? 'outline' : 'ghost'}
|
|
|
|
href={`/typebots/${typebot?.id}/results/analytics`}
|
|
|
|
size="sm"
|
|
|
|
>
|
|
|
|
Analytics
|
|
|
|
</Button>
|
|
|
|
</HStack>
|
2022-01-03 17:39:59 +01:00
|
|
|
</Flex>
|
2022-03-24 11:44:34 +01:00
|
|
|
<Flex pt={['10px', '60px']} w="full" justify="center">
|
2022-02-17 10:46:04 +01:00
|
|
|
{publishedTypebot &&
|
2021-12-30 10:24:16 +01:00
|
|
|
(isAnalytics ? (
|
2022-01-03 17:39:59 +01:00
|
|
|
<AnalyticsContent stats={stats} />
|
2021-12-30 10:24:16 +01:00
|
|
|
) : (
|
|
|
|
<SubmissionsContent
|
2022-02-17 10:46:04 +01:00
|
|
|
typebotId={publishedTypebot.typebotId}
|
2022-01-04 12:25:48 +01:00
|
|
|
onDeleteResults={handleDeletedResults}
|
2022-01-03 17:39:59 +01:00
|
|
|
totalResults={stats?.totalStarts ?? 0}
|
2022-02-12 12:54:16 +01:00
|
|
|
totalHiddenResults={
|
|
|
|
isFreePlan(user)
|
|
|
|
? (stats?.totalStarts ?? 0) - (stats?.totalCompleted ?? 0)
|
|
|
|
: undefined
|
|
|
|
}
|
2021-12-30 10:24:16 +01:00
|
|
|
/>
|
|
|
|
))}
|
2022-01-03 17:39:59 +01:00
|
|
|
</Flex>
|
2021-12-24 10:08:41 +01:00
|
|
|
</Flex>
|
|
|
|
)
|
|
|
|
}
|