From 70f9fae0c6f2292af096c78974866d14a450555b Mon Sep 17 00:00:00 2001 From: Jacek Pluta Date: Thu, 26 Sep 2024 10:55:33 +0200 Subject: [PATCH] [ACS-8779] Keep selections and question after going to the previous page --- projects/aca-content/assets/i18n/en.json | 3 +- .../favorite-libraries.component.html | 1 + .../favorites/favorites.component.html | 1 + .../lib/components/files/files.component.html | 1 + .../search-ai-input-container.component.html | 4 +- ...earch-ai-input-container.component.spec.ts | 76 ++++++----- .../search-ai-input-container.component.ts | 39 ++---- .../search-ai-input.component.spec.ts | 48 ++++--- .../search-ai-input.component.ts | 23 +++- .../search-ai-results.component.ts | 11 +- .../libraries/libraries.component.html | 1 + .../recent-files/recent-files.component.html | 1 + .../search-results.component.html | 1 + .../shared-files/shared-files.component.html | 1 + .../sidenav/sidenav.component.spec.ts | 22 +++- .../components/sidenav/sidenav.component.ts | 16 ++- .../trashcan/trashcan.component.html | 1 + .../search-ai-navigation.service.spec.ts | 20 +-- .../services/search-ai-navigation.service.ts | 9 +- .../lib/store/effects/search-ai.effects.ts | 3 +- .../document-base-page.component.ts | 23 +++- .../document-base-page.spec.ts | 95 +++++++++++++- .../navigation-history.service.spec.ts | 122 ++++++++++++++++++ .../services/navigation-history.service.ts | 53 ++++++++ projects/aca-shared/src/public-api.ts | 1 + .../store/src/actions/search-ai.actions.ts | 2 +- 26 files changed, 456 insertions(+), 122 deletions(-) create mode 100644 projects/aca-shared/src/lib/services/navigation-history.service.spec.ts create mode 100644 projects/aca-shared/src/lib/services/navigation-history.service.ts diff --git a/projects/aca-content/assets/i18n/en.json b/projects/aca-content/assets/i18n/en.json index 5247c9700e..cc02939813 100644 --- a/projects/aca-content/assets/i18n/en.json +++ b/projects/aca-content/assets/i18n/en.json @@ -626,7 +626,8 @@ "SEARCH_INPUT": { "ASK_BUTTON_LABEL": "Ask", "DEFAULT_PLACEHOLDER": "Please ask your question with as much detail as possible...", - "HIDE_INPUT": "Hide input" + "HIDE_INPUT": "Hide input", + "HIDE_ANSWER": "Hide answer" }, "ERRORS": { "AGENTS_FETCHING": "Error while fetching agents.", diff --git a/projects/aca-content/src/lib/components/favorite-libraries/favorite-libraries.component.html b/projects/aca-content/src/lib/components/favorite-libraries/favorite-libraries.component.html index f927b311cf..5721fac44f 100644 --- a/projects/aca-content/src/lib/components/favorite-libraries/favorite-libraries.component.html +++ b/projects/aca-content/src/lib/components/favorite-libraries/favorite-libraries.component.html @@ -20,6 +20,7 @@

[sorting]="['title', 'asc']" [sortingMode]="'client'" [displayCheckboxesOnHover]="true" + [preselectNodes]="selectedNodesState?.nodes" (node-dblclick)="handleNodeClick($event)" [imageResolver]="imageResolver" (selectedItemsCountChanged)="onSelectedItemsCountChanged($event)" diff --git a/projects/aca-content/src/lib/components/favorites/favorites.component.html b/projects/aca-content/src/lib/components/favorites/favorites.component.html index 75190109ee..03d10e364f 100644 --- a/projects/aca-content/src/lib/components/favorites/favorites.component.html +++ b/projects/aca-content/src/lib/components/favorites/favorites.component.html @@ -25,6 +25,7 @@

[multiselect]="true" [navigate]="false" [sorting]="['modifiedAt', 'desc']" + [preselectNodes]="selectedNodesState?.nodes" [sortingMode]="'client'" [imageResolver]="imageResolver" [displayCheckboxesOnHover]="true" diff --git a/projects/aca-content/src/lib/components/files/files.component.html b/projects/aca-content/src/lib/components/files/files.component.html index 35a5d0494b..c0eb9995c7 100644 --- a/projects/aca-content/src/lib/components/files/files.component.html +++ b/projects/aca-content/src/lib/components/files/files.component.html @@ -36,6 +36,7 @@ [node]="nodeResult" [allowDropFiles]="true" [displayCheckboxesOnHover]="true" + [preselectNodes]="selectedNodesState?.nodes" [navigate]="false" [sorting]="['name', 'asc']" [imageResolver]="imageResolver" diff --git a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.html b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.html index 526f6ab3c0..0ca1d4b14c 100644 --- a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.html +++ b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.html @@ -1,5 +1,5 @@ @@ -12,7 +12,7 @@ mat-icon-button (click)="leaveSearchInput()" data-automation-id="aca-search-ai-input-container-leaving-search-button" - [title]="'KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.HIDE_INPUT' | translate" + [title]="(isKnowledgeRetrievalPage ? 'KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.HIDE_ANSWER' : 'KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.HIDE_INPUT') | translate" class="aca-search-ai-input-container-close"> close diff --git a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.spec.ts b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.spec.ts index e09f40770a..caca479e5b 100644 --- a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.spec.ts +++ b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.spec.ts @@ -34,21 +34,40 @@ import { DebugElement } from '@angular/core'; import { MatIconButton } from '@angular/material/button'; import { MatIcon } from '@angular/material/icon'; import { SearchAiNavigationService } from '../../../../services/search-ai-navigation.service'; -import { NavigationEnd, NavigationStart, Router, RouterEvent } from '@angular/router'; +import { NavigationEnd, Router, RouterEvent } from '@angular/router'; import { getAppSelection } from '@alfresco/aca-shared/store'; describe('SearchAiInputContainerComponent', () => { + const routingEvents$: Subject = new Subject(); + let component: SearchAiInputContainerComponent; let fixture: ComponentFixture; - let routingEvents$: Subject; let searchAiService: SearchAiService; let store: MockStore; + let mockSearchAiService: jasmine.SpyObj; + let searchNavigationService: SearchAiNavigationService; + let mockRouter: any; beforeEach(() => { + mockSearchAiService = jasmine.createSpyObj('SearchAiService', ['updateSearchAiInputState'], { + toggleSearchAiInput$: of(true) + }); + + mockRouter = { + url: '/some-url', + events: routingEvents$.asObservable(), + routerState: { + root: {} + }, + snapshot: {} + }; + TestBed.configureTestingModule({ imports: [SearchAiInputContainerComponent, ContentTestingModule], providers: [ + { provide: Router, useValue: mockRouter }, provideMockStore(), + { provide: SearchAiService, useValue: mockSearchAiService }, { provide: AgentService, useValue: { @@ -70,6 +89,7 @@ describe('SearchAiInputContainerComponent', () => { component = fixture.componentInstance; store = TestBed.inject(MockStore); searchAiService = TestBed.inject(SearchAiService); + searchNavigationService = TestBed.inject(SearchAiNavigationService); store.overrideSelector(getAppSelection, { nodes: [], isEmpty: true, @@ -77,8 +97,6 @@ describe('SearchAiInputContainerComponent', () => { libraries: [] }); component.agentId = '1'; - routingEvents$ = new Subject(); - spyOnProperty(TestBed.inject(Router), 'events').and.returnValue(routingEvents$); fixture.detectChanges(); }); @@ -115,13 +133,10 @@ describe('SearchAiInputContainerComponent', () => { expect(inputComponent.useStoredNodes).toBeTrue(); }); - it('should call updateSearchAiInputState on SearchAiService when triggered searchSubmitted event', () => { - spyOn(searchAiService, 'updateSearchAiInputState'); - inputComponent.searchSubmitted.emit(); + it('should set inputState$ to toggleSearchAiInput$ from the service on ngOnInit', () => { + component.ngOnInit(); - expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({ - active: false - }); + expect(component.inputState$).toBe(mockSearchAiService.toggleSearchAiInput$); }); }); @@ -140,44 +155,35 @@ describe('SearchAiInputContainerComponent', () => { button = fixture.debugElement.query(By.directive(MatIconButton)); }); - it('should have correct title', () => { + it('should have correct title when page is not knowledge-retrieval', () => { + mockRouter.url = '/other-page'; + + component.ngOnInit(); + expect(button.nativeElement.title).toBe('KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.HIDE_INPUT'); }); - it('should contain close icon', () => { - expect(button.query(By.directive(MatIcon)).nativeElement.textContent).toBe('close'); - }); + it('should have correct title when page is knowledge-retrieval', () => { + mockRouter.url = '/knowledge-retrieval/some-data'; - it('should call updateSearchAiInputState on SearchAiService when clicked', () => { - spyOn(searchAiService, 'updateSearchAiInputState'); - button.nativeElement.click(); + component.ngOnInit(); + fixture.detectChanges(); - expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({ - active: false - }); + expect(button.nativeElement.title).toBe('KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.HIDE_ANSWER'); }); - it('should call navigateToPreviousRoute on SearchAiNavigationService when clicked', () => { - const searchNavigationService = TestBed.inject(SearchAiNavigationService); - spyOn(searchNavigationService, 'navigateToPreviousRoute'); - button.nativeElement.click(); - - expect(searchNavigationService.navigateToPreviousRoute).toHaveBeenCalled(); + it('should contain close icon', () => { + expect(button.query(By.directive(MatIcon)).nativeElement.textContent).toBe('close'); }); - }); - describe('Navigation', () => { - it('should call updateSearchAiInputState on SearchAiService when navigation starts', () => { - spyOn(searchAiService, 'updateSearchAiInputState'); - routingEvents$.next(new NavigationStart(1, '')); + it('should call navigateToPreviousRoute on SearchAiService when clicked', () => { + spyOn(searchNavigationService, 'navigateToPreviousRouteOrCloseInput'); + button.nativeElement.click(); - expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({ - active: false - }); + expect(searchNavigationService.navigateToPreviousRouteOrCloseInput).toHaveBeenCalled(); }); it('should not call updateSearchAiInputState on SearchAiService when there is different event than navigation starts', () => { - spyOn(searchAiService, 'updateSearchAiInputState'); routingEvents$.next(new NavigationEnd(1, '', '')); expect(searchAiService.updateSearchAiInputState).not.toHaveBeenCalled(); diff --git a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.ts b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.ts index af5fa2a162..ffba9d9606 100644 --- a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.ts +++ b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input-container/search-ai-input-container.component.ts @@ -22,27 +22,27 @@ * from Hyland Software. If not, see . */ -import { Component, Input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; +import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { SearchAiInputComponent } from '../search-ai-input/search-ai-input.component'; import { MatDividerModule } from '@angular/material/divider'; import { SearchAiNavigationService } from '../../../../services/search-ai-navigation.service'; -import { NavigationStart, Router } from '@angular/router'; -import { filter, takeUntil } from 'rxjs/operators'; -import { SearchAiService } from '@alfresco/adf-content-services'; +import { SearchAiService, SearchAiInputState } from '@alfresco/adf-content-services'; import { TranslateModule } from '@ngx-translate/core'; -import { Subject } from 'rxjs'; +import { Observable } from 'rxjs'; +import { AsyncPipe } from '@angular/common'; +import { Router } from '@angular/router'; @Component({ standalone: true, - imports: [SearchAiInputComponent, MatIconModule, MatDividerModule, MatButtonModule, TranslateModule], + imports: [SearchAiInputComponent, MatIconModule, MatDividerModule, MatButtonModule, TranslateModule, AsyncPipe], selector: 'aca-search-ai-input-container', templateUrl: './search-ai-input-container.component.html', styleUrls: ['./search-ai-input-container.component.scss'], encapsulation: ViewEncapsulation.None }) -export class SearchAiInputContainerComponent implements OnInit, OnDestroy { +export class SearchAiInputContainerComponent implements OnInit { @Input() placeholder = 'KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.DEFAULT_PLACEHOLDER'; @Input() @@ -50,32 +50,17 @@ export class SearchAiInputContainerComponent implements OnInit, OnDestroy { @Input() useStoredNodes: boolean; - private onDestroy$ = new Subject(); + inputState$: Observable; + isKnowledgeRetrievalPage = false; constructor(private searchAiService: SearchAiService, private searchNavigationService: SearchAiNavigationService, private router: Router) {} ngOnInit(): void { - this.router.events - .pipe( - filter((event) => event instanceof NavigationStart), - takeUntil(this.onDestroy$) - ) - .subscribe(() => this.hideSearchInput()); - } - - ngOnDestroy(): void { - this.onDestroy$.next(); - this.onDestroy$.complete(); - } - - hideSearchInput(): void { - this.searchAiService.updateSearchAiInputState({ - active: false - }); + this.isKnowledgeRetrievalPage = this.router.url.startsWith('/knowledge-retrieval'); + this.inputState$ = this.searchAiService.toggleSearchAiInput$; } leaveSearchInput(): void { - this.searchNavigationService.navigateToPreviousRoute(); - this.hideSearchInput(); + this.searchNavigationService.navigateToPreviousRouteOrCloseInput(); } } diff --git a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.spec.ts b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.spec.ts index 7434043a73..8a6ece244f 100644 --- a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.spec.ts +++ b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.spec.ts @@ -28,7 +28,7 @@ import { MatSelect, MatSelectModule } from '@angular/material/select'; import { By } from '@angular/platform-browser'; import { MockStore, provideMockStore } from '@ngrx/store/testing'; import { AgentService, ContentTestingModule, SearchAiService } from '@alfresco/adf-content-services'; -import { getAppSelection, SearchByTermAiAction } from '@alfresco/aca-shared/store'; +import { getAppSelection, SearchByTermAiAction, ToggleAISearchInput } from '@alfresco/aca-shared/store'; import { of, Subject } from 'rxjs'; import { Agent, NodeEntry } from '@alfresco/js-api'; import { FormControlDirective } from '@angular/forms'; @@ -70,6 +70,7 @@ describe('SearchAiInputComponent', () => { let store: MockStore; let agents$: Subject; let dialog: MatDialog; + let activatedRoute: ActivatedRoute; const prepareBeforeTest = (): void => { selectionState = { @@ -101,6 +102,7 @@ describe('SearchAiInputComponent', () => { ] }); + activatedRoute = TestBed.inject(ActivatedRoute); fixture = TestBed.createComponent(SearchAiInputComponent); component = fixture.componentInstance; store = TestBed.inject(MockStore); @@ -133,6 +135,23 @@ describe('SearchAiInputComponent', () => { expect(selectElement.componentInstance.hideSingleSelectionIndicator).toBeTrue(); }); + it('should set queryControl value to searchTerm if searchTerm is defined', () => { + const query = 'some new query'; + component.searchTerm = query; + + component.ngOnInit(); + + expect(component.queryControl.value).toBe(query); + }); + + it('should set queryControl value to query param if searchTerm is not defined', () => { + component.searchTerm = undefined; + + component.ngOnInit(); + + expect(component.queryControl.value).toBe('some query'); + }); + it('should get agents on init', () => { agents$.next(agentList); component.ngOnInit(); @@ -239,6 +258,11 @@ describe('SearchAiInputComponent', () => { }); it('should be disabled by default', () => { + activatedRoute.snapshot.queryParams = { query: '' }; + + component.ngOnInit(); + fixture.detectChanges(); + expect(submitButton.nativeElement.disabled).toBeTrue(); }); @@ -371,12 +395,13 @@ describe('SearchAiInputComponent', () => { spyOn(store, 'dispatch'); submittingTrigger(); - expect(store.dispatch).toHaveBeenCalledOnceWith( + expect(store.dispatch).toHaveBeenCalledWith( new SearchByTermAiAction({ searchTerm: query, agentId: component.agentId }) ); + expect(store.dispatch).toHaveBeenCalledWith(new ToggleAISearchInput('2', 'some query')); }); it('should call dispatch on store with correct parameter if selected agent was changed', async () => { @@ -388,26 +413,13 @@ describe('SearchAiInputComponent', () => { }); submittingTrigger(); - expect(store.dispatch).toHaveBeenCalledOnceWith( + expect(store.dispatch).toHaveBeenCalledWith( new SearchByTermAiAction({ searchTerm: query, agentId: '1' }) ); - }); - - it('should reset query input', () => { - spyOn(component.queryControl, 'reset'); - submittingTrigger(); - - expect(component.queryControl.reset).toHaveBeenCalled(); - }); - - it('should emit searchSubmitted event', () => { - spyOn(component.searchSubmitted, 'emit'); - submittingTrigger(); - - expect(component.searchSubmitted.emit).toHaveBeenCalled(); + expect(store.dispatch).toHaveBeenCalledWith(new ToggleAISearchInput('1', 'some query')); }); it('should call open modal if there was a previous search phrase in url', () => { @@ -418,13 +430,11 @@ describe('SearchAiInputComponent', () => { it('should open Unsaved Changes Modal and run callback successfully', () => { const modalAiSpy = spyOn(modalAiService, 'openUnsavedChangesModal').and.callThrough(); - spyOn(component.searchSubmitted, 'emit'); fixture.detectChanges(); submittingTrigger(); expect(modalAiSpy).toHaveBeenCalledWith(jasmine.any(Function)); - expect(component.searchSubmitted.emit).toHaveBeenCalled(); }); }); } diff --git a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.ts b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.ts index c5cc146247..125ac0ef92 100644 --- a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.ts +++ b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-input/search-ai-input.component.ts @@ -22,7 +22,7 @@ * from Hyland Software. If not, see . */ -import { Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewEncapsulation } from '@angular/core'; +import { Component, Input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; import { CommonModule } from '@angular/common'; import { TranslateModule, TranslateService } from '@ngx-translate/core'; import { MatButtonModule } from '@angular/material/button'; @@ -34,7 +34,7 @@ import { AvatarComponent, IconComponent, NotificationService, UserPreferencesSer import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms'; import { Subject } from 'rxjs'; import { Store } from '@ngrx/store'; -import { AiSearchByTermPayload, AppStore, getAppSelection, SearchByTermAiAction } from '@alfresco/aca-shared/store'; +import { AiSearchByTermPayload, AppStore, getAppSelection, SearchByTermAiAction, ToggleAISearchInput } from '@alfresco/aca-shared/store'; import { takeUntil } from 'rxjs/operators'; import { SelectionState } from '@alfresco/adf-extensions'; import { MatSelectModule } from '@angular/material/select'; @@ -49,6 +49,7 @@ import { import { ModalAiService } from '../../../../services/modal-ai.service'; import { Agent } from '@alfresco/js-api'; import { getAgentsWithMockedAvatars } from '../search-ai-utils'; +import { ActivatedRoute } from '@angular/router'; const MatTooltipOptions: MatTooltipDefaultOptions = { ...MAT_TOOLTIP_DEFAULT_OPTIONS_FACTORY(), @@ -89,8 +90,8 @@ export class SearchAiInputComponent implements OnInit, OnDestroy { @Input() useStoredNodes: boolean; - @Output() - searchSubmitted = new EventEmitter(); + @Input() + searchTerm: string; private readonly storedNodesKey = 'knowledgeRetrievalNodes'; @@ -126,10 +127,19 @@ export class SearchAiInputComponent implements OnInit, OnDestroy { private agentService: AgentService, private userPreferencesService: UserPreferencesService, private translateService: TranslateService, - private modalAiService: ModalAiService + private modalAiService: ModalAiService, + private route: ActivatedRoute ) {} ngOnInit(): void { + if (this.searchTerm) { + this.queryControl.setValue(this.searchTerm); + } else if (this.route.snapshot?.queryParams?.query?.length > 0) { + this.queryControl.setValue(this.route.snapshot.queryParams.query); + } else { + this.queryControl.setValue(null); + } + if (!this.useStoredNodes) { this.store .select(getAppSelection) @@ -180,8 +190,7 @@ export class SearchAiInputComponent implements OnInit, OnDestroy { }; this.userPreferencesService.set(this.storedNodesKey, JSON.stringify(this.selectedNodesState)); this.store.dispatch(new SearchByTermAiAction(payload)); - this.queryControl.reset(); - this.searchSubmitted.emit(); + this.store.dispatch(new ToggleAISearchInput(this.agentControl.value.id, this.queryControl.value)); } } } diff --git a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-results/search-ai-results.component.ts b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-results/search-ai-results.component.ts index 60bb0017b3..c0a027f2e4 100644 --- a/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-results/search-ai-results.component.ts +++ b/projects/aca-content/src/lib/components/knowledge-retrieval/search-ai/search-ai-results/search-ai-results.component.ts @@ -26,15 +26,7 @@ import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { PageComponent, PageLayoutComponent, ToolbarActionComponent, ToolbarComponent } from '@alfresco/aca-shared'; import { concatMap, delay, filter, finalize, retryWhen, skipWhile, switchMap, takeUntil } from 'rxjs/operators'; -import { - AvatarComponent, - ClipboardService, - EmptyContentComponent, - ThumbnailService, - ToolbarModule, - UnsavedChangesGuard, - UserPreferencesService -} from '@alfresco/adf-core'; +import { AvatarComponent, ClipboardService, EmptyContentComponent, ThumbnailService, ToolbarModule, UnsavedChangesGuard } from '@alfresco/adf-core'; import { AiAnswer, Node } from '@alfresco/js-api'; import { CommonModule } from '@angular/common'; import { SearchAiInputContainerComponent } from '../search-ai-input-container/search-ai-input-container.component'; @@ -124,7 +116,6 @@ export class SearchAiResultsComponent extends PageComponent implements OnInit, O private clipboardService: ClipboardService, private thumbnailService: ThumbnailService, private nodesApiService: NodesApiService, - private userPreferencesService: UserPreferencesService, private translateService: TranslateService, private unsavedChangesGuard: UnsavedChangesGuard, private modalAiService: ModalAiService, diff --git a/projects/aca-content/src/lib/components/libraries/libraries.component.html b/projects/aca-content/src/lib/components/libraries/libraries.component.html index bc9f484e1b..a4d5433959 100644 --- a/projects/aca-content/src/lib/components/libraries/libraries.component.html +++ b/projects/aca-content/src/lib/components/libraries/libraries.component.html @@ -18,6 +18,7 @@

[navigate]="false" [sorting]="['title', 'asc']" [sortingMode]="'client'" + [preselectNodes]="selectedNodesState?.nodes" [imageResolver]="imageResolver" [displayCheckboxesOnHover]="true" [isResizingEnabled]="true" diff --git a/projects/aca-content/src/lib/components/recent-files/recent-files.component.html b/projects/aca-content/src/lib/components/recent-files/recent-files.component.html index 2a2a84fb83..f064cb680a 100644 --- a/projects/aca-content/src/lib/components/recent-files/recent-files.component.html +++ b/projects/aca-content/src/lib/components/recent-files/recent-files.component.html @@ -25,6 +25,7 @@

[multiselect]="true" [navigate]="false" [sorting]="['modifiedAt', 'desc']" + [preselectNodes]="selectedNodesState?.nodes" [sortingMode]="'client'" [imageResolver]="imageResolver" [isResizingEnabled]="true" diff --git a/projects/aca-content/src/lib/components/search/search-results/search-results.component.html b/projects/aca-content/src/lib/components/search/search-results/search-results.component.html index d2b32cba2d..2c71186abe 100644 --- a/projects/aca-content/src/lib/components/search/search-results/search-results.component.html +++ b/projects/aca-content/src/lib/components/search/search-results/search-results.component.html @@ -47,6 +47,7 @@ [multiselect]="true" [sortingMode]="'server'" [sorting]="sorting" + [preselectNodes]="selectedNodesState?.nodes" [displayCheckboxesOnHover]="true" [imageResolver]="imageResolver" [isResizingEnabled]="true" diff --git a/projects/aca-content/src/lib/components/shared-files/shared-files.component.html b/projects/aca-content/src/lib/components/shared-files/shared-files.component.html index 33e6fe8b0b..103540cf1d 100644 --- a/projects/aca-content/src/lib/components/shared-files/shared-files.component.html +++ b/projects/aca-content/src/lib/components/shared-files/shared-files.component.html @@ -26,6 +26,7 @@

[multiselect]="true" [sorting]="['modifiedAt', 'desc']" [imageResolver]="imageResolver" + [preselectNodes]="selectedNodesState?.nodes" [sortingMode]="'client'" [isResizingEnabled]="true" [displayCheckboxesOnHover]="true" diff --git a/projects/aca-content/src/lib/components/sidenav/sidenav.component.spec.ts b/projects/aca-content/src/lib/components/sidenav/sidenav.component.spec.ts index 2c4e16c939..0ee8a9d0a0 100644 --- a/projects/aca-content/src/lib/components/sidenav/sidenav.component.spec.ts +++ b/projects/aca-content/src/lib/components/sidenav/sidenav.component.spec.ts @@ -26,17 +26,22 @@ import { NO_ERRORS_SCHEMA } from '@angular/core'; import { TestBed, ComponentFixture } from '@angular/core/testing'; import { SidenavComponent } from './sidenav.component'; import { AppTestingModule } from '../../testing/app-testing.module'; -import { AppExtensionService, AppService } from '@alfresco/aca-shared'; +import { AppExtensionService, AppService, NavigationHistoryService } from '@alfresco/aca-shared'; import { BehaviorSubject, Subject } from 'rxjs'; import { SidenavLayoutComponent } from '@alfresco/adf-core'; +import { NavigationEnd } from '@angular/router'; describe('SidenavComponent', () => { let fixture: ComponentFixture; let component: SidenavComponent; let extensionService: AppExtensionService; let sidenavLayoutComponent: SidenavLayoutComponent; + let navigationHistoryService: jasmine.SpyObj; + let routerEvents$: Subject; beforeEach(() => { + const navigationHistoryServiceSpy = jasmine.createSpyObj('NavigationHistoryService', ['listenToRouteChanges', 'setHistory']); + TestBed.configureTestingModule({ imports: [AppTestingModule, SidenavComponent], providers: [ @@ -48,6 +53,7 @@ describe('SidenavComponent', () => { setAppNavbarMode: jasmine.createSpy('setAppNavbarMode') } }, + { provide: NavigationHistoryService, useValue: navigationHistoryServiceSpy }, SidenavLayoutComponent ], schemas: [NO_ERRORS_SCHEMA] @@ -73,6 +79,11 @@ describe('SidenavComponent', () => { component.data = { layout: sidenavLayoutComponent }; + + navigationHistoryService = TestBed.inject(NavigationHistoryService) as jasmine.SpyObj; + + routerEvents$ = new Subject(); + navigationHistoryService.listenToRouteChanges.and.returnValue(routerEvents$.asObservable()); }); it('should set the sidenav data', async () => { @@ -89,4 +100,13 @@ describe('SidenavComponent', () => { title: 'item-1' }); }); + + it('should call setHistory when a NavigationEnd event occurs', () => { + const mockNavigationEnd = new NavigationEnd(1, '/path', '/redirect'); + + component.ngOnInit(); + routerEvents$.next(mockNavigationEnd); + + expect(navigationHistoryService.setHistory).toHaveBeenCalledWith(mockNavigationEnd, 3); + }); }); diff --git a/projects/aca-content/src/lib/components/sidenav/sidenav.component.ts b/projects/aca-content/src/lib/components/sidenav/sidenav.component.ts index 3a306d5848..9f808fbe60 100755 --- a/projects/aca-content/src/lib/components/sidenav/sidenav.component.ts +++ b/projects/aca-content/src/lib/components/sidenav/sidenav.component.ts @@ -28,12 +28,13 @@ import { Store } from '@ngrx/store'; import { AppStore, getSideNavState } from '@alfresco/aca-shared/store'; import { Subject } from 'rxjs'; import { takeUntil, distinctUntilChanged, debounceTime } from 'rxjs/operators'; -import { AppExtensionService, AppService } from '@alfresco/aca-shared'; +import { AppExtensionService, AppService, NavigationHistoryService } from '@alfresco/aca-shared'; import { SidenavLayoutComponent } from '@alfresco/adf-core'; import { CommonModule } from '@angular/common'; import { SidenavHeaderComponent } from './components/sidenav-header.component'; import { MatListModule } from '@angular/material/list'; import { ExpandMenuComponent } from './components/expand-menu.component'; +import { NavigationEnd } from '@angular/router'; @Component({ standalone: true, @@ -54,7 +55,12 @@ export class SidenavComponent implements OnInit, OnDestroy { groups: Array = []; private onDestroy$ = new Subject(); - constructor(private store: Store, private extensions: AppExtensionService, private appService: AppService) {} + constructor( + private store: Store, + private extensions: AppExtensionService, + private appService: AppService, + private navigationHistoryService: NavigationHistoryService + ) {} ngOnInit() { this.store @@ -67,6 +73,12 @@ export class SidenavComponent implements OnInit, OnDestroy { this.appService.setAppNavbarMode(this.data.mode); this.appService.toggleAppNavBar$.pipe(takeUntil(this.onDestroy$)).subscribe(() => this.toggleNavBar()); this.data.layout.expanded.pipe(takeUntil(this.onDestroy$)).subscribe(() => this.setNavBarMode()); + this.navigationHistoryService + .listenToRouteChanges() + .pipe(takeUntil(this.onDestroy$)) + .subscribe((event: NavigationEnd) => { + this.navigationHistoryService.setHistory(event, 3); + }); } trackByGroupId(_: number, obj: NavBarGroupRef): string { diff --git a/projects/aca-content/src/lib/components/trashcan/trashcan.component.html b/projects/aca-content/src/lib/components/trashcan/trashcan.component.html index b1d5aba1cb..3a9818cc55 100644 --- a/projects/aca-content/src/lib/components/trashcan/trashcan.component.html +++ b/projects/aca-content/src/lib/components/trashcan/trashcan.component.html @@ -18,6 +18,7 @@

[multiselect]="true" [navigate]="false" [sortingMode]="'client'" + [preselectNodes]="selectedNodesState?.nodes" [imageResolver]="imageResolver" [displayCheckboxesOnHover]="true" (selectedItemsCountChanged)="onSelectedItemsCountChanged($event)" diff --git a/projects/aca-content/src/lib/services/search-ai-navigation.service.spec.ts b/projects/aca-content/src/lib/services/search-ai-navigation.service.spec.ts index 7da4d1c804..7cbcc01cd9 100644 --- a/projects/aca-content/src/lib/services/search-ai-navigation.service.spec.ts +++ b/projects/aca-content/src/lib/services/search-ai-navigation.service.spec.ts @@ -25,11 +25,12 @@ import { SearchAiNavigationService } from './search-ai-navigation.service'; import { Params, Router } from '@angular/router'; import { TestBed } from '@angular/core/testing'; -import { ContentTestingModule } from '@alfresco/adf-content-services'; +import { ContentTestingModule, SearchAiService } from '@alfresco/adf-content-services'; describe('SearchAiNavigationService', () => { let service: SearchAiNavigationService; let router: Router; + let searchAiService: SearchAiService; const knowledgeRetrievalUrl = '/knowledge-retrieval'; @@ -39,6 +40,7 @@ describe('SearchAiNavigationService', () => { }); service = TestBed.inject(SearchAiNavigationService); router = TestBed.inject(Router); + searchAiService = TestBed.inject(SearchAiService); }); describe('navigateToPreviousRoute', () => { @@ -55,16 +57,18 @@ describe('SearchAiNavigationService', () => { it('should navigate to personal files if there is not previous route and actual route is knowledge retrieval', () => { urlSpy.and.returnValue(knowledgeRetrievalUrl); - service.navigateToPreviousRoute(); + service.navigateToPreviousRouteOrCloseInput(); expect(navigateByUrlSpy).toHaveBeenCalledWith(personalFilesUrl); }); - it('should not navigate if there is not previous route and actual route is not knowledge retrieval', () => { + it('should not navigate if there is not previous route and actual route is not knowledge retrieval but should updateSearchAiInputState', () => { + spyOn(searchAiService, 'updateSearchAiInputState'); urlSpy.and.returnValue('/some-url'); - service.navigateToPreviousRoute(); + service.navigateToPreviousRouteOrCloseInput(); expect(navigateByUrlSpy).not.toHaveBeenCalled(); + expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({ active: false }); }); it('should navigate to previous route if there is some previous route and actual route is knowledge retrieval', () => { @@ -74,7 +78,7 @@ describe('SearchAiNavigationService', () => { }); urlSpy.and.returnValue(knowledgeRetrievalUrl); navigateByUrlSpy.calls.reset(); - service.navigateToPreviousRoute(); + service.navigateToPreviousRouteOrCloseInput(); expect(navigateByUrlSpy).toHaveBeenCalledWith(sourceUrl); }); @@ -86,7 +90,7 @@ describe('SearchAiNavigationService', () => { }); urlSpy.and.returnValue('/some-different-url'); navigateByUrlSpy.calls.reset(); - service.navigateToPreviousRoute(); + service.navigateToPreviousRouteOrCloseInput(); expect(navigateByUrlSpy).not.toHaveBeenCalled(); }); @@ -97,7 +101,7 @@ describe('SearchAiNavigationService', () => { agentId: 'some agent id' }); navigateByUrlSpy.calls.reset(); - service.navigateToPreviousRoute(); + service.navigateToPreviousRouteOrCloseInput(); expect(navigateByUrlSpy).toHaveBeenCalledWith(personalFilesUrl); }); @@ -109,7 +113,7 @@ describe('SearchAiNavigationService', () => { }); urlSpy.and.returnValue(sourceUrl); navigateByUrlSpy.calls.reset(); - service.navigateToPreviousRoute(); + service.navigateToPreviousRouteOrCloseInput(); expect(navigateByUrlSpy).not.toHaveBeenCalled(); }); diff --git a/projects/aca-content/src/lib/services/search-ai-navigation.service.ts b/projects/aca-content/src/lib/services/search-ai-navigation.service.ts index 6bc745e3ca..d3508d5b7a 100644 --- a/projects/aca-content/src/lib/services/search-ai-navigation.service.ts +++ b/projects/aca-content/src/lib/services/search-ai-navigation.service.ts @@ -24,6 +24,7 @@ import { Injectable } from '@angular/core'; import { Params, Router } from '@angular/router'; +import { SearchAiService } from '@alfresco/adf-content-services'; @Injectable({ providedIn: 'root' }) export class SearchAiNavigationService { @@ -31,11 +32,15 @@ export class SearchAiNavigationService { private previousRoute = ''; - constructor(private router: Router) {} + constructor(private router: Router, private searchAiService: SearchAiService) {} - navigateToPreviousRoute(): void { + navigateToPreviousRouteOrCloseInput(): void { if (this.router.url.includes(this.knowledgeRetrievalRoute)) { void this.router.navigateByUrl(this.previousRoute || '/personal-files'); + } else { + this.searchAiService.updateSearchAiInputState({ + active: false + }); } } diff --git a/projects/aca-content/src/lib/store/effects/search-ai.effects.ts b/projects/aca-content/src/lib/store/effects/search-ai.effects.ts index 4670b8a77a..eebc5f8620 100644 --- a/projects/aca-content/src/lib/store/effects/search-ai.effects.ts +++ b/projects/aca-content/src/lib/store/effects/search-ai.effects.ts @@ -56,7 +56,8 @@ export class SearchAiEffects { map((action) => this.searchAiService.updateSearchAiInputState({ active: true, - selectedAgentId: action.agentId + selectedAgentId: action.agentId, + searchTerm: action.searchTerm }) ) ), diff --git a/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts b/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts index 1e8b881321..7a11b8e53a 100644 --- a/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts +++ b/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts @@ -30,7 +30,7 @@ import { ShareDataRow, UploadService } from '@alfresco/adf-content-services'; -import { ShowHeaderMode } from '@alfresco/adf-core'; +import { ShowHeaderMode, UserPreferencesService } from '@alfresco/adf-core'; import { ContentActionRef, DocumentListPresetRef, SelectionState } from '@alfresco/adf-extensions'; import { OnDestroy, OnInit, OnChanges, ViewChild, SimpleChanges, Directive, inject, HostListener } from '@angular/core'; import { Store } from '@ngrx/store'; @@ -53,6 +53,7 @@ import { AutoDownloadService } from '../../services/auto-download.service'; import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; import { Router } from '@angular/router'; import { AppSettingsService } from '../../services/app-settings.service'; +import { NavigationHistoryService } from '../../services/navigation-history.service'; /* eslint-disable @angular-eslint/directive-class-suffix */ @Directive() @@ -77,6 +78,7 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges { createActions: ContentActionRef[] = []; isSmallScreen = false; selectedRowItemsCount = 0; + selectedNodesState: SelectionState; protected documentListService = inject(DocumentListService); protected settings = inject(AppSettingsService); @@ -86,9 +88,11 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges { protected breakpointObserver = inject(BreakpointObserver); protected uploadService = inject(UploadService); protected router = inject(Router); + protected userPreferencesService = inject(UserPreferencesService); + protected searchAiService = inject(SearchAiService); private autoDownloadService = inject(AutoDownloadService, { optional: true }); + private navigationHistoryService = inject(NavigationHistoryService); - protected searchAiService: SearchAiService = inject(SearchAiService); protected subscriptions: Subscription[] = []; private _searchAiInputState: SearchAiInputState = { @@ -155,6 +159,8 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges { this.searchAiService.toggleSearchAiInput$ .pipe(takeUntil(this.onDestroy$)) .subscribe((searchAiInputState) => (this._searchAiInputState = searchAiInputState)); + + this.setKnowledgeRetrievalState(); } ngOnChanges(changes: SimpleChanges) { @@ -233,6 +239,19 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges { return obj.id; } + private setKnowledgeRetrievalState() { + const nodes = this.userPreferencesService.get('knowledgeRetrievalNodes'); + if (nodes && this.navigationHistoryService.shouldReturnLastSelection('/knowledge-retrieval')) { + this.selectedNodesState = JSON.parse(nodes); + } + + if (!this.selectedNodesState && !this.router.url.startsWith('/knowledge-retrieval')) { + this.searchAiService.updateSearchAiInputState({ + active: false + }); + } + } + private isOutletPreviewUrl(): boolean { return location.href.includes('viewer:view'); } diff --git a/projects/aca-shared/src/lib/components/document-base-page/document-base-page.spec.ts b/projects/aca-shared/src/lib/components/document-base-page/document-base-page.spec.ts index 01bc24ca0d..8d6264154b 100644 --- a/projects/aca-shared/src/lib/components/document-base-page/document-base-page.spec.ts +++ b/projects/aca-shared/src/lib/components/document-base-page/document-base-page.spec.ts @@ -30,11 +30,13 @@ import { NodeEntry, NodePaging } from '@alfresco/js-api'; import { DocumentBasePageService } from './document-base-page.service'; import { Store } from '@ngrx/store'; import { Component } from '@angular/core'; -import { DiscoveryApiService, DocumentListComponent, DocumentListService } from '@alfresco/adf-content-services'; +import { DiscoveryApiService, DocumentListComponent, DocumentListService, SearchAiService } from '@alfresco/adf-content-services'; import { MockStore, provideMockStore } from '@ngrx/store/testing'; -import { AuthModule } from '@alfresco/adf-core'; -import { Subscription } from 'rxjs'; +import { AuthModule, UserPreferencesService } from '@alfresco/adf-core'; +import { of, Subscription } from 'rxjs'; import { MatDialogModule } from '@angular/material/dialog'; +import { NavigationHistoryService } from '../../services/navigation-history.service'; +import { Router } from '@angular/router'; @Component({ selector: 'aca-test', @@ -53,23 +55,49 @@ class TestComponent extends PageComponent { } describe('PageComponent', () => { + const mockNodes = JSON.stringify({ node: 'mockNode' }); + let component: TestComponent; let store: Store; let fixture: ComponentFixture; let documentListService: DocumentListService; + let userPreferencesService: jasmine.SpyObj; + let navigationHistoryService: { shouldReturnLastSelection: jasmine.Spy }; + let searchAiService: SearchAiService; + let router: { url: string }; beforeEach(() => { + userPreferencesService = jasmine.createSpyObj('UserPreferencesService', ['get', 'set']); + navigationHistoryService = jasmine.createSpyObj('NavigationHistoryService', ['shouldReturnLastSelection']); + router = { url: '/some-url' }; + searchAiService = jasmine.createSpyObj('SearchAiService', ['updateSearchAiInputState', 'toggleSearchAiInput$']); + searchAiService.toggleSearchAiInput$ = of({ active: false }); + TestBed.configureTestingModule({ imports: [LibTestingModule, AuthModule.forRoot(), MatDialogModule], declarations: [TestComponent], providers: [ { provide: DocumentBasePageService, useClass: DocumentBasePageServiceMock }, { provide: DiscoveryApiService, useValue: discoveryApiServiceMockValue }, - AppExtensionService + AppExtensionService, + { + provide: UserPreferencesService, + useValue: userPreferencesService + }, + { + provide: NavigationHistoryService, + useValue: navigationHistoryService + }, + { + provide: Router, + useValue: router + }, + { provide: SearchAiService, useValue: searchAiService } ] }); store = TestBed.inject(Store); + searchAiService = TestBed.inject(SearchAiService); documentListService = TestBed.inject(DocumentListService); fixture = TestBed.createComponent(TestComponent); component = fixture.componentInstance; @@ -191,6 +219,65 @@ describe('PageComponent', () => { expect(store.dispatch).toHaveBeenCalledWith(new ViewNodeAction(id)); }); }); + + describe('setKnowledgeRetrievalState()', () => { + it('should set selectedNodesState when nodes exist and last selection is valid', () => { + userPreferencesService.get.and.returnValue(mockNodes); + navigationHistoryService.shouldReturnLastSelection.and.returnValue(true); + + component.ngOnInit(); + + expect(component.selectedNodesState).toEqual(JSON.parse(mockNodes)); + }); + + it('should not set selectedNodesState when nodes do not exist', () => { + userPreferencesService.get.and.returnValue(null); + navigationHistoryService.shouldReturnLastSelection.and.returnValue(true); + + component.ngOnInit(); + + expect(component.selectedNodesState).toBeUndefined(); + }); + + it('should not set selectedNodesState when shouldReturnLastSelection returns false', () => { + userPreferencesService.get.and.returnValue(mockNodes); + navigationHistoryService.shouldReturnLastSelection.and.returnValue(false); + + component.ngOnInit(); + + expect(component.selectedNodesState).toBeUndefined(); + }); + + it('should update searchAiInputState when selectedNodesState is undefined and url does not start with /knowledge-retrieval', () => { + userPreferencesService.get.and.returnValue(mockNodes); + navigationHistoryService.shouldReturnLastSelection.and.returnValue(false); + router.url = '/some-other-url'; + + component.ngOnInit(); + + expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({ active: false }); + }); + + it('should not update searchAiInputState when url starts with /knowledge-retrieval', () => { + userPreferencesService.get.and.returnValue(undefined); + navigationHistoryService.shouldReturnLastSelection.and.returnValue(true); + router.url = '/knowledge-retrieval'; + + component.ngOnInit(); + + expect(searchAiService.updateSearchAiInputState).not.toHaveBeenCalled(); + }); + + it('should not update searchAiInputState when selectedNodesState in not null', () => { + userPreferencesService.get.and.returnValue(mockNodes); + navigationHistoryService.shouldReturnLastSelection.and.returnValue(true); + router.url = '/other'; + + component.ngOnInit(); + + expect(searchAiService.updateSearchAiInputState).not.toHaveBeenCalled(); + }); + }); }); describe('Info Drawer state', () => { diff --git a/projects/aca-shared/src/lib/services/navigation-history.service.spec.ts b/projects/aca-shared/src/lib/services/navigation-history.service.spec.ts new file mode 100644 index 0000000000..4f2f239cb9 --- /dev/null +++ b/projects/aca-shared/src/lib/services/navigation-history.service.spec.ts @@ -0,0 +1,122 @@ +/*! + * Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Alfresco Example Content Application + * + * This file is part of the Alfresco Example Content Application. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * The Alfresco Example Content Application is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Alfresco Example Content Application is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * from Hyland Software. If not, see . + */ + +import { NavigationEnd, NavigationStart, Router } from '@angular/router'; +import { TestBed } from '@angular/core/testing'; +import { NavigationHistoryService } from './navigation-history.service'; +import { Subject } from 'rxjs'; + +describe('NavigationHistoryService', () => { + let service: NavigationHistoryService; + let routerEvents$: Subject; + + const triggerNavigationEnd = (id: number, url: string) => { + routerEvents$.next(new NavigationEnd(id, url, url)); + }; + + beforeEach(() => { + routerEvents$ = new Subject(); + TestBed.configureTestingModule({ + providers: [NavigationHistoryService, { provide: Router, useValue: { events: routerEvents$.asObservable(), url: '/initial' } }] + }); + + service = TestBed.inject(NavigationHistoryService); + TestBed.inject(Router); + }); + + it('should store route changes in history', () => { + service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 3)); + triggerNavigationEnd(1, '/page1'); + triggerNavigationEnd(2, '/page2'); + + expect(service.history).toEqual(['/initial', '/page1', '/page2']); + }); + + it('should not exceed the max history length', () => { + service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 6)); + + triggerNavigationEnd(1, '/page1'); + triggerNavigationEnd(2, '/page2'); + triggerNavigationEnd(3, '/page3'); + triggerNavigationEnd(4, '/page4'); + triggerNavigationEnd(5, '/page5'); + triggerNavigationEnd(6, '/page6'); + + expect(service.history).toEqual(['/page1', '/page2', '/page3', '/page4', '/page5', '/page6']); + }); + + it('should store different route changes in history', () => { + service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 4)); + triggerNavigationEnd(1, '/page1'); + triggerNavigationEnd(2, '/page2'); + triggerNavigationEnd(3, '/page1'); + triggerNavigationEnd(4, '/page2'); + triggerNavigationEnd(5, '/page4'); + triggerNavigationEnd(6, '/page2'); + triggerNavigationEnd(7, '/page1'); + triggerNavigationEnd(8, '/page2'); + + expect(service.history).toEqual(['/page4', '/page2', '/page1', '/page2']); + }); + + it('should return true for a valid last selection', () => { + service.history = ['/page1', '/page2', '/page1']; + + expect(service.shouldReturnLastSelection('/page2')).toBeTrue(); + }); + + it('should return false for an invalid last selection', () => { + service.history = ['/page1', '/page3', '/page1']; + + expect(service.shouldReturnLastSelection('/page2')).toBeFalse(); + }); + + it('should initialize history with the current route', () => { + service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 3)); + expect(service.history).toEqual(['/initial']); + }); + + it('should only store NavigationEnd events in history', () => { + service.listenToRouteChanges().subscribe((event) => service.setHistory(event, 3)); + routerEvents$.next(new NavigationStart(1, '/page-start')); + routerEvents$.next(new NavigationEnd(1, '/page1', '/page1')); + + expect(service.history).toEqual(['/initial', '/page1']); + }); + + it('should return false if second-to-last URL does not match in shouldReturnLastSelection', () => { + service.history = ['/page1', '/page2', '/page3']; + expect(service.shouldReturnLastSelection('/page1')).toBeFalse(); + }); + + it('should return false if first and third URL are not equal', () => { + service.history = ['/page1', '/page2', '/page3']; + expect(service.shouldReturnLastSelection('/page2')).toBeFalse(); + }); + + it('should return false if the current URL does not match the last two URLs', () => { + service.history = ['/page1', '/page4', '/page1']; + expect(service.shouldReturnLastSelection('/page3')).toBeFalse(); + }); +}); diff --git a/projects/aca-shared/src/lib/services/navigation-history.service.ts b/projects/aca-shared/src/lib/services/navigation-history.service.ts new file mode 100644 index 0000000000..2d179d0b14 --- /dev/null +++ b/projects/aca-shared/src/lib/services/navigation-history.service.ts @@ -0,0 +1,53 @@ +/*! + * Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Alfresco Example Content Application + * + * This file is part of the Alfresco Example Content Application. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * The Alfresco Example Content Application is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Alfresco Example Content Application is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * from Hyland Software. If not, see . + */ + +import { Injectable } from '@angular/core'; +import { NavigationEnd, Router } from '@angular/router'; +import { filter, startWith } from 'rxjs/operators'; +import { Observable } from 'rxjs'; + +@Injectable({ providedIn: 'root' }) +export class NavigationHistoryService { + history: string[] = []; + + constructor(private router: Router) {} + + listenToRouteChanges(): Observable { + return this.router.events.pipe( + startWith(new NavigationEnd(0, this.router.url, this.router.url)), + filter((event: NavigationEnd) => event instanceof NavigationEnd) + ); + } + + shouldReturnLastSelection(url: string): boolean { + return this.history.length > 2 && this.history[1].startsWith(url) && this.history[0] === this.history[2]; + } + + setHistory(event: NavigationEnd, maxHistoryLength: number) { + this.history.push(event.urlAfterRedirects); + if (maxHistoryLength > 0 && this.history.length > maxHistoryLength) { + this.history.shift(); + } + } +} diff --git a/projects/aca-shared/src/public-api.ts b/projects/aca-shared/src/public-api.ts index 6f8d4aa336..69623f3a99 100644 --- a/projects/aca-shared/src/public-api.ts +++ b/projects/aca-shared/src/public-api.ts @@ -58,6 +58,7 @@ export * from './lib/services/app-hook.service'; export * from './lib/services/auto-download.service'; export * from './lib/services/app-settings.service'; export * from './lib/services/user-profile.service'; +export * from './lib/services/navigation-history.service'; export * from './lib/utils/node.utils'; export * from './lib/testing/lib-testing-module'; diff --git a/projects/aca-shared/store/src/actions/search-ai.actions.ts b/projects/aca-shared/store/src/actions/search-ai.actions.ts index e4d54683ee..7eaf564c4e 100644 --- a/projects/aca-shared/store/src/actions/search-ai.actions.ts +++ b/projects/aca-shared/store/src/actions/search-ai.actions.ts @@ -38,5 +38,5 @@ export class SearchByTermAiAction implements Action { export class ToggleAISearchInput implements Action { readonly type = SearchAiActionTypes.ToggleAiSearchInput; - constructor(public agentId: string) {} + constructor(public agentId: string, public searchTerm?: string) {} }