Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewer: adjust behavior with sharing #2696

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/cozy-viewer/src/Footer/BottomSheetContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BottomSheetItem } from 'cozy-ui/transpiled/react/BottomSheet'

import getPanelBlocks, { getPanelBlocksSpecs } from '../Panel/getPanelBlocks'

const BottomSheetContent = ({ file, isPublic }) => {
const BottomSheetContent = ({ file, isPublic, isReadOnly }) => {
const panelBlocks = getPanelBlocks({
panelBlocksSpecs: getPanelBlocksSpecs(isPublic),
file
Expand All @@ -17,14 +17,15 @@ const BottomSheetContent = ({ file, isPublic }) => {
disableGutters
disableElevation={index === panelBlocks.length - 1}
>
<PanelBlock file={file} isPublic={isPublic} />
<PanelBlock file={file} isPublic={isPublic} isReadOnly={isReadOnly} />
</BottomSheetItem>
))
}

BottomSheetContent.propTypes = {
file: PropTypes.object.isRequired,
isPublic: PropTypes.bool
isPublic: PropTypes.bool,
isReadOnly: PropTypes.bool
}

export default BottomSheetContent
15 changes: 13 additions & 2 deletions packages/cozy-viewer/src/Footer/FooterContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ const useStyles = makeStyles(theme => ({
}
}))

const FooterContent = ({ file, toolbarRef, children, isPublic }) => {
const FooterContent = ({
file,
toolbarRef,
children,
isPublic,
isReadOnly
}) => {
const styles = useStyles()

const toolbarProps = useMemo(() => ({ ref: toolbarRef }), [toolbarRef])
Expand Down Expand Up @@ -69,7 +75,11 @@ const FooterContent = ({ file, toolbarRef, children, isPublic }) => {
FooterActionButtonsWithFile={FooterActionButtonsWithFile}
/>
</BottomSheetHeader>
<BottomSheetContent file={file} isPublic={isPublic} />
<BottomSheetContent
file={file}
isPublic={isPublic}
isReadOnly={isReadOnly}
/>
</BottomSheet>
)
}
Expand All @@ -78,6 +88,7 @@ FooterContent.propTypes = {
file: PropTypes.object.isRequired,
toolbarRef: PropTypes.object,
isPublic: PropTypes.bool,
isReadOnly: PropTypes.bool,
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node)
Expand Down
87 changes: 45 additions & 42 deletions packages/cozy-viewer/src/Panel/ActionMenuWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,54 @@ import ActionMenuDesktop from './ActionMenuDesktop'
import ActionMenuMobile from './ActionMenuMobile'
import { isEditableAttribute } from '../helpers'

const ActionMenuWrapper = forwardRef(({ file, optionFile, onClose }, ref) => {
const { name, value } = optionFile
const { isMobile } = useBreakpoints()
const { t } = useI18n()
const { showAlert } = useAlert()
const ActionMenuWrapper = forwardRef(
({ file, optionFile, isReadOnly, onClose }, ref) => {
const { name, value } = optionFile
const { isMobile } = useBreakpoints()
const { t } = useI18n()
const { showAlert } = useAlert()

const isEditable = isEditableAttribute(name, file)
const editPath = `${file.metadata.qualification.label}/${file._id}/edit/information?metadata=${optionFile.name}`
const isEditable = isEditableAttribute(name, file) && !isReadOnly
const editPath = `${file.metadata.qualification.label}/${file._id}/edit/information?metadata=${optionFile.name}`

const handleCopy = async () => {
try {
await navigator.clipboard.writeText(value)
showAlert({
message: t(`Viewer.snackbar.copiedToClipboard.success`),
severity: 'success',
variant: 'filled',
icon: false
})
} catch (error) {
showAlert({
message: t(`Viewer.snackbar.copiedToClipboard.error`),
severity: 'error',
variant: 'filled',
icon: false
})
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(value)
showAlert({
message: t(`Viewer.snackbar.copiedToClipboard.success`),
severity: 'success',
variant: 'filled',
icon: false
})
} catch (error) {
showAlert({
message: t(`Viewer.snackbar.copiedToClipboard.error`),
severity: 'error',
variant: 'filled',
icon: false
})
}
onClose()
}

if (isMobile) {
return (
<ActionMenuMobile
file={file}
optionFile={optionFile}
actions={{
copy: { onClick: handleCopy },
edit: { path: editPath }
}}
isEditable={isEditable}
onClose={onClose}
/>
)
}
onClose()
}

if (isMobile) {
return (
<ActionMenuMobile
<ActionMenuDesktop
ref={ref}
file={file}
optionFile={optionFile}
actions={{
Expand All @@ -52,21 +68,7 @@ const ActionMenuWrapper = forwardRef(({ file, optionFile, onClose }, ref) => {
/>
)
}

return (
<ActionMenuDesktop
ref={ref}
file={file}
optionFile={optionFile}
actions={{
copy: { onClick: handleCopy },
edit: { path: editPath }
}}
isEditable={isEditable}
onClose={onClose}
/>
)
})
)

ActionMenuWrapper.displayName = 'ActionMenuWrapper'

Expand All @@ -76,6 +78,7 @@ ActionMenuWrapper.propTypes = {
name: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
}),
isReadOnly: PropTypes.bool,
onClose: PropTypes.func
}

Expand Down
9 changes: 5 additions & 4 deletions packages/cozy-viewer/src/Panel/Qualification.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ComponentFromMetadataQualificationType = {
bills: QualificationListItemInformation
}

const Qualification = ({ file, isReadOnly }) => {
const Qualification = ({ file, isPublic, isReadOnly }) => {
const { metadata = {} } = file
const actionBtnRef = useRef([])
const [showQualifModal, setShowQualifModal] = useState(false)
Expand Down Expand Up @@ -75,13 +75,13 @@ const Qualification = ({ file, isReadOnly }) => {
{hasSupportedQualification(file) ? (
<QualificationListItemQualification
file={file}
isReadOnly={isReadOnly}
isReadOnly={isPublic ? true : isReadOnly}
onClick={() => setShowQualifModal(true)}
/>
) : (
<QualificationListItemQualificationEmpty
file={file}
isReadOnly={isReadOnly}
isReadOnly={isPublic ? true : isReadOnly}
onClick={() => setShowQualifModal(true)}
/>
)}
Expand Down Expand Up @@ -109,7 +109,7 @@ const Qualification = ({ file, isReadOnly }) => {
<Fragment key={idx}>
<QualificationListItemComp
file={file}
isReadOnly={isReadOnly}
isReadOnly={isPublic ? true : isReadOnly}
ref={actionBtnRef.current[idx]}
formattedMetadataQualification={meta}
toggleActionsMenu={val => toggleActionsMenu(idx, name, val)}
Expand All @@ -124,6 +124,7 @@ const Qualification = ({ file, isReadOnly }) => {
file={file}
optionFile={optionFile}
ref={actionBtnRef.current[optionFile.id]}
isReadOnly={isPublic ? true : isReadOnly}
/>
)}
</List>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const QualificationListItemContact = ({ file, isReadOnly }) => {
file={file}
optionFile={optionFile}
ref={actionBtnRef}
isReadOnly={isReadOnly}
/>
)}
</>
Expand Down
15 changes: 10 additions & 5 deletions packages/cozy-viewer/src/ViewerContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { createRef, useState, useEffect } from 'react'

import { useClient } from 'cozy-client'
import { isDocumentReadOnly } from 'cozy-client/dist/models/permission'
import { useSharingContext } from 'cozy-sharing'
import Modal from 'cozy-ui/transpiled/react/Modal'
import { FileDoctype } from 'cozy-ui/transpiled/react/proptypes'
import AlertProvider from 'cozy-ui/transpiled/react/providers/Alert'
Expand Down Expand Up @@ -34,6 +35,8 @@ const ViewerContainer = props => {
const [isReadOnly, setIsReadOnly] = useState(true)
const client = useClient()
useExtendI18n(locales)
const { hasWriteAccess } = useSharingContext()

const currentFile = files[currentIndex]
const fileCount = files.length
const hasPrevious = currentIndex > 0
Expand All @@ -51,16 +54,18 @@ const ViewerContainer = props => {

useEffect(() => {
const getIsReadOnly = async () => {
const res = await isDocumentReadOnly({
document: currentFile,
client
})
const res = isPublic
? await isDocumentReadOnly({
document: currentFile,
client
})
: !hasWriteAccess(currentFile._id)

setIsReadOnly(res)
}

getIsReadOnly()
}, [client, currentFile])
}, [client, currentFile, hasWriteAccess, isPublic])

return (
<AlertProvider>
Expand Down
1 change: 1 addition & 0 deletions packages/cozy-viewer/src/ViewerInformationsWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const ViewerInformationsWrapper = ({
file={currentFile}
toolbarRef={toolbarRef}
isPublic={isPublic}
isReadOnly={isReadOnly}
>
{children}
</FooterContent>
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-viewer/src/components/ViewerControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class ViewerControls extends Component {
showFilePath={showFilePath}
onMouseEnter={this.showControls}
onMouseLeave={this.hideControls}
onClose={showClose && onClose}
onClose={showClose ? onClose : undefined}
>
{children}
</Toolbar>
Expand Down
Loading