Skip to content

Commit b5d18fe

Browse files
AndrewKushnirdevversion
authored andcommitted
refactor: use signals in the AI Assistant UI
This commit updates the AI Assistant UI to use signals, to fix the problem with not clearing the textarea after the message submission (likely appeared after switching to zoneless).
1 parent 669fd90 commit b5d18fe

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

report-app/src/app/shared/ai-assistant/ai-assistant.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ <h2>AI Assistant</h2>
1515
<div class="header-actions">
1616
<button class="expand-button" (click)="toggleExpanded()">
1717
<span class="material-symbols-outlined">
18-
@if (isExpanded) {
18+
@if (isExpanded()) {
1919
close_fullscreen
2020
} @else {
2121
open_in_full
@@ -38,7 +38,7 @@ <h2>AI Assistant</h2>
3838
<div [innerHTML]="message.text"></div>
3939
</div>
4040
}
41-
@if (isLoading) {
41+
@if (isLoading()) {
4242
<div class="message model-message">
4343
<message-spinner message="Thinking..." />
4444
</div>
@@ -47,12 +47,13 @@ <h2>AI Assistant</h2>
4747
<div class="input-container">
4848
<textarea
4949
#userInputElement
50-
[(ngModel)]="userInput"
50+
[ngModel]="userInput()"
51+
(ngModelChange)="userInput.set($event)"
5152
placeholder="Ask a question about the report..."
52-
(keydown.enter)="send()"
53-
[readonly]="isLoading"
53+
(keydown.enter)="$event.preventDefault(); send()"
54+
[readonly]="isLoading()"
5455
></textarea>
55-
<button (click)="send()" [disabled]="isLoading">
56+
<button (click)="send()" [disabled]="isLoading()">
5657
<span class="material-symbols-outlined">send</span>
5758
</button>
5859
</div>

report-app/src/app/shared/ai-assistant/ai-assistant.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {HttpClient} from '@angular/common/http';
2-
import {Component, input, output} from '@angular/core';
2+
import {Component, inject, input, output, signal} from '@angular/core';
33
import {FormsModule} from '@angular/forms';
44
import {firstValueFrom} from 'rxjs';
55
import {
@@ -20,17 +20,19 @@ interface Model {
2020
styleUrl: './ai-assistant.scss',
2121
imports: [FormsModule, MessageSpinner],
2222
host: {
23-
'[class.expanded]': 'isExpanded',
23+
'[class.expanded]': 'isExpanded()',
2424
},
2525
})
2626
export class AiAssistant {
2727
readonly reportGroupId = input.required<string>();
2828
readonly close = output();
2929

3030
protected messages: AiChatMessage[] = [];
31-
protected userInput = '';
32-
protected isLoading = false;
33-
protected isExpanded = false;
31+
protected userInput = signal('');
32+
protected isLoading = signal(false);
33+
protected isExpanded = signal(false);
34+
35+
private readonly http = inject(HttpClient);
3436

3537
protected readonly models: Model[] = [
3638
{id: 'gemini-2.5-flash', name: 'Gemini 2.5 Flash'},
@@ -39,23 +41,21 @@ export class AiAssistant {
3941
];
4042
protected selectedModel = this.models[0].id;
4143

42-
constructor(private readonly http: HttpClient) {}
43-
4444
protected toggleExpanded(): void {
45-
this.isExpanded = !this.isExpanded;
45+
this.isExpanded.set(!this.isExpanded());
4646
}
4747

4848
async send(): Promise<void> {
49-
if (!this.userInput.trim() || this.isLoading) {
49+
if (!this.userInput().trim() || this.isLoading()) {
5050
return;
5151
}
5252

5353
const pastMessages = this.messages.slice();
5454

55-
this.messages.push({role: 'user', text: this.userInput});
56-
const prompt = this.userInput;
57-
this.userInput = '';
58-
this.isLoading = true;
55+
this.messages.push({role: 'user', text: this.userInput()});
56+
const prompt = this.userInput();
57+
this.userInput.set('');
58+
this.isLoading.set(true);
5959

6060
const payload: AiChatRequest = {
6161
prompt,
@@ -75,7 +75,7 @@ export class AiAssistant {
7575
text: 'Sorry, I failed to get a response. Please try again.',
7676
});
7777
} finally {
78-
this.isLoading = false;
78+
this.isLoading.set(false);
7979
}
8080
}
8181
}

0 commit comments

Comments
 (0)