Skip to content

Commit 7af9055

Browse files
committed
feat: table prefs now use sessionStorage instead of localStorage
1 parent 22921bf commit 7af9055

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

projects/common/src/preference/preference.service.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
import { Injectable } from '@angular/core';
22
import { Observable, of, throwError } from 'rxjs';
33
import { map, switchMap } from 'rxjs/operators';
4+
import { AbstractStorage } from '../utilities/browser/storage/abstract-storage';
45
import { LocalStorage } from '../utilities/browser/storage/local-storage';
6+
import { SessionStorage } from '../utilities/browser/storage/session-storage';
57
import { BooleanCoercer } from '../utilities/coercers/boolean-coercer';
68
import { NumberCoercer } from '../utilities/coercers/number-coercer';
79

10+
export const enum StorageType {
11+
Local = 'local',
12+
Session = 'session'
13+
}
14+
815
@Injectable({
916
providedIn: 'root'
1017
})
1118
export class PreferenceService {
19+
private static readonly DEFAULT_STORAGE_TYPE: StorageType = StorageType.Local;
20+
1221
private static readonly PREFERENCE_STORAGE_NAMESPACE: string = 'preference';
1322
private static readonly SEPARATOR_CHAR: string = '.';
1423
private static readonly SEPARATOR_REGEX: RegExp = /\.(.+)/;
1524
private readonly numberCoercer: NumberCoercer = new NumberCoercer();
1625
private readonly booleanCoercer: BooleanCoercer = new BooleanCoercer();
1726

18-
public constructor(private readonly preferenceStorage: LocalStorage) {}
27+
public constructor(
28+
private readonly localStorage: LocalStorage,
29+
private readonly sessionStorage: SessionStorage
30+
) {}
1931

2032
/**
2133
* Returns the current storage value if defined, else the default value. The observable
2234
* will continue to emit as the preference is updated, reverting to default value if the
2335
* preference becomes unset. If default value is not provided, the observable will
2436
* throw in the case the preference is unset.
2537
*/
26-
public get<T extends PreferenceValue>(key: PreferenceKey, defaultValue?: T): Observable<T> {
27-
return this.preferenceStorage.watch(this.asStorageKey(key)).pipe(
38+
public get<T extends PreferenceValue>(
39+
key: PreferenceKey,
40+
defaultValue?: T,
41+
type: StorageType = PreferenceService.DEFAULT_STORAGE_TYPE
42+
): Observable<T> {
43+
return this.preferenceStorage(type).watch(this.asStorageKey(key)).pipe(
2844
map(storedValue => this.fromStorageValue<T>(storedValue) ?? defaultValue),
2945
switchMap(value =>
3046
value === undefined
@@ -34,9 +50,13 @@ export class PreferenceService {
3450
);
3551
}
3652

37-
public set(key: PreferenceKey, value: PreferenceValue): void {
53+
public set(
54+
key: PreferenceKey,
55+
value: PreferenceValue,
56+
type: StorageType = PreferenceService.DEFAULT_STORAGE_TYPE
57+
): void {
3858
const val = this.asStorageValue(value);
39-
this.preferenceStorage.set(this.asStorageKey(key), val);
59+
this.preferenceStorage(type).set(this.asStorageKey(key), val);
4060
}
4161

4262
private asStorageKey(key: PreferenceKey): PreferenceStorageKey {
@@ -70,6 +90,16 @@ export class PreferenceService {
7090
return undefined;
7191
}
7292
}
93+
94+
private preferenceStorage(type: StorageType): AbstractStorage {
95+
switch (type) {
96+
case StorageType.Session:
97+
return this.sessionStorage;
98+
case StorageType.Local:
99+
default:
100+
return this.localStorage;
101+
}
102+
}
73103
}
74104

75105
type PreferenceStorageKey = string;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Injectable } from '@angular/core';
2+
import { AbstractStorage } from './abstract-storage';
3+
4+
@Injectable({ providedIn: 'root' })
5+
export class SessionStorage extends AbstractStorage {
6+
public constructor() {
7+
super(sessionStorage);
8+
}
9+
}

projects/components/src/multi-select/multi-select.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
QueryList
1111
} from '@angular/core';
1212
import { IconType } from '@hypertrace/assets-library';
13-
import { queryListAndChanges$, SubscriptionLifecycle } from '@hypertrace/common';
13+
import { queryListAndChanges$ } from '@hypertrace/common';
1414
import { BehaviorSubject, combineLatest, EMPTY, Observable, of, Subject } from 'rxjs';
1515
import { map } from 'rxjs/operators';
1616
import { ButtonRole, ButtonStyle } from '../button/button';
@@ -24,7 +24,6 @@ import { MultiSelectJustify } from './multi-select-justify';
2424
selector: 'ht-multi-select',
2525
styleUrls: ['./multi-select.component.scss'],
2626
changeDetection: ChangeDetectionStrategy.OnPush,
27-
providers: [SubscriptionLifecycle],
2827
template: `
2928
<div
3029
class="multi-select"

projects/observability/src/shared/dashboard/widgets/table/table-widget-renderer.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
forkJoinSafeEmpty,
66
isEqualIgnoreFunctions,
77
isNonEmptyString,
8-
PreferenceService
8+
PreferenceService,
9+
StorageType
910
} from '@hypertrace/common';
1011
import {
1112
FilterAttribute,
@@ -31,7 +32,7 @@ import {
3132
} from '@hypertrace/components';
3233
import { WidgetRenderer } from '@hypertrace/dashboards';
3334
import { Renderer } from '@hypertrace/hyperdash';
34-
import { RendererApi, RENDERER_API } from '@hypertrace/hyperdash-angular';
35+
import { RENDERER_API, RendererApi } from '@hypertrace/hyperdash-angular';
3536
import { capitalize, isEmpty, isEqual, pick } from 'lodash-es';
3637
import { BehaviorSubject, combineLatest, Observable, of, Subject } from 'rxjs';
3738
import {
@@ -548,27 +549,27 @@ export class TableWidgetRendererComponent
548549

549550
private getViewPreferences(): Observable<TableWidgetViewPreferences> {
550551
return isNonEmptyString(this.model.viewId)
551-
? this.preferenceService.get<TableWidgetViewPreferences>(this.model.viewId, {}).pipe(first())
552+
? this.preferenceService.get<TableWidgetViewPreferences>(this.model.viewId, {}, StorageType.Session).pipe(first())
552553
: of({});
553554
}
554555

555556
private setViewPreferences(preferences: TableWidgetViewPreferences): void {
556557
if (isNonEmptyString(this.model.viewId)) {
557-
this.preferenceService.set(this.model.viewId, preferences);
558+
this.preferenceService.set(this.model.viewId, preferences, StorageType.Session);
558559
}
559560
}
560561

561562
private getPreferences(
562563
defaultPreferences: TableWidgetPreferences = TableWidgetRendererComponent.DEFAULT_PREFERENCES
563564
): Observable<TableWidgetPreferences> {
564565
return isNonEmptyString(this.model.getId())
565-
? this.preferenceService.get<TableWidgetPreferences>(this.model.getId()!, defaultPreferences).pipe(first())
566+
? this.preferenceService.get<TableWidgetPreferences>(this.model.getId()!, defaultPreferences, StorageType.Session).pipe(first())
566567
: of(defaultPreferences);
567568
}
568569

569570
private setPreferences(preferences: TableWidgetPreferences): void {
570571
if (isNonEmptyString(this.model.getId())) {
571-
this.preferenceService.set(this.model.getId()!, preferences);
572+
this.preferenceService.set(this.model.getId()!, preferences, StorageType.Session);
572573
}
573574
}
574575

0 commit comments

Comments
 (0)