link-sharing.js 7.9 KB

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