@ -4,13 +4,24 @@ import { ChangeDetectionStrategy, Component, computed, effect, inject, signal }
import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
import { TranslateService } from '@ngx-translate/core' ;
import { interval } from 'rxjs' ;
import { ChatInfo } from '@shared/models/ai-chat.models' ;
import { ChatLevel } from '@shared/models/ai-chat.models' ;
import { homeAgo , homeElapsed } from '@home/pages/home-page/home-list.models' ;
import { Store } from '@ngrx/store' ;
import { AppState } from '@core/core.state' ;
import { getCurrentAuthUser } from '@core/auth/auth.selectors' ;
import { readStored , writeStored } from '@core/local-storage/stored' ;
import { AiChatsService } from '@core/services/ai-chats.service' ;
import { AiSolutionsService } from '@core/services/ai-solutions.service' ;
interface WaitingItem {
id : string ;
title : string ;
level : ChatLevel ;
tip : string ;
updatedTime? : number ;
recency : number ;
queryParams : { chatId? : string ; solutionId? : string } ;
}
const SHOWN = 2 ;
@ -19,7 +30,7 @@ const TICK_MS = 60_000;
@Component ( {
selector : 'tb-waiting-ai-chats-home' ,
template : `
@if ( ! chats . statusR esolved( ) ) {
@if ( ! r esolved( ) ) {
@if ( reserved ) {
< div class = "tb-ai-attn-block reserve" aria - hidden = "true" >
< div class = "tb-ai-attn-h" > { { 'home.ai.waiting' | translate } } < / div >
@ -34,13 +45,13 @@ const TICK_MS = 60_000;
< div class = "tb-ai-attn-block" [ class.appear ] = " ! reserved " >
< div class = "tb-ai-attn-h" id = "tb-ai-attn-h" > { { 'home.ai.waiting' | translate } } < / div >
< div class = "tb-ai-attn" role = "group" aria - labelledby = "tb-ai-attn-h" >
@for ( chat of shown ( ) ; track chat . id ) {
@let elapsed = elapsedOf ( chat ) ;
< a class = "tb-chat-pill" routerLink = "/ai-assistant" [ queryParams ] = " { chatId : chat.id } "
[ matTooltip ] = "elapsed ? ('home.ai.waiting-tip' | translate: {tip: chats.tipOf(chat), ago: agoOf(chat)}) : chats.tipOf(chat) "
@for ( item of shown ( ) ; track item . id ) {
@let elapsed = elapsedOf ( item ) ;
< a class = "tb-chat-pill" routerLink = "/ai-assistant" [ queryParams ] = " item.queryParams "
[ matTooltip ] = "elapsed ? ('home.ai.waiting-tip' | translate: {tip: item.tip, ago: agoOf(item)}) : item.tip "
matTooltipPosition = "below" >
< span class = "tb-ai-status-mark" [ class ] = " chats.levelOf ( chat ) " aria - hidden = "true" > < / span >
< span class = "tb-chat-pill-t" > { { chat . title || ( 'ai-assistant.new-chat' | translate ) } } < / span >
< span class = "tb-ai-status-mark" [ class ] = " item.level " aria - hidden = "true" > < / span >
< span class = "tb-chat-pill-t" > { { item . title } } < / span >
@if ( elapsed ) {
< span class = "tb-chat-pill-m" > { { elapsed } } < / span >
}
@ -62,8 +73,29 @@ export class WaitingAIChatsHomeComponent {
private readonly now = signal ( Date . now ( ) ) ;
readonly waiting = computed ( ( ) = > this . chats . statusChats ( ) . filter ( chat = > this . chats . needsAttention ( chat ) )
. sort ( ( a , b ) = > ( this . chats . levelOf ( a ) === 'error' ? 0 : 1 ) - ( this . chats . levelOf ( b ) === 'error' ? 0 : 1 ) ) ) ;
private readonly solutions = inject ( AiSolutionsService ) ;
readonly resolved = computed ( ( ) = > this . chats . statusResolved ( ) && this . solutions . loaded ( ) ) ;
readonly waiting = computed < WaitingItem [ ] > ( ( ) = > [
. . . this . chats . statusChats ( ) . filter ( chat = > this . chats . needsAttention ( chat ) ) . map ( chat = > ( {
id : chat.id ,
title : chat.title || this . translate . instant ( 'ai-assistant.new-chat' ) ,
level : this.chats.levelOf ( chat ) ,
tip : this.chats.tipOf ( chat ) ,
updatedTime : chat.updatedTime ,
recency : chat.updatedTime ? ? chat . createdTime ? ? 0 ,
queryParams : { chatId : chat.id }
} ) ) ,
. . . this . solutions . attention ( ) . map ( solution = > ( {
id : solution.id ,
title : this.solutions.titleOf ( solution ) ,
level : this.solutions.levelOf ( solution ) ,
tip : this.solutions.tipOf ( solution ) ,
recency : solution.createdTime ? ? 0 ,
queryParams : { solutionId : solution.id }
} ) )
] . sort ( ( a , b ) = > ( a . level === 'error' ? 0 : 1 ) - ( b . level === 'error' ? 0 : 1 ) || b . recency - a . recency ) ) ;
readonly shown = computed ( ( ) = > this . waiting ( ) . slice ( 0 , SHOWN ) ) ;
@ -75,20 +107,21 @@ export class WaitingAIChatsHomeComponent {
constructor ( ) {
this . chats . watchStatus ( ) ;
this . solutions . ensureLoaded ( ) ;
interval ( TICK_MS ) . pipe ( takeUntilDestroyed ( ) ) . subscribe ( ( ) = > this . now . set ( Date . now ( ) ) ) ;
effect ( ( ) = > {
if ( this . chats . statusR esolved( ) ) {
if ( this . r esolved( ) ) {
this . writeShown ( this . shown ( ) . length ) ;
}
} ) ;
}
elapsedOf ( chat : ChatInfo ) : string | null {
return chat . updatedTime ? homeElapsed ( this . translate , Math . max ( 0 , this . now ( ) - chat . updatedTime ) ) : null ;
elapsedOf ( item : WaitingItem ) : string | null {
return item . updatedTime ? homeElapsed ( this . translate , Math . max ( 0 , this . now ( ) - item . updatedTime ) ) : null ;
}
agoOf ( chat : ChatInfo ) : string {
return homeAgo ( this . translate , Math . max ( 0 , this . now ( ) - chat . updatedTime ) ) ;
agoOf ( item : WaitingItem ) : string {
return homeAgo ( this . translate , Math . max ( 0 , this . now ( ) - item . updatedTime ) ) ;
}
private readShown ( ) : number {