import {
booleanAttribute,
ChangeDetectionStrategy,
Component,
computed,
inject,
input,
} from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { NavigationEnd, Router, RouterLink } from '@angular/router';
import { filter, map, startWith } from 'rxjs';
import { AfricaniesIconComponent } from '@africanies/africanies-icons';
import { BreadcrumbComponent } from '../navigation/breadcrumb';
import {
resolveCatalogRootLink,
resolveContentBackTarget,
resolveParentPathFromRootSnapshot,
} from '../navigation/header-back.util';
import type { AfricaniesNavItem } from '../navigation/nav-item';
import type { AfricaniesSideNavItem } from '../navigation/side-nav';
import { PageHeaderComponent } from './page-header.component';
/**
* Content chrome above the app-shell body: Back, breadcrumbs, then the page
* title and optional subtitle.
*/
@Component({
selector: 'africanies-app-shell-content-header',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [AfricaniesIconComponent, BreadcrumbComponent, PageHeaderComponent, RouterLink],
template: `
@if (showBackButton() || breadcrumbs().length || title() || subtitle()) {
@if (showBackButton() || breadcrumbs().length) {
@if (showBackButton()) {
}
@if (breadcrumbs().length) {
}
}
@if (title() || subtitle()) {
}
}
`,
})
/**
* Content chrome above the app-shell body: Back, breadcrumbs, then the page title and optional subtitle.
*/
export class AppShellContentHeaderComponent {
private readonly router = inject(Router);
readonly breadcrumbs = input([]);
readonly title = input('');
/** Supporting line under the title (what this page is for). */
readonly subtitle = input('');
readonly url = input('');
readonly catalogNav = input([]);
readonly showBack = input(true, { transform: booleanAttribute });
readonly backLink = input(null);
readonly backLabel = input('Back');
private readonly routeUrl = toSignal(
this.router.events.pipe(
filter((event): event is NavigationEnd => event instanceof NavigationEnd),
map((event) => event.urlAfterRedirects),
startWith(this.router.url),
),
{ initialValue: this.router.url },
);
protected readonly backTarget = computed(() => {
const url = this.routeUrl() || this.url();
return resolveContentBackTarget(
resolveParentPathFromRootSnapshot(this.router.routerState.snapshot.root),
url,
resolveCatalogRootLink(url, this.catalogNav()),
this.backLink(),
);
});
protected readonly showBackButton = computed(
() => this.showBack() && this.backTarget() != null,
);
}