diff --git a/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.html b/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.html
index d23c2dc0..5be3be18 100644
--- a/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.html
+++ b/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.html
@@ -45,7 +45,9 @@
}
@for (topic of topics(); track topic) {
- {{ topic }}
+ {{
+ topic.display_name
+ }}
}
@if (
diff --git a/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.ts b/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.ts
index 6b14edd7..9dbb3796 100644
--- a/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.ts
+++ b/webapp/src/app/shared/ui/topic-autocomplete/topic-autocomplete.component.ts
@@ -19,6 +19,7 @@ import {
of,
switchMap,
} from 'rxjs';
+import { Topic } from '../../../api';
@Component({
selector: 'app-topic-autocomplete',
@@ -42,10 +43,10 @@ export class TopicAutocompleteComponent {
readonly minCharacters = input(2);
readonly limit = input(12);
- readonly topicSelected = output();
+ readonly topicSelected = output();
readonly searchControl = new FormControl('', { nonNullable: true });
- readonly topics = signal([]);
+ readonly topics = signal([]);
readonly loading = signal(false);
constructor(
@@ -60,12 +61,12 @@ export class TopicAutocompleteComponent {
const normalizedQuery = query.trim();
if (normalizedQuery.length < this.minCharacters()) {
this.loading.set(false);
- return of([] as string[]);
+ return of([]);
}
this.loading.set(true);
return this.topicsService
- .autocompleteTopics(
+ .listTopics(
normalizedQuery,
undefined,
undefined,
@@ -73,21 +74,19 @@ export class TopicAutocompleteComponent {
this.limit(),
)
.pipe(
- map((response) =>
- Array.from(
- new Set(
- (response.results ?? [])
- .map((topic) => topic.display_name?.trim() ?? '')
- .filter((topic) => !!topic),
- ),
- ),
- ),
+ map(({ results }) => {
+ const seen = new Map();
+ for (const topic of results ?? []) {
+ if (topic?.id != null) seen.set(String(topic.id), topic);
+ }
+ return Array.from(seen.values());
+ }),
catchError((error) => {
console.error(
'Error fetching topic autocomplete options:',
error,
);
- return of([] as string[]);
+ return of([] as Topic[]);
}),
finalize(() => this.loading.set(false)),
);
@@ -101,19 +100,45 @@ export class TopicAutocompleteComponent {
selectTopic(topic: string): void {
this.searchControl.setValue('', { emitEvent: false });
+ const selectedTopic = this.getTopicByDisplayName(topic);
this.topics.set([]);
- this.topicSelected.emit(topic);
+ if (selectedTopic) {
+ this.topicSelected.emit(selectedTopic);
+ } else {
+ const newTopic: Topic = {
+ display_name: topic,
+ id: null,
+ };
+ this.topicSelected.emit(newTopic);
+ }
}
onKeydownTab(event: KeyboardEvent): void {
event.preventDefault();
- this.searchControl.setValue(this.topics()[0], { emitEvent: false });
+ const first = this.topics()[0];
+ if (!first) return;
+ this.searchControl.setValue(first.display_name ?? '', { emitEvent: false });
+ }
+ getTopicByDisplayName(displayName: string): Topic | undefined {
+ const normalized = displayName.trim().toLowerCase();
+ return this.topics().find(
+ (topic) => topic.display_name?.trim().toLowerCase() === normalized,
+ );
}
submitCurrentValue(): void {
const topic = this.searchControl.value.trim();
if (!topic) return;
this.searchControl.setValue('', { emitEvent: false });
this.topics.set([]);
- this.topicSelected.emit(topic);
+ const selectedTopic = this.getTopicByDisplayName(topic);
+ if (selectedTopic) {
+ this.topicSelected.emit(selectedTopic);
+ } else {
+ const newTopic: Topic = {
+ display_name: topic,
+ id: null,
+ };
+ this.topicSelected.emit(newTopic);
+ }
}
clear(): void {