link-sharing.js 6.7 KB

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