2021-06-23 05:37:08 -04:00
|
|
|
import { useCallback, useState } from 'react'
|
2021-03-12 05:23:46 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2022-05-18 09:46:10 -04:00
|
|
|
import { Button, Col, Row } from 'react-bootstrap'
|
2023-01-12 17:48:58 -05:00
|
|
|
import { Trans } from 'react-i18next'
|
2022-05-18 09:46:10 -04:00
|
|
|
import Tooltip from '../../../shared/components/tooltip'
|
2021-03-12 05:23:46 -05:00
|
|
|
import Icon from '../../../shared/components/icon'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { useShareProjectContext } from './share-project-modal'
|
2021-03-12 05:23:46 -05:00
|
|
|
import { setProjectAccessLevel } from '../utils/api'
|
|
|
|
import CopyLink from '../../../shared/components/copy-link'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
2021-08-19 11:42:49 -04:00
|
|
|
import * as eventTracking from '../../../infrastructure/event-tracking'
|
2022-03-31 06:36:37 -04:00
|
|
|
import { useUserContext } from '../../../shared/context/user-context'
|
2021-03-12 05:23:46 -05:00
|
|
|
|
2022-07-15 09:44:47 -04:00
|
|
|
export default function LinkSharing({ canAddCollaborators }) {
|
2021-03-12 05:23:46 -05:00
|
|
|
const [inflight, setInflight] = useState(false)
|
|
|
|
|
2021-04-15 05:04:58 -04:00
|
|
|
const { monitorRequest } = useShareProjectContext()
|
2021-03-12 05:23:46 -05:00
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
const { _id: projectId, publicAccessLevel } = useProjectContext()
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
// set the access level of a project
|
|
|
|
const setAccessLevel = useCallback(
|
2022-01-10 10:47:10 -05:00
|
|
|
newPublicAccessLevel => {
|
2021-03-12 05:23:46 -05:00
|
|
|
setInflight(true)
|
2022-01-10 10:47:10 -05:00
|
|
|
monitorRequest(() =>
|
|
|
|
setProjectAccessLevel(projectId, newPublicAccessLevel)
|
|
|
|
)
|
2021-03-12 05:23:46 -05:00
|
|
|
.then(() => {
|
2021-04-15 05:04:58 -04:00
|
|
|
// NOTE: not calling `updateProject` here as it receives data via
|
|
|
|
// project:publicAccessLevel:changed and project:tokens:changed
|
|
|
|
// over the websocket connection
|
2022-01-10 10:47:10 -05:00
|
|
|
// TODO: eventTracking.sendMB('project-make-token-based') when newPublicAccessLevel is 'tokenBased'
|
2021-03-12 05:23:46 -05:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setInflight(false)
|
|
|
|
})
|
|
|
|
},
|
2022-01-10 10:47:10 -05:00
|
|
|
[monitorRequest, projectId]
|
2021-03-12 05:23:46 -05:00
|
|
|
)
|
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
switch (publicAccessLevel) {
|
2021-03-12 05:23:46 -05:00
|
|
|
// Private (with token-access available)
|
|
|
|
case 'private':
|
|
|
|
return (
|
|
|
|
<PrivateSharing setAccessLevel={setAccessLevel} inflight={inflight} />
|
|
|
|
)
|
|
|
|
|
|
|
|
// Token-based access
|
|
|
|
case 'tokenBased':
|
|
|
|
return (
|
|
|
|
<TokenBasedSharing
|
|
|
|
setAccessLevel={setAccessLevel}
|
|
|
|
inflight={inflight}
|
2022-07-15 09:44:47 -04:00
|
|
|
canAddCollaborators={canAddCollaborators}
|
2021-03-12 05:23:46 -05:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
// Legacy public-access
|
|
|
|
case 'readAndWrite':
|
|
|
|
case 'readOnly':
|
|
|
|
return (
|
|
|
|
<LegacySharing
|
|
|
|
setAccessLevel={setAccessLevel}
|
2022-01-10 10:47:10 -05:00
|
|
|
accessLevel={publicAccessLevel}
|
2021-03-12 05:23:46 -05:00
|
|
|
inflight={inflight}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 09:44:47 -04:00
|
|
|
LinkSharing.propTypes = {
|
|
|
|
canAddCollaborators: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
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"
|
2021-08-19 11:42:49 -04:00
|
|
|
onClick={() => {
|
|
|
|
setAccessLevel('tokenBased')
|
|
|
|
eventTracking.sendMB('link-sharing-click')
|
|
|
|
}}
|
2021-03-12 05:23:46 -05:00
|
|
|
disabled={inflight}
|
|
|
|
>
|
|
|
|
<Trans i18nKey="turn_on_link_sharing" />
|
|
|
|
</Button>
|
|
|
|
<span> </span>
|
|
|
|
<LinkSharingInfo />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
PrivateSharing.propTypes = {
|
|
|
|
setAccessLevel: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
inflight: PropTypes.bool,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
2022-07-15 09:44:47 -04:00
|
|
|
function TokenBasedSharing({ setAccessLevel, inflight, canAddCollaborators }) {
|
2022-01-10 10:47:10 -05:00
|
|
|
const { tokens } = useProjectContext()
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
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
|
2022-01-10 10:47:10 -05:00
|
|
|
token={tokens?.readAndWrite}
|
2021-03-12 05:23:46 -05:00
|
|
|
path="/"
|
|
|
|
tooltipId="tooltip-copy-link-rw"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="access-token-wrapper">
|
|
|
|
<strong>
|
|
|
|
<Trans i18nKey="anyone_with_link_can_view" />
|
|
|
|
</strong>
|
|
|
|
<AccessToken
|
2022-01-10 10:47:10 -05:00
|
|
|
token={tokens?.readOnly}
|
2021-03-12 05:23:46 -05:00
|
|
|
path="/read/"
|
|
|
|
tooltipId="tooltip-copy-link-ro"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
TokenBasedSharing.propTypes = {
|
|
|
|
setAccessLevel: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
inflight: PropTypes.bool,
|
2022-07-15 09:44:47 -04:00
|
|
|
canAddCollaborators: PropTypes.bool,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
LegacySharing.propTypes = {
|
|
|
|
accessLevel: PropTypes.string.isRequired,
|
|
|
|
setAccessLevel: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
inflight: PropTypes.bool,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function ReadOnlyTokenLink() {
|
2022-01-10 10:47:10 -05:00
|
|
|
const { tokens } = useProjectContext()
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
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
|
2022-01-10 10:47:10 -05:00
|
|
|
token={tokens?.readOnly}
|
2021-03-12 05:23:46 -05:00
|
|
|
path="/read/"
|
|
|
|
tooltipId="tooltip-copy-link-ro"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function AccessToken({ token, path, tooltipId }) {
|
2022-03-31 06:36:37 -04:00
|
|
|
const { isAdmin } = useUserContext()
|
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
if (!token) {
|
|
|
|
return (
|
|
|
|
<pre className="access-token">
|
|
|
|
<span>
|
|
|
|
<Trans i18nKey="loading" />…
|
|
|
|
</span>
|
|
|
|
</pre>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-31 06:36:37 -04:00
|
|
|
let origin = window.location.origin
|
|
|
|
if (isAdmin) {
|
|
|
|
origin = window.ExposedSettings.siteUrl
|
|
|
|
}
|
|
|
|
const link = `${origin}${path}${token}`
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<pre className="access-token">
|
|
|
|
<span>{link}</span>
|
|
|
|
<CopyLink link={link} tooltipId={tooltipId} />
|
|
|
|
</pre>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
AccessToken.propTypes = {
|
|
|
|
token: PropTypes.string,
|
|
|
|
tooltipId: PropTypes.string.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
path: PropTypes.string.isRequired,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function LinkSharingInfo() {
|
|
|
|
return (
|
2022-05-18 09:46:10 -04:00
|
|
|
<Tooltip
|
|
|
|
id="link-sharing-info"
|
|
|
|
description={<Trans i18nKey="learn_more_about_link_sharing" />}
|
2021-03-12 05:23:46 -05:00
|
|
|
>
|
|
|
|
<a
|
|
|
|
href="/learn/how-to/What_is_Link_Sharing%3F"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener"
|
|
|
|
>
|
|
|
|
<Icon type="question-circle" />
|
|
|
|
</a>
|
2022-05-18 09:46:10 -04:00
|
|
|
</Tooltip>
|
2021-03-12 05:23:46 -05:00
|
|
|
)
|
|
|
|
}
|