start-free-trial-button.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { useCallback, useEffect } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { startFreeTrial } from '@/main/account-upgrade'
  4. import * as eventTracking from '../../infrastructure/event-tracking'
  5. import OLButton from '@/features/ui/components/ol/ol-button'
  6. type StartFreeTrialButtonProps = {
  7. source: string
  8. variant?: string
  9. buttonProps?: React.ComponentProps<typeof OLButton>
  10. children?: React.ReactNode
  11. handleClick?: React.ComponentProps<typeof OLButton>['onClick']
  12. segmentation?: eventTracking.Segmentation
  13. }
  14. export default function StartFreeTrialButton({
  15. buttonProps = {
  16. variant: 'secondary',
  17. },
  18. children,
  19. handleClick,
  20. source,
  21. variant,
  22. segmentation,
  23. }: StartFreeTrialButtonProps) {
  24. const { t } = useTranslation()
  25. useEffect(() => {
  26. const eventSegmentation: { [key: string]: unknown } = {
  27. 'paywall-type': source,
  28. ...segmentation,
  29. }
  30. if (variant) {
  31. eventSegmentation.variant = variant
  32. }
  33. eventTracking.sendMB('paywall-prompt', eventSegmentation)
  34. }, [source, variant, segmentation])
  35. const onClick = useCallback(
  36. (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
  37. event.preventDefault()
  38. if (handleClick) {
  39. handleClick(event)
  40. }
  41. startFreeTrial(source, variant, segmentation)
  42. },
  43. [handleClick, source, variant, segmentation]
  44. )
  45. return (
  46. <OLButton {...buttonProps} onClick={onClick}>
  47. {children || t('start_free_trial')}
  48. </OLButton>
  49. )
  50. }