2023-02-17 16:19:39 +01:00
|
|
|
import { Text, HStack, Link, Spinner, Stack, Heading } from '@chakra-ui/react'
|
2022-11-15 09:35:48 +01:00
|
|
|
import { useToast } from '@/hooks/useToast'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { Plan } from '@typebot.io/prisma'
|
2023-02-17 16:19:39 +01:00
|
|
|
import React from 'react'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { PlanTag } from './PlanTag'
|
2023-02-17 16:19:39 +01:00
|
|
|
import { BillingPortalButton } from './BillingPortalButton'
|
|
|
|
|
import { trpc } from '@/lib/trpc'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { Workspace } from '@typebot.io/schemas'
|
2023-04-06 17:31:23 +02:00
|
|
|
import { useScopedI18n } from '@/locales'
|
2022-09-17 16:37:33 +02:00
|
|
|
|
2023-03-15 11:51:30 +01:00
|
|
|
type Props = {
|
2023-02-17 16:19:39 +01:00
|
|
|
workspace: Pick<Workspace, 'id' | 'plan' | 'stripeId'>
|
2022-09-17 16:37:33 +02:00
|
|
|
onCancelSuccess: () => void
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:51:30 +01:00
|
|
|
export const CurrentSubscriptionSummary = ({
|
2023-02-17 16:19:39 +01:00
|
|
|
workspace,
|
2022-09-17 16:37:33 +02:00
|
|
|
onCancelSuccess,
|
2023-03-15 11:51:30 +01:00
|
|
|
}: Props) => {
|
2023-04-06 17:31:23 +02:00
|
|
|
const scopedT = useScopedI18n('billing.currentSubscription')
|
2022-09-20 19:15:47 +02:00
|
|
|
const { showToast } = useToast()
|
2022-09-17 16:37:33 +02:00
|
|
|
|
2023-02-17 16:19:39 +01:00
|
|
|
const { mutate: cancelSubscription, isLoading: isCancelling } =
|
|
|
|
|
trpc.billing.cancelSubscription.useMutation({
|
|
|
|
|
onError: (error) => {
|
|
|
|
|
showToast({
|
|
|
|
|
description: error.message,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
onSuccess: onCancelSuccess,
|
|
|
|
|
})
|
2022-09-17 16:37:33 +02:00
|
|
|
|
2023-02-17 16:19:39 +01:00
|
|
|
const isSubscribed =
|
|
|
|
|
(workspace.plan === Plan.STARTER || workspace.plan === Plan.PRO) &&
|
|
|
|
|
workspace.stripeId
|
2022-09-18 19:01:37 +02:00
|
|
|
|
2022-09-17 16:37:33 +02:00
|
|
|
return (
|
2022-12-20 16:55:43 +01:00
|
|
|
<Stack spacing="4">
|
2023-04-06 17:31:23 +02:00
|
|
|
<Heading fontSize="3xl">{scopedT('heading')}</Heading>
|
2022-10-01 08:36:49 +02:00
|
|
|
<HStack data-testid="current-subscription">
|
2023-04-06 17:31:23 +02:00
|
|
|
<Text>{scopedT('subheading')} </Text>
|
2022-09-20 19:15:47 +02:00
|
|
|
{isCancelling ? (
|
|
|
|
|
<Spinner color="gray.500" size="xs" />
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
2023-02-17 16:19:39 +01:00
|
|
|
<PlanTag plan={workspace.plan} />
|
2022-09-20 19:15:47 +02:00
|
|
|
{isSubscribed && (
|
|
|
|
|
<Link
|
|
|
|
|
as="button"
|
|
|
|
|
color="gray.500"
|
|
|
|
|
textDecor="underline"
|
|
|
|
|
fontSize="sm"
|
2023-02-17 16:19:39 +01:00
|
|
|
onClick={() =>
|
|
|
|
|
cancelSubscription({ workspaceId: workspace.id })
|
|
|
|
|
}
|
2022-09-20 19:15:47 +02:00
|
|
|
>
|
2023-04-06 17:31:23 +02:00
|
|
|
{scopedT('cancelLink')}
|
2022-09-20 19:15:47 +02:00
|
|
|
</Link>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2022-09-18 19:01:37 +02:00
|
|
|
)}
|
2022-09-17 16:37:33 +02:00
|
|
|
</HStack>
|
|
|
|
|
|
2022-09-20 19:15:47 +02:00
|
|
|
{isSubscribed && !isCancelling && (
|
2022-09-17 16:37:33 +02:00
|
|
|
<>
|
2022-12-20 16:55:43 +01:00
|
|
|
<Stack spacing="4">
|
2023-04-06 17:31:23 +02:00
|
|
|
<Text fontSize="sm">{scopedT('billingPortalDescription')}</Text>
|
2023-02-17 16:19:39 +01:00
|
|
|
<BillingPortalButton workspaceId={workspace.id} />
|
2022-09-17 16:37:33 +02:00
|
|
|
</Stack>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</Stack>
|
|
|
|
|
)
|
|
|
|
|
}
|