2020-10-27 02:58:29 +07:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-12-13 11:56:24 +09:00
|
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
2020-10-27 02:58:29 +07:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class StorageService {
|
2021-12-13 11:56:24 +09:00
|
|
|
constructor(private router: Router, private route: ActivatedRoute) {
|
2022-01-06 19:59:33 +09:00
|
|
|
this.setDefaultValueIfNeeded('graphWindowPreference', '2h');
|
2022-04-11 18:17:36 +09:00
|
|
|
this.setDefaultValueIfNeeded('miningWindowPreference', '1w');
|
2022-01-06 19:59:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultValueIfNeeded(key: string, defaultValue: string) {
|
2022-04-11 18:17:36 +09:00
|
|
|
const graphWindowPreference: string = this.getValue(key);
|
2023-03-29 15:10:59 +09:00
|
|
|
const fragment = window.location.hash.replace('#', '');
|
|
|
|
|
2021-12-13 11:56:24 +09:00
|
|
|
if (graphWindowPreference === null) { // First visit to mempool.space
|
2023-03-29 15:10:59 +09:00
|
|
|
if (window.location.pathname.includes('graphs') && key === 'graphWindowPreference' ||
|
|
|
|
window.location.pathname.includes('pools') && key === 'miningWindowPreference'
|
2022-01-19 13:03:43 +09:00
|
|
|
) {
|
2023-03-29 15:10:59 +09:00
|
|
|
this.setValue(key, fragment ? fragment : defaultValue);
|
2021-12-13 11:56:24 +09:00
|
|
|
} else {
|
2022-01-06 19:59:33 +09:00
|
|
|
this.setValue(key, defaultValue);
|
2021-12-13 11:56:24 +09:00
|
|
|
}
|
2023-03-29 15:10:59 +09:00
|
|
|
} else if (window.location.pathname.includes('graphs') && key === 'graphWindowPreference' ||
|
|
|
|
window.location.pathname.includes('pools') && key === 'miningWindowPreference'
|
2022-01-19 13:03:43 +09:00
|
|
|
) {
|
|
|
|
// Visit a different graphs#fragment from last visit
|
2023-03-29 15:10:59 +09:00
|
|
|
if (fragment !== null && graphWindowPreference !== fragment) {
|
|
|
|
this.setValue(key, fragment);
|
2021-12-13 11:56:24 +09:00
|
|
|
}
|
2022-01-06 19:59:33 +09:00
|
|
|
}
|
2021-12-13 11:56:24 +09:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:58:29 +07:00
|
|
|
getValue(key: string): string {
|
|
|
|
try {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2020-11-16 19:27:06 +07:00
|
|
|
return '';
|
2020-10-27 02:58:29 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setValue(key: string, value: any): void {
|
|
|
|
try {
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 17:01:23 +00:00
|
|
|
|
|
|
|
removeItem(key: string): void {
|
|
|
|
try {
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
2023-08-20 08:11:55 +02:00
|
|
|
|
|
|
|
getAuth(): any | null {
|
|
|
|
try {
|
|
|
|
return JSON.parse(localStorage.getItem('auth'));
|
|
|
|
} catch(e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 02:58:29 +07:00
|
|
|
}
|