From 3a5312ba6f8f804be3ca5ca02cecfe420004eca7 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 28 Aug 2026 10:52:30 -0700 Subject: [PATCH] Fix: ensure ui reset of suggestionsLoading when changing docs (#13840) --- .../document-detail.component.spec.ts | 22 ++++++++++++++++++- .../document-detail.component.ts | 6 ++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 30e7a4bdf..471e65487 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -24,7 +24,7 @@ import { } from '@ng-bootstrap/ng-bootstrap' import { NgxBootstrapIconsModule, allIcons } from 'ngx-bootstrap-icons' import { DeviceDetectorService } from 'ngx-device-detector' -import { of, throwError } from 'rxjs' +import { Subject, of, throwError } from 'rxjs' import { routes } from 'src/app/app-routing.module' import { Correspondent } from 'src/app/data/correspondent' import { CustomFieldDataType } from 'src/app/data/custom-field' @@ -1444,6 +1444,26 @@ describe('DocumentDetailComponent', () => { }) }) + it('should reset the suggestions loading state if the document changes mid-request', () => { + const getSetting = settingsService.get.bind(settingsService) + jest + .spyOn(settingsService, 'get') + .mockImplementation((key) => + key === SETTINGS_KEYS.AI_ENABLED ? true : getSetting(key) + ) + const pending = new Subject() + jest + .spyOn(documentService, 'getAiSuggestions') + .mockReturnValue(pending.asObservable()) + initNormally() + expect(component.suggestionsLoading()).toBeTruthy() + + // the in-flight request is cancelled, e.g. by a websocket-driven reload + component.docChangeNotifier.next(component.documentId()) + + expect(component.suggestionsLoading()).toBeFalsy() + }) + it('should show error if needed for get suggestions', () => { const suggestionsSpy = jest.spyOn(documentService, 'getSuggestions') const errorSpy = jest.spyOn(toastService, 'showError') diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts index bb6898a48..d58d7ad1e 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.ts @@ -34,6 +34,7 @@ import { debounceTime, distinctUntilChanged, filter, + finalize, first, map, switchMap, @@ -1016,16 +1017,15 @@ export class DocumentDetailComponent .pipe( first(), takeUntil(this.unsubscribeNotifier), - takeUntil(this.docChangeNotifier) + takeUntil(this.docChangeNotifier), + finalize(() => this.suggestionsLoading.set(false)) ) .subscribe({ next: (result) => { this.suggestions.set(result) - this.suggestionsLoading.set(false) }, error: (error) => { this.suggestions.set(null) - this.suggestionsLoading.set(false) this.toastService.showError( $localize`Error retrieving suggestions.`, error