Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

1.2 KB
Raw Blame
import { Injector, NgModule } from '@angular/core';

import { CurrentUserService } from './current-user.service';
import { CurrentUserStore } from './current-user.store';
import { CurrentUserQuery } from './current-user.query';
import { firstValueFrom } from 'rxjs';

function loadUserMetadata(currentUserService:CurrentUserService) {
  const userMeta = document.querySelector<HTMLMetaElement>('meta[name=current_user]');
  currentUserService.setUser({
    id: userMeta?.dataset.id || null,
    name: userMeta?.dataset.name || null,
    loggedIn: userMeta?.dataset.loggedIn === 'true',
  });
}

export function bootstrapModule(injector:Injector):void {
  const currentUserService = injector.get(CurrentUserService);

  window.ErrorReporter
    .addHook(
      () => firstValueFrom(currentUserService.user$)
        .then(({ id }) => ({ user: id || 'anon' })),
    );

  loadUserMetadata(currentUserService);
  document.addEventListener('turbo:load', () => loadUserMetadata(currentUserService));
}

@NgModule({
  providers: [
    CurrentUserService,
    CurrentUserStore,
    CurrentUserQuery,
  ],
})
export class CurrentUserModule {
  constructor(injector:Injector) {
    bootstrapModule(injector);
  }
}