Browse Source

Refactoring

pull/5656/head
Thomas Kaul 3 months ago
parent
commit
d236476dc9
  1. 40
      libs/ui/src/lib/assistant/assistant.component.ts

40
libs/ui/src/lib/assistant/assistant.component.ts

@ -172,8 +172,6 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
private readonly PRESELECTION_DELAY = 100; private readonly PRESELECTION_DELAY = 100;
private keyManager: FocusKeyManager<GfAssistantListItemComponent>; private keyManager: FocusKeyManager<GfAssistantListItemComponent>;
private preselectionTimeout: ReturnType<typeof setTimeout>;
private unsubscribeSubject = new Subject<void>();
private filterTypes: Filter['type'][] = [ private filterTypes: Filter['type'][] = [
'ACCOUNT', 'ACCOUNT',
@ -183,6 +181,9 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
'TAG' 'TAG'
]; ];
private preselectionTimeout: ReturnType<typeof setTimeout>;
private unsubscribeSubject = new Subject<void>();
public constructor( public constructor(
private adminService: AdminService, private adminService: AdminService,
private changeDetectorRef: ChangeDetectorRef, private changeDetectorRef: ChangeDetectorRef,
@ -348,6 +349,7 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
.subscribe({ .subscribe({
next: (searchResults) => { next: (searchResults) => {
this.searchResults = searchResults; this.searchResults = searchResults;
this.preselectFirstItem(); this.preselectFirstItem();
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
@ -374,14 +376,6 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
}); });
} }
public ngOnDestroy() {
if (this.preselectionTimeout) {
clearTimeout(this.preselectionTimeout);
}
this.unsubscribeSubject.next();
this.unsubscribeSubject.complete();
}
public ngOnChanges() { public ngOnChanges() {
this.accounts = this.user?.accounts ?? []; this.accounts = this.user?.accounts ?? [];
@ -598,27 +592,34 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
this.isOpen = aIsOpen; this.isOpen = aIsOpen;
} }
public ngOnDestroy() {
if (this.preselectionTimeout) {
clearTimeout(this.preselectionTimeout);
}
this.unsubscribeSubject.next();
this.unsubscribeSubject.complete();
}
private getCurrentAssistantListItem() { private getCurrentAssistantListItem() {
return this.assistantListItems.find(({ getHasFocus }) => { return this.assistantListItems.find(({ getHasFocus }) => {
return getHasFocus; return getHasFocus;
}); });
} }
/**
* Gets the first search result item based on priority order:
* Quick Links Accounts Holdings Asset Profiles
*/
private getFirstSearchResultItem() { private getFirstSearchResultItem() {
// Priority order: Quick Links → Accounts → Holdings → Asset Profiles
if (this.searchResults.quickLinks?.length > 0) { if (this.searchResults.quickLinks?.length > 0) {
return this.searchResults.quickLinks[0]; return this.searchResults.quickLinks[0];
} }
if (this.searchResults.accounts?.length > 0) { if (this.searchResults.accounts?.length > 0) {
return this.searchResults.accounts[0]; return this.searchResults.accounts[0];
} }
if (this.searchResults.holdings?.length > 0) { if (this.searchResults.holdings?.length > 0) {
return this.searchResults.holdings[0]; return this.searchResults.holdings[0];
} }
if (this.searchResults.assetProfiles?.length > 0) { if (this.searchResults.assetProfiles?.length > 0) {
return this.searchResults.assetProfiles[0]; return this.searchResults.assetProfiles[0];
} }
@ -626,18 +627,12 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
return null; return null;
} }
/**
* Preselects the first search result item with debouncing
*/
private preselectFirstItem() { private preselectFirstItem() {
// Clear any existing timeout
if (this.preselectionTimeout) { if (this.preselectionTimeout) {
clearTimeout(this.preselectionTimeout); clearTimeout(this.preselectionTimeout);
} }
// Debounce preselection to handle rapid search changes
this.preselectionTimeout = setTimeout(() => { this.preselectionTimeout = setTimeout(() => {
// Check if we have search results and the assistant is open
if (!this.isOpen || !this.searchFormControl.value) { if (!this.isOpen || !this.searchFormControl.value) {
return; return;
} }
@ -648,15 +643,12 @@ export class GfAssistantComponent implements OnChanges, OnDestroy, OnInit {
return; return;
} }
// Clear any existing focus
for (const item of this.assistantListItems) { for (const item of this.assistantListItems) {
item.removeFocus(); item.removeFocus();
} }
// Set the first item as active in the key manager
this.keyManager.setFirstItemActive(); this.keyManager.setFirstItemActive();
// Get the currently focused item and apply focus styling
const currentFocusedItem = this.getCurrentAssistantListItem(); const currentFocusedItem = this.getCurrentAssistantListItem();
if (currentFocusedItem) { if (currentFocusedItem) {

Loading…
Cancel
Save