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

92 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-06-02 07:54:48 +02:00
import { Button, Flex, HStack, Tag, Text } from '@chakra-ui/react'
2021-12-24 10:08:41 +01:00
import { NextChakraLink } from 'components/nextChakra/NextChakraLink'
2022-06-02 07:54:48 +02:00
import { useToast } from 'components/shared/hooks/useToast'
2022-01-06 09:40:56 +01:00
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
2022-05-13 15:22:44 -07:00
import { useWorkspace } from 'contexts/WorkspaceContext'
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-05-13 15:22:44 -07:00
import { isFreePlan } from 'services/workspace'
2021-12-24 10:08:41 +01:00
import { AnalyticsContent } from './AnalyticsContent'
import { SubmissionsContent } from './SubmissionContent'
export const ResultsContent = () => {
const router = useRouter()
2022-05-13 15:22:44 -07:00
const { workspace } = useWorkspace()
const { typebot, publishedTypebot } = useTypebot()
2021-12-24 10:08:41 +01:00
const isAnalytics = useMemo(
() => router.pathname.endsWith('analytics'),
[router.pathname]
)
2022-06-02 07:54:48 +02:00
const { showToast } = useToast()
2021-12-30 10:24:16 +01:00
const { stats, mutate } = useStats({
typebotId: publishedTypebot?.typebotId,
2022-06-02 07:54:48 +02:00
onError: (err) => showToast({ title: err.name, description: err.message }),
2021-12-30 10:24:16 +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"
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>
<Flex pt={['10px', '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={
2022-05-13 15:22:44 -07:00
isFreePlan(workspace)
? (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>
)
}