grammarly-advert.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { useCallback, useEffect, useState } from 'react'
  2. import { Button } from 'react-bootstrap'
  3. import { useTranslation } from 'react-i18next'
  4. import Notification from '@/shared/components/notification'
  5. import useRemindMeLater from '@/shared/hooks/use-remind-me-later'
  6. import GrammarlyLogo from '@/shared/svgs/grammarly-logo'
  7. import * as eventTracking from '../../../infrastructure/event-tracking'
  8. import useWaitForGrammarlyCheck from '@/shared/hooks/use-wait-for-grammarly-check'
  9. export default function GrammarlyAdvert() {
  10. const [show, setShow] = useState(false)
  11. const { t } = useTranslation()
  12. // grammarly can take some time to load, we should assume its installed and hide until we know for sure
  13. const grammarlyInstalled = useWaitForGrammarlyCheck({ initialState: false })
  14. const { stillDissmissed, remindThemLater, saveDismissed } =
  15. useRemindMeLater('grammarly_advert')
  16. useEffect(() => {
  17. const showGrammarlyAdvert = grammarlyInstalled && !stillDissmissed
  18. if (showGrammarlyAdvert) {
  19. eventTracking.sendMB('grammarly-advert-shown')
  20. setShow(true)
  21. }
  22. }, [stillDissmissed, grammarlyInstalled, setShow])
  23. const handleDismiss = useCallback(() => {
  24. setShow(false)
  25. saveDismissed()
  26. eventTracking.sendMB('grammarly-advert-dismissed')
  27. }, [saveDismissed])
  28. const handleClickClaim = useCallback(() => {
  29. eventTracking.sendMB('promo-click', {
  30. location: 'notification',
  31. name: 'grammarly-advert',
  32. type: 'click',
  33. })
  34. saveDismissed()
  35. setShow(false)
  36. window.open(
  37. 'https://grammarly.go2cloud.org/aff_c?offer_id=373&aff_id=142242'
  38. )
  39. }, [saveDismissed])
  40. const handleLater = useCallback(() => {
  41. eventTracking.sendMB('promo-click', {
  42. location: 'notification',
  43. name: 'grammarly-advert',
  44. type: 'pause',
  45. })
  46. setShow(false)
  47. remindThemLater()
  48. }, [remindThemLater])
  49. if (!show) {
  50. return null
  51. }
  52. const actions = (
  53. <div>
  54. <Button
  55. bsStyle={null}
  56. className="btn-secondary"
  57. onClick={handleClickClaim}
  58. >
  59. {t('claim_discount')}
  60. </Button>
  61. <Button className="btn-bg-ghost" bsStyle={null} onClick={handleLater}>
  62. {t('maybe_later')}
  63. </Button>
  64. </div>
  65. )
  66. return (
  67. <Notification
  68. action={actions}
  69. ariaLive="polite"
  70. className="editor-notification ol-overlay"
  71. content={
  72. <div>
  73. <p>
  74. Get 25% off Grammarly Premium with this exclusive offer for Overleaf
  75. users.
  76. </p>
  77. </div>
  78. }
  79. customIcon={
  80. <div>
  81. <GrammarlyLogo width="50" height="50" background="#fff" />
  82. </div>
  83. }
  84. isActionBelowContent
  85. isDismissible
  86. onDismiss={handleDismiss}
  87. title="Love Grammarly? Then you're in luck!"
  88. type="offer"
  89. />
  90. )
  91. }