-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Warn the user when the OnlyOffice file has been deleted during …
…editing
- Loading branch information
Showing
7 changed files
with
175 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useMemo, useState, useEffect } from 'react' | ||
import { useSearchParams } from 'react-router-dom' | ||
|
||
import { | ||
useClient, | ||
generateWebLink, | ||
deconstructRedirectLink | ||
} from 'cozy-client' | ||
|
||
const useRedirectLink = () => { | ||
const [searchParams] = useSearchParams() | ||
const params = new URLSearchParams(location.search) | ||
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]) | ||
|
||
/** | ||
* 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 redirectWebLink = useMemo(() => { | ||
if (redirectLink === null && fetchStatus !== 'loaded') return null | ||
|
||
const { slug, pathname, hash } = deconstructRedirectLink(redirectLink) | ||
const { subdomain: subDomainType } = client.getInstanceOptions() | ||
|
||
return generateWebLink({ | ||
cozyUrl: instance, | ||
subDomainType, | ||
slug, | ||
pathname, | ||
hash, | ||
searchParams: [] | ||
}) | ||
}, [client, instance, redirectLink, fetchStatus]) | ||
|
||
return { | ||
redirectWebLink, | ||
redirectLink | ||
} | ||
} | ||
|
||
export { useRedirectLink } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/drive/web/modules/views/OnlyOffice/components/FileDeletedModal.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React, { useState } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
|
||
import { useClient } from 'cozy-client' | ||
import { ConfirmDialog } from 'cozy-ui/transpiled/react/CozyDialogs' | ||
import Buttons from 'cozy-ui/transpiled/react/Buttons' | ||
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n' | ||
import Typography from 'cozy-ui/transpiled/react/Typography' | ||
import Alert from 'cozy-ui/transpiled/react/Alert' | ||
|
||
import { DOCTYPE_FILES } from 'drive/lib/doctypes' | ||
import { useOnlyOfficeContext } from 'drive/web/modules/views/OnlyOffice/OnlyOfficeProvider' | ||
import { makeOnlyOfficeFileRoute } from 'drive/web/modules/views/OnlyOffice/helpers' | ||
import { useRedirectLink } from 'drive/hooks/useRedirectLink' | ||
|
||
const FileDeletedModal = () => { | ||
const { fileId, setFileDeleted, editorMode } = useOnlyOfficeContext() | ||
const navigate = useNavigate() | ||
const client = useClient() | ||
const { t } = useI18n() | ||
|
||
const [isErrorAlertDisplayed, setErrorAlertDisplayed] = useState(false) | ||
const [isBusy, setBusy] = useState(false) | ||
|
||
const { redirectLink, redirectWebLink } = useRedirectLink() | ||
|
||
const restore = async () => { | ||
setErrorAlertDisplayed(false) | ||
setBusy(isBusy) | ||
try { | ||
const resp = await client.collection(DOCTYPE_FILES).restore(fileId) | ||
const route = makeOnlyOfficeFileRoute(resp.data.id, { | ||
fromRedirect: redirectLink, | ||
fromEdit: editorMode === 'edit' | ||
}) | ||
navigate(route) | ||
setFileDeleted(false) | ||
} catch { | ||
setErrorAlertDisplayed(true) | ||
} finally { | ||
setBusy(false) | ||
} | ||
} | ||
|
||
const goBack = () => { | ||
window.location = redirectWebLink | ||
} | ||
|
||
return ( | ||
<ConfirmDialog | ||
open | ||
title={t('FileDeletedModal.title')} | ||
content={ | ||
<> | ||
{isErrorAlertDisplayed ? ( | ||
<Alert severity="error" className="u-mb-1"> | ||
{t('FileDeletedModal.error')} | ||
</Alert> | ||
) : null} | ||
<Typography>{t('FileDeletedModal.content')}</Typography> | ||
</> | ||
} | ||
actions={ | ||
<> | ||
<Buttons | ||
disabled={isBusy} | ||
variant="secondary" | ||
color="error" | ||
label={t('FileDeletedModal.cancel')} | ||
onClick={goBack} | ||
/> | ||
<Buttons | ||
busy={isBusy} | ||
disabled={isBusy} | ||
label={t('FileDeletedModal.confirm')} | ||
onClick={restore} | ||
/> | ||
</> | ||
} | ||
/> | ||
) | ||
} | ||
|
||
export { FileDeletedModal } |