1- import { ChangeDetectionStrategy , Component , Input } from '@angular/core' ;
2- import { Breadcrumb } from '@hypertrace/common' ;
3- import { Observable } from 'rxjs' ;
4- import { map } from 'rxjs/operators' ;
1+ import { ChangeDetectionStrategy , Component , Input , OnInit } from '@angular/core' ;
2+ import {
3+ Breadcrumb ,
4+ isNonEmptyString ,
5+ NavigationService ,
6+ PreferenceService ,
7+ SubscriptionLifecycle
8+ } from '@hypertrace/common' ;
9+ import { Observable , of } from 'rxjs' ;
10+ import { first , map } from 'rxjs/operators' ;
511import { BreadcrumbsService } from '../../breadcrumbs/breadcrumbs.service' ;
612import { IconSize } from '../../icon/icon-size' ;
713import { NavigableTab } from '../../tabs/navigable/navigable-tab' ;
@@ -10,6 +16,7 @@ import { NavigableTab } from '../../tabs/navigable/navigable-tab';
1016 selector : 'ht-page-header' ,
1117 styleUrls : [ './page-header.component.scss' ] ,
1218 changeDetection : ChangeDetectionStrategy . OnPush ,
19+ providers : [ SubscriptionLifecycle ] ,
1320 template : `
1421 <div
1522 *ngIf="this.breadcrumbs$ | async as breadcrumbs"
@@ -34,7 +41,7 @@ import { NavigableTab } from '../../tabs/navigable/navigable-tab';
3441
3542 <ng-content></ng-content>
3643
37- <ht-navigable-tab-group *ngIf="this.tabs?.length" class="tabs">
44+ <ht-navigable-tab-group *ngIf="this.tabs?.length" class="tabs" (tabChange)="this.onTabChange($event)" >
3845 <ht-navigable-tab
3946 *ngFor="let tab of this.tabs"
4047 [path]="tab.path"
@@ -47,7 +54,10 @@ import { NavigableTab } from '../../tabs/navigable/navigable-tab';
4754 </div>
4855 `
4956} )
50- export class PageHeaderComponent {
57+ export class PageHeaderComponent implements OnInit {
58+ @Input ( )
59+ public persistenceId ?: string ;
60+
5161 @Input ( )
5262 public tabs ?: NavigableTab [ ] = [ ] ;
5363
@@ -62,5 +72,47 @@ export class PageHeaderComponent {
6272 map ( breadcrumbs => ( breadcrumbs . length > 0 ? breadcrumbs [ breadcrumbs . length - 1 ] : { } ) )
6373 ) ;
6474
65- public constructor ( protected readonly breadcrumbsService : BreadcrumbsService ) { }
75+ public constructor (
76+ protected readonly navigationService : NavigationService ,
77+ protected readonly preferenceService : PreferenceService ,
78+ protected readonly subscriptionLifecycle : SubscriptionLifecycle ,
79+ protected readonly breadcrumbsService : BreadcrumbsService
80+ ) { }
81+
82+ public ngOnInit ( ) : void {
83+ this . subscriptionLifecycle . add (
84+ this . getPreferences ( ) . subscribe ( preferences => this . navigateIfPersistedActiveTab ( preferences ) )
85+ ) ;
86+ }
87+
88+ private navigateIfPersistedActiveTab ( preferences : PageHeaderPreferences ) : void {
89+ if ( isNonEmptyString ( this . persistenceId ) && isNonEmptyString ( preferences . selectedTabPath ) ) {
90+ this . navigationService . navigateWithinApp (
91+ preferences . selectedTabPath ,
92+ this . navigationService . getCurrentActivatedRoute ( ) . parent !
93+ ) ;
94+ }
95+ }
96+
97+ public onTabChange ( path ?: string ) : void {
98+ this . setPreferences ( path ) ;
99+ }
100+
101+ private getPreferences ( ) : Observable < PageHeaderPreferences > {
102+ return isNonEmptyString ( this . persistenceId )
103+ ? this . preferenceService . get < PageHeaderPreferences > ( this . persistenceId , { } ) . pipe ( first ( ) )
104+ : of ( { } ) ;
105+ }
106+
107+ private setPreferences ( selectedTabPath ?: string ) : void {
108+ if ( isNonEmptyString ( this . persistenceId ) ) {
109+ this . preferenceService . set ( this . persistenceId , {
110+ selectedTabPath : selectedTabPath
111+ } ) ;
112+ }
113+ }
114+ }
115+
116+ interface PageHeaderPreferences {
117+ selectedTabPath ?: string ;
66118}
0 commit comments