link-sharing.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { useCallback, useState } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { Button, Col, Row } from 'react-bootstrap'
  4. import { Trans } from 'react-i18next'
  5. import Tooltip from '../../../shared/components/tooltip'
  6. import Icon from '../../../shared/components/icon'
  7. import { useShareProjectContext } from './share-project-modal'
  8. import { setProjectAccessLevel } from '../utils/api'
  9. import CopyLink from '../../../shared/components/copy-link'
  10. import { useProjectContext } from '../../../shared/context/project-context'
  11. import * as eventTracking from '../../../infrastructure/event-tracking'
  12. import { useUserContext } from '../../../shared/context/user-context'
  13. export default function LinkSharing() {
  14. const [inflight, setInflight] = useState(false)
  15. const { monitorRequest } = useShareProjectContext()
  16. const { _id: projectId, publicAccessLevel } = useProjectContext()
  17. // set the access level of a project
  18. const setAccessLevel = useCallback(
  19. newPublicAccessLevel => {
  20. setInflight(true)
  21. monitorRequest(() =>
  22. setProjectAccessLevel(projectId, newPublicAccessLevel)
  23. )
  24. .then(() => {
  25. // NOTE: not calling `updateProject` here as it receives data via
  26. // project:publicAccessLevel:changed and project:tokens:changed
  27. // over the websocket connection
  28. // TODO: eventTracking.sendMB('project-make-token-based') when newPublicAccessLevel is 'tokenBased'
  29. })
  30. .finally(() => {
  31. setInflight(false)
  32. })
  33. },
  34. [monitorRequest, projectId]
  35. )
  36. switch (publicAccessLevel) {
  37. // Private (with token-access available)
  38. case 'private':
  39. return (
  40. <PrivateSharing setAccessLevel={setAccessLevel} inflight={inflight} />
  41. )
  42. // Token-based access
  43. case 'tokenBased':
  44. return (
  45. <TokenBasedSharing
  46. setAccessLevel={setAccessLevel}
  47. inflight={inflight}
  48. />
  49. )
  50. // Legacy public-access
  51. case 'readAndWrite':
  52. case 'readOnly':
  53. return (
  54. <LegacySharing
  55. setAccessLevel={setAccessLevel}
  56. accessLevel={publicAccessLevel}
  57. inflight={inflight}
  58. />
  59. )
  60. default:
  61. return null
  62. }
  63. }
  64. function PrivateSharing({ setAccessLevel, inflight }) {
  65. return (
  66. <Row className="public-access-level">
  67. <Col xs={12} className="text-center">
  68. <Trans i18nKey="link_sharing_is_off" />
  69. <span>&nbsp;&nbsp;</span>
  70. <Button
  71. type="button"
  72. bsStyle="link"
  73. className="btn-inline-link"
  74. onClick={() => {
  75. setAccessLevel('tokenBased')
  76. eventTracking.sendMB('link-sharing-click')
  77. }}
  78. disabled={inflight}
  79. >
  80. <Trans i18nKey="turn_on_link_sharing" />
  81. </Button>
  82. <span>&nbsp;&nbsp;</span>
  83. <LinkSharingInfo />
  84. </Col>
  85. </Row>
  86. )
  87. }
  88. PrivateSharing.propTypes = {
  89. setAccessLevel: PropTypes.func.isRequired,
  90. inflight: PropTypes.bool,
  91. }
  92. function TokenBasedSharing({ setAccessLevel, inflight }) {
  93. const { tokens } = useProjectContext()
  94. return (
  95. <Row className="public-access-level">
  96. <Col xs={12} className="text-center">
  97. <strong>
  98. <Trans i18nKey="link_sharing_is_on" />
  99. </strong>
  100. <span>&nbsp;&nbsp;</span>
  101. <Button
  102. bsStyle="link"
  103. className="btn-inline-link"
  104. onClick={() => setAccessLevel('private')}
  105. disabled={inflight}
  106. >
  107. <Trans i18nKey="turn_off_link_sharing" />
  108. </Button>
  109. <span>&nbsp;&nbsp;</span>
  110. <LinkSharingInfo />
  111. </Col>
  112. <Col xs={12} className="access-token-display-area">
  113. <div className="access-token-wrapper">
  114. <strong>
  115. <Trans i18nKey="anyone_with_link_can_edit" />
  116. </strong>
  117. <AccessToken
  118. token={tokens?.readAndWrite}
  119. path="/"
  120. tooltipId="tooltip-copy-link-rw"
  121. />
  122. </div>
  123. <div className="access-token-wrapper">
  124. <strong>
  125. <Trans i18nKey="anyone_with_link_can_view" />
  126. </strong>
  127. <AccessToken
  128. token={tokens?.readOnly}
  129. path="/read/"
  130. tooltipId="tooltip-copy-link-ro"
  131. />
  132. </div>
  133. </Col>
  134. </Row>
  135. )
  136. }
  137. TokenBasedSharing.propTypes = {
  138. setAccessLevel: PropTypes.func.isRequired,
  139. inflight: PropTypes.bool,
  140. }
  141. function LegacySharing({ accessLevel, setAccessLevel, inflight }) {
  142. return (
  143. <Row className="public-access-level">
  144. <Col xs={12} className="text-center">
  145. <strong>
  146. {accessLevel === 'readAndWrite' && (
  147. <Trans i18nKey="this_project_is_public" />
  148. )}
  149. {accessLevel === 'readOnly' && (
  150. <Trans i18nKey="this_project_is_public_read_only" />
  151. )}
  152. </strong>
  153. <span>&nbsp;&nbsp;</span>
  154. <Button
  155. type="button"
  156. bsStyle="link"
  157. className="btn-inline-link"
  158. onClick={() => setAccessLevel('private')}
  159. disabled={inflight}
  160. >
  161. <Trans i18nKey="make_private" />
  162. </Button>
  163. <span>&nbsp;&nbsp;</span>
  164. <LinkSharingInfo />
  165. </Col>
  166. </Row>
  167. )
  168. }
  169. LegacySharing.propTypes = {
  170. accessLevel: PropTypes.string.isRequired,
  171. setAccessLevel: PropTypes.func.isRequired,
  172. inflight: PropTypes.bool,
  173. }
  174. export function ReadOnlyTokenLink() {
  175. const { tokens } = useProjectContext()
  176. return (
  177. <Row className="public-access-level">
  178. <Col xs={12} className="access-token-display-area">
  179. <div className="access-token-wrapper">
  180. <strong>
  181. <Trans i18nKey="anyone_with_link_can_view" />
  182. </strong>
  183. <AccessToken
  184. token={tokens?.readOnly}
  185. path="/read/"
  186. tooltipId="tooltip-copy-link-ro"
  187. />
  188. </div>
  189. </Col>
  190. </Row>
  191. )
  192. }
  193. function AccessToken({ token, path, tooltipId }) {
  194. const { isAdmin } = useUserContext()
  195. if (!token) {
  196. return (
  197. <pre className="access-token">
  198. <span>
  199. <Trans i18nKey="loading" />…
  200. </span>
  201. </pre>
  202. )
  203. }
  204. let origin = window.location.origin
  205. if (isAdmin) {
  206. origin = window.ExposedSettings.siteUrl
  207. }
  208. const link = `${origin}${path}${token}`
  209. return (
  210. <pre className="access-token">
  211. <span>{link}</span>
  212. <CopyLink link={link} tooltipId={tooltipId} />
  213. </pre>
  214. )
  215. }
  216. AccessToken.propTypes = {
  217. token: PropTypes.string,
  218. tooltipId: PropTypes.string.isRequired,
  219. path: PropTypes.string.isRequired,
  220. }
  221. function LinkSharingInfo() {
  222. return (
  223. <Tooltip
  224. id="link-sharing-info"
  225. description={<Trans i18nKey="learn_more_about_link_sharing" />}
  226. >
  227. <a
  228. href="/learn/how-to/What_is_Link_Sharing%3F"
  229. target="_blank"
  230. rel="noopener"
  231. >
  232. <Icon type="question-circle" />
  233. </a>
  234. </Tooltip>
  235. )
  236. }