diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/user/DefaultTbUserSettingsService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/user/DefaultTbUserSettingsService.java index ddb154c88a..a045db3b86 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/user/DefaultTbUserSettingsService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/user/DefaultTbUserSettingsService.java @@ -1,5 +1,6 @@ // SPDX-FileCopyrightText: Copyright The ThingsBoard Authors -// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 package org.thingsboard.server.service.entitiy.user; import com.fasterxml.jackson.databind.JsonNode; @@ -39,7 +40,7 @@ import java.util.stream.Collectors; @Slf4j public class DefaultTbUserSettingsService implements TbUserSettingsService { - private static final int MAX_DASHBOARD_INFO_LIST_SIZE = 10; + private static final int MAX_DASHBOARD_INFO_LIST_SIZE = 50; private static final Predicate EMPTY_TITLE = i -> StringUtils.isEmpty(i.getTitle()); private final UserSettingsService settingsService; diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home-links.module.ts b/ui-ngx/src/app/modules/home/pages/home-links/home-links.module.ts index dd1b74da61..1d379ccfd5 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home-links.module.ts +++ b/ui-ngx/src/app/modules/home/pages/home-links/home-links.module.ts @@ -31,6 +31,8 @@ import { MobileAppHomeComponent } from '@home/pages/home-links/home/components/m import { HomeCardsFooterComponent } from '@home/pages/home-links/home/components/home-cards-footer.component'; import { CustomizeHomeDialogComponent } from '@home/pages/home-links/home/components/customize-home-dialog.component'; import { HomeHscrollComponent } from '@home/pages/home-links/home/components/home-hscroll.component'; +import { HomeToggleComponent } from '@home/pages/home-links/home/components/home-toggle.component'; +import { HomeListFooterComponent } from '@home/pages/home-links/home/components/home-list-footer.component'; import { AiModule } from '@home/components/ai/ai.module'; @NgModule({ @@ -58,7 +60,9 @@ import { AiModule } from '@home/components/ai/ai.module'; MobileAppHomeComponent, HomeCardsFooterComponent, CustomizeHomeDialogComponent, - HomeHscrollComponent + HomeHscrollComponent, + HomeToggleComponent, + HomeListFooterComponent ], imports: [ CommonModule, diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.html b/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.html index 40e2044f0a..1565807801 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.html +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.html @@ -7,10 +7,7 @@ @if (total()) { -
- - -
+ } @if (tenantAdmin) { - } - - @if (shown() < listed()) { - {{ 'home.alarms.shown-of' | translate: {shown: shown(), total: (listed() | shortNumber)} }} - } - @if (listed()) { - {{ 'home.alarms.view-all' | translate }}chevron_right - } - - } + }
diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.scss b/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.scss index 75f709b8bf..1265051ed7 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.scss +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.scss @@ -6,44 +6,6 @@ container: tb-alarms-home / inline-size; } -.tb-toggle-header { - display: inline-flex; - box-sizing: border-box; - height: 32px; - max-width: 100%; - padding: 2px; - border-radius: 100px; - background: rgba(0, 0, 0, 0.06); -} - -.tb-toggle-option { - flex: 1 1 auto; - height: 28px; - margin: 0; - padding: 0 12px; - border: 1px solid transparent; - border-radius: 20px; - background: none; - font: 400 14px/26px var(--tb-font, Roboto, sans-serif); - letter-spacing: 0.2px; - white-space: nowrap; - color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); - cursor: pointer; - - &[aria-pressed="true"] { - background: #fff; - box-shadow: 0 1px 2px rgba(16, 24, 40, 0.10), 0 1px 4px rgba(16, 24, 40, 0.06); - font-weight: 500; - line-height: 24px; - color: var(--tb-ink, rgba(0, 0, 0, 0.87)); - } - - &:focus-visible { - outline: 2px solid var(--tb-primary-500); - outline-offset: -2px; - } -} - .tb-alarms-add.mat-mdc-button { .mat-icon { margin-right: 6px; @@ -79,7 +41,7 @@ } } -.tb-alarms-clear-link, .tb-alarms-link, .tb-alarms-view-all { +.tb-alarms-clear-link, .tb-alarms-link { &, &:hover, &:focus, &:active { border-bottom: none; } @@ -473,63 +435,6 @@ color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); } -.tb-alarms-footer { - display: flex; - flex-wrap: wrap; - align-items: center; - gap: 8px; - min-height: 36px; - padding-top: 8px; - border-top: 1px solid rgba(0, 0, 0, 0.05); - font: 400 14px/20px var(--tb-font, Roboto, sans-serif); - color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); - - .tb-alarms-toggle.mat-mdc-button { - margin-left: -8px; - color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); - } -} - -.tb-alarms-spacer { - flex: 1 1 auto; -} - -.tb-alarms-footer-count { - font-variant-numeric: tabular-nums; -} - -.tb-alarms-view-all { - display: inline-flex; - align-items: center; - height: 36px; - margin-right: -8px; - padding: 0 4px 0 8px; - border-radius: 4px; - font-weight: 500; - font-size: 14px; - letter-spacing: 0.03125em; - white-space: nowrap; - text-decoration: none; - color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); - transition: background-color .15s ease; - - .mat-icon { - width: 18px; - height: 18px; - font-size: 18px; - } - - &:hover { - background: rgba(0, 0, 0, 0.04); - color: var(--tb-ink, rgba(0, 0, 0, 0.87)); - } - - &:focus-visible { - outline: 2px solid var(--tb-primary-500); - outline-offset: -2px; - } -} - @container tb-alarms-home (max-width: 599px) { .tb-alarms-table { .col-origin { diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.ts b/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.ts index 5a7872c087..70c6eada2a 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.ts +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/alarms-home.component.ts @@ -32,10 +32,9 @@ import { HomeCardId } from '@shared/models/user-settings.models'; import { getEntityDetailsPageURL } from '@core/utils'; import { HOME_CARDS } from '@home/pages/home-links/home/home-cards.models'; import { AlarmDetailsDialogComponent, AlarmDetailsDialogData } from '@home/components/alarm/alarm-details-dialog.component'; +import { homeAgo, homeListPageSize, homeListShown } from '@home/pages/home-links/home/home-list.models'; +import { HomeToggleOption } from '@home/pages/home-links/home/components/home-toggle.component'; -const PREVIEW = 5; -const SLACK = 2; -const ROW_LIMIT = 10; const MINUTE = 60000; const MINE_KEY = 'tb-home-alarms-mine'; const SEVERITIES = [AlarmSeverity.CRITICAL, AlarmSeverity.MAJOR, AlarmSeverity.MINOR, AlarmSeverity.WARNING]; @@ -89,6 +88,11 @@ export class AlarmsHomeComponent { readonly card = HOME_CARDS[HomeCardId.ALARMS]; + readonly whoseOptions: HomeToggleOption[] = [ + {value: false, label: 'home.alarms.all'}, + {value: true, label: 'home.alarms.mine'} + ]; + private telemetryWsService = inject(TelemetryWebsocketService); private zone = inject(NgZone); private store = inject(Store); @@ -131,26 +135,9 @@ export class AlarmsHomeComponent { }).filter(tile => tile.severity === AlarmSeverity.CRITICAL || tile.count > 0 || tile.pressed); }); - readonly few = computed(() => this.listed() <= PREVIEW + SLACK); - - readonly shown = computed(() => { - const listed = this.listed(); - if (this.few()) { - return listed; - } - return this.expanded() ? Math.min(listed, ROW_LIMIT) : PREVIEW; - }); - - readonly toggleLabel = computed(() => { - if (this.expanded()) { - return 'home.alarms.show-less'; - } - return this.listed() <= ROW_LIMIT ? 'home.alarms.show-all' : 'home.alarms.show-more'; - }); - readonly rows = computed(() => { const now = this.now(); - return (this.alarms() ?? []).slice(0, this.shown()).map(alarm => this.toRow(alarm, now)); + return (this.alarms() ?? []).slice(0, homeListShown(this.listed(), this.expanded())).map(alarm => this.toRow(alarm, now)); }); readonly emptyNote = computed<{key: string; params: Record}>(() => { @@ -179,7 +166,7 @@ export class AlarmsHomeComponent { const desc = this.sortDesc(); return { page: 0, - pageSize: this.expanded() ? ROW_LIMIT : PREVIEW + SLACK, + pageSize: homeListPageSize(this.expanded()), sortOrder: { key: {type: EntityKeyType.ALARM_FIELD, key}, direction: key === 'severity' ? (desc ? Direction.ASC : Direction.DESC) : (desc ? Direction.DESC : Direction.ASC) @@ -242,10 +229,6 @@ export class AlarmsHomeComponent { return this.sortDesc() ? 'descending' : 'ascending'; } - toggleExpanded(): void { - this.expanded.set(!this.expanded()); - } - addAlarmRule(): void { this.router.navigate(['/alarms/alarm-rules'], {queryParams: {action: 'add'}}); } @@ -400,28 +383,11 @@ export class AlarmsHomeComponent { const originator = alarm.originator; return { alarm, - ago: this.ago(now - (alarm.createdTime ?? now)), + ago: homeAgo(this.translate, now - (alarm.createdTime ?? now)), originatorLink: originator ? getEntityDetailsPageURL(originator.id, originator.entityType as EntityType) : null, assigneeName, initials, mine: !!alarm.assigneeId && alarm.assigneeId.id === this.userId }; } - - private ago(ms: number): string { - const min = ms / MINUTE; - if (min < 1) { - return this.translate.instant('home.alarms.ago.now'); - } - if (min < 60) { - return this.translate.instant('home.alarms.ago.min', {count: Math.round(min)}); - } - if (min < 24 * 60) { - return this.translate.instant('home.alarms.ago.hour', {count: Math.floor(min / 60)}); - } - if (min < 7 * 24 * 60) { - return this.translate.instant('home.alarms.ago.day', {count: Math.floor(min / (24 * 60))}); - } - return this.translate.instant('home.alarms.ago.week', {count: Math.floor(min / (7 * 24 * 60))}); - } } diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.html b/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.html new file mode 100644 index 0000000000..af286abf9b --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.html @@ -0,0 +1,109 @@ + + + + @if (total()) { + + } + @if (canAdd) { + + add + {{ 'home.dashboards.add' | translate }} + + } + + @if (loaded()) { + @if (!total()) { +
+ + @if (tenantAdmin) { + + } @else { +
{{ 'home.dashboards.none-assigned' | translate }}
+ } +
+ } @else { + @if (rows().length) { + + + + + + + + + + @for (row of rows(); track row.id) { + + + + + + } + +
{{ 'home.dashboards.name' | translate }}{{ 'home.dashboards.last-viewed' | translate }}
+ + {{ row.title | customTranslate }}{{ row.ago }}
+ } @else { +
{{ (tab() === 'last' ? 'home.dashboards.none-viewed' : 'home.dashboards.none-starred') | translate }}
+ } + @if (tab() === 'starred') { +
+ @if (picking()) { + +
+ @for (candidate of candidates(); track candidate.id; let i = $index) { +
+ star_border + {{ candidate.title | customTranslate }} + @if (candidate.ago) { + {{ candidate.ago }} + } +
+ } @empty { + @if (query().trim()) { +
{{ 'home.dashboards.no-match' | translate: {query: query().trim()} }}
+ } + } +
+ } @else { + + } +
+ } + + } + } +
diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.scss b/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.scss new file mode 100644 index 0000000000..d284b6434f --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.scss @@ -0,0 +1,439 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +:host { + display: block; + min-width: 0; + container: tb-dashboards-home / inline-size; +} + +.tb-dashboards-add.mat-mdc-button { + &, &:hover, &:focus, &:active { + border-bottom: none; + } + + .mat-icon { + margin-right: 6px; + } +} + +.tb-dashboards-table { + width: 100%; + border-collapse: collapse; + table-layout: auto; + + th { + height: 28px; + padding: 0 12px 0 0; + font: 400 12px/16px var(--tb-font, Roboto, sans-serif); + letter-spacing: 0.25px; + text-align: left; + white-space: nowrap; + color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); + } + + td { + height: 36px; + padding: 0 12px 0 0; + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + letter-spacing: 0.2px; + vertical-align: middle; + font-variant-numeric: tabular-nums; + color: var(--tb-ink, rgba(0, 0, 0, 0.87)); + } + + tbody tr { + cursor: pointer; + + &:hover td { + background: rgba(0, 0, 0, 0.02); + } + + &:hover .col-title a { + text-decoration: underline; + } + } + + .col-star { + width: 40px; + min-width: 40px; + } + + td.col-title { + max-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + + a { + &, &:hover, &:focus, &:active { + border-bottom: none; + } + + color: var(--tb-ink, rgba(0, 0, 0, 0.87)); + text-decoration: none; + + &:hover { + text-decoration: underline; + } + } + } + + .col-time { + width: 96px; + text-align: right; + white-space: nowrap; + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); + } +} + +.tb-dashboards-star { + position: relative; + display: inline-flex; + margin: 0; + padding: 0; + border: 0; + background: none; + vertical-align: middle; + color: var(--tb-ink-12, rgba(0, 0, 0, 0.12)); + cursor: pointer; + + &::after { + content: ''; + position: absolute; + inset: -4px; + border-radius: 50%; + } + + &:hover { + color: rgba(0, 0, 0, 0.18); + } + + &.starred { + color: var(--tb-primary-500); + + &:hover { + color: var(--tb-primary-700); + } + } + + &:focus-visible { + outline: 2px solid var(--tb-primary-500); + outline-offset: 2px; + border-radius: 50%; + } + + .mat-icon { + color: inherit; + } +} + +.tb-dashboards-empty-note { + padding: 4px 0; + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + letter-spacing: 0.2px; + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); +} + +.tb-dashboards-picker { + position: relative; +} + +.tb-dashboards-picker-add { + display: flex; + align-items: center; + gap: 8px; + box-sizing: border-box; + width: 100%; + height: 40px; + margin: 0; + padding: 0 12px; + border: 1px dashed rgba(0, 0, 0, 0.2); + border-radius: 8px; + background: none; + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + letter-spacing: 0.2px; + text-align: left; + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); + cursor: pointer; + transition: border-color .15s ease, color .15s ease, background-color .15s ease; + + .mat-icon { + width: 18px; + height: 18px; + font-size: 18px; + color: inherit; + } + + &:hover { + border-color: var(--tb-primary-500); + background: color-mix(in srgb, var(--tb-primary-500) 4%, #fff); + color: var(--tb-primary-500); + } + + &:focus-visible { + outline: 2px solid var(--tb-primary-500); + outline-offset: 2px; + } + + &:disabled { + border-style: solid; + border-color: rgba(0, 0, 0, 0.06); + background: none; + color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); + cursor: default; + } +} + +.tb-dashboards-search { + display: flex; + align-items: center; + gap: 8px; + box-sizing: border-box; + height: 40px; + padding: 0 4px 0 12px; + border-radius: 8px; + background: rgba(0, 0, 0, 0.04); + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); + cursor: text; + + &:focus-within { + background: #fff; + box-shadow: 0 0 0 2px var(--tb-primary-500), 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 4px rgba(16, 24, 40, 0.04); + color: var(--tb-primary-500); + } + + > .mat-icon { + flex: none; + width: 20px; + height: 20px; + font-size: 20px; + color: inherit; + } + + input { + flex: 1; + min-width: 0; + border: 0; + outline: 0; + background: transparent; + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + color: var(--tb-ink, rgba(0, 0, 0, 0.87)); + caret-color: var(--tb-primary-500); + + &::placeholder { + color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); + } + } +} + +.tb-dashboards-search-close.mat-mdc-icon-button { + --mdc-icon-button-state-layer-size: 32px; + --mat-icon-button-state-layer-size: 32px; + display: inline-flex; + align-items: center; + justify-content: center; + width: 32px; + height: 32px; + padding: 0; + color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); + + .mat-icon { + width: 20px; + height: 20px; + font-size: 20px; + } + + .mat-mdc-button-touch-target { + width: 32px; + height: 32px; + } +} + +.tb-dashboards-options { + position: absolute; + top: calc(100% + 4px); + left: 0; + right: 0; + z-index: 5; + display: flex; + flex-direction: column; + max-height: 248px; + overflow-y: auto; + padding: 4px; + border-radius: 8px; + background: #fff; + box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14), 0 1px 3px rgba(0, 0, 0, 0.08); + + &:empty { + display: none; + } +} + +.tb-dashboards-option { + display: flex; + align-items: center; + gap: 10px; + min-height: 40px; + padding: 0 10px; + border-radius: 6px; + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + color: var(--tb-ink, rgba(0, 0, 0, 0.87)); + cursor: pointer; + + .mat-icon { + flex: none; + width: 18px; + height: 18px; + font-size: 18px; + color: var(--tb-ink-38, rgba(0, 0, 0, 0.38)); + } + + small { + font-size: 12px; + white-space: nowrap; + color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); + } + + &.active { + background: rgba(0, 0, 0, 0.04); + + .mat-icon { + color: var(--tb-primary-500); + } + } +} + +.tb-dashboards-option-label { + flex: 1; + min-width: 0; + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; +} + +.tb-dashboards-options-empty { + padding: 10px; + font-size: 13px; + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); +} + +.tb-dashboards-empty { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 8px; + padding: 8px 16px 12px; + text-align: center; +} + +.tb-dashboards-empty-glyph { + display: inline-flex; + flex: none; + align-items: center; + justify-content: center; + width: 44px; + height: 44px; + border-radius: 50%; + background: color-mix(in srgb, var(--tb-primary-500) 10%, transparent); + color: var(--tb-primary-500); + + .mat-icon { + width: 20px; + height: 20px; + font-size: 20px; + color: inherit; + } +} + +.tb-dashboards-empty-text { + display: flex; + flex-direction: column; + gap: 4px; + min-width: 0; + + p { + max-width: 36em; + margin: 0; + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + letter-spacing: 0.2px; + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); + } +} + +.tb-dashboards-empty-title { + font: 500 14px/20px var(--tb-font, Roboto, sans-serif); + letter-spacing: 0.25px; + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); +} + +.tb-dashboards-empty-actions { + display: flex; + flex-wrap: wrap; + align-items: center; + justify-content: center; + gap: 4px 16px; + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + + a { + &, &:hover, &:focus, &:active { + border-bottom: none; + } + + border-radius: 4px; + text-decoration: underline; + text-decoration-thickness: 1px; + text-underline-offset: 3px; + text-decoration-color: transparent; + color: var(--tb-primary-500); + transition: text-decoration-color .15s ease; + + &:hover { + text-decoration-color: currentColor; + } + + &:focus-visible { + outline: 2px solid var(--tb-primary-500); + outline-offset: 2px; + } + } +} + +.tb-dashboards-ext { + width: 14px; + height: 14px; + margin-left: 2px; + font-size: 14px; + line-height: 14px; + vertical-align: -2px; + opacity: .7; +} + +.tb-dashboards-sr { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0 0 0 0); + white-space: nowrap; +} + +@container tb-dashboards-home (max-width: 459px) { + .tb-dashboards-add.mat-mdc-button { + min-width: 40px; + padding: 0 8px; + + .mat-icon { + margin: 0; + } + } + + .tb-dashboards-add-label { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0 0 0 0); + white-space: nowrap; + } +} diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.ts b/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.ts index b15dfddc07..6f7fe2aeef 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.ts +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/dashboards-home.component.ts @@ -1,16 +1,283 @@ // SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. // SPDX-License-Identifier: BUSL-1.1 -import { ChangeDetectionStrategy, Component } from '@angular/core'; -import { HomeCardId } from '@shared/models/user-settings.models'; +import { ChangeDetectionStrategy, Component, computed, DestroyRef, ElementRef, inject, signal, viewChild } from '@angular/core'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { Router } from '@angular/router'; +import { Store } from '@ngrx/store'; +import { TranslateService } from '@ngx-translate/core'; +import { of, Subject } from 'rxjs'; +import { catchError, debounceTime, map, switchMap } from 'rxjs/operators'; +import { AppState } from '@core/core.state'; +import { getCurrentAuthState, getCurrentAuthUser } from '@core/auth/auth.selectors'; +import { UserSettingsService } from '@core/http/user-settings.service'; +import { DashboardService } from '@core/http/dashboard.service'; +import { UserPermissionsService } from '@core/http/user-permissions.service'; +import { ToastNotificationService } from '@core/services/toast-notification.service'; +import { Authority } from '@shared/models/authority.enum'; +import { EntityType } from '@shared/models/entity-type.models'; +import { Operation, Resource } from '@shared/models/security.models'; +import { HelpLinks } from '@shared/models/constants'; +import { PageLink } from '@shared/models/page/page-link'; +import { Direction } from '@shared/models/page/sort-order'; +import { HomeCardId, UserDashboardAction, UserDashboardsInfo } from '@shared/models/user-settings.models'; import { HOME_CARDS } from '@home/pages/home-links/home/home-cards.models'; +import { homeAgo, homeListShown } from '@home/pages/home-links/home/home-list.models'; +import { HomeToggleOption } from '@home/pages/home-links/home/components/home-toggle.component'; + +type DashboardsTab = 'last' | 'starred'; + +const MINUTE = 60000; +const SEARCH_SIZE = 10; +const FILL_SIZE = 20; + +export interface DashboardHomeRow { + id: string; + title: string; + starred: boolean; + lastVisited: number | null; + ago: string; + url: string; +} + +interface StarCandidate { + id: string; + title: string; + ago: string | null; +} @Component({ selector: 'tb-dashboards-home', - template: ``, + templateUrl: './dashboards-home.component.html', + styleUrls: ['./dashboards-home.component.scss'], standalone: false, changeDetection: ChangeDetectionStrategy.OnPush }) export class DashboardsHomeComponent { readonly card = HOME_CARDS[HomeCardId.DASHBOARDS]; + + readonly tabOptions: HomeToggleOption[] = [ + {value: 'last', label: 'home.dashboards.last-viewed'}, + {value: 'starred', label: 'home.dashboards.starred'} + ]; + + readonly howToLink = HelpLinks.linksMap.dashboards; + + private store = inject(Store); + private router = inject(Router); + private translate = inject(TranslateService); + private userSettingsService = inject(UserSettingsService); + private dashboardService = inject(DashboardService); + private userPermissions = inject(UserPermissionsService); + private toast = inject(ToastNotificationService); + + private readonly authUser = getCurrentAuthUser(this.store); + private readonly fullscreen = getCurrentAuthState(this.store).forceFullscreen; + + readonly tenantAdmin = this.authUser?.authority === Authority.TENANT_ADMIN; + readonly canAdd = this.tenantAdmin && this.userPermissions.hasGenericPermission(Resource.DASHBOARD, Operation.CREATE); + readonly dashboardsLink = this.userPermissions.hasReadGenericPermission(Resource.DASHBOARD) ? '/dashboards/all' + : this.userPermissions.hasSharedReadGroupsPermission(EntityType.DASHBOARD) ? '/dashboards/shared' : null; + + readonly tab = signal('last'); + readonly expanded = signal(false); + readonly total = signal(null); + private readonly info = signal(null); + private readonly others = signal<{id: string; title: string}[]>([]); + private readonly now = signal(Date.now()); + + readonly picking = signal(false); + readonly query = signal(''); + readonly active = signal(0); + private readonly searchResults = signal([]); + private readonly search$ = new Subject(); + + private readonly searchInput = viewChild>('searchInput'); + + readonly loaded = computed(() => this.total() !== null && this.info() !== null); + + private readonly starredIds = computed(() => new Set((this.info()?.starred ?? []).map(dashboard => dashboard.id))); + + private readonly visits = computed(() => new Map((this.info()?.last ?? []).map(dashboard => [dashboard.id, dashboard.lastVisited]))); + + private readonly allRows = computed(() => { + const info = this.info(); + if (!info) { + return []; + } + const now = this.now(); + const starred = this.starredIds(); + const visits = this.visits(); + if (this.tab() === 'last') { + const viewed = [...info.last] + .sort((a, b) => b.lastVisited - a.lastVisited) + .map(dashboard => this.toRow(dashboard.id, dashboard.title, starred.has(dashboard.id), dashboard.lastVisited, now)); + const rest = this.others() + .filter(dashboard => !visits.has(dashboard.id)) + .map(dashboard => this.toRow(dashboard.id, dashboard.title, starred.has(dashboard.id), null, now)); + return [...viewed, ...rest]; + } + return [...info.starred] + .sort((a, b) => (visits.get(b.id) ?? 0) - (visits.get(a.id) ?? 0) || b.starredAt - a.starredAt) + .map(dashboard => this.toRow(dashboard.id, dashboard.title, true, visits.get(dashboard.id) ?? null, now)); + }); + + readonly listed = computed(() => this.tab() === 'last' ? Math.max(this.total() ?? 0, this.allRows().length) : this.allRows().length); + + readonly rows = computed(() => this.allRows().slice(0, homeListShown(this.listed(), this.expanded()))); + + readonly allStarred = computed(() => { + const total = this.total(); + return total !== null && total > 0 && this.starredIds().size >= total; + }); + + readonly candidates = computed(() => { + const starred = this.starredIds(); + if (this.query().trim()) { + return this.searchResults().filter(candidate => !starred.has(candidate.id)); + } + const now = this.now(); + return [...(this.info()?.last ?? [])] + .filter(dashboard => !starred.has(dashboard.id)) + .sort((a, b) => b.lastVisited - a.lastVisited) + .slice(0, SEARCH_SIZE) + .map(dashboard => ({id: dashboard.id, title: dashboard.title, ago: homeAgo(this.translate, now - dashboard.lastVisited)})); + }); + + constructor() { + if (this.dashboardsLink && this.authUser?.authority !== Authority.SYS_ADMIN) { + this.userSettingsService.getUserDashboardsInfo({ignoreLoading: true}).subscribe(info => { + this.info.set({last: info?.last ?? [], starred: info?.starred ?? []}); + }); + this.dashboardService.getUserDashboards(this.authUser.userId, null, + new PageLink(FILL_SIZE, 0, null, {property: 'createdTime', direction: Direction.DESC}), {ignoreLoading: true}).pipe( + catchError(() => of(null)) + ).subscribe(page => { + this.others.set((page?.data ?? []).map(dashboard => ({id: dashboard.id.id, title: dashboard.title}))); + this.total.set(page?.totalElements ?? 0); + }); + } else { + this.info.set({last: [], starred: []}); + this.total.set(0); + } + this.search$.pipe( + debounceTime(200), + switchMap(query => query + ? this.dashboardService.getUserDashboards(this.authUser.userId, null, + new PageLink(SEARCH_SIZE + this.starredIds().size, 0, query, {property: 'title', direction: Direction.ASC}), {ignoreLoading: true}).pipe( + map(page => page.data.map(dashboard => { + const visited = this.visits().get(dashboard.id.id); + return {id: dashboard.id.id, title: dashboard.title, ago: visited ? homeAgo(this.translate, Date.now() - visited) : null}; + })), + catchError(() => of([])) + ) + : of([])), + takeUntilDestroyed() + ).subscribe(results => { + this.searchResults.set(results); + this.active.set(0); + }); + const tick = setInterval(() => this.now.set(Date.now()), MINUTE); + inject(DestroyRef).onDestroy(() => clearInterval(tick)); + } + + setTab(tab: DashboardsTab): void { + this.tab.set(tab); + this.closePicker(); + } + + open(row: DashboardHomeRow): void { + this.router.navigateByUrl(row.url); + } + + toggleStar(row: DashboardHomeRow, $event: Event): void { + $event.stopPropagation(); + const info = this.info(); + if (!info) { + return; + } + if (row.starred) { + this.info.set({...info, starred: info.starred.filter(dashboard => dashboard.id !== row.id)}); + this.userSettingsService.reportUserDashboardAction(row.id, UserDashboardAction.UNSTAR, {ignoreLoading: true}).subscribe(); + } else { + this.star(row.id, row.title); + } + } + + openPicker(): void { + this.query.set(''); + this.searchResults.set([]); + this.active.set(0); + this.picking.set(true); + setTimeout(() => this.searchInput()?.nativeElement.focus()); + } + + closePicker(): void { + this.picking.set(false); + } + + onQuery(value: string): void { + this.query.set(value); + this.active.set(0); + this.search$.next(value.trim()); + } + + onPickerKeydown(event: KeyboardEvent): void { + const count = this.candidates().length; + if (event.key === 'ArrowDown' && count) { + event.preventDefault(); + this.active.set((this.active() + 1) % count); + } else if (event.key === 'ArrowUp' && count) { + event.preventDefault(); + this.active.set((this.active() - 1 + count) % count); + } else if (event.key === 'Enter') { + event.preventDefault(); + const candidate = this.candidates()[this.active()]; + if (candidate) { + this.pick(candidate); + } + } else if (event.key === 'Escape') { + event.preventDefault(); + this.closePicker(); + } + } + + onPickerBlur(): void { + if (document.hasFocus()) { + this.closePicker(); + } + } + + pick(candidate: StarCandidate): void { + this.closePicker(); + this.star(candidate.id, candidate.title); + this.toast.dispatchNotification({ + message: this.translate.instant('home.dashboards.starred-toast', {title: candidate.title}), + type: 'info', + duration: 4000, + horizontalPosition: 'right', + verticalPosition: 'bottom', + panelClass: 'tb-home-toast' + }); + } + + private star(id: string, title: string): void { + const info = this.info(); + if (!info || this.starredIds().has(id)) { + return; + } + this.info.set({...info, starred: [...info.starred, {id, title, starred: true, starredAt: Date.now()}]}); + this.userSettingsService.reportUserDashboardAction(id, UserDashboardAction.STAR, {ignoreLoading: true}).subscribe(); + } + + private toRow(id: string, title: string, starred: boolean, lastVisited: number | null, now: number): DashboardHomeRow { + return { + id, + title, + starred, + lastVisited, + ago: lastVisited ? homeAgo(this.translate, now - lastVisited) : '', + url: (this.fullscreen ? '/dashboard/' : '/dashboards/all/') + id + }; + } } diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-list-footer.component.scss b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-list-footer.component.scss new file mode 100644 index 0000000000..9adb27b3e7 --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-list-footer.component.scss @@ -0,0 +1,66 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +:host { + display: block; +} + +.tb-home-list-footer { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px; + min-height: 36px; + padding-top: 8px; + border-top: 1px solid rgba(0, 0, 0, 0.05); + font: 400 14px/20px var(--tb-font, Roboto, sans-serif); + color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); +} + +.tb-home-list-toggle.mat-mdc-button { + margin-left: -8px; + color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); +} + +.tb-home-list-spacer { + flex: 1 1 auto; +} + +.tb-home-list-count { + font-variant-numeric: tabular-nums; +} + +.tb-home-list-view-all { + &, &:hover, &:focus, &:active { + border-bottom: none; + } + + display: inline-flex; + align-items: center; + height: 36px; + margin-right: -8px; + padding: 0 4px 0 8px; + border-radius: 4px; + font-weight: 500; + font-size: 14px; + letter-spacing: 0.03125em; + white-space: nowrap; + text-decoration: none; + color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); + transition: background-color .15s ease; + + .mat-icon { + width: 18px; + height: 18px; + font-size: 18px; + } + + &:hover { + background: rgba(0, 0, 0, 0.04); + color: var(--tb-ink, rgba(0, 0, 0, 0.87)); + } + + &:focus-visible { + outline: 2px solid var(--tb-primary-500); + outline-offset: -2px; + } +} diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-list-footer.component.ts b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-list-footer.component.ts new file mode 100644 index 0000000000..924f756af9 --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-list-footer.component.ts @@ -0,0 +1,54 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +import { ChangeDetectionStrategy, Component, computed, input, output } from '@angular/core'; +import { Params } from '@angular/router'; +import { HOME_LIST_LIMIT, homeListFew, homeListShown } from '@home/pages/home-links/home/home-list.models'; + +@Component({ + selector: 'tb-home-list-footer', + template: ` + @if (!few() || (total() && link())) { + + } + `, + styleUrls: ['./home-list-footer.component.scss'], + standalone: false, + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class HomeListFooterComponent { + + total = input(0); + + expanded = input(false); + + link = input(null); + + params = input(null); + + expandedChange = output(); + + readonly few = computed(() => homeListFew(this.total())); + + readonly shown = computed(() => homeListShown(this.total(), this.expanded())); + + readonly toggleLabel = computed(() => { + if (this.expanded()) { + return 'home.list.show-less'; + } + return this.total() <= HOME_LIST_LIMIT ? 'home.list.show-all' : 'home.list.show-more'; + }); +} diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-toggle.component.scss b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-toggle.component.scss new file mode 100644 index 0000000000..4c6899afba --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-toggle.component.scss @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +:host { + display: inline-flex; + max-width: 100%; +} + +.tb-home-toggle { + display: inline-flex; + box-sizing: border-box; + height: 32px; + max-width: 100%; + padding: 2px; + border-radius: 100px; + background: rgba(0, 0, 0, 0.06); +} + +.tb-home-toggle-option { + flex: 1 1 auto; + height: 28px; + margin: 0; + padding: 0 12px; + border: 1px solid transparent; + border-radius: 20px; + background: none; + font: 400 14px/26px var(--tb-font, Roboto, sans-serif); + letter-spacing: 0.2px; + white-space: nowrap; + color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); + cursor: pointer; + + &[aria-pressed="true"] { + background: #fff; + box-shadow: 0 1px 2px rgba(16, 24, 40, 0.10), 0 1px 4px rgba(16, 24, 40, 0.06); + font-weight: 500; + line-height: 24px; + color: var(--tb-ink, rgba(0, 0, 0, 0.87)); + } + + &:focus-visible { + outline: 2px solid var(--tb-primary-500); + outline-offset: -2px; + } +} diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-toggle.component.ts b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-toggle.component.ts new file mode 100644 index 0000000000..b214616a03 --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/home-toggle.component.ts @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +import { ChangeDetectionStrategy, Component, input, output } from '@angular/core'; + +export interface HomeToggleOption { + value: T; + label: string; +} + +@Component({ + selector: 'tb-home-toggle', + template: ` +
+ @for (option of options(); track option.value) { + + } +
+ `, + styleUrls: ['./home-toggle.component.scss'], + standalone: false, + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class HomeToggleComponent { + + options = input.required[]>(); + + value = input.required(); + + label = input(); + + valueChange = output(); +} diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/home-cards.models.ts b/ui-ngx/src/app/modules/home/pages/home-links/home/home-cards.models.ts index d7901b04ff..5d649a9dbf 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home/home-cards.models.ts +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/home-cards.models.ts @@ -13,7 +13,7 @@ export interface HomeCardDefinition { export const HOME_CARDS: Record = { [HomeCardId.IOT_HUB]: {id: HomeCardId.IOT_HUB, icon: 'hub', name: 'home.cards.iot-hub', link: '/iot-hub', height: 500}, [HomeCardId.ALARMS]: {id: HomeCardId.ALARMS, icon: 'warning', name: 'home.cards.alarms', link: '/alarms'}, - [HomeCardId.DASHBOARDS]: {id: HomeCardId.DASHBOARDS, icon: 'dashboard', name: 'home.cards.dashboards', link: '/dashboards', height: 357}, + [HomeCardId.DASHBOARDS]: {id: HomeCardId.DASHBOARDS, icon: 'dashboard', name: 'home.cards.dashboards', link: '/dashboards'}, [HomeCardId.DEVICES]: {id: HomeCardId.DEVICES, icon: 'memory', name: 'home.cards.devices', link: '/entities/devices', height: 430}, [HomeCardId.ACTIVITY]: {id: HomeCardId.ACTIVITY, icon: 'show_chart', name: 'home.cards.activity', height: 250}, [HomeCardId.GET_STARTED]: {id: HomeCardId.GET_STARTED, icon: 'rocket_launch', name: 'home.cards.get-started', height: 405}, diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/home-list.models.ts b/ui-ngx/src/app/modules/home/pages/home-links/home/home-list.models.ts new file mode 100644 index 0000000000..2bceb270db --- /dev/null +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/home-list.models.ts @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: BUSL-1.1 +import { TranslateService } from '@ngx-translate/core'; + +export const HOME_LIST_PREVIEW = 5; +export const HOME_LIST_SLACK = 2; +export const HOME_LIST_LIMIT = 10; + +const MINUTE = 60000; + +export const homeListPageSize = (expanded: boolean): number => expanded ? HOME_LIST_LIMIT : HOME_LIST_PREVIEW + HOME_LIST_SLACK; + +export const homeListFew = (total: number): boolean => total <= HOME_LIST_PREVIEW + HOME_LIST_SLACK; + +export const homeListShown = (total: number, expanded: boolean): number => { + if (homeListFew(total)) { + return total; + } + return expanded ? Math.min(total, HOME_LIST_LIMIT) : HOME_LIST_PREVIEW; +}; + +export const homeAgo = (translate: TranslateService, ms: number): string => { + const min = ms / MINUTE; + if (min < 1) { + return translate.instant('home.ago.now'); + } + if (min < 60) { + return translate.instant('home.ago.min', {count: Math.round(min)}); + } + if (min < 24 * 60) { + return translate.instant('home.ago.hour', {count: Math.floor(min / 60)}); + } + if (min < 7 * 24 * 60) { + return translate.instant('home.ago.day', {count: Math.floor(min / (24 * 60))}); + } + return translate.instant('home.ago.week', {count: Math.floor(min / (7 * 24 * 60))}); +}; diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 455a2b512e..5fae4ff733 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -8603,19 +8603,44 @@ "none-active": "No active alarms.", "none-mine": "No alarms assigned to you.", "none-severity": "No {{severity}} alarms.", - "none-severity-mine": "No {{severity}} alarms assigned to you.", + "none-severity-mine": "No {{severity}} alarms assigned to you." + }, + "dashboards": { + "view": "Dashboards view", + "last-viewed": "Last viewed", + "starred": "Starred", + "add": "Add dashboard", + "name": "Name", + "star": "Star {{title}}", + "unstar": "Unstar {{title}}", + "star-tip": "Star", + "unstar-tip": "Unstar", + "none": "No dashboards yet. Add a dashboard to open it from here.", + "none-assigned": "No dashboards assigned to you yet", + "from-template": "Start from a template", + "how-to": "How to build dashboards", + "none-viewed": "No dashboards viewed yet.", + "none-starred": "No starred dashboards yet.", + "star-a-dashboard": "Star a dashboard", + "all-starred": "Every dashboard is starred", + "search-to-star": "Search dashboards to star", + "no-match": "No dashboards match “{{query}}”.", + "starred-toast": "“{{title}}” starred", + "opens-new-tab": "(opens in a new tab)" + }, + "list": { "show-more": "Show more", "show-all": "Show all", "show-less": "Show less", "shown-of": "{{shown}} of {{total}}", - "view-all": "View all", - "ago": { - "now": "now", - "min": "{{count}} min ago", - "hour": "{{count}}h ago", - "day": "{{count}}d ago", - "week": "{{count}}w ago" - } + "view-all": "View all" + }, + "ago": { + "now": "now", + "min": "{{count}} min ago", + "hour": "{{count}}h ago", + "day": "{{count}}d ago", + "week": "{{count}}w ago" }, "ai": { "placeholder": "Ask the AI Assistant…",