enable-widget.tsx 2.7 KB

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