sharing-updates-root.tsx 5.3 KB

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