sharing-updates-root.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import useWaitForI18n from '@/shared/hooks/use-wait-for-i18n'
  2. import { Trans, useTranslation } from 'react-i18next'
  3. import withErrorBoundary from '@/infrastructure/error-boundary'
  4. import { GenericErrorBoundaryFallback } from '@/shared/components/generic-error-boundary-fallback'
  5. import { useCallback } from 'react'
  6. import getMeta from '@/utils/meta'
  7. import { postJSON } from '@/infrastructure/fetch-json'
  8. import { debugConsole } from '@/utils/debugging'
  9. import useAsync from '@/shared/hooks/use-async'
  10. import Notification from '@/shared/components/notification'
  11. import { sendMB } from '@/infrastructure/event-tracking'
  12. function SharingUpdatesRoot() {
  13. const { isReady } = useWaitForI18n()
  14. const { t } = useTranslation()
  15. const { isLoading, isSuccess, isError, runAsync } = useAsync()
  16. const projectId = getMeta('ol-project_id')
  17. const joinProject = useCallback(() => {
  18. sendMB('notification-click', {
  19. name: 'link-sharing-collaborator',
  20. button: 'ok',
  21. })
  22. runAsync(postJSON(`/project/${projectId}/sharing-updates/join`))
  23. .then(() => {
  24. location.assign(`/project/${projectId}`)
  25. })
  26. .catch(debugConsole.error)
  27. }, [runAsync, projectId])
  28. const viewProject = useCallback(() => {
  29. sendMB('notification-click', {
  30. name: 'link-sharing-collaborator',
  31. button: 'anonymous',
  32. })
  33. runAsync(postJSON(`/project/${projectId}/sharing-updates/view`))
  34. .then(() => {
  35. location.assign(`/project/${projectId}`)
  36. })
  37. .catch(debugConsole.error)
  38. }, [runAsync, projectId])
  39. const leaveProject = useCallback(() => {
  40. sendMB('notification-click', {
  41. name: 'link-sharing-collaborator',
  42. button: 'leave',
  43. })
  44. runAsync(postJSON(`/project/${projectId}/leave`))
  45. .then(() => {
  46. location.assign('/project')
  47. })
  48. .catch(debugConsole.error)
  49. }, [runAsync, projectId])
  50. if (!isReady) {
  51. return null
  52. }
  53. return (
  54. <div className="container">
  55. <div className="row">
  56. <div className="col-md-6 col-md-offset-3">
  57. <div className="card sharing-updates">
  58. <div className="row">
  59. <div className="col-md-12">
  60. <h1 className="sharing-updates-h1">
  61. {t('updates_to_project_sharing')}
  62. </h1>
  63. </div>
  64. </div>
  65. <div className="row row-spaced">
  66. <div className="col-md-12">
  67. <p>
  68. <Trans
  69. i18nKey="were_making_some_changes_to_project_sharing_this_means_you_will_be_visible"
  70. components={[
  71. // eslint-disable-next-line jsx-a11y/anchor-has-content, react/jsx-key
  72. <a
  73. href="/blog/changes-to-project-sharing"
  74. rel="noopener noreferrer"
  75. target="_blank"
  76. onClick={() => {
  77. sendMB('notification-click', {
  78. name: 'link-sharing-collaborator',
  79. button: 'learn',
  80. })
  81. }}
  82. />,
  83. ]}
  84. />
  85. </p>
  86. </div>
  87. </div>
  88. <div className="row row-spaced">
  89. <div className="col-md-12">
  90. <button
  91. className="btn btn-primary"
  92. onClick={() => joinProject()}
  93. disabled={isLoading}
  94. >
  95. {t('ok_continue_to_project')}
  96. </button>
  97. </div>
  98. </div>
  99. {isError && (
  100. <div className="row row-spaced">
  101. <div className="col-md-12">
  102. <Notification
  103. type="error"
  104. content={t('generic_something_went_wrong')}
  105. />
  106. </div>
  107. </div>
  108. )}
  109. <div className="row row-spaced">
  110. <div className="col-md-12">
  111. <p>
  112. <small>
  113. <Trans
  114. i18nKey="you_can_also_choose_to_view_anonymously_or_leave_the_project"
  115. components={[
  116. // eslint-disable-next-line react/jsx-key
  117. <button
  118. className="btn btn-inline-link"
  119. onClick={() => viewProject()}
  120. disabled={isLoading || isSuccess}
  121. />,
  122. // eslint-disable-next-line react/jsx-key
  123. <button
  124. className="btn btn-inline-link"
  125. onClick={() => leaveProject()}
  126. disabled={isLoading || isSuccess}
  127. />,
  128. ]}
  129. />
  130. </small>
  131. </p>
  132. </div>
  133. </div>
  134. </div>
  135. </div>
  136. </div>
  137. </div>
  138. )
  139. }
  140. export default withErrorBoundary(
  141. SharingUpdatesRoot,
  142. GenericErrorBoundaryFallback
  143. )