sharing-updates-root.tsx 5.7 KB

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