link-sharing.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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({ canAddCollaborators }) {
  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. canAddCollaborators={canAddCollaborators}
  49. />
  50. )
  51. // Legacy public-access
  52. case 'readAndWrite':
  53. case 'readOnly':
  54. return (
  55. <LegacySharing
  56. setAccessLevel={setAccessLevel}
  57. accessLevel={publicAccessLevel}
  58. inflight={inflight}
  59. />
  60. )
  61. default:
  62. return null
  63. }
  64. }
  65. LinkSharing.propTypes = {
  66. canAddCollaborators: PropTypes.bool,
  67. }
  68. function PrivateSharing({ setAccessLevel, inflight }) {
  69. return (
  70. <Row className="public-access-level">
  71. <Col xs={12} className="text-center">
  72. <Trans i18nKey="link_sharing_is_off" />
  73. <span>&nbsp;&nbsp;</span>
  74. <Button
  75. type="button"
  76. bsStyle="link"
  77. className="btn-inline-link"
  78. onClick={() => {
  79. setAccessLevel('tokenBased')
  80. eventTracking.sendMB('link-sharing-click')
  81. }}
  82. disabled={inflight}
  83. >
  84. <Trans i18nKey="turn_on_link_sharing" />
  85. </Button>
  86. <span>&nbsp;&nbsp;</span>
  87. <LinkSharingInfo />
  88. </Col>
  89. </Row>
  90. )
  91. }
  92. PrivateSharing.propTypes = {
  93. setAccessLevel: PropTypes.func.isRequired,
  94. inflight: PropTypes.bool,
  95. }
  96. function TokenBasedSharing({ setAccessLevel, inflight, canAddCollaborators }) {
  97. const { tokens } = useProjectContext()
  98. return (
  99. <Row className="public-access-level">
  100. <Col xs={12} className="text-center">
  101. <strong>
  102. <Trans i18nKey="link_sharing_is_on" />
  103. </strong>
  104. <span>&nbsp;&nbsp;</span>
  105. <Button
  106. bsStyle="link"
  107. className="btn-inline-link"
  108. onClick={() => setAccessLevel('private')}
  109. disabled={inflight}
  110. >
  111. <Trans i18nKey="turn_off_link_sharing" />
  112. </Button>
  113. <span>&nbsp;&nbsp;</span>
  114. <LinkSharingInfo />
  115. </Col>
  116. <Col xs={12} className="access-token-display-area">
  117. <div className="access-token-wrapper">
  118. <strong>
  119. <Trans i18nKey="anyone_with_link_can_edit" />
  120. </strong>
  121. <AccessToken
  122. token={tokens?.readAndWrite}
  123. path="/"
  124. tooltipId="tooltip-copy-link-rw"
  125. />
  126. </div>
  127. <div className="access-token-wrapper">
  128. <strong>
  129. <Trans i18nKey="anyone_with_link_can_view" />
  130. </strong>
  131. <AccessToken
  132. token={tokens?.readOnly}
  133. path="/read/"
  134. tooltipId="tooltip-copy-link-ro"
  135. />
  136. </div>
  137. </Col>
  138. </Row>
  139. )
  140. }
  141. TokenBasedSharing.propTypes = {
  142. setAccessLevel: PropTypes.func.isRequired,
  143. inflight: PropTypes.bool,
  144. canAddCollaborators: PropTypes.bool,
  145. }
  146. function LegacySharing({ accessLevel, setAccessLevel, inflight }) {
  147. return (
  148. <Row className="public-access-level">
  149. <Col xs={12} className="text-center">
  150. <strong>
  151. {accessLevel === 'readAndWrite' && (
  152. <Trans i18nKey="this_project_is_public" />
  153. )}
  154. {accessLevel === 'readOnly' && (
  155. <Trans i18nKey="this_project_is_public_read_only" />
  156. )}
  157. </strong>
  158. <span>&nbsp;&nbsp;</span>
  159. <Button
  160. type="button"
  161. bsStyle="link"
  162. className="btn-inline-link"
  163. onClick={() => setAccessLevel('private')}
  164. disabled={inflight}
  165. >
  166. <Trans i18nKey="make_private" />
  167. </Button>
  168. <span>&nbsp;&nbsp;</span>
  169. <LinkSharingInfo />
  170. </Col>
  171. </Row>
  172. )
  173. }
  174. LegacySharing.propTypes = {
  175. accessLevel: PropTypes.string.isRequired,
  176. setAccessLevel: PropTypes.func.isRequired,
  177. inflight: PropTypes.bool,
  178. }
  179. export function ReadOnlyTokenLink() {
  180. const { tokens } = useProjectContext()
  181. return (
  182. <Row className="public-access-level">
  183. <Col xs={12} className="access-token-display-area">
  184. <div className="access-token-wrapper">
  185. <strong>
  186. <Trans i18nKey="anyone_with_link_can_view" />
  187. </strong>
  188. <AccessToken
  189. token={tokens?.readOnly}
  190. path="/read/"
  191. tooltipId="tooltip-copy-link-ro"
  192. />
  193. </div>
  194. </Col>
  195. </Row>
  196. )
  197. }
  198. function AccessToken({ token, path, tooltipId }) {
  199. const { isAdmin } = useUserContext()
  200. if (!token) {
  201. return (
  202. <pre className="access-token">
  203. <span>
  204. <Trans i18nKey="loading" />…
  205. </span>
  206. </pre>
  207. )
  208. }
  209. let origin = window.location.origin
  210. if (isAdmin) {
  211. origin = window.ExposedSettings.siteUrl
  212. }
  213. const link = `${origin}${path}${token}`
  214. return (
  215. <pre className="access-token">
  216. <span>{link}</span>
  217. <CopyLink link={link} tooltipId={tooltipId} />
  218. </pre>
  219. )
  220. }
  221. AccessToken.propTypes = {
  222. token: PropTypes.string,
  223. tooltipId: PropTypes.string.isRequired,
  224. path: PropTypes.string.isRequired,
  225. }
  226. function LinkSharingInfo() {
  227. return (
  228. <Tooltip
  229. id="link-sharing-info"
  230. description={<Trans i18nKey="learn_more_about_link_sharing" />}
  231. >
  232. <a
  233. href="/learn/how-to/What_is_Link_Sharing%3F"
  234. target="_blank"
  235. rel="noopener"
  236. >
  237. <Icon type="question-circle" />
  238. </a>
  239. </Tooltip>
  240. )
  241. }