import { Component, ChangeDetectionStrategy, signal } from '@angular/core';
import { RouterLink, Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs';
import { SEO_ROUTES, crumbName, isListed, normalizeRouteKey } from '../seo/seo.types';
import { ToolGroup, TOOL_GROUPS, groupForTool } from '../tool-groups';
import { relatedFor } from './related-links';
import familyData from './tool-families.json';
import { allTools, chromeLang, chromeLangFor, chromeText, moreTools } from '../shared/chrome-lang';
const { families: FAMILIES, topUpFrom: TOP_UP_FROM } = familyData as { families: { tools: string[] }[]; topUpFrom: string[] };
interface RelatedLink {
path: string;
/** The tool's plain name, trimmed the same way a breadcrumb is. */
name: string;
}
/**
* "Related tools" links under each tool page.
*
* The nav dropdown already links every tool, but a link in site-wide chrome
* says nothing about which pages belong together - it is identical on all of
* them. Links in the body of a page do carry that signal, and more practically,
* somebody who landed on the SVG optimiser from a search has no idea the image
* toolkit exists two entries away in a menu they never opened.
*
* Names come from the SEO data (seo-runtime.json, from seo-data.json) rather than being written again here, so they
* cannot drift from the pages they point at. They are trimmed with the same
* crumbName() the breadcrumbs use: ogTitle often pairs a name with an explainer
* ("CSV Toolkit — View, Convert and Chart CSV"), which is right for a title and
* far too long for one of six pills in a row.
*
* Which tools: the page's families first, topped up from its category only
* where the category is one theme all through (related-links.ts).
* When every link is in the page's own category the heading says so ("More PDF
* tools"); when a family reaches across categories, as Repair does, it is just
* "Related tools". A format page under /open-file has no category, so its
* "see all" link goes to the /open-file table instead.
*/
@Component({
selector: 'app-related-tools',
standalone: true,
imports: [RouterLink],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@if (links().length) {
}
`,
styleUrl: './related-tools.component.scss',
})
export class RelatedToolsComponent {
protected readonly group = signal(undefined);
protected readonly links = signal([]);
/** True when every link is in this page's own category. */
protected readonly sameGroup = signal(false);
protected readonly lang = chromeLang();
protected readonly moreTools = moreTools;
protected readonly allTools = allTools;
protected t(english: string): string {
return chromeText(this.lang(), english);
}
/** A link's name is in its own page's language; marked when that is not this page's. */
protected linkLang(path: string): string | null {
const theirs = chromeLangFor(path) ?? 'en';
return theirs === (this.lang() ?? 'en') ? null : theirs;
}
/** "Design, 3D & maps" reads "design, 3D & maps" mid-sentence: words with a digit or a second capital keep their case. */
protected inSentence(name: string): string {
return name.replace(/\S+/g, (w) => (/\d|.[A-Z]/.test(w) ? w : w.toLowerCase()));
}
constructor(private router: Router) {
this.apply(this.router.url);
this.router.events
.pipe(filter((e): e is NavigationEnd => e instanceof NavigationEnd))
.subscribe((e) => this.apply(e.urlAfterRedirects));
}
private apply(url: string): void {
const key = normalizeRouteKey(url);
// Only on a page that is itself indexable, and only to pages that are: a
// transient state like a drill in progress should not be growing links out
// of the bottom of it. relatedFor() returns nothing for those. A parked page
// counts as indexable here: it is a real tool, only kept out of search.
const related = relatedFor(key, { groups: TOOL_GROUPS, families: FAMILIES, topUpFrom: TOP_UP_FROM, index: isListed });
const group = related.length ? groupForTool(key) : undefined;
this.group.set(group);
this.sameGroup.set(!!group && related.every((p) => group.tools.includes(p)));
this.links.set(related.map((path) => ({ path, name: crumbName(SEO_ROUTES[path].ogTitle) })));
}
}