link-sharing.jsx 8.7 KB

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