1 import { useAuthentication } from '@proton/components';
2 import { getAppHref } from '@proton/shared/lib/apps/helper';
3 import { APPS } from '@proton/shared/lib/constants';
6 * When coming back from the account sign up or sign in, we are usually coming back after the user initiated the auth
7 * by pressing Bookmark or Make Copy button. The `action` param in the URL will tell us where to pick back up once
11 Bookmark = 'bookmark',
12 MakeCopy = 'make-copy',
17 mode: 'open' | 'convert' | 'download' | 'history';
28 action?: RedirectAction;
35 * Reauth occurs when a user is first in a public context (viewing a public doc), then selects an option that
36 * redirects them to sign up / sign in. Instead of redirecting back to the public context immediately on
37 * sign up, we first need to redirect to the private docs context, so that the the user's session can be persisted.
38 * Then we redirect back to the public context where the user can be correctly loaded.
40 mode: 'open-url-reauth';
41 action?: RedirectAction;
46 /** Make a copy of a public document */
50 export const useOpenDocument = () => {
51 const { getLocalID } = useAuthentication();
54 * Opens a document in a new window.
56 * In the Drive application, this should not be used directly, prefer `useDocumentActions`.
58 const openDocumentWindow = (action: DocumentAction & { window: Window }) => {
59 const { mode, window } = action;
61 const href = getAppHref(`/doc`, APPS.PROTONDOCS, getLocalID());
62 const url = new URL(href);
64 url.searchParams.append('mode', mode);
66 if ('volumeId' in action && action.volumeId) {
67 url.searchParams.append('volumeId', action.volumeId);
68 } else if ('token' in action && action.token) {
69 url.searchParams.append('token', action.token);
72 if ('linkId' in action && action.linkId) {
73 url.searchParams.append('linkId', action.linkId);
74 } else if ('parentLinkId' in action && action.parentLinkId) {
75 url.searchParams.append('parentLinkId', action.parentLinkId);
78 if ('urlPassword' in action && action.urlPassword) {
79 url.hash = action.urlPassword;
82 window.location.assign(url);