Skip to content

Commit

Permalink
feat(sharing): Replace old sharing link menu with the new modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed Jan 6, 2025
1 parent 82106b7 commit f543d29
Showing 1 changed file with 28 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,156 +1,47 @@
import PropTypes from 'prop-types'
import React, { useState, useRef } from 'react'

import DropdownButton from 'cozy-ui/transpiled/react/DropdownButton'
import Icon from 'cozy-ui/transpiled/react/Icon'
import TrashIcon from 'cozy-ui/transpiled/react/Icons/Trash'
import Spinner from 'cozy-ui/transpiled/react/Spinner'
import Typography from 'cozy-ui/transpiled/react/Typography'
import ActionMenu, {
ActionMenuItem,
ActionMenuRadio
} from 'cozy-ui/transpiled/react/deprecated/ActionMenu'
import Alerter from 'cozy-ui/transpiled/react/deprecated/Alerter'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { isOnlyReadOnlyLinkAllowed } from '../../helpers/link'
import { checkIsReadOnlyPermissions } from '../../helpers/permissions'
import logger from '../../logger'
import { ShareRestrictionModal } from '../ShareRestrictionModal/ShareRestrictionModal'

const LinkRecipientPermissions = ({
className,
document,
documentType,
permissions,
onChangePermissions,
onDisable
}) => {
const LinkRecipientPermissions = ({ className, document, permissions }) => {
const { t } = useI18n()
const buttonRef = useRef()
const [menuIsOpen, setMenuIsOpen] = useState(false)
const [loading, setLoading] = useState(false)
const [openShareRestrictionModal, setOpenShareRestrictionModal] =
useState(false)

const isReadOnlyPermissions = checkIsReadOnlyPermissions(permissions)

const toggleMenu = () => {
setMenuIsOpen(!menuIsOpen)
}

const deleteShareLink = async () => {
try {
setLoading(true)
await onDisable(document)
} catch (e) {
Alerter.error(t(`${documentType}.share.error.revoke`))
logger.log(e)
} finally {
setLoading(false)
}
}

const updateLinkPermissions = ({ isReadOnly }) => {
const verbs = isReadOnly ? ['GET'] : ['GET', 'POST', 'PUT', 'PATCH']
try {
onChangePermissions(document, verbs)
} catch (err) {
Alerter.error(t(`${documentType}.share.shareByLink.permserror`))
logger.log(err)
}
}
return (
<div className={className}>
{loading && <Spinner />}
{!loading && (
<>
<DropdownButton
onClick={toggleMenu}
ref={buttonRef}
textVariant="body2"
>
{t(
`Share.type.${isReadOnlyPermissions ? 'one-way' : 'two-way'}`
).toLowerCase()}
</DropdownButton>
{menuIsOpen && (
<ActionMenu
onClose={toggleMenu}
popperOptions={{
placement: 'bottom-end',
strategy: 'fixed'
}}
anchorElRef={buttonRef}
>
<ActionMenuItem
left={
<ActionMenuRadio
name="permissionLinkMenu"
value="true"
checked={isReadOnlyPermissions}
/>
}
onClick={() => {
toggleMenu()
updateLinkPermissions({ isReadOnly: true })
}}
>
<>
{document?.type === 'directory'
? t(`Share.permissionLink.seeFolder`)
: t(`Share.permissionLink.seeFile`)}
<Typography
className="u-mt-half"
variant="caption"
color="textSecondary"
>
{t('Share.permissionLink.readDescription')}
</Typography>
</>
</ActionMenuItem>
{!isOnlyReadOnlyLinkAllowed({ documentType }) && (
<ActionMenuItem
left={
<ActionMenuRadio
name="permissionLinkMenu"
value="false"
checked={!isReadOnlyPermissions}
/>
}
onClick={() => {
toggleMenu()
updateLinkPermissions({ isReadOnly: false })
}}
>
<>
{document?.type === 'directory'
? t(`Share.permissionLink.modifyFolder`)
: t(`Share.permissionLink.modifyFile`)}
<Typography
className="u-mt-half"
variant="caption"
color="textSecondary"
>
{t('Share.permissionLink.writeDescription')}
</Typography>
</>
</ActionMenuItem>
)}
<hr />
<ActionMenuItem
left={<Icon icon={TrashIcon} color="var(--errorColor)" />}
onClick={() => {
toggleMenu()
deleteShareLink()
}}
>
<Typography className="u-error" variant="body1">
{t('Share.permissionLink.deactivate')}
</Typography>
</ActionMenuItem>
</ActionMenu>
)}
</>
)}
<>
<DropdownButton
ref={buttonRef}
textVariant="body2"
onClick={() => setOpenShareRestrictionModal(true)}
>
{t(
`Share.type.${isReadOnlyPermissions ? 'one-way' : 'two-way'}`
).toLowerCase()}
</DropdownButton>
{openShareRestrictionModal && (
<ShareRestrictionModal
file={document}
onClose={() => setOpenShareRestrictionModal(false)}
/>
)}
</>
</div>
)
}

LinkRecipientPermissions.propTypes = {
className: PropTypes.string,
document: PropTypes.object.isRequired,
permissions: PropTypes.object.isRequired
}

export default LinkRecipientPermissions

0 comments on commit f543d29

Please sign in to comment.