Files
bot/apps/builder/src/features/analytics/components/AnalyticsGraphContainer.tsx

75 lines
2.2 KiB
TypeScript
Raw Normal View History

import {
Flex,
Spinner,
useColorModeValue,
useDisclosure,
} from '@chakra-ui/react'
import { useToast } from '@/hooks/useToast'
import { useTypebot } from '@/features/editor'
2022-01-06 09:40:56 +01:00
import { Stats } from 'models'
2021-12-24 10:08:41 +01:00
import React from 'react'
import { useAnswersCount } from '../hooks/useAnswersCount'
import {
Graph,
GraphProvider,
GroupsCoordinatesProvider,
} from '@/features/graph'
import { ChangePlanModal, LimitReached } from '@/features/billing'
import { StatsCards } from './StatsCards'
2021-12-24 10:08:41 +01:00
export const AnalyticsGraphContainer = ({ stats }: { stats?: Stats }) => {
2022-02-13 06:53:48 +01:00
const { isOpen, onOpen, onClose } = useDisclosure()
2022-01-03 17:39:59 +01:00
const { typebot, publishedTypebot } = useTypebot()
2022-06-02 07:54:48 +02:00
const { showToast } = useToast()
2022-01-03 17:39:59 +01:00
const { answersCounts } = useAnswersCount({
typebotId: publishedTypebot && typebot?.id,
2022-06-02 07:54:48 +02:00
onError: (err) => showToast({ title: err.name, description: err.message }),
2022-01-03 17:39:59 +01:00
})
return (
<Flex
w="full"
pos="relative"
bgColor={useColorModeValue('white', 'gray.850')}
backgroundImage={useColorModeValue(
'radial-gradient(#c6d0e1 1px, transparent 0)',
'radial-gradient(#2f2f39 1px, transparent 0)'
)}
backgroundSize="40px 40px"
backgroundPosition="-19px -19px"
2022-01-03 17:39:59 +01:00
h="full"
justifyContent="center"
>
{publishedTypebot && answersCounts && stats ? (
<GraphProvider isReadOnly>
<GroupsCoordinatesProvider groups={publishedTypebot?.groups}>
<Graph
flex="1"
typebot={publishedTypebot}
onUnlockProPlanClick={onOpen}
answersCounts={[
{ ...answersCounts[0], totalAnswers: stats?.totalStarts },
...answersCounts?.slice(1),
]}
/>
</GroupsCoordinatesProvider>
2022-02-11 18:06:59 +01:00
</GraphProvider>
) : (
<Flex
justify="center"
align="center"
boxSize="full"
bgColor="rgba(255, 255, 255, 0.5)"
>
<Spinner color="gray" />
</Flex>
2022-01-03 17:39:59 +01:00
)}
<ChangePlanModal
onClose={onClose}
isOpen={isOpen}
type={LimitReached.ANALYTICS}
/>
2022-01-03 17:39:59 +01:00
<StatsCards stats={stats} pos="absolute" top={10} />
</Flex>
)
2021-12-24 10:08:41 +01:00
}