| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- import { useCallback, useState } from 'react'
- import PropTypes from 'prop-types'
- import { Button, Col, Row } from 'react-bootstrap'
- import { Trans } from 'react-i18next'
- import Tooltip from '../../../shared/components/tooltip'
- import Icon from '../../../shared/components/icon'
- import { useShareProjectContext } from './share-project-modal'
- import { setProjectAccessLevel } from '../utils/api'
- import CopyLink from '../../../shared/components/copy-link'
- import { useProjectContext } from '../../../shared/context/project-context'
- import * as eventTracking from '../../../infrastructure/event-tracking'
- import { useUserContext } from '../../../shared/context/user-context'
- export default function LinkSharing({ canAddCollaborators }) {
- const [inflight, setInflight] = useState(false)
- const { monitorRequest } = useShareProjectContext()
- const { _id: projectId, publicAccessLevel } = useProjectContext()
- // set the access level of a project
- const setAccessLevel = useCallback(
- newPublicAccessLevel => {
- setInflight(true)
- monitorRequest(() =>
- setProjectAccessLevel(projectId, newPublicAccessLevel)
- )
- .then(() => {
- // NOTE: not calling `updateProject` here as it receives data via
- // project:publicAccessLevel:changed and project:tokens:changed
- // over the websocket connection
- // TODO: eventTracking.sendMB('project-make-token-based') when newPublicAccessLevel is 'tokenBased'
- })
- .finally(() => {
- setInflight(false)
- })
- },
- [monitorRequest, projectId]
- )
- switch (publicAccessLevel) {
- // Private (with token-access available)
- case 'private':
- return (
- <PrivateSharing setAccessLevel={setAccessLevel} inflight={inflight} />
- )
- // Token-based access
- case 'tokenBased':
- return (
- <TokenBasedSharing
- setAccessLevel={setAccessLevel}
- inflight={inflight}
- canAddCollaborators={canAddCollaborators}
- />
- )
- // Legacy public-access
- case 'readAndWrite':
- case 'readOnly':
- return (
- <LegacySharing
- setAccessLevel={setAccessLevel}
- accessLevel={publicAccessLevel}
- inflight={inflight}
- />
- )
- default:
- return null
- }
- }
- LinkSharing.propTypes = {
- canAddCollaborators: PropTypes.bool,
- }
- function PrivateSharing({ setAccessLevel, inflight }) {
- return (
- <Row className="public-access-level">
- <Col xs={12} className="text-center">
- <Trans i18nKey="link_sharing_is_off" />
- <span> </span>
- <Button
- type="button"
- bsStyle="link"
- className="btn-inline-link"
- onClick={() => {
- setAccessLevel('tokenBased')
- eventTracking.sendMB('link-sharing-click')
- }}
- disabled={inflight}
- >
- <Trans i18nKey="turn_on_link_sharing" />
- </Button>
- <span> </span>
- <LinkSharingInfo />
- </Col>
- </Row>
- )
- }
- PrivateSharing.propTypes = {
- setAccessLevel: PropTypes.func.isRequired,
- inflight: PropTypes.bool,
- }
- function TokenBasedSharing({ setAccessLevel, inflight, canAddCollaborators }) {
- const { tokens } = useProjectContext()
- return (
- <Row className="public-access-level">
- <Col xs={12} className="text-center">
- <strong>
- <Trans i18nKey="link_sharing_is_on" />
- </strong>
- <span> </span>
- <Button
- bsStyle="link"
- className="btn-inline-link"
- onClick={() => setAccessLevel('private')}
- disabled={inflight}
- >
- <Trans i18nKey="turn_off_link_sharing" />
- </Button>
- <span> </span>
- <LinkSharingInfo />
- </Col>
- <Col xs={12} className="access-token-display-area">
- <div className="access-token-wrapper">
- <strong>
- <Trans i18nKey="anyone_with_link_can_edit" />
- </strong>
- <AccessToken
- token={tokens?.readAndWrite}
- path="/"
- tooltipId="tooltip-copy-link-rw"
- />
- </div>
- <div className="access-token-wrapper">
- <strong>
- <Trans i18nKey="anyone_with_link_can_view" />
- </strong>
- <AccessToken
- token={tokens?.readOnly}
- path="/read/"
- tooltipId="tooltip-copy-link-ro"
- />
- </div>
- </Col>
- </Row>
- )
- }
- TokenBasedSharing.propTypes = {
- setAccessLevel: PropTypes.func.isRequired,
- inflight: PropTypes.bool,
- canAddCollaborators: PropTypes.bool,
- }
- function LegacySharing({ accessLevel, setAccessLevel, inflight }) {
- return (
- <Row className="public-access-level">
- <Col xs={12} className="text-center">
- <strong>
- {accessLevel === 'readAndWrite' && (
- <Trans i18nKey="this_project_is_public" />
- )}
- {accessLevel === 'readOnly' && (
- <Trans i18nKey="this_project_is_public_read_only" />
- )}
- </strong>
- <span> </span>
- <Button
- type="button"
- bsStyle="link"
- className="btn-inline-link"
- onClick={() => setAccessLevel('private')}
- disabled={inflight}
- >
- <Trans i18nKey="make_private" />
- </Button>
- <span> </span>
- <LinkSharingInfo />
- </Col>
- </Row>
- )
- }
- LegacySharing.propTypes = {
- accessLevel: PropTypes.string.isRequired,
- setAccessLevel: PropTypes.func.isRequired,
- inflight: PropTypes.bool,
- }
- export function ReadOnlyTokenLink() {
- const { tokens } = useProjectContext()
- return (
- <Row className="public-access-level">
- <Col xs={12} className="access-token-display-area">
- <div className="access-token-wrapper">
- <strong>
- <Trans i18nKey="anyone_with_link_can_view" />
- </strong>
- <AccessToken
- token={tokens?.readOnly}
- path="/read/"
- tooltipId="tooltip-copy-link-ro"
- />
- </div>
- </Col>
- </Row>
- )
- }
- function AccessToken({ token, path, tooltipId }) {
- const { isAdmin } = useUserContext()
- if (!token) {
- return (
- <pre className="access-token">
- <span>
- <Trans i18nKey="loading" />…
- </span>
- </pre>
- )
- }
- let origin = window.location.origin
- if (isAdmin) {
- origin = window.ExposedSettings.siteUrl
- }
- const link = `${origin}${path}${token}`
- return (
- <pre className="access-token">
- <span>{link}</span>
- <CopyLink link={link} tooltipId={tooltipId} />
- </pre>
- )
- }
- AccessToken.propTypes = {
- token: PropTypes.string,
- tooltipId: PropTypes.string.isRequired,
- path: PropTypes.string.isRequired,
- }
- function LinkSharingInfo() {
- return (
- <Tooltip
- id="link-sharing-info"
- description={<Trans i18nKey="learn_more_about_link_sharing" />}
- >
- <a
- href="/learn/how-to/What_is_Link_Sharing%3F"
- target="_blank"
- rel="noopener"
- >
- <Icon type="question-circle" />
- </a>
- </Tooltip>
- )
- }
|