2024-06-25 02:08:24 -04:00
|
|
|
import { useCallback, useState, useEffect } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import Icon from '@/shared/components/icon'
|
|
|
|
import { useShareProjectContext } from './share-project-modal'
|
|
|
|
import { setProjectAccessLevel } from '../../utils/api'
|
|
|
|
import { CopyToClipboard } from '@/shared/components/copy-to-clipboard'
|
|
|
|
import { useProjectContext } from '@/shared/context/project-context'
|
|
|
|
import * as eventTracking from '../../../../infrastructure/event-tracking'
|
|
|
|
import { useUserContext } from '@/shared/context/user-context'
|
|
|
|
import { sendMB } from '../../../../infrastructure/event-tracking'
|
|
|
|
import { getJSON } from '../../../../infrastructure/fetch-json'
|
|
|
|
import useAbortController from '@/shared/hooks/use-abort-controller'
|
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-07-03 04:44:56 -04:00
|
|
|
import getMeta from '@/utils/meta'
|
2024-10-09 08:13:50 -04:00
|
|
|
import OLRow from '@/features/ui/components/ol/ol-row'
|
|
|
|
import OLCol from '@/features/ui/components/ol/ol-col'
|
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
|
|
|
import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
|
|
|
|
import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
|
|
|
|
import MaterialIcon from '@/shared/components/material-icon'
|
2024-06-25 02:08:24 -04:00
|
|
|
|
|
|
|
export default function LinkSharing() {
|
|
|
|
const [inflight, setInflight] = useState(false)
|
2024-07-18 02:44:03 -04:00
|
|
|
const [showLinks, setShowLinks] = useState(true)
|
2024-06-25 02:08:24 -04:00
|
|
|
|
|
|
|
const { monitorRequest } = useShareProjectContext()
|
|
|
|
|
|
|
|
const { _id: projectId, publicAccessLevel } = useProjectContext()
|
|
|
|
|
|
|
|
// set the access level of a project
|
|
|
|
const setAccessLevel = useCallback(
|
|
|
|
newPublicAccessLevel => {
|
|
|
|
setInflight(true)
|
|
|
|
sendMB('link-sharing-click-off', {
|
|
|
|
project_id: projectId,
|
|
|
|
})
|
|
|
|
monitorRequest(() =>
|
|
|
|
setProjectAccessLevel(projectId, newPublicAccessLevel)
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
// NOTE: not calling `updateProject` here as it receives data via
|
|
|
|
// project:publicAccessLevel: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}
|
|
|
|
projectId={projectId}
|
2024-06-28 02:18:37 -04:00
|
|
|
setShowLinks={setShowLinks}
|
2024-06-25 02:08:24 -04:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
// Token-based access
|
|
|
|
case 'tokenBased':
|
|
|
|
return (
|
|
|
|
<TokenBasedSharing
|
|
|
|
setAccessLevel={setAccessLevel}
|
|
|
|
inflight={inflight}
|
|
|
|
setShowLinks={setShowLinks}
|
|
|
|
showLinks={showLinks}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
// Legacy public-access
|
|
|
|
case 'readAndWrite':
|
|
|
|
case 'readOnly':
|
|
|
|
return (
|
|
|
|
<LegacySharing
|
|
|
|
setAccessLevel={setAccessLevel}
|
|
|
|
accessLevel={publicAccessLevel}
|
|
|
|
inflight={inflight}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-28 02:18:37 -04:00
|
|
|
function PrivateSharing({ setAccessLevel, inflight, projectId, setShowLinks }) {
|
2024-06-25 02:08:24 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLRow className="public-access-level">
|
|
|
|
<OLCol xs={12} className="text-center">
|
2024-06-25 02:08:24 -04:00
|
|
|
<strong>{t('link_sharing_is_off_short')}</strong>
|
|
|
|
<span> </span>
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLButton
|
|
|
|
variant="link"
|
2024-06-25 02:08:24 -04:00
|
|
|
className="btn-inline-link"
|
|
|
|
onClick={() => {
|
|
|
|
setAccessLevel('tokenBased')
|
|
|
|
eventTracking.sendMB('link-sharing-click', { projectId })
|
2024-06-28 02:18:37 -04:00
|
|
|
setShowLinks(true)
|
2024-06-25 02:08:24 -04:00
|
|
|
}}
|
|
|
|
disabled={inflight}
|
|
|
|
>
|
|
|
|
{t('turn_on_link_sharing')}
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLButton>
|
2024-06-25 02:08:24 -04:00
|
|
|
<span> </span>
|
|
|
|
<LinkSharingInfo />
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLCol>
|
|
|
|
</OLRow>
|
2024-06-25 02:08:24 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
PrivateSharing.propTypes = {
|
|
|
|
setAccessLevel: PropTypes.func.isRequired,
|
|
|
|
inflight: PropTypes.bool,
|
|
|
|
projectId: PropTypes.string,
|
2024-06-28 02:18:37 -04:00
|
|
|
setShowLinks: PropTypes.func.isRequired,
|
2024-06-25 02:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function TokenBasedSharing({
|
|
|
|
setAccessLevel,
|
|
|
|
inflight,
|
|
|
|
setShowLinks,
|
|
|
|
showLinks,
|
|
|
|
}) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
|
|
|
|
const [tokens, setTokens] = useState(null)
|
|
|
|
|
|
|
|
const { signal } = useAbortController()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getJSON(`/project/${projectId}/tokens`, { signal })
|
|
|
|
.then(data => setTokens(data))
|
|
|
|
.catch(debugConsole.error)
|
|
|
|
}, [projectId, signal])
|
|
|
|
|
|
|
|
return (
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLRow className="public-access-level">
|
|
|
|
<OLCol xs={12} className="text-center">
|
2024-06-25 02:08:24 -04:00
|
|
|
<strong>{t('link_sharing_is_on')}</strong>
|
|
|
|
<span> </span>
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLButton
|
|
|
|
variant="link"
|
2024-06-25 02:08:24 -04:00
|
|
|
className="btn-inline-link"
|
|
|
|
onClick={() => setAccessLevel('private')}
|
|
|
|
disabled={inflight}
|
|
|
|
>
|
|
|
|
{t('turn_off_link_sharing')}
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLButton>
|
2024-06-25 02:08:24 -04:00
|
|
|
<span> </span>
|
|
|
|
<LinkSharingInfo />
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLButton
|
|
|
|
variant="link"
|
|
|
|
className="btn-chevron align-middle"
|
2024-06-25 02:08:24 -04:00
|
|
|
onClick={() => setShowLinks(!showLinks)}
|
|
|
|
>
|
2024-10-09 08:13:50 -04:00
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={<Icon type={showLinks ? 'chevron-up' : 'chevron-down'} fw />}
|
|
|
|
bs5={
|
|
|
|
<MaterialIcon
|
|
|
|
type={showLinks ? 'keyboard_arrow_up' : 'keyboard_arrow_down'}
|
2024-06-25 02:08:24 -04:00
|
|
|
/>
|
2024-10-09 08:13:50 -04:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</OLButton>
|
|
|
|
</OLCol>
|
|
|
|
{showLinks && (
|
|
|
|
<OLCol xs={12} className="access-token-display-area">
|
|
|
|
<div className="access-token-wrapper">
|
|
|
|
<strong>{t('anyone_with_link_can_edit')}</strong>
|
|
|
|
<AccessToken
|
|
|
|
token={tokens?.readAndWrite}
|
|
|
|
tokenHashPrefix={tokens?.readAndWriteHashPrefix}
|
|
|
|
path="/"
|
|
|
|
tooltipId="tooltip-copy-link-rw"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="access-token-wrapper">
|
|
|
|
<strong>{t('anyone_with_link_can_view')}</strong>
|
|
|
|
<AccessToken
|
|
|
|
token={tokens?.readOnly}
|
|
|
|
tokenHashPrefix={tokens?.readOnlyHashPrefix}
|
|
|
|
path="/read/"
|
|
|
|
tooltipId="tooltip-copy-link-ro"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</OLCol>
|
2024-06-25 02:08:24 -04:00
|
|
|
)}
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLRow>
|
2024-06-25 02:08:24 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenBasedSharing.propTypes = {
|
|
|
|
setAccessLevel: PropTypes.func.isRequired,
|
|
|
|
inflight: PropTypes.bool,
|
|
|
|
setShowLinks: PropTypes.func.isRequired,
|
|
|
|
showLinks: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
function LegacySharing({ accessLevel, setAccessLevel, inflight }) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLRow className="public-access-level">
|
|
|
|
<OLCol xs={12} className="text-center">
|
2024-06-25 02:08:24 -04:00
|
|
|
<strong>
|
|
|
|
{accessLevel === 'readAndWrite' && t('this_project_is_public')}
|
|
|
|
{accessLevel === 'readOnly' && t('this_project_is_public_read_only')}
|
|
|
|
</strong>
|
|
|
|
<span> </span>
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLButton
|
|
|
|
variant="link"
|
2024-06-25 02:08:24 -04:00
|
|
|
className="btn-inline-link"
|
|
|
|
onClick={() => setAccessLevel('private')}
|
|
|
|
disabled={inflight}
|
|
|
|
>
|
|
|
|
{t('make_private')}
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLButton>
|
2024-06-25 02:08:24 -04:00
|
|
|
<span> </span>
|
|
|
|
<LinkSharingInfo />
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLCol>
|
|
|
|
</OLRow>
|
2024-06-25 02:08:24 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
LegacySharing.propTypes = {
|
|
|
|
accessLevel: PropTypes.string.isRequired,
|
|
|
|
setAccessLevel: PropTypes.func.isRequired,
|
|
|
|
inflight: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ReadOnlyTokenLink() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
|
|
|
|
const [tokens, setTokens] = useState(null)
|
|
|
|
|
|
|
|
const { signal } = useAbortController()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getJSON(`/project/${projectId}/tokens`, { signal })
|
|
|
|
.then(data => setTokens(data))
|
|
|
|
.catch(debugConsole.error)
|
|
|
|
}, [projectId, signal])
|
|
|
|
|
|
|
|
return (
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLRow className="public-access-level">
|
|
|
|
<OLCol className="access-token-display-area">
|
2024-06-25 02:08:24 -04:00
|
|
|
<div className="access-token-wrapper">
|
|
|
|
<strong>{t('anyone_with_link_can_view')}</strong>
|
|
|
|
<AccessToken
|
|
|
|
token={tokens?.readOnly}
|
|
|
|
tokenHashPrefix={tokens?.readOnlyHashPrefix}
|
|
|
|
path="/read/"
|
|
|
|
tooltipId="tooltip-copy-link-ro"
|
|
|
|
/>
|
|
|
|
</div>
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLCol>
|
|
|
|
</OLRow>
|
2024-06-25 02:08:24 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function AccessToken({ token, tokenHashPrefix, path, tooltipId }) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { isAdmin } = useUserContext()
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return (
|
|
|
|
<pre className="access-token">
|
|
|
|
<span>{t('loading')}…</span>
|
|
|
|
</pre>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
let origin = window.location.origin
|
|
|
|
if (isAdmin) {
|
2024-07-03 04:44:56 -04:00
|
|
|
origin = getMeta('ol-ExposedSettings').siteUrl
|
2024-06-25 02:08:24 -04:00
|
|
|
}
|
|
|
|
const link = `${origin}${path}${token}${
|
|
|
|
tokenHashPrefix ? `#${tokenHashPrefix}` : ''
|
|
|
|
}`
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="access-token">
|
|
|
|
<code>{link}</code>
|
|
|
|
<CopyToClipboard content={link} tooltipId={tooltipId} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessToken.propTypes = {
|
|
|
|
token: PropTypes.string,
|
|
|
|
tokenHashPrefix: PropTypes.string,
|
|
|
|
tooltipId: PropTypes.string.isRequired,
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
function LinkSharingInfo() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
2024-10-09 08:13:50 -04:00
|
|
|
<OLTooltip
|
2024-06-25 02:08:24 -04:00
|
|
|
id="link-sharing-info"
|
|
|
|
description={t('learn_more_about_link_sharing')}
|
|
|
|
>
|
|
|
|
<a
|
|
|
|
href="/learn/how-to/What_is_Link_Sharing%3F"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener"
|
|
|
|
>
|
2024-10-09 08:13:50 -04:00
|
|
|
<BootstrapVersionSwitcher
|
|
|
|
bs3={<Icon type="question-circle" />}
|
|
|
|
bs5={<MaterialIcon type="help" className="align-middle" />}
|
|
|
|
/>
|
2024-06-25 02:08:24 -04:00
|
|
|
</a>
|
2024-10-09 08:13:50 -04:00
|
|
|
</OLTooltip>
|
2024-06-25 02:08:24 -04:00
|
|
|
)
|
|
|
|
}
|