link-sharing.js 7.9 KB

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