209 lines
5.9 KiB
TypeScript
209 lines
5.9 KiB
TypeScript
import { Component, input, model, output, 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 { MatRadioModule } from '@angular/material/radio';
|
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
|
import { TopicsService } from '../../api/api/topics.service';
|
|
import { topicHierarchyColor } from '../../resolvers/utils/topic-colors';
|
|
import { combineLatest, of } from 'rxjs';
|
|
import { toObservable } from '@angular/core/rxjs-interop';
|
|
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;
|
|
fieldName: string;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-topic-hierarchy',
|
|
standalone: true,
|
|
imports: [
|
|
CommonModule,
|
|
MatCheckboxModule,
|
|
MatIconModule,
|
|
MatProgressSpinnerModule,
|
|
MatRadioModule,
|
|
MatTooltipModule,
|
|
],
|
|
templateUrl: './topic-hierarchy.component.html',
|
|
styleUrl: './topic-hierarchy.component.scss',
|
|
})
|
|
export class TopicHierarchyComponent implements OnInit {
|
|
selectedTopics = input<Set<string>>(new Set<string>());
|
|
searchQuery = input<string>('');
|
|
searchType = model<'hybrid' | 'semantic'>('hybrid');
|
|
|
|
selectionChange = output<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>();
|
|
|
|
constructor(private topicsService: TopicsService) {
|
|
combineLatest([
|
|
toObservable(this.searchQuery),
|
|
toObservable(this.searchType),
|
|
])
|
|
.pipe(
|
|
debounceTime(300),
|
|
distinctUntilChanged(
|
|
(prev, curr) => prev[0] === curr[0] && prev[1] === curr[1],
|
|
),
|
|
switchMap(([query, searchType]) => {
|
|
if (!query || query.length < 2) {
|
|
this.filteredFlatTopics = [];
|
|
return of({ results: [] });
|
|
}
|
|
this.loading.set(true);
|
|
return this.topicsService
|
|
.vectorSearchTopics(query, 50, searchType)
|
|
.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 || '',
|
|
fieldName: topic.field?.display_name || '',
|
|
};
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
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}`,
|
|
fieldName: field.name,
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
this.flatTopics = flat;
|
|
}
|
|
|
|
onSearchTypeChange(type: 'hybrid' | 'semantic'): void {
|
|
this.searchType.set(type);
|
|
}
|
|
|
|
getFieldColor(fieldName: string | null | undefined): string {
|
|
return topicHierarchyColor({ field_name: fieldName });
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|