-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbreadcrumb.component.ts
More file actions
92 lines (73 loc) · 3.11 KB
/
breadcrumb.component.ts
File metadata and controls
92 lines (73 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { select } from '@ngxs/store';
import { filter, map, startWith } from 'rxjs';
import { Component, computed, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { ActivatedRoute, ActivatedRouteSnapshot, NavigationEnd, Router } from '@angular/router';
import { RouteData } from '@core/models/route-data.model';
import { ProviderSelectors } from '@core/store/provider';
import { InstitutionsAdminSelectors } from '@osf/features/admin-institutions/store';
import { IconComponent } from '@osf/shared/components/icon/icon.component';
import { InstitutionsSearchSelectors } from '@shared/stores/institutions-search';
@Component({
selector: 'osf-breadcrumb',
imports: [IconComponent],
templateUrl: './breadcrumb.component.html',
styleUrl: './breadcrumb.component.scss',
})
export class BreadcrumbComponent {
private readonly router = inject(Router);
private readonly route = inject(ActivatedRoute);
currentProvider = select(ProviderSelectors.getCurrentProvider);
institution = select(InstitutionsSearchSelectors.getInstitution);
institutionDashboard = select(InstitutionsAdminSelectors.getInstitution);
readonly routeData = toSignal(
this.router.events.pipe(
filter((event) => event instanceof NavigationEnd),
map(() => this.getCurrentRouteData()),
startWith(this.getCurrentRouteData())
),
{ initialValue: { skipBreadcrumbs: false } as RouteData }
);
readonly showBreadcrumb = computed(() => this.routeData()?.skipBreadcrumbs !== true);
readonly breadcrumbs = computed<string[]>(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const currentRoute = this.routeData(); // Trigger recomputation when route data changes
const providerName = this.currentProvider()?.name;
const institution = this.institution()?.name;
const institutionDashboard = this.institutionDashboard()?.name;
return this.buildBreadcrumbs(this.route.root.snapshot, providerName, institution ?? institutionDashboard);
});
private buildBreadcrumbs(
route: ActivatedRouteSnapshot,
providerName: string | undefined,
institutionName: string | undefined,
segments: string[] = []
): string[] {
for (const segment of route.url) {
const segmentPath = segment.path;
let label = segmentPath.replace(/-/g, ' ');
const isProviderIdSegment = route.params['providerId'] === segmentPath;
const isInstitutionIdSegment = route.params['institutionId'] === segmentPath;
if (isProviderIdSegment && providerName) {
label = providerName;
}
if (isInstitutionIdSegment && institutionName) {
label = institutionName;
}
if (label && label.trim().length > 0) {
segments.push(label);
}
}
if (route.firstChild) {
return this.buildBreadcrumbs(route.firstChild, providerName, institutionName, segments);
}
return segments;
}
private getCurrentRouteData(): RouteData {
let currentRoute = this.route;
while (currentRoute.firstChild) {
currentRoute = currentRoute.firstChild;
}
return currentRoute.snapshot.data as RouteData;
}
}