Browse Source

Better error handling.

pull/1023/head
Sebastian 3 years ago
parent
commit
447203b754
  1. 23
      frontend/src/app/features/content/shared/forms/chat-dialog.component.html
  2. 35
      frontend/src/app/features/content/shared/forms/chat-dialog.component.ts

23
frontend/src/app/features/content/shared/forms/chat-dialog.component.html

@ -20,10 +20,10 @@
</div> </div>
<div class="item" *ngFor="let item of snapshot.chatTalk"> <div class="item" *ngFor="let item of snapshot.chatTalk">
<div class="row mt-3" *ngIf="item.isUser"> <div class="row mt-3" *ngIf="item.type === 'user'">
<div class="col"> <div class="col">
<div class="bubble bubble-left"> <div class="bubble bubble-left">
{{item.prompt}} {{item.text}}
</div> </div>
</div> </div>
<div class="col-auto"> <div class="col-auto">
@ -31,7 +31,20 @@
</div> </div>
</div> </div>
<div class="row mt-3" *ngIf="!item.isUser"> <div class="row mt-3" *ngIf="item.type === 'system'">
<div class="col-auto">
<div class="squid squid-sm d-flex align-items-center justify-content-center">
<img src="./images/squid.svg">
</div>
</div>
<div class="col">
<div class="bubble bubble-right">
{{ item.text | sqxTranslate}}
</div>
</div>
</div>
<div class="row mt-3" *ngIf="item.type === 'bot'">
<div class="col-auto"> <div class="col-auto">
<div class="squid squid-sm d-flex align-items-center justify-content-center"> <div class="squid squid-sm d-flex align-items-center justify-content-center">
<img src="./images/squid.svg"> <img src="./images/squid.svg">
@ -43,10 +56,10 @@
{{ 'chat.answer' | sqxTranslate}} {{ 'chat.answer' | sqxTranslate}}
</div> </div>
<textarea class="form-control" readonly [ngModel]="item.prompt"></textarea> <textarea class="form-control" readonly [ngModel]="item.text"></textarea>
<div class="mt-2"> <div class="mt-2">
<button type="button" class="btn btn-primary" (click)="select.emit(item.prompt)"> <button type="button" class="btn btn-primary" (click)="select.emit(item.text)">
{{ 'chat.use' | sqxTranslate }} {{ 'chat.use' | sqxTranslate }}
</button> </button>
</div> </div>

35
frontend/src/app/features/content/shared/forms/chat-dialog.component.ts

@ -6,6 +6,7 @@
*/ */
import { Component, ElementRef, EventEmitter, Output, ViewChild } from '@angular/core'; import { Component, ElementRef, EventEmitter, Output, ViewChild } from '@angular/core';
import { delay } from 'rxjs/operators';
import { AppsState, AuthService, StatefulComponent, TranslationsService } from '@app/shared'; import { AppsState, AuthService, StatefulComponent, TranslationsService } from '@app/shared';
interface State { interface State {
@ -16,7 +17,7 @@ interface State {
chatQuestion: string; chatQuestion: string;
// The answers. // The answers.
chatTalk: ReadonlyArray<{ prompt: string; isUser?: boolean }>; chatTalk: ReadonlyArray<{ text: string; type: 'user' | 'bot' | 'system' }>;
} }
@Component({ @Component({
@ -64,22 +65,34 @@ export class ChatDialogComponent extends StatefulComponent<State> {
chatQuestion: '', chatQuestion: '',
chatTalk: [ chatTalk: [
...s.chatTalk, ...s.chatTalk,
{ prompt, isUser: true }, { text: prompt, type: 'user' },
], ],
isRunning: true, isRunning: true,
})); }));
this.translator.ask(this.appsState.appName, { prompt }) this.translator.ask(this.appsState.appName, { prompt }).pipe(delay(500))
.subscribe({ .subscribe({
next: chatAnswers => { next: chatAnswers => {
this.next(s => ({ if (chatAnswers.length === 0) {
...s, this.next(s => ({
chatTalk: [ ...s,
...s.chatTalk, chatQuestion: '',
...chatAnswers.map(answer => ({ prompt: answer })), chatTalk: [
], ...s.chatTalk,
isRunning: false, { text: 'i18n:chat.answersEmpty', type: 'system' },
})); ],
isRunning: true,
}));
} else {
this.next(s => ({
...s,
chatTalk: [
...s.chatTalk,
...chatAnswers.map(text => ({ text, type: 'bot' } as any)),
],
isRunning: false,
}));
}
setTimeout(() => { setTimeout(() => {
this.input.nativeElement.focus(); this.input.nativeElement.focus();

Loading…
Cancel
Save