Skip to content

Commit f06a7a5

Browse files
authored
feat: table prefs now use sessionStorage instead of localStorage (#1231)
* feat: table prefs now use sessionStorage instead of localStorage * style: prettier * style: lint imports
1 parent 22921bf commit f06a7a5

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

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

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
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(private readonly localStorage: LocalStorage, private readonly sessionStorage: SessionStorage) {}
1928

2029
/**
2130
* Returns the current storage value if defined, else the default value. The observable
2231
* will continue to emit as the preference is updated, reverting to default value if the
2332
* preference becomes unset. If default value is not provided, the observable will
2433
* throw in the case the preference is unset.
2534
*/
26-
public get<T extends PreferenceValue>(key: PreferenceKey, defaultValue?: T): Observable<T> {
27-
return this.preferenceStorage.watch(this.asStorageKey(key)).pipe(
28-
map(storedValue => this.fromStorageValue<T>(storedValue) ?? defaultValue),
29-
switchMap(value =>
30-
value === undefined
31-
? throwError(Error(`No value found or default provided for preferenceKey: ${key}`))
32-
: of(value)
33-
)
34-
);
35+
public get<T extends PreferenceValue>(
36+
key: PreferenceKey,
37+
defaultValue?: T,
38+
type: StorageType = PreferenceService.DEFAULT_STORAGE_TYPE
39+
): Observable<T> {
40+
return this.preferenceStorage(type)
41+
.watch(this.asStorageKey(key))
42+
.pipe(
43+
map(storedValue => this.fromStorageValue<T>(storedValue) ?? defaultValue),
44+
switchMap(value =>
45+
value === undefined
46+
? throwError(Error(`No value found or default provided for preferenceKey: ${key}`))
47+
: of(value)
48+
)
49+
);
3550
}
3651

37-
public set(key: PreferenceKey, value: PreferenceValue): void {
52+
public set(
53+
key: PreferenceKey,
54+
value: PreferenceValue,
55+
type: StorageType = PreferenceService.DEFAULT_STORAGE_TYPE
56+
): void {
3857
const val = this.asStorageValue(value);
39-
this.preferenceStorage.set(this.asStorageKey(key), val);
58+
this.preferenceStorage(type).set(this.asStorageKey(key), val);
4059
}
4160

4261
private asStorageKey(key: PreferenceKey): PreferenceStorageKey {
@@ -70,6 +89,16 @@ export class PreferenceService {
7089
return undefined;
7190
}
7291
}
92+
93+
private preferenceStorage(type: StorageType): AbstractStorage {
94+
switch (type) {
95+
case StorageType.Session:
96+
return this.sessionStorage;
97+
case StorageType.Local:
98+
default:
99+
return this.localStorage;
100+
}
101+
}
73102
}
74103

75104
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: 8 additions & 5 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,
@@ -548,27 +549,29 @@ 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
567+
.get<TableWidgetPreferences>(this.model.getId()!, defaultPreferences, StorageType.Session)
568+
.pipe(first())
566569
: of(defaultPreferences);
567570
}
568571

569572
private setPreferences(preferences: TableWidgetPreferences): void {
570573
if (isNonEmptyString(this.model.getId())) {
571-
this.preferenceService.set(this.model.getId()!, preferences);
574+
this.preferenceService.set(this.model.getId()!, preferences, StorageType.Session);
572575
}
573576
}
574577

0 commit comments

Comments
 (0)