common-ui/src/lib/utils/tokens.ts
2024-12-13 15:17:05 +02:00

30 lines
978 B
TypeScript

import { inject, InjectionToken } from '@angular/core';
export const UI_ROOT = new InjectionToken<string>('UI path root - different from BASE_HREF');
export const UI_ROOT_PATH_FN = new InjectionToken<(path: string) => string>('Append UI root to path', {
factory: () => {
const root = inject(UI_ROOT);
return (path: string) => {
if (path.startsWith('/')) {
return root + path;
}
return root + '/' + path;
};
},
});
export const GET_TENANT_FROM_PATH_FN = new InjectionToken<() => string>('Parse tenant from path considering UI root', {
factory: () => {
const root = inject(UI_ROOT);
return () => {
const pathSegments = location.pathname.split('/').filter(Boolean);
const rootPathIndex = pathSegments.indexOf(root.replace('/', ''));
const tenant = pathSegments[rootPathIndex + 1];
return tenant ?? '';
};
},
});