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

115 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-03-24 00:52:08 +07:00
import { Injectable } from '@angular/core';
import { Title, Meta } from '@angular/platform-browser';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { filter, map, switchMap } from 'rxjs';
import { StateService } from './state.service';
2020-03-24 00:52:08 +07:00
@Injectable({
providedIn: 'root'
})
export class SeoService {
network = '';
baseTitle = 'mempool';
2023-08-28 13:10:08 +09:00
baseDescription = 'Explore the full Bitcoin ecosystem with The Mempool Open Projectâ„¢.';
2020-03-24 00:52:08 +07:00
2023-08-22 02:54:23 +09:00
canonicalLink: HTMLElement = document.getElementById('canonical');
2020-03-24 00:52:08 +07:00
constructor(
private titleService: Title,
private metaService: Meta,
private stateService: StateService,
private router: Router,
private activatedRoute: ActivatedRoute,
) {
this.stateService.networkChanged$.subscribe((network) => this.network = network);
this.router.events.pipe(
filter(event => event instanceof NavigationEnd),
map(() => this.activatedRoute),
map(route => {
while (route.firstChild) route = route.firstChild;
return route;
}),
filter(route => route.outlet === 'primary'),
switchMap(route => route.data),
).subscribe((data) => {
this.clearSoft404();
});
}
2020-03-24 00:52:08 +07:00
2020-08-12 14:04:04 +07:00
setTitle(newTitle: string): void {
this.titleService.setTitle(newTitle + ' - ' + this.getTitle());
this.metaService.updateTag({ property: 'og:title', content: newTitle});
2022-07-27 00:55:17 +00:00
this.metaService.updateTag({ property: 'twitter:title', content: newTitle});
this.metaService.updateTag({ property: 'og:meta:ready', content: 'ready'});
2020-03-24 00:52:08 +07:00
}
2020-08-12 14:04:04 +07:00
resetTitle(): void {
this.titleService.setTitle(this.getTitle());
this.metaService.updateTag({ property: 'og:title', content: this.getTitle()});
2022-07-27 00:55:17 +00:00
this.metaService.updateTag({ property: 'twitter:title', content: this.getTitle()});
this.metaService.updateTag({ property: 'og:meta:ready', content: 'ready'});
2020-08-12 14:04:04 +07:00
}
2022-07-21 19:58:12 +02:00
setEnterpriseTitle(title: string) {
this.baseTitle = title + ' - ' + this.baseTitle;
this.resetTitle();
}
setDescription(newDescription: string): void {
this.metaService.updateTag({ name: 'description', content: newDescription});
this.metaService.updateTag({ name: 'twitter:description', content: newDescription});
this.metaService.updateTag({ property: 'og:description', content: newDescription});
}
2023-08-28 13:10:08 +09:00
resetDescription(): void {
this.metaService.updateTag({ name: 'description', content: this.getDescription()});
this.metaService.updateTag({ name: 'twitter:description', content: this.getDescription()});
this.metaService.updateTag({ property: 'og:description', content: this.getDescription()});
}
2023-08-22 02:54:23 +09:00
updateCanonical(path) {
let domain = 'mempool.space';
if (this.stateService.env.BASE_MODULE === 'liquid') {
domain = 'liquid.network';
} else if (this.stateService.env.BASE_MODULE === 'bisq') {
domain = 'bisq.markets';
}
this.canonicalLink.setAttribute('href', 'https://' + domain + path);
}
2020-08-12 14:04:04 +07:00
getTitle(): string {
if (this.network === 'testnet')
2022-07-21 19:58:12 +02:00
return this.baseTitle + ' - Bitcoin Testnet';
if (this.network === 'signet')
2022-07-21 19:58:12 +02:00
return this.baseTitle + ' - Bitcoin Signet';
if (this.network === 'liquid')
2022-07-21 19:58:12 +02:00
return this.baseTitle + ' - Liquid Network';
if (this.network === 'liquidtestnet')
2022-07-21 19:58:12 +02:00
return this.baseTitle + ' - Liquid Testnet';
if (this.network === 'bisq')
2022-07-21 19:58:12 +02:00
return this.baseTitle + ' - Bisq Markets';
return this.baseTitle + ' - ' + (this.network ? this.ucfirst(this.network) : 'Bitcoin') + ' Explorer';
2020-08-12 14:04:04 +07:00
}
2023-08-28 13:10:08 +09:00
getDescription(): string {
if ( (this.network === 'testnet') || (this.network === 'signet') || (this.network === '') || (this.network == 'mainnet') )
return this.baseDescription + ' See the real-time status of your transactions, browse network stats, and more.';
if ( (this.network === 'liquid') || (this.network === 'liquidtestnet') )
return this.baseDescription + ' See Liquid transactions & assets, get network info, and more.';
if (this.network === 'bisq')
return this.baseDescription + ' See Bisq market prices, trading activity, and more.';
}
2020-08-12 14:04:04 +07:00
ucfirst(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
2020-03-24 00:52:08 +07:00
}
clearSoft404() {
window['soft404'] = false;
}
logSoft404() {
window['soft404'] = true;
}
2020-03-24 00:52:08 +07:00
}