Browse Source

fix(iot-hub): keep an explicit sort choice on the widget picker's Installed list

From the #16148 re-review:
- onIotHubSortChange compared the picked option with the effective sort, so
  picking "Most installed" while it was already shown as the Installed list's
  fallback for relevance stored nothing, and the IoT Hub list reopened on
  "Most relevant". The choice is now always stored; the list reloads only
  when the effective sort changes.
- INSTALLED_SORT_OPTIONS moves below the imports.
- The search page's load pipeline carries a failure as null and applies the
  result in applyResult() instead of an inline block with a { result } wrapper.
- filterByLabel trims the search itself, so the facet list and the type pages
  treat a trailing space alike.
- isCompactItemType() now also backs TbIotHubBrowseComponent.isCompactType and
  the detail dialog's isCompactLayout; the home component's pass-through
  becomes a readonly alias.
pull/16148/head
dshvaika 2 months ago
parent
commit
3710071ee2
  1. 12
      ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-widget-select.component.ts
  2. 5
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-browse.component.ts
  3. 2
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-facet-list.component.ts
  4. 6
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-item-detail-dialog.component.ts
  5. 32
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-search.component.ts
  6. 2
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-utils.ts
  7. 6
      ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.ts

12
ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-widget-select.component.ts

@ -30,8 +30,6 @@ import {
WidgetCategory
} from '@shared/models/iot-hub/iot-hub-item.models';
/** The Installed list is sorted locally, and only the Hub can rank by relevance. */
const INSTALLED_SORT_OPTIONS = IOT_HUB_SORT_OPTIONS.filter(o => o.value !== RELEVANCE_SORT_PROPERTY);
import { IotHubInstalledItem } from '@shared/models/iot-hub/iot-hub-installed-item.models';
import { IotHubApiService } from '@core/http/iot-hub-api.service';
import { IotHubActionsService } from '@home/components/iot-hub/iot-hub-actions.service';
@ -44,6 +42,9 @@ import {
} from '@home/components/iot-hub/iot-hub-utils';
import { IotHubBuiltInService } from '@home/components/iot-hub/iot-hub-built-in.service';
/** The Installed list is sorted locally, and only the Hub can rank by relevance. */
const INSTALLED_SORT_OPTIONS = IOT_HUB_SORT_OPTIONS.filter(o => o.value !== RELEVANCE_SORT_PROPERTY);
type selectWidgetMode = 'installed' | 'iotHub';
type installedSubMode = 'default' | 'allWidgets';
type iotHubSubMode = 'default' | 'allWidgets' | 'installed' | 'category';
@ -580,8 +581,11 @@ export class DashboardWidgetSelectComponent {
}
onIotHubSortChange(option: SortOption): void {
if (this.iotHubSelectedSort.value !== option.value) {
this.iotHubSelectedSortValue = option.value;
// Store the choice even when it is already the effective sort (the Installed list's fallback
// for relevance), so it survives a switch back to the IoT Hub list; reload only on a change.
const effective = this.iotHubSelectedSort.value;
this.iotHubSelectedSortValue = option.value;
if (effective !== option.value) {
this.installedWidgetVersions = null;
this.reloadIotHubWidgets();
}

5
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-browse.component.ts

@ -11,6 +11,7 @@ import {
CREATOR_VISIBLE_ITEM_TYPES,
FilterParamInfo,
IOT_HUB_SORT_OPTIONS,
isCompactItemType,
ItemType
} from '@shared/models/iot-hub/iot-hub-item.models';
import { widgetTypeTranslations, cfTypeTranslations, ruleChainTypeTranslations } from '@shared/models/iot-hub/iot-hub-version.models';
@ -80,9 +81,7 @@ export class TbIotHubBrowseComponent implements OnInit, AfterViewInit, OnDestroy
}
get isCompactType(): boolean {
return this._activeType === ItemType.CALCULATED_FIELD
|| this._activeType === ItemType.ALARM_RULE
|| this._activeType === ItemType.RULE_CHAIN;
return isCompactItemType(this._activeType);
}
get searchPlaceholderKey(): string {

2
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-facet-list.component.ts

@ -54,7 +54,7 @@ export class TbIotHubFacetListComponent implements OnChanges {
}
private applySearch(): void {
this.visibleOptions = filterByLabel(this.options, this.search.trim(), this.labelFor);
this.visibleOptions = filterByLabel(this.options, this.search, this.labelFor);
this.scrollable = this.visibleOptions.length > IOT_HUB_FILTER_SCROLL_THRESHOLD;
}
}

6
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-item-detail-dialog.component.ts

@ -7,7 +7,7 @@ import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { DialogComponent } from '@shared/components/dialog.component';
import { MpItemVersionView, cfTypeTranslations, cfTypeIcons, ruleChainTypeTranslations, widgetTypeTranslations, NodeInfo } from '@shared/models/iot-hub/iot-hub-version.models';
import { getItemTypeIcon, ItemType, itemTypeTranslations } from '@shared/models/iot-hub/iot-hub-item.models';
import { getItemTypeIcon, isCompactItemType, ItemType, itemTypeTranslations } from '@shared/models/iot-hub/iot-hub-item.models';
import { getInstalledItemUrl, IotHubInstalledItem } from '@shared/models/iot-hub/iot-hub-installed-item.models';
import { IotHubApiService } from '@core/http/iot-hub-api.service';
import { TranslateService } from '@ngx-translate/core';
@ -87,9 +87,7 @@ export class TbIotHubItemDetailDialogComponent extends DialogComponent<TbIotHubI
}
isCompactLayout(): boolean {
return this.item.type === ItemType.CALCULATED_FIELD
|| this.item.type === ItemType.ALARM_RULE
|| this.item.type === ItemType.RULE_CHAIN;
return isCompactItemType(this.item.type);
}
getPreviewUrl(): string | null {

32
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-search.component.ts

@ -4,7 +4,7 @@ import { Component, OnInit, OnDestroy, Input, Output, EventEmitter, ViewChild, E
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { forkJoin, of, Subject, Subscription } from 'rxjs';
import { catchError, debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
import { catchError, debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
import { PageLink } from '@shared/models/page/page-link';
import { SortOrder } from '@shared/models/page/sort-order';
import { MpItemVersionQuery, MpItemVersionView } from '@shared/models/iot-hub/iot-hub-version.models';
@ -109,21 +109,9 @@ export class TbIotHubSearchComponent implements OnInit, OnDestroy {
this.loadInstalledItems();
this.loadSubscription = this.loadSubject.pipe(
switchMap(() => this.fetchResults(this.searchText || '').pipe(
map(result => ({ result })),
catchError(() => of({ result: null as PageData<MpItemVersionView> | null }))
catchError(() => of(null))
))
).subscribe(({ result }) => {
this.isLoading = false;
if (result) {
this.totalElements = result.totalElements;
this.results = result.data;
this.hasError = false;
} else {
this.hasError = true;
this.results = [];
this.totalElements = 0;
}
});
).subscribe(result => this.applyResult(result));
this.searchSubscription = this.searchSubject.pipe(
debounceTime(300),
distinctUntilChanged()
@ -405,6 +393,20 @@ export class TbIotHubSearchComponent implements OnInit, OnDestroy {
this.loadSubject.next();
}
/** A null result is a failed request. */
private applyResult(result: PageData<MpItemVersionView> | null): void {
this.isLoading = false;
if (result) {
this.totalElements = result.totalElements;
this.results = result.data;
this.hasError = false;
} else {
this.hasError = true;
this.results = [];
this.totalElements = 0;
}
}
private fetchResults(text: string) {
const sort = this.sortOptions[this.selectedSortIndex];
const sortOrder: SortOrder = { property: sort.value, direction: sort.direction };

2
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-utils.ts

@ -65,7 +65,7 @@ export interface IotHubFilterGroup {
}
export function filterByLabel<T>(items: T[], search: string, labelOf: (item: T) => string): T[] {
const normalized = (search || '').toLowerCase();
const normalized = (search || '').trim().toLowerCase();
if (!normalized) {
return items;
}

6
ui-ngx/src/app/modules/home/pages/iot-hub/iot-hub-home.component.ts

@ -55,6 +55,7 @@ const SEARCH_POPUP_PAGE_SIZE = 10;
export class TbIotHubHomeComponent implements OnInit, OnDestroy {
readonly ItemType = ItemType;
readonly isCompactType = isCompactItemType;
searchText = '';
searchResults: MpItemVersionView[] = [];
@ -267,11 +268,6 @@ export class TbIotHubHomeComponent implements OnInit, OnDestroy {
void this.router.navigate(['/iot-hub/search'], { queryParams: { search } });
}
isCompactType(type: ItemType): boolean {
return isCompactItemType(type);
}
getCompactIcon(item: MpItemVersionView): string {
return item.icon || getItemTypeIcon(item.type);
}

Loading…
Cancel
Save