enable-widget.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { ReactNode } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { Button } from 'react-bootstrap'
  4. import { sendMB } from '@/infrastructure/event-tracking'
  5. function trackUpgradeClick() {
  6. sendMB('settings-upgrade-click')
  7. }
  8. type EnableWidgetProps = {
  9. logo: ReactNode
  10. title: string
  11. description: string
  12. helpPath: string
  13. helpTextOverride?: string
  14. hasFeature?: boolean
  15. isPremiumFeature?: boolean
  16. statusIndicator?: ReactNode
  17. children?: ReactNode
  18. linked?: boolean
  19. handleLinkClick: () => void
  20. handleUnlinkClick: () => void
  21. disabled?: boolean
  22. }
  23. export function EnableWidget({
  24. logo,
  25. title,
  26. description,
  27. helpPath,
  28. helpTextOverride,
  29. hasFeature,
  30. isPremiumFeature,
  31. statusIndicator,
  32. linked,
  33. handleLinkClick,
  34. handleUnlinkClick,
  35. children,
  36. disabled,
  37. }: EnableWidgetProps) {
  38. const { t } = useTranslation()
  39. const helpText = helpTextOverride || t('learn_more')
  40. return (
  41. <div className="settings-widget-container">
  42. <div>{logo}</div>
  43. <div className="description-container">
  44. <div className="title-row">
  45. <h4>{title}</h4>
  46. {!hasFeature && isPremiumFeature && (
  47. <span className="label label-info">{t('premium_feature')}</span>
  48. )}
  49. </div>
  50. <p className="small">
  51. {description}{' '}
  52. <a href={helpPath} target="_blank" rel="noreferrer">
  53. {helpText}
  54. </a>
  55. </p>
  56. {children}
  57. {hasFeature && statusIndicator}
  58. </div>
  59. <div>
  60. <ActionButton
  61. hasFeature={hasFeature}
  62. linked={linked}
  63. handleUnlinkClick={handleUnlinkClick}
  64. handleLinkClick={handleLinkClick}
  65. disabled={disabled}
  66. />
  67. </div>
  68. </div>
  69. )
  70. }
  71. type ActionButtonProps = {
  72. hasFeature?: boolean
  73. linked?: boolean
  74. handleUnlinkClick: () => void
  75. handleLinkClick: () => void
  76. disabled?: boolean
  77. }
  78. function ActionButton({
  79. linked,
  80. handleUnlinkClick,
  81. handleLinkClick,
  82. hasFeature,
  83. disabled,
  84. }: ActionButtonProps) {
  85. const { t } = useTranslation()
  86. if (!hasFeature) {
  87. return (
  88. <Button
  89. bsStyle={null}
  90. className="btn-primary"
  91. href="/user/subscription/plans"
  92. onClick={trackUpgradeClick}
  93. >
  94. <span className="text-capitalize">{t('upgrade')}</span>
  95. </Button>
  96. )
  97. } else if (linked) {
  98. return (
  99. <Button
  100. className="btn-danger-ghost"
  101. onClick={handleUnlinkClick}
  102. bsStyle={null}
  103. disabled={disabled}
  104. >
  105. {t('turn_off')}
  106. </Button>
  107. )
  108. } else {
  109. return (
  110. <Button
  111. disabled={disabled}
  112. bsStyle={null}
  113. onClick={handleLinkClick}
  114. className="btn btn-secondary-info btn-secondary text-capitalize"
  115. >
  116. {t('turn_on')}
  117. </Button>
  118. )
  119. }
  120. }
  121. export default EnableWidget