Browse Source

Fixed the AI Assistant button missing from the header after Login as from the system administrator until another page was opened: AI availability is one signal in UserPermissionsService that follows the auth state, read by the header button, the docked panel host and the tour

pull/16882/head
Igor Kulikov 1 day ago
parent
commit
356e73b9e8
  1. 12
      ui-ngx/src/app/core/http/user-permissions.service.ts
  2. 2
      ui-ngx/src/app/core/services/tour.service.ts
  3. 17
      ui-ngx/src/app/modules/home/components/ai/ai-assistant-button.component.ts
  4. 9
      ui-ngx/src/app/modules/home/home.component.ts

12
ui-ngx/src/app/core/http/user-permissions.service.ts

@ -1,9 +1,10 @@
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: BUSL-1.1
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { distinctUntilChanged, map, tap } from 'rxjs/operators';
import {
AllowedPermissionsInfo,
groupResourceByGroupType,
@ -19,7 +20,7 @@ import { isArray } from '@core/utils';
import { defaultHttpOptionsFromConfig, RequestConfig } from '@core/http/http-utils';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { getCurrentAuthState } from '@core/auth/auth.selectors';
import { getCurrentAuthState, selectAuth } from '@core/auth/auth.selectors';
import { Authority } from '@shared/models/authority.enum';
@Injectable({
@ -35,6 +36,11 @@ export class UserPermissionsService {
private userPermissions: MergedUserPermissions;
private userOwnerId: EntityId;
readonly aiAvailable = toSignal(inject(Store<AppState>).select(selectAuth).pipe(
map(() => this.isAiAvailable()),
distinctUntilChanged()
), {initialValue: false});
constructor(
private http: HttpClient,
private store: Store<AppState>

2
ui-ngx/src/app/core/services/tour.service.ts

@ -32,7 +32,7 @@ export class TourService {
private offered = false;
aiAvailable(): boolean {
return this.userPermissions.isAiAvailable();
return this.userPermissions.aiAvailable();
}
offerWelcome(): void {

17
ui-ngx/src/app/modules/home/components/ai/ai-assistant-button.component.ts

@ -41,7 +41,7 @@ const intro: { owner: AiAssistantButtonComponent | null } = {owner: null};
@Component({
selector: 'tb-ai-assistant-button',
template: `
@if (available && !panelService.open() && !panelService.suppressed()) {
@if (available() && !panelService.open() && !panelService.suppressed()) {
@let status = cue();
<button #aiButton tb-ai-mark class="tb-ai-assistant-button" [cue]="markCue()"
[working]="status?.level === 'running'" [glow]="introOpen()"
@ -76,7 +76,7 @@ export class AiAssistantButtonComponent {
private readonly authState = getCurrentAuthState(this.store);
readonly available = inject(UserPermissionsService).isAiAvailable();
readonly available = inject(UserPermissionsService).aiAvailable;
private dialog = inject(MatDialog);
@ -136,7 +136,7 @@ export class AiAssistantButtonComponent {
return level === 'needs' || level === 'error' ? level : null;
});
private introWanted = computed(() => this.available && this.welcomeSeen() && !this.tourService.active() && !this.introSeen() && this.cue()?.level !== 'running' && !this.panelService.open()
private introWanted = computed(() => this.available() && this.welcomeSeen() && !this.tourService.active() && !this.introSeen() && this.cue()?.level !== 'running' && !this.panelService.open()
&& !this.panelService.suppressed() && !!this.buttonRef());
private introEffect = effect(() => {
@ -151,10 +151,13 @@ export class AiAssistantButtonComponent {
});
});
constructor() {
if (this.available) {
this.chats.watchStatus();
private statusWatch = effect(() => {
if (this.available()) {
untracked(() => this.chats.watchStatus());
}
});
constructor() {
inject(Router).events.pipe(
filter(event => event instanceof NavigationStart || event instanceof NavigationEnd),
takeUntilDestroyed()
@ -167,7 +170,7 @@ export class AiAssistantButtonComponent {
}
});
const removeOpener = inject(AiSlashService).setOpener({
canOpen: () => this.available && !this.panelService.open() && !this.panelService.suppressed(),
canOpen: () => this.available() && !this.panelService.open() && !this.panelService.suppressed(),
open: () => this.openChat()
});
inject(DestroyRef).onDestroy(() => {

9
ui-ngx/src/app/modules/home/home.component.ts

@ -4,13 +4,12 @@
import { AfterViewInit, Component, computed, createNgModule, ElementRef, Inject, Injector, OnDestroy, OnInit, Renderer2, signal, ViewChild, ViewContainerRef, inject } from '@angular/core';
import { combineLatest, Observable, skip, startWith, Subject } from 'rxjs';
import { select, Store } from '@ngrx/store';
import { toSignal } from '@angular/core/rxjs-interop';
import { debounceTime, distinctUntilChanged, map, share, take, takeUntil } from 'rxjs/operators';
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
import { PageComponent } from '@shared/components/page.component';
import { AppState } from '@core/core.state';
import { getCurrentAuthState, selectAuth, selectUserSettingsProperty } from '@core/auth/auth.selectors';
import { getCurrentAuthState, selectUserSettingsProperty } from '@core/auth/auth.selectors';
import { MediaBreakpoints } from '@shared/models/constants';
import screenfull from 'screenfull';
import { MatSidenav } from '@angular/material/sidenav';
@ -22,6 +21,7 @@ import { FormBuilder } from '@angular/forms';
import { WhiteLabelingService } from '@core/http/white-labeling.service';
import { TranslateService } from '@ngx-translate/core';
import { TourService } from '@core/services/tour.service';
import { UserPermissionsService } from '@core/http/user-permissions.service';
import { AiAssistantPanelService } from '@core/services/ai-assistant-panel.service';
import { ActionPreferencesPutUserSettings } from '@core/auth/auth.actions';
import { HomeService } from '@core/services/home.service';
@ -75,10 +75,7 @@ export class HomeComponent extends PageComponent implements AfterViewInit, OnIni
private tourService = inject(TourService);
readonly aiAvailable = toSignal(inject(Store<AppState>).select(selectAuth).pipe(
map(() => this.tourService.aiAvailable()),
distinctUntilChanged()
), {initialValue: this.tourService.aiAvailable()});
readonly aiAvailable = inject(UserPermissionsService).aiAvailable;
readonly aiPanelLoaded = signal(false);

Loading…
Cancel
Save