diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home/components/ai-assistant-home.component.ts b/ui-ngx/src/app/modules/home/pages/home-links/home/components/ai-assistant-home.component.ts index af5c6fd775..a435503f87 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home/components/ai-assistant-home.component.ts +++ b/ui-ngx/src/app/modules/home/pages/home-links/home/components/ai-assistant-home.component.ts @@ -1,6 +1,27 @@ // SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. // SPDX-License-Identifier: BUSL-1.1 -import { ChangeDetectionStrategy, Component, HostListener, inject, signal, ViewChild } from '@angular/core'; +import { + AfterViewInit, + ChangeDetectionStrategy, + Component, + DestroyRef, + ElementRef, + HostListener, + inject, + Renderer2, + signal, + untracked, + ViewChild, + ViewContainerRef +} from '@angular/core'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { getCurrentUserSettings } from '@core/auth/auth.selectors'; +import { ActionPreferencesUpdateAiTour } from '@core/auth/auth.actions'; +import { AiTourStep } from '@shared/models/user-settings.models'; +import { TourService } from '@core/services/tour.service'; +import { AiChatsService } from '@core/services/ai-chats.service'; +import { escapeTourText, TourPopupRef, TourPopupService } from '@shared/components/tour/tour-popup.service'; import { Router } from '@angular/router'; import { MatDialog } from '@angular/material/dialog'; import { TranslateService } from '@ngx-translate/core'; @@ -8,6 +29,8 @@ import { AiPromptMessage } from '@shared/models/ai-chat.models'; import { AiAssistantPanelService } from '@core/services/ai-assistant-panel.service'; import { AiPromptInputComponent } from '@home/components/ai/ai-prompt-input.component'; +const OFFER_DELAY_MS = 900; + @Component({ selector: 'tb-ai-assistant-home', template: ` @@ -27,16 +50,83 @@ import { AiPromptInputComponent } from '@home/components/ai/ai-prompt-input.comp standalone: false, changeDetection: ChangeDetectionStrategy.OnPush }) -export class AiAssistantHomeComponent { +export class AiAssistantHomeComponent implements AfterViewInit { @ViewChild(AiPromptInputComponent) composer: AiPromptInputComponent; + @ViewChild(AiPromptInputComponent, {read: ElementRef}) composerHost: ElementRef; + readonly composerFocused = signal(false); private router = inject(Router); private panelService = inject(AiAssistantPanelService); private dialog = inject(MatDialog); private translate = inject(TranslateService); + private store = inject(Store); + private tourService = inject(TourService); + private chats = inject(AiChatsService); + private tourPopup = inject(TourPopupService); + private renderer = inject(Renderer2); + private viewContainerRef = inject(ViewContainerRef); + + private offer: TourPopupRef | null = null; + private offerTimer: ReturnType | null = null; + + constructor() { + inject(DestroyRef).onDestroy(() => { + clearTimeout(this.offerTimer); + this.offer?.close(); + }); + } + + ngAfterViewInit(): void { + if (this.tourService.aiAvailable() && this.seen(AiTourStep.WELCOME) && !this.seen(AiTourStep.HOME)) { + this.chats.watchStatus(); + this.offerTimer = setTimeout(() => this.showOffer(), OFFER_DELAY_MS); + } + } + + private showOffer(): void { + this.offerTimer = null; + const host = this.composerHost?.nativeElement; + const target = host?.querySelector('.tb-ai-composer') ?? host; + if (!target || this.seen(AiTourStep.HOME) || this.tourService.active() || this.dialog.openDialogs.length + || untracked(this.chats.statusChats).some(chat => this.chats.levelOf(chat) === 'running')) { + return; + } + const keyboard = matchMedia('(hover: hover) and (pointer: fine)').matches; + this.offer = this.tourPopup.show({ + target, + hostView: this.viewContainerRef, + renderer: this.renderer, + placement: ['bottom', 'top'], + hint: true, + title: this.translate.instant('home.ai.offer-title'), + text: escapeTourText(this.translate.instant('home.ai.offer-text')), + foot: keyboard ? `/ ${escapeTourText(this.translate.instant('home.ai.offer-key'))}` : null, + nextLabel: this.translate.instant('home.ai.offer-action'), + onNext: () => this.closeOffer(true), + onClose: () => this.closeOffer(false), + onEscape: () => this.closeOffer(false), + onHidden: () => this.offer = null + }); + } + + private closeOffer(ask: boolean): void { + const offer = this.offer; + this.offer = null; + offer?.close(); + if (!this.seen(AiTourStep.HOME)) { + this.store.dispatch(new ActionPreferencesUpdateAiTour({step: AiTourStep.HOME, seen: true})); + } + if (ask) { + this.composer?.focus(); + } + } + + private seen(step: AiTourStep): boolean { + return !!getCurrentUserSettings(this.store)?.aiTour?.[step]; + } send(prompt: AiPromptMessage): void { this.panelService.handOff(prompt); diff --git a/ui-ngx/src/app/shared/components/tour/tour-popup.component.scss b/ui-ngx/src/app/shared/components/tour/tour-popup.component.scss index f9f1aa84dc..7b7d78c6ca 100644 --- a/ui-ngx/src/app/shared/components/tour/tour-popup.component.scss +++ b/ui-ngx/src/app/shared/components/tour/tour-popup.component.scss @@ -10,28 +10,36 @@ } &.tb-popover-placement-bottom, &.tb-popover-placement-bottomLeft, &.tb-popover-placement-bottomRight { + padding-top: 14px; + > .tb-popover-content > .tb-popover-arrow { - top: 4px; + top: 8px; } } &.tb-popover-placement-top, &.tb-popover-placement-topLeft, &.tb-popover-placement-topRight { + padding-bottom: 14px; + > .tb-popover-content > .tb-popover-arrow { - bottom: 4px; + bottom: 8px; box-shadow: 1px 1px 0 rgba(0, 0, 0, 0.04); } } &.tb-popover-placement-right, &.tb-popover-placement-rightTop, &.tb-popover-placement-rightBottom { + padding-left: 14px; + > .tb-popover-content > .tb-popover-arrow { - left: 4px; + left: 8px; box-shadow: -1px 1px 0 rgba(0, 0, 0, 0.04); } } &.tb-popover-placement-left, &.tb-popover-placement-leftTop, &.tb-popover-placement-leftBottom { + padding-right: 14px; + > .tb-popover-content > .tb-popover-arrow { - right: 4px; + right: 8px; box-shadow: 1px -1px 0 rgba(0, 0, 0, 0.04); } } @@ -43,6 +51,10 @@ .tb-popover-inner-content { padding: 20px 48px 16px 24px; + + &:has(.tb-tour-popup.hint) { + padding: 14px 44px 12px 16px; + } } } @@ -52,6 +64,18 @@ gap: 8px; width: min(288px, calc(100vw - 88px)); + kbd { + display: inline-block; + min-width: 18px; + padding: 0 5px; + border-radius: 4px; + background: var(--chat-key, #f6f8f8); + box-shadow: inset 0 -1px 0 var(--tb-line, rgba(0, 0, 0, 0.12)); + font: 500 12px/18px var(--tb-font, Roboto, sans-serif); + text-align: center; + color: var(--tb-ink, rgba(0, 0, 0, 0.87)); + } + .tb-tour-popup-title { margin: 0; font: 500 17px/24px var(--tb-font, Roboto, sans-serif); @@ -92,17 +116,6 @@ letter-spacing: 0; color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); - kbd { - display: inline-block; - min-width: 18px; - padding: 0 5px; - border-radius: 4px; - background: var(--chat-key, #f6f8f8); - box-shadow: inset 0 -1px 0 var(--tb-line, rgba(0, 0, 0, 0.12)); - font: 500 12px/18px var(--tb-font, Roboto, sans-serif); - text-align: center; - color: var(--tb-ink, rgba(0, 0, 0, 0.87)); - } } .tb-tour-popup-actions { @@ -113,6 +126,7 @@ margin: 4px -28px 0 0; .mdc-button { + min-width: 0; height: 36px; padding: 0 14px; border-radius: 8px; @@ -136,4 +150,44 @@ --mat-button-text-label-text-color: var(--tb-ink); --mdc-text-button-label-text-color: var(--tb-ink); } + + &.hint { + width: min(260px, calc(100vw - 76px)); + + .tb-tour-popup-title { + display: flex; + align-items: center; + gap: 8px; + font: 500 15px/22px var(--tb-font, Roboto, sans-serif); + } + + .tb-tour-popup-mark { + --m: 28px; + } + + .tb-tour-popup-text { + margin-top: 4px; + text-wrap: pretty; + } + + .tb-tour-popup-actions { + margin: 12px -28px 0 0; + + .mdc-button { + height: 32px; + } + } + + .tb-tour-popup-x.mat-mdc-icon-button.mat-mdc-button-base { + top: 8px; + right: 8px; + } + } + + .tb-tour-popup-key { + display: inline-flex; + align-items: center; + gap: 6px; + font-size: 13px; + } } diff --git a/ui-ngx/src/app/shared/components/tour/tour-popup.component.ts b/ui-ngx/src/app/shared/components/tour/tour-popup.component.ts index 6739e7dd85..fbefc50c3d 100644 --- a/ui-ngx/src/app/shared/components/tour/tour-popup.component.ts +++ b/ui-ngx/src/app/shared/components/tour/tour-popup.component.ts @@ -5,8 +5,13 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output, ViewEn @Component({ selector: 'tb-tour-popup', template: ` -