mempool/frontend/src/app/services/navigation.service.ts

86 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-10-12 22:13:29 +00:00
import { Injectable } from '@angular/core';
2024-06-15 00:22:33 +02:00
import { Router, NavigationEnd, ActivatedRouteSnapshot } from '@angular/router';
2022-10-12 22:13:29 +00:00
import { BehaviorSubject } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { StateService } from './state.service';
@Injectable({
providedIn: 'root'
})
export class NavigationService {
subnetPaths = new BehaviorSubject<Record<string,string>>({});
2024-06-15 00:22:33 +02:00
networkModules = {
bitcoin: {
subnets: [
{ name: 'mainnet', path: '' },
{ name: 'testnet', path: this.stateService.env.DEFAULT_NETWORK === 'testnet' ? '/' : '/testnet' },
{ name: 'testnet4', path: this.stateService.env.DEFAULT_NETWORK === 'testnet4' ? '/' : '/testnet4' },
{ name: 'signet', path: this.stateService.env.DEFAULT_NETWORK === 'signet' ? '/' : '/signet' },
],
},
liquid: {
subnets: [
{ name: 'liquid', path: '' },
{ name: 'liquidtestnet', path: '/testnet' },
],
}
};
networks = Object.keys(this.networkModules);
2022-10-12 22:13:29 +00:00
constructor(
private stateService: StateService,
private router: Router,
) {
this.router.events.pipe(
filter(event => event instanceof NavigationEnd),
map(() => this.router.routerState.snapshot.root),
).subscribe((state) => {
this.updateSubnetPaths(state);
});
}
2024-03-17 18:22:38 +09:00
// For each network (bitcoin/liquid), find and save the longest url path compatible with the current route
2022-10-12 22:13:29 +00:00
updateSubnetPaths(root: ActivatedRouteSnapshot): void {
let path = '';
const networkPaths = {};
let route = root;
// traverse the router state tree until all network paths are set, or we reach the end of the tree
2024-06-15 00:22:33 +02:00
while (!this.networks.reduce((acc, network) => acc && !!networkPaths[network], true) && route) {
2022-10-12 22:13:29 +00:00
// 'networkSpecific' paths may correspond to valid routes on other networks, but aren't directly compatible
// (e.g. we shouldn't link a mainnet transaction page to the same txid on testnet or liquid)
if (route.data?.networkSpecific) {
2024-06-15 00:22:33 +02:00
this.networks.forEach(network => {
2022-10-12 22:13:29 +00:00
if (networkPaths[network] == null) {
networkPaths[network] = path;
}
});
}
// null or empty networks list is shorthand for "compatible with every network"
if (route.data?.networks?.length) {
// if the list is non-empty, only those networks are compatible
2024-06-15 00:22:33 +02:00
this.networks.forEach(network => {
2022-10-12 22:13:29 +00:00
if (!route.data.networks.includes(network)) {
if (networkPaths[network] == null) {
networkPaths[network] = path;
}
}
});
}
if (route.url?.length) {
path = [path, ...route.url.map(segment => segment.path).filter(path => {
2024-05-06 15:40:32 +00:00
return path.length && !['testnet', 'testnet4', 'signet'].includes(path);
2022-10-12 22:13:29 +00:00
})].join('/');
}
route = route.firstChild;
}
const subnetPaths = {};
2024-06-15 00:22:33 +02:00
Object.entries(this.networkModules).forEach(([key, network]) => {
2022-10-12 22:13:29 +00:00
network.subnets.forEach(subnet => {
subnetPaths[subnet.name] = subnet.path + (networkPaths[key] != null ? networkPaths[key] : path);
});
});
this.subnetPaths.next(subnetPaths);
}
}