[ACS-8779] Keep selections and question after going to the previous page (#4147)

This commit is contained in:
jacekpluta 2024-10-04 14:54:09 +02:00 committed by GitHub
parent 64dfc48468
commit 3ec6d0be32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 457 additions and 123 deletions

View File

@ -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.",

View File

@ -20,6 +20,7 @@
[sorting]="['title', 'asc']"
[sortingMode]="'client'"
[displayCheckboxesOnHover]="true"
[preselectNodes]="selectedNodesState?.nodes"
(node-dblclick)="handleNodeClick($event)"
[imageResolver]="imageResolver"
(selectedItemsCountChanged)="onSelectedItemsCountChanged($event)"

View File

@ -25,6 +25,7 @@
[multiselect]="true"
[navigate]="false"
[sorting]="['modifiedAt', 'desc']"
[preselectNodes]="selectedNodesState?.nodes"
[sortingMode]="'client'"
[imageResolver]="imageResolver"
[displayCheckboxesOnHover]="true"

View File

@ -36,6 +36,7 @@
[node]="nodeResult"
[allowDropFiles]="true"
[displayCheckboxesOnHover]="true"
[preselectNodes]="selectedNodesState?.nodes"
[navigate]="false"
[sorting]="['name', 'asc']"
[imageResolver]="imageResolver"

View File

@ -1,5 +1,5 @@
<aca-search-ai-input
(searchSubmitted)="hideSearchInput()"
[searchTerm]="(inputState$ | async).searchTerm"
[placeholder]="placeholder"
[agentId]="agentId"
[useStoredNodes]="useStoredNodes">
@ -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">
<mat-icon>close</mat-icon>
</button>

View File

@ -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<RouterEvent> = new Subject();
let component: SearchAiInputContainerComponent;
let fixture: ComponentFixture<SearchAiInputContainerComponent>;
let routingEvents$: Subject<RouterEvent>;
let searchAiService: SearchAiService;
let store: MockStore;
let mockSearchAiService: jasmine.SpyObj<SearchAiService>;
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<RouterEvent>();
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 have correct title when page is knowledge-retrieval', () => {
mockRouter.url = '/knowledge-retrieval/some-data';
component.ngOnInit();
fixture.detectChanges();
expect(button.nativeElement.title).toBe('KNOWLEDGE_RETRIEVAL.SEARCH.SEARCH_INPUT.HIDE_ANSWER');
});
it('should contain close icon', () => {
expect(button.query(By.directive(MatIcon)).nativeElement.textContent).toBe('close');
});
it('should call updateSearchAiInputState on SearchAiService when clicked', () => {
spyOn(searchAiService, 'updateSearchAiInputState');
it('should call navigateToPreviousRoute on SearchAiService when clicked', () => {
spyOn(searchNavigationService, 'navigateToPreviousRouteOrCloseInput');
button.nativeElement.click();
expect(searchAiService.updateSearchAiInputState).toHaveBeenCalledWith({
active: false
});
});
it('should call navigateToPreviousRoute on SearchAiNavigationService when clicked', () => {
const searchNavigationService = TestBed.inject(SearchAiNavigationService);
spyOn(searchNavigationService, 'navigateToPreviousRoute');
button.nativeElement.click();
expect(searchNavigationService.navigateToPreviousRoute).toHaveBeenCalled();
});
});
describe('Navigation', () => {
it('should call updateSearchAiInputState on SearchAiService when navigation starts', () => {
spyOn(searchAiService, 'updateSearchAiInputState');
routingEvents$.next(new NavigationStart(1, ''));
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();

View File

@ -22,27 +22,27 @@
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
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<void>();
inputState$: Observable<SearchAiInputState>;
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();
}
}

View File

@ -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<Agent[]>;
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();
});
});
}

View File

@ -22,7 +22,7 @@
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
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<void>();
@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));
}
}
}

View File

@ -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,

View File

@ -18,6 +18,7 @@
[navigate]="false"
[sorting]="['title', 'asc']"
[sortingMode]="'client'"
[preselectNodes]="selectedNodesState?.nodes"
[imageResolver]="imageResolver"
[displayCheckboxesOnHover]="true"
[isResizingEnabled]="true"

View File

@ -25,6 +25,7 @@
[multiselect]="true"
[navigate]="false"
[sorting]="['modifiedAt', 'desc']"
[preselectNodes]="selectedNodesState?.nodes"
[sortingMode]="'client'"
[imageResolver]="imageResolver"
[isResizingEnabled]="true"

View File

@ -47,6 +47,7 @@
[multiselect]="true"
[sortingMode]="'server'"
[sorting]="sorting"
[preselectNodes]="selectedNodesState?.nodes"
[displayCheckboxesOnHover]="true"
[imageResolver]="imageResolver"
[isResizingEnabled]="true"

View File

@ -26,6 +26,7 @@
[multiselect]="true"
[sorting]="['modifiedAt', 'desc']"
[imageResolver]="imageResolver"
[preselectNodes]="selectedNodesState?.nodes"
[sortingMode]="'client'"
[isResizingEnabled]="true"
[displayCheckboxesOnHover]="true"

View File

@ -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<SidenavComponent>;
let component: SidenavComponent;
let extensionService: AppExtensionService;
let sidenavLayoutComponent: SidenavLayoutComponent;
let navigationHistoryService: jasmine.SpyObj<NavigationHistoryService>;
let routerEvents$: Subject<NavigationEnd>;
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<NavigationHistoryService>;
routerEvents$ = new Subject<NavigationEnd>();
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);
});
});

View File

@ -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<NavBarGroupRef> = [];
private onDestroy$ = new Subject<boolean>();
constructor(private store: Store<AppStore>, private extensions: AppExtensionService, private appService: AppService) {}
constructor(
private store: Store<AppStore>,
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 {

View File

@ -18,6 +18,7 @@
[multiselect]="true"
[navigate]="false"
[sortingMode]="'client'"
[preselectNodes]="selectedNodesState?.nodes"
[imageResolver]="imageResolver"
[displayCheckboxesOnHover]="true"
(selectedItemsCountChanged)="onSelectedItemsCountChanged($event)"

View File

@ -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();
});

View File

@ -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
});
}
}

View File

@ -56,7 +56,8 @@ export class SearchAiEffects {
map((action) =>
this.searchAiService.updateSearchAiInputState({
active: true,
selectedAgentId: action.agentId
selectedAgentId: action.agentId,
searchTerm: action.searchTerm
})
)
),

View File

@ -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');
}

View File

@ -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<AppState>;
let fixture: ComponentFixture<TestComponent>;
let documentListService: DocumentListService;
let userPreferencesService: jasmine.SpyObj<UserPreferencesService>;
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', () => {

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<NavigationEnd | NavigationStart>;
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();
});
});

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<NavigationEnd> {
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();
}
}
}

View File

@ -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';

View File

@ -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) {}
}