18 changed files with 712 additions and 152 deletions
@ -0,0 +1,58 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { computed, inject, Injectable } from '@angular/core'; |
|||
import { toSignal } from '@angular/core/rxjs-interop'; |
|||
import { NavigationEnd, Router } from '@angular/router'; |
|||
import { TranslateService } from '@ngx-translate/core'; |
|||
import { filter, map, startWith } from 'rxjs/operators'; |
|||
import { aiPageView, AiChatClientContext } from '@shared/models/ai-chat.models'; |
|||
import { getDefaultTimezone } from '@shared/models/time/time.models'; |
|||
import { BreadCrumb } from '@shared/components/breadcrumb'; |
|||
import { BreadcrumbService } from '@core/services/breadcrumb.service'; |
|||
import { UtilsService } from '@core/services/utils.service'; |
|||
import { AiAssistantPanelService } from '@core/services/ai-assistant-panel.service'; |
|||
import { aiWelcome } from '@home/components/ai/ai-welcome.models'; |
|||
|
|||
@Injectable({providedIn: 'root'}) |
|||
export class AiAssistantContextService { |
|||
|
|||
private panelService = inject(AiAssistantPanelService); |
|||
private translate = inject(TranslateService); |
|||
private utils = inject(UtilsService); |
|||
private router = inject(Router); |
|||
|
|||
private pageTitle = toSignal(inject(BreadcrumbService).breadcrumbs$.pipe( |
|||
map(crumbs => crumbs.map(crumb => this.crumbLabel(crumb)).filter(label => !!label).join(' › ')) |
|||
), {initialValue: ''}); |
|||
|
|||
readonly pagePath = toSignal(this.router.events.pipe( |
|||
filter((event): event is NavigationEnd => event instanceof NavigationEnd), |
|||
map(event => event.urlAfterRedirects), |
|||
startWith(this.router.url), |
|||
map(url => (url || '/').split(/[?#]/)[0]) |
|||
), {initialValue: '/'}); |
|||
|
|||
readonly clientContext = computed<AiChatClientContext | undefined>(() => { |
|||
const context = this.panelService.clientContext(); |
|||
if (context) { |
|||
const name = this.panelService.viewName(); |
|||
return context.view?.entityId && name && !context.view.title ? {...context, view: {...context.view, title: name}} : context; |
|||
} |
|||
const title = this.pageTitle(); |
|||
const path = this.pagePath(); |
|||
const type = aiPageView(path); |
|||
return title || path !== '/' ? {view: {type, title, path}, timeZone: getDefaultTimezone()} : undefined; |
|||
}); |
|||
|
|||
readonly welcome = computed(() => aiWelcome(this.clientContext()?.view, this.panelService.viewName(), this.panelService.config(), |
|||
this.translate)); |
|||
|
|||
private crumbLabel(crumb: BreadCrumb): string { |
|||
try { |
|||
return crumb.labelFunction ? crumb.labelFunction() : crumb.customTranslate ? this.utils.customTranslation(crumb.label, crumb.label) |
|||
: this.translate.instant(crumb.label); |
|||
} catch { |
|||
return ''; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-ai-button-intro', |
|||
template: ` |
|||
<div class="tb-ai-intro" data-ai role="status" aria-labelledby="tb-ai-intro-title"> |
|||
<h3 class="tb-ai-intro-title" id="tb-ai-intro-title">{{ title }}</h3> |
|||
<button mat-icon-button type="button" class="tb-ai-intro-x" [attr.aria-label]="'action.close' | translate" (click)="dismiss.emit()"> |
|||
<mat-icon fontSet="material-icons-round">close</mat-icon> |
|||
</button> |
|||
<p class="tb-ai-intro-body"> |
|||
{{ lead }} |
|||
@if (keyboard) { |
|||
{{ 'ai-assistant.intro.press' | translate }} <kbd>/</kbd> {{ 'ai-assistant.intro.on-any-page' | translate }} |
|||
} |
|||
</p> |
|||
<div class="tb-ai-intro-actions"> |
|||
<button mat-button type="button" class="tb-ai-intro-skip" (click)="dismiss.emit()">{{ 'ai-assistant.intro.got-it' | translate }}</button> |
|||
<button mat-flat-button type="button" class="tb-ai-intro-go" (click)="openChat.emit()">{{ 'ai-assistant.intro.open-chat' | translate }}</button> |
|||
</div> |
|||
</div> |
|||
`,
|
|||
standalone: false, |
|||
changeDetection: ChangeDetectionStrategy.OnPush |
|||
}) |
|||
export class AiButtonIntroComponent { |
|||
|
|||
@Input() title: string; |
|||
|
|||
@Input() lead: string; |
|||
|
|||
@Input() keyboard = false; |
|||
|
|||
@Output() dismiss = new EventEmitter<void>(); |
|||
|
|||
@Output() openChat = new EventEmitter<void>(); |
|||
} |
|||
@ -0,0 +1,74 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { ChangeDetectionStrategy, Component, ElementRef, inject, input, numberAttribute, booleanAttribute } from '@angular/core'; |
|||
|
|||
export type AiMarkCue = 'needs' | 'error'; |
|||
|
|||
const MARK_CYCLE_MS = 16000; |
|||
const LARGE_MARK = 44; |
|||
|
|||
@Component({ |
|||
selector: 'tb-ai-mark, button[tb-ai-mark]', |
|||
template: ` |
|||
<ng-template #glyph> |
|||
@if (icon()) { |
|||
<mat-icon fontSet="material-icons-round">{{ icon() }}</mat-icon> |
|||
} @else if (large()) { |
|||
<tb-ai-spark></tb-ai-spark> |
|||
} @else { |
|||
<mat-icon fontSet="material-icons-round">auto_awesome</mat-icon> |
|||
} |
|||
</ng-template> |
|||
@if (button) { |
|||
<span class="tb-ai-mark" [class.lg]="large()" [style.--m]="sizePx()" [style.animation-delay]="phase" aria-hidden="true"> |
|||
<ng-container [ngTemplateOutlet]="glyph"></ng-container> |
|||
</span> |
|||
@if (cue()) { |
|||
<span class="tb-ai-mark-cue" [class]="cue()" aria-hidden="true"></span> |
|||
} |
|||
} @else { |
|||
<ng-container [ngTemplateOutlet]="glyph"></ng-container> |
|||
} |
|||
`,
|
|||
standalone: false, |
|||
changeDetection: ChangeDetectionStrategy.OnPush, |
|||
host: { |
|||
'[class.tb-ai-mark]': '!button', |
|||
'[class.lg]': '!button && large()', |
|||
'[style.--m]': 'button ? null : sizePx()', |
|||
'[style.animation-delay]': 'button ? null : phase', |
|||
'[attr.aria-hidden]': 'button ? null : "true"', |
|||
'[class.tb-ai-mark-button]': 'button', |
|||
'[attr.type]': 'button ? "button" : null', |
|||
'[attr.aria-pressed]': 'button ? pressed() : null', |
|||
'[class.working]': 'button && working()', |
|||
'[class.glow]': 'button && glow()' |
|||
} |
|||
}) |
|||
export class AiMarkComponent { |
|||
|
|||
readonly button = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement.tagName === 'BUTTON'; |
|||
|
|||
readonly phase = `-${Date.now() % MARK_CYCLE_MS}ms`; |
|||
|
|||
size = input<number | undefined, unknown>(undefined, {transform: (value: unknown) => value == null || value === '' ? undefined : numberAttribute(value)}); |
|||
|
|||
icon = input<string>(); |
|||
|
|||
pressed = input(false, {transform: booleanAttribute}); |
|||
|
|||
working = input(false, {transform: booleanAttribute}); |
|||
|
|||
glow = input(false, {transform: booleanAttribute}); |
|||
|
|||
cue = input<AiMarkCue | null>(null); |
|||
|
|||
large(): boolean { |
|||
return (this.size() ?? 0) >= LARGE_MARK; |
|||
} |
|||
|
|||
sizePx(): string | null { |
|||
const size = this.size() ?? (this.button ? 36 : undefined); |
|||
return size ? `${size}px` : null; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue