1 import isTruthy from '@proton/utils/isTruthy';
3 import { getLocalIDPath, stripLocalBasenameFromPathname } from '../authentication/pathnameHelper';
4 import type { ExtensionApp } from '../browser/extension';
5 import type { APP_NAMES } from '../constants';
6 import { APPS, APPS_CONFIGURATION, EXTENSIONS, VPN_HOSTNAME } from '../constants';
13 } from '../helpers/desktop';
14 import { stripLeadingAndTrailingSlash } from '../helpers/string';
15 import window from '../window';
17 interface TargetLocation {
23 const getSSOAppTargetLocation = (location: TargetLocation = window.location): TargetLocation => {
24 if (location.hostname === VPN_HOSTNAME) {
26 hostname: 'proton.me',
34 export const getAppHref = (
38 targetLocation: TargetLocation = window.location
40 const { subdomain: targetSubdomain } = APPS_CONFIGURATION[toApp];
41 const { hostname, protocol, port } = getSSOAppTargetLocation(targetLocation);
42 const lastIndex = hostname.lastIndexOf('.');
43 const secondLevelIndex = hostname.indexOf('.');
44 // If there's no second level, just use the original hostname. NOTE: Does not work for tlds as .co.uk
45 const secondLevelDomain = lastIndex !== secondLevelIndex ? hostname.substr(secondLevelIndex + 1) : hostname;
46 const targetDomain = [targetSubdomain, secondLevelDomain].filter(isTruthy).join('.');
47 const targetPort = port.length > 0 ? `:${port}` : '';
50 targetDomain + targetPort,
51 stripLeadingAndTrailingSlash(''),
52 getLocalIDPath(localID),
53 stripLeadingAndTrailingSlash(stripLocalBasenameFromPathname(to)),
58 return `${protocol}//${path}`;
61 export const getAppHrefBundle = (to: string, toApp: APP_NAMES) => {
62 const path = [APPS_CONFIGURATION[toApp].publicPath, to]
63 .map(stripLeadingAndTrailingSlash)
69 export const getAccountSettingsApp = () => APPS.PROTONACCOUNT;
71 export const getClientID = (appName: APP_NAMES): string => {
72 const app = isElectronMail ? APPS.PROTONMAIL : isElectronPass ? APPS.PROTONPASS : appName;
76 windowsClientID = clientID,
77 macosClientID = clientID,
78 linuxClientID = clientID,
79 } = APPS_CONFIGURATION[app];
81 if (isElectronOnWindows) {
82 return windowsClientID;
84 if (isElectronOnMac) {
87 if (isElectronOnLinux) {
93 export const isExtension = (appName: APP_NAMES): appName is ExtensionApp => {
94 return EXTENSIONS[appName as keyof typeof EXTENSIONS] !== undefined;
97 export const getExtension = (appName: APP_NAMES) => {
98 return EXTENSIONS[appName as keyof typeof EXTENSIONS];
101 export const getAppName = (appName: APP_NAMES) => {
102 return APPS_CONFIGURATION[appName].name;
105 export const getAppShortName = (appName: APP_NAMES) => {
106 return APPS_CONFIGURATION[appName].bareName;
109 export const getInvoicesPathname = () => {
110 return '/dashboard#invoices';