Files
smartguide/webapp/src/app/modules/hierarchy/hierarchy.component.ts
T

153 lines
5.7 KiB
TypeScript

import {
Component,
DestroyRef,
inject,
input,
OnInit,
signal,
computed,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router } from '@angular/router';
import { HierarchyChartComponent } from './hierarchy-chart/hierarchy-chart.component';
import { OrgminerContentComponent } from './orgminer-content/orgminer-content.component';
import { ClassifiedPagesComponent } from './classified-pages/classified-pages.component';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatIconModule } from '@angular/material/icon';
import { HierarchyResponse } from '../../api/model/hierarchyResponse';
import { CredentialService } from '../../shared/services/credential.service';
import { HierarchySuggestionsComponent } from './hierarchy-suggestions/hierarchy-suggestions.component';
import { HierarchySuggestion } from '../../api/model/hierarchySuggestion';
import { PageContainerComponent } from '../../shared/ui/page-container/page-container.component';
const ORGANIZATION_URL_PARAM = 'orgUrl';
const ORGANIZATION_NAME_PARAM = 'orgName';
const ORGANIZATION_FOCUS_PARAM = 'orgFocus';
const ORGANIZATION_FOCUS_PATH_PARAM = 'orgFocusPath';
const ORGMINER_EXPANDED_PARAM = 'orgExpanded';
const ORGMINER_SELECTION_CATEGORY_PARAM = 'orgSelectionCategory';
const ORGMINER_SELECTION_INDEX_PARAM = 'orgSelectionIndex';
const CLASSIFIED_CATEGORY_PARAM = 'classifiedCategory';
const CLASSIFIED_PAGE_PARAM = 'classifiedPage';
const HIERARCHY_PANEL_PARAM = 'hierarchyExpanded';
const ORGANIZATION_PANEL_PARAM = 'organizationExpanded';
const CLASSIFIED_PAGES_PANEL_PARAM = 'classifiedPagesExpanded';
@Component({
selector: 'app-hierarchy',
imports: [
HierarchyChartComponent,
OrgminerContentComponent,
ClassifiedPagesComponent,
MatExpansionModule,
MatIconModule,
HierarchySuggestionsComponent,
PageContainerComponent,
],
templateUrl: './hierarchy.component.html',
styleUrl: './hierarchy.component.scss',
})
export class HierarchyComponent implements OnInit {
hierarchyData = input.required<HierarchyResponse>();
suggestions = input<HierarchySuggestion[] | []>([]);
hierarchyExpanded = signal(true);
organizationExpanded = signal(true);
classifiedPagesExpanded = signal(false);
orgUrl = signal<string | null>(null);
orgName = signal<string | null>(null);
private readonly router = inject(Router);
private readonly route = inject(ActivatedRoute);
private readonly destroyRef = inject(DestroyRef);
readonly credentialService = inject(CredentialService);
readonly suggestionsOnly = computed(() =>
this.credentialService.isEffectiveSuperAdmin(),
);
ngOnInit(): void {
this.route.queryParamMap
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((params) => {
const organizationUrl = params.get(ORGANIZATION_URL_PARAM);
this.orgUrl.set(organizationUrl);
this.hierarchyExpanded.set(
this.readBooleanParam(params.get(HIERARCHY_PANEL_PARAM), true),
);
this.organizationExpanded.set(
this.readBooleanParam(params.get(ORGANIZATION_PANEL_PARAM), true),
);
this.classifiedPagesExpanded.set(
this.readBooleanParam(
params.get(CLASSIFIED_PAGES_PANEL_PARAM),
false,
),
);
this.orgName.set(
organizationUrl
? (params.get(ORGANIZATION_NAME_PARAM) ?? 'Organisation')
: null,
);
});
}
reloadSuggestions(): void {
void this.router.navigateByUrl(this.router.url);
}
onHierarchyExpandedChange(expanded: boolean): void {
if (this.hierarchyExpanded() === expanded) return;
this.hierarchyExpanded.set(expanded);
this.syncPanelState({ [HIERARCHY_PANEL_PARAM]: expanded });
}
onOrganizationExpandedChange(expanded: boolean): void {
if (this.organizationExpanded() === expanded) return;
this.organizationExpanded.set(expanded);
this.syncPanelState({ [ORGANIZATION_PANEL_PARAM]: expanded });
}
onClassifiedPagesExpandedChange(expanded: boolean): void {
if (this.classifiedPagesExpanded() === expanded) return;
this.classifiedPagesExpanded.set(expanded);
this.syncPanelState({ [CLASSIFIED_PAGES_PANEL_PARAM]: expanded });
}
async analyzeOrg(org: { name: string; url: string }): Promise<void> {
const organizationUrl = org.url.trim();
if (!organizationUrl) return;
// No local signal.set() here: the queryParamMap subscription applies the state.
// Setting hierarchyExpanded directly made the expansion panel emit
// expandedChange, which started a second navigation without the org params.
const navigated = await this.router.navigate([], {
relativeTo: this.route,
queryParams: {
[ORGANIZATION_URL_PARAM]: organizationUrl,
[ORGANIZATION_NAME_PARAM]: org.name,
[ORGANIZATION_FOCUS_PARAM]: null,
[ORGANIZATION_FOCUS_PATH_PARAM]: null,
[ORGMINER_EXPANDED_PARAM]: null,
[ORGMINER_SELECTION_CATEGORY_PARAM]: null,
[ORGMINER_SELECTION_INDEX_PARAM]: null,
[CLASSIFIED_CATEGORY_PARAM]: null,
[CLASSIFIED_PAGE_PARAM]: null,
[HIERARCHY_PANEL_PARAM]: false,
[ORGANIZATION_PANEL_PARAM]: true,
},
queryParamsHandling: 'merge',
replaceUrl: true,
});
if (!navigated) console.warn('[Hierarchy] navigation was cancelled');
}
private syncPanelState(queryParams: Record<string, boolean>): void {
void this.router.navigate([], {
relativeTo: this.route,
queryParams,
queryParamsHandling: 'merge',
replaceUrl: true,
});
}
private readBooleanParam(value: string | null, fallback: boolean): boolean {
return value === null ? fallback : value === 'true';
}
}