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

215 lines
5.5 KiB
TypeScript

import {
Component,
Input,
Output,
EventEmitter,
OnInit,
signal,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatIconModule } from '@angular/material/icon';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { TopicsService } from '../../api/api/topics.service';
import { Subject, of } from 'rxjs';
import {
debounceTime,
distinctUntilChanged,
switchMap,
catchError,
} from 'rxjs/operators';
export interface TopicItem {
id: string;
name: string;
}
export interface SubfieldNode {
name: string;
topics: TopicItem[];
}
export interface FieldNode {
name: string;
subfields: SubfieldNode[];
}
export interface DomainNode {
name: string;
fields: FieldNode[];
}
interface FlatTopic {
id: string;
name: string;
path: string;
}
@Component({
selector: 'app-topic-hierarchy',
standalone: true,
imports: [
CommonModule,
MatCheckboxModule,
MatIconModule,
MatProgressSpinnerModule,
],
templateUrl: './topic-hierarchy.component.html',
styleUrl: './topic-hierarchy.component.scss',
})
export class TopicHierarchyComponent implements OnInit {
@Input() selectedTopics: Set<string> = new Set<string>();
private _searchQuery = '';
@Input()
set searchQuery(value: string) {
if (value.length < 2) {
this._searchQuery = '';
} else {
this._searchQuery = value;
}
this.filterFlatTopics();
}
get searchQuery(): string {
return this._searchQuery;
}
@Output() selectionChange = new EventEmitter<Set<string>>();
domains: DomainNode[] = [];
flatTopics: FlatTopic[] = [];
filteredFlatTopics: FlatTopic[] = [];
loading = signal(true);
// Set of names of expanded nodes (lazy content rendering)
expandedDomains = new Set<string>();
expandedFields = new Set<string>();
expandedSubfields = new Set<string>();
private readonly searchSubject = new Subject<string>();
constructor(private topicsService: TopicsService) {
this.searchSubject
.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap((query) => {
if (!query) {
this.filteredFlatTopics = [];
return of({ results: [] });
}
this.loading.set(true);
return this.topicsService.vectorSearchTopics(query, 50).pipe(
catchError((err) => {
console.error('Error in semantic topic search:', err);
// Fallback to local filtering
const q = query.toLowerCase();
this.filteredFlatTopics = this.flatTopics.filter(
(t) =>
t.name.toLowerCase().includes(q) ||
t.path.toLowerCase().includes(q),
);
return of({ results: [] });
}),
);
}),
)
.subscribe((response) => {
this.loading.set(false);
if (response && response.results) {
this.filteredFlatTopics = response.results.map((topic) => {
const parts = [];
if (topic.domain?.display_name)
parts.push(topic.domain.display_name);
if (topic.field?.display_name) parts.push(topic.field.display_name);
if (topic.subfield?.display_name)
parts.push(topic.subfield.display_name);
return {
id: topic.id || '',
name: topic.display_name || '',
path:
parts.length > 0 ? parts.join(' > ') : topic.description || '',
};
});
}
});
}
ngOnInit(): void {
this.topicsService.getTopicHierarchy().subscribe({
next: (res: any) => {
this.domains = res.domains || [];
this.flattenTopics(this.domains);
this.loading.set(false);
},
error: (err) => {
console.error('Error fetching topic hierarchy:', err);
this.loading.set(false);
},
});
}
flattenTopics(domains: DomainNode[]): void {
const flat: FlatTopic[] = [];
domains.forEach((domain) => {
domain.fields?.forEach((field) => {
field.subfields?.forEach((subfield) => {
subfield.topics?.forEach((topic) => {
if (topic.name && topic.id) {
flat.push({
id: topic.id,
name: topic.name,
path: `${domain.name} > ${field.name} > ${subfield.name}`,
});
}
});
});
});
});
this.flatTopics = flat;
this.filterFlatTopics();
}
filterFlatTopics(): void {
if (!this.searchQuery) {
this.filteredFlatTopics = [];
return;
}
this.searchSubject.next(this.searchQuery);
}
toggleTopic(name: string): void {
const nextSelection = new Set(this.selectedTopics);
if (nextSelection.has(name)) {
nextSelection.delete(name);
} else {
nextSelection.add(name);
}
this.selectionChange.emit(nextSelection);
}
toggleDomain(name: string): void {
if (this.expandedDomains.has(name)) {
this.expandedDomains.delete(name);
} else {
this.expandedDomains.add(name);
}
}
toggleField(name: string): void {
if (this.expandedFields.has(name)) {
this.expandedFields.delete(name);
} else {
this.expandedFields.add(name);
}
}
toggleSubfield(name: string): void {
if (this.expandedSubfields.has(name)) {
this.expandedSubfields.delete(name);
} else {
this.expandedSubfields.add(name);
}
}
}