Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / apps / slugHelper.ts
blob48e03122c563dd95e8aad3d3a5789bc75f1d3894
1 import { stripLocalBasenameFromPathname } from '../authentication/pathnameHelper';
2 import type { APP_NAMES } from '../constants';
3 import { APPS, APPS_CONFIGURATION } from '../constants';
4 import { stripLeadingAndTrailingSlash } from '../helpers/string';
6 export const DEFAULT_APP = APPS.PROTONMAIL;
8 export const ALLOWED_APPS = [
9     APPS.PROTONMAIL,
10     APPS.PROTONCALENDAR,
11     APPS.PROTONCONTACTS,
12     APPS.PROTONVPN_SETTINGS,
13     APPS.PROTONDRIVE,
14     APPS.PROTONPASS,
15     APPS.PROTONDOCS,
16     APPS.PROTONWALLET,
19 export const getSlugFromApp = (app: APP_NAMES) => APPS_CONFIGURATION[app].settingsSlug;
21 export const getAppFromPathname = (pathname: string): APP_NAMES | undefined => {
22     return ALLOWED_APPS.find((appName) => {
23         const slug = getSlugFromApp(appName);
24         // Expects a trimmed string
25         return pathname.match(`^${slug}(/|$)`);
26     });
29 export const getAppFromHostname = (hostname: string): APP_NAMES | undefined => {
30     return ALLOWED_APPS.find((appName) => {
31         const slug = getSlugFromApp(appName);
32         return hostname.match(`^${slug}\.`);
33     });
36 export const getAppFromPathnameSafe = (pathname: string) => {
37     const trimmedPathname = stripLeadingAndTrailingSlash(stripLocalBasenameFromPathname(pathname));
38     return getAppFromPathname(trimmedPathname);
41 export const ALLOWED_SLUGS = ALLOWED_APPS.map((app) => APPS_CONFIGURATION[app].settingsSlug);
43 export type AppSlug = (typeof ALLOWED_SLUGS)[number];
45 export const stripSlugFromPathname = (pathname: string) => {
46     return pathname.replace(new RegExp(`/(${ALLOWED_SLUGS.join('|')})`), '');