sharing-updates-root.tsx 5.6 KB

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