1 import { c } from 'ttag';
3 import { useNotifications } from '@proton/components';
4 import { requestFork } from '@proton/pass/lib/auth/fork';
5 import browser from '@proton/pass/lib/globals/browser';
6 import type { ForkType } from '@proton/shared/lib/authentication/fork/constants';
7 import { APPS, PASS_APP_NAME } from '@proton/shared/lib/constants';
8 import noop from '@proton/utils/noop';
10 import { SSO_URL } from '../../app/config';
11 import { promptForPermissions } from '../utils/permissions';
12 import { usePermissionsGranted } from './usePermissionsGranted';
14 /* depending on where we execute this : we may or may not
15 * have access to the tabs API - Firefox content-scripts have
16 * very limited support for the tabs API */
17 export const useRequestFork = () => async (forkType?: ForkType, replace?: boolean) => {
18 const { url } = requestFork({ host: SSO_URL, forkType, app: APPS.PROTONPASSBROWSEREXTENSION });
20 if (replace) return window.location.replace(url);
21 return browser.tabs ? browser.tabs.create({ url }).catch(noop) : window.open(url, '_BLANK');
24 /* before navigating to login we should prompt the
25 * user for any extension permissions required for PASS
26 * to work correctly. IE: on FF we absolutely need the user
27 * to do so for fallback account communication to work */
28 export const useRequestForkWithPermissions = (options?: { autoClose?: boolean; replace?: boolean }) => {
29 const { createNotification } = useNotifications();
30 const accountFork = useRequestFork();
31 const permissionsGranted = usePermissionsGranted();
33 return async (forkType?: ForkType) => {
34 if (permissionsGranted || (await promptForPermissions())) {
35 return accountFork(forkType, options?.replace).finally(async () => {
36 if (options?.autoClose) window.close();
42 text: c('Error').t`Please grant ${PASS_APP_NAME} the necessary extension permissions in order to continue`,