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

fix: Make working back button for a file at another Cozy #2999

Merged
merged 4 commits into from
Oct 9, 2023
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
10 changes: 0 additions & 10 deletions src/drive/targets/public/components/AppRouter.jsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Can we add a bit more of context please?

Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ const AppRouter = ({
element={<OnlyOfficePaywallView isPublic={true} />}
/>
</Route>
<Route
path="onlyoffice/:fileId/fromCreate"
element={
<OnlyOfficeView
isPublic={true}
isReadOnly={isReadOnly}
isInSharedFolder={!isFile}
/>
}
/>
<Route
path="onlyoffice/create/:folderId/:fileClass"
element={<OnlyOfficeCreateView />}
Expand Down
7 changes: 6 additions & 1 deletion src/drive/web/modules/filelist/FileOpener.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useRef } from 'react'
import Hammer from '@egjs/hammerjs'
import propagating from 'propagating-hammerjs'
import { useLocation } from 'react-router-dom'

import { models } from 'cozy-client'

Expand Down Expand Up @@ -59,6 +60,7 @@ const FileOpener = ({
children
}) => {
const rowRef = useRef()
const { pathname } = useLocation()

useEffect(() => {
if (!rowRef || !rowRef.current) return
Expand Down Expand Up @@ -100,7 +102,10 @@ const FileOpener = ({
className={`${styles['file-opener']} ${styles['file-opener__a']}`}
ref={rowRef}
id={file.id}
href={makeOnlyOfficeFileRoute(file)}
href={makeOnlyOfficeFileRoute(file.id, {
withoutRouter: true,
fromPathname: pathname
})}
onClick={ev => {
ev.preventDefault()
}}
Expand Down
4 changes: 0 additions & 4 deletions src/drive/web/modules/navigation/AppRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ const AppRoute = () => (
<Route path="onlyoffice/:fileId" element={<OnlyOfficeView />}>
<Route path="paywall" element={<OnlyOfficePaywallView />} />
</Route>
<Route
path="onlyoffice/:fileId/fromCreate"
element={<OnlyOfficeView />}
/>
<Route
path="onlyoffice/create/:folderId/:fileClass"
element={<OnlyOfficeCreateView />}
Expand Down
13 changes: 9 additions & 4 deletions src/drive/web/modules/public/LightFileViewer.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import { useNavigate } from 'react-router-dom'
import { useLocation, useNavigate } from 'react-router-dom'

import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'
import { SharingBannerPlugin, useSharingInfos } from 'cozy-sharing'
Expand All @@ -20,12 +20,17 @@ import styles from 'drive/web/modules/viewer/barviewer.styl'
const LightFileViewer = ({ files }) => {
const sharingInfos = useSharingInfos()
const { isDesktop, isMobile } = useBreakpoints()

const { pathname } = useLocation()
const navigate = useNavigate()

const onlyOfficeOpener = useCallback(
file => navigate(makeOnlyOfficeFileRoute(file, true)),
[navigate]
file => {
const route = makeOnlyOfficeFileRoute(file.id, {
fromPathname: pathname
})
navigate(route)
},
[navigate, pathname]
)

return (
Expand Down
2 changes: 1 addition & 1 deletion src/drive/web/modules/search/components/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const makeNormalizedFile = (client, folders, file) => {
url = `/n/${file.id}`
openOn = 'notes'
} else if (models.file.shouldBeOpenedByOnlyOffice(file)) {
url = makeOnlyOfficeFileRoute(file, true)
url = makeOnlyOfficeFileRoute(file.id, { fromPathname: urlToFolder })
} else {
url = `${urlToFolder}/file/${file._id}`
}
Expand Down
3 changes: 1 addition & 2 deletions src/drive/web/modules/viewer/FileOpenerExternal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ export class FileOpener extends Component {
componentsProps={{
OnlyOfficeViewer: {
isEnabled: isOfficeEnabled(isDesktop),
opener: file =>
navigate(makeOnlyOfficeFileRoute(file, true))
opener: file => navigate(makeOnlyOfficeFileRoute(file.id))
}
}}
>
Expand Down
2 changes: 1 addition & 1 deletion src/drive/web/modules/viewer/FilesViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const FilesViewer = ({ filesQuery, files, onClose, onChange }) => {
componentsProps={{
OnlyOfficeViewer: {
isEnabled: isOfficeEnabled(isDesktop),
opener: file => navigate(makeOnlyOfficeFileRoute(file, true))
opener: file => navigate(makeOnlyOfficeFileRoute(file.id))
},
toolbarProps: {
showFilePath: true
Expand Down
9 changes: 6 additions & 3 deletions src/drive/web/modules/views/Folder/FolderViewBody.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useContext, useState, useEffect } from 'react'
import { useDispatch } from 'react-redux'
import get from 'lodash/get'
import { useNavigate } from 'react-router-dom'
import { useLocation, useNavigate } from 'react-router-dom'

import { useClient, useCapabilities } from 'cozy-client'
import { useVaultClient, VaultUnlocker } from 'cozy-keys-lib'
Expand Down Expand Up @@ -50,6 +50,7 @@ const FolderViewBody = ({
const { isDesktop } = useBreakpoints()
const navigate = useNavigate()
const client = useClient()
const { pathname } = useLocation()
/**
* Since we are not able to restore the scroll correctly,
* and force the scroll to top every time we change the
Expand Down Expand Up @@ -102,7 +103,8 @@ const FolderViewBody = ({
openInNewTab: url => window.open(url, '_blank'),
routeTo: url => navigate(url),
isOfficeEnabled: isOfficeEnabled(isDesktop),
webviewIntent
webviewIntent,
pathname
})({
event,
file,
Expand All @@ -116,7 +118,8 @@ const FolderViewBody = ({
isFlatDomain,
navigate,
webviewIntent,
isDesktop
isDesktop,
pathname
]
)

Expand Down
16 changes: 13 additions & 3 deletions src/drive/web/modules/views/Folder/createFileOpeningHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const createFileOpeningHandler =
openInNewTab,
routeTo,
isOfficeEnabled,
webviewIntent
webviewIntent,
pathname
}) =>
async ({ event, file, isAvailableOffline }) => {
if (isAvailableOffline) {
Expand Down Expand Up @@ -59,9 +60,18 @@ const createFileOpeningHandler =
}
} else if (isOnlyOffice && isOfficeEnabled) {
if (event.ctrlKey || event.metaKey || event.shiftKey) {
openInNewTab(makeOnlyOfficeFileRoute(file))
openInNewTab(
makeOnlyOfficeFileRoute(file.id, {
withoutRouter: true,
fromPathname: pathname
})
)
} else {
routeTo(makeOnlyOfficeFileRoute(file, true))
routeTo(
makeOnlyOfficeFileRoute(file.id, {
fromPathname: pathname
})
)
}
} else {
navigateToFile(file)
Expand Down
16 changes: 13 additions & 3 deletions src/drive/web/modules/views/OnlyOffice/Create.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@ import Spinner from 'cozy-ui/transpiled/react/Spinner'
import Oops from 'components/Error/Oops'

import useCreateFile from 'drive/web/modules/views/OnlyOffice/useCreateFile'
import { canWriteOfficeDocument } from 'drive/web/modules/views/OnlyOffice/helpers'
import {
canWriteOfficeDocument,
makeOnlyOfficeFileRoute
} from 'drive/web/modules/views/OnlyOffice/helpers'

const Create = () => {
const navigate = useNavigate()
const { folderId, fileClass } = useParams()
const { status, fileId } = useCreateFile(folderId, fileClass)
const folderPath = `/folder/${folderId}`

if (!canWriteOfficeDocument()) {
return <Navigate to={`/folder/${folderId}/paywall`} />
return <Navigate to={`${folderPath}/paywall`} />
}

if (status === 'error') {
return <Oops />
}

if (status === 'loaded' && fileId) {
return navigate(`/onlyoffice/${fileId}/fromCreate`)
const url = makeOnlyOfficeFileRoute(fileId, {
fromCreate: true,
fromPathname: folderPath
})
return navigate(url, {
replace: true
})
}

return (
Expand Down
9 changes: 3 additions & 6 deletions src/drive/web/modules/views/OnlyOffice/Editor.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ jest.mock('cozy-ui/transpiled/react/providers/Breakpoints', () => ({
jest.mock('cozy-client/dist/hooks/useQuery', () => jest.fn())

jest.mock('cozy-flags')
jest.mock(
'drive/web/modules/views/OnlyOffice/Toolbar/HomeLinker',
() =>
({ children }) =>
<div>{children}</div>
)
jest.mock('drive/web/modules/views/OnlyOffice/Toolbar', () => () => (
<div>Toolbar</div>
))

const client = createMockClient({})
client.plugins = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import React from 'react'
import IconButton from 'cozy-ui/transpiled/react/IconButton'
import Icon from 'cozy-ui/transpiled/react/Icon'
import PreviousIcon from 'cozy-ui/transpiled/react/Icons/Previous'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

const BackButton = ({ onClick }) => {
// TODO: remove u-ml-half-s when https://github.com/cozy/cozy-ui/issues/1808 is fixed
const { t } = useI18n()

return (
<IconButton
data-testid="onlyoffice-backButton"
className="u-ml-half-s"
onClick={onClick}
size="medium"
aria-label={t('button.back')}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

>
<Icon icon={PreviousIcon} />
</IconButton>
Expand Down
92 changes: 62 additions & 30 deletions src/drive/web/modules/views/OnlyOffice/Toolbar/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React, { useContext, useCallback, useMemo } from 'react'
import { useNavigate } from 'react-router-dom'
import React, { useContext, useState, useEffect } from 'react'
import { useSearchParams } from 'react-router-dom'

import { RealTimeQueries } from 'cozy-client'
import {
RealTimeQueries,
useClient,
generateWebLink,
deconstructRedirectLink
} from 'cozy-client'
import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'

import { DOCTYPE_FILES } from 'drive/lib/doctypes'
Expand All @@ -18,37 +23,64 @@ import Sharing from 'drive/web/modules/views/OnlyOffice/Toolbar/Sharing'

const Toolbar = () => {
const { isMobile } = useBreakpoints()
const {
fileId,
isPublic,
isFromSharing,
isReadOnly,
isEditorReady,
isFromCreate
} = useContext(OnlyOfficeContext)
const { fileId, isPublic, isReadOnly, isEditorReady } =
useContext(OnlyOfficeContext)
const client = useClient()
const [fetchStatus, setFetchStatus] = useState('pending')
const [instance, setInstance] = useState(client.getStackClient().uri)

useEffect(() => {
const fetch = async () => {
try {
setFetchStatus('loading')
const permissions = await client
.collection('io.cozy.permissions')
.fetchOwnPermissions()
if (permissions.included.length > 0) {
setInstance(permissions.included[0].attributes.instance)
}
setFetchStatus('loaded')
} catch {
setFetchStatus('error')
}
}
fetch()
}, [client])

const [searchParams] = useSearchParams()
const params = new URLSearchParams(location.search)

/**
* We search for redirectLink using two methods because
* the searchParam differs depending on the position in the url :
* - for /#hash?searchParam, you need useSearchParams
* - for /?searchParam#hash, you need location.search
*/
const redirectLink =
searchParams.get('redirectLink') || params.get('redirectLink')

const { data: fileWithPath } = useFileWithPath(fileId)
const navigate = useNavigate()

const hasOnyMoreHistoryEntry = useMemo(
() => isFromSharing || isFromCreate,
[isFromSharing, isFromCreate]
)
// The condition is different in the case of a only office file that has been shared with us.
// In this case there is a double redirection (one to know that the file is a share, the other
// to open it on the host instance), so there is an additional entry in the history.
const showBackButton = useMemo(
() =>
hasOnyMoreHistoryEntry
? window.history.length > 2
: window.history.length > 1,
[hasOnyMoreHistoryEntry]
)
let link
if (redirectLink) {
const { slug, pathname, hash } = deconstructRedirectLink(redirectLink)
const { subdomain: subDomainType } = client.getInstanceOptions()

const handleOnClick = useCallback(
() => (hasOnyMoreHistoryEntry ? navigate(-2) : navigate(-1)),
[hasOnyMoreHistoryEntry, navigate]
)
link = generateWebLink({
cozyUrl: instance,
subDomainType,
slug,
pathname,
hash,
searchParams: []
})
}

const showBackButton = redirectLink !== null && fetchStatus === 'loaded'

const handleOnClick = () => {
window.location = link
}

return (
<>
Expand Down
Loading
Loading