2
0
Files
bot/apps/builder/layouts/results/ResultsContent.tsx

93 lines
2.8 KiB
TypeScript
Raw Normal View History

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'
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()
const { user } = useUser()
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',
})
const { stats, mutate } = useStats({
typebotId: publishedTypebot?.typebotId,
2021-12-30 10:24:16 +01:00
onError: (err) => toast({ title: err.name, description: err.message }),
})
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"
>
<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>
<Flex pt="60px" w="full" justify="center">
{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
typebotId={publishedTypebot.typebotId}
onDeleteResults={handleDeletedResults}
2022-01-03 17:39:59 +01:00
totalResults={stats?.totalStarts ?? 0}
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>
)
}