Browse Source

feat: Home offers Ask from Home once, after the welcome: a small hint on the composer with the AI spark, one line, / to ask and Ask, which puts the cursor in the composer; Ask, close and Esc each mark it seen; tour popups sit 14px from their target and their buttons fit their label, as the design's do

pull/16848/head
Igor Kulikov 2 weeks ago
parent
commit
ef6242c3d0
  1. 94
      ui-ngx/src/app/modules/home/pages/home-links/home/components/ai-assistant-home.component.ts
  2. 84
      ui-ngx/src/app/shared/components/tour/tour-popup.component.scss
  3. 15
      ui-ngx/src/app/shared/components/tour/tour-popup.component.ts
  4. 20
      ui-ngx/src/app/shared/components/tour/tour-popup.service.ts
  5. 6
      ui-ngx/src/assets/locale/locale.constant-en_US.json

94
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<HTMLElement>;
readonly composerFocused = signal(false);
private router = inject(Router);
private panelService = inject(AiAssistantPanelService);
private dialog = inject(MatDialog);
private translate = inject(TranslateService);
private store = inject(Store<AppState>);
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<typeof setTimeout> | 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 ? `<kbd>/</kbd> ${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);

84
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;
}
}

15
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: `
<div class="tb-tour-popup" data-ai role="dialog" [attr.aria-labelledby]="titleId">
<h2 class="tb-tour-popup-title" [id]="titleId">{{ title }}</h2>
<div class="tb-tour-popup" [class.hint]="hint" data-ai [attr.role]="hint ? 'status' : 'dialog'" [attr.aria-labelledby]="titleId">
<h2 class="tb-tour-popup-title" [id]="titleId">
@if (hint) {
<span class="tb-ai-mark tb-tour-popup-mark" aria-hidden="true"><mat-icon fontSet="material-icons-round">auto_awesome</mat-icon></span>
}
{{ title }}
</h2>
@if (closable) {
<button mat-icon-button type="button" class="tb-tour-popup-x" [attr.aria-label]="'action.close' | translate" (click)="closed.emit()">
<mat-icon fontSet="material-icons-round">close</mat-icon>
@ -16,6 +21,8 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output, ViewEn
<div class="tb-tour-popup-actions">
@if (count) {
<span class="tb-tour-popup-count">{{ count }}</span>
} @else if (foot) {
<span class="tb-tour-popup-count tb-tour-popup-key" [innerHTML]="foot"></span>
}
@if (skipLabel) {
<button mat-button type="button" class="tb-tour-popup-skip" (click)="skip.emit()">{{ skipLabel }}</button>
@ -47,6 +54,10 @@ export class TourPopupComponent {
@Input() closable = true;
@Input() hint = false;
@Input() foot: string | null = null;
@Output() next = new EventEmitter<void>();
@Output() skip = new EventEmitter<void>();

20
ui-ngx/src/app/shared/components/tour/tour-popup.service.ts

@ -17,9 +17,12 @@ export interface TourPopupConfig {
count?: string | null;
skipLabel?: string | null;
closable?: boolean;
hint?: boolean;
foot?: string | null;
onNext?: () => void;
onSkip?: () => void;
onClose?: () => void;
onEscape?: () => void;
onHidden?: () => void;
}
@ -37,6 +40,15 @@ export class TourPopupService {
show(config: TourPopupConfig): TourPopupRef {
let open = true;
const onKey = (event: KeyboardEvent) => {
if (event.key === 'Escape' && open) {
event.preventDefault();
config.onEscape();
}
};
if (config.onEscape) {
document.addEventListener('keydown', onKey, true);
}
const popover: TbPopoverComponent<TourPopupComponent> = this.popoverService.displayPopover({
trigger: config.target,
renderer: config.renderer,
@ -51,9 +63,14 @@ export class TourPopupService {
nextLabel: config.nextLabel,
count: config.count ?? null,
skipLabel: config.skipLabel ?? null,
closable: config.closable ?? true
closable: config.closable ?? true,
hint: config.hint ?? false,
foot: config.foot ?? null
},
visibleFn: visible => {
if (!visible) {
document.removeEventListener('keydown', onKey, true);
}
if (!visible && open) {
open = false;
config.onHidden?.();
@ -68,6 +85,7 @@ export class TourPopupService {
instance.closed.subscribe(() => config.onClose?.());
return {
close: () => {
document.removeEventListener('keydown', onKey, true);
if (open) {
open = false;
popover.hide();

6
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -8570,7 +8570,11 @@
"ai": {
"placeholder": "Ask the AI Assistant…",
"chats": "Chats",
"waiting": "Waiting for you"
"waiting": "Waiting for you",
"offer-title": "Ask from Home",
"offer-text": "Ask a question, or have it connect a device, build a dashboard or add an alarm rule.",
"offer-action": "Ask",
"offer-key": "to ask"
},
"customize": {
"title": "Customize Home",

Loading…
Cancel
Save