@@ -7,7 +7,7 @@ import { Component, OnInit, inject, signal, input, DestroyRef } from '@angular/c
77import { catchError , finalize , switchMap } from 'rxjs/operators'
88import { MatSnackBar } from '@angular/material/snack-bar'
99import { Router } from '@angular/router'
10- import { Observable , of , forkJoin } from 'rxjs'
10+ import { Observable , of , forkJoin , EMPTY } from 'rxjs'
1111import { DevicesService } from '../devices.service'
1212import SnackbarDefaults from '../../shared/config/snackBarDefault'
1313import { AMTFeaturesResponse , BootDetails , Device , UserConsentResponse } from '../../../models/models'
@@ -164,7 +164,7 @@ export class DeviceToolbarComponent implements OnInit {
164164 this . device = data
165165 this . devicesService . device . next ( this . device )
166166 this . isPinned . set ( this . device ?. certHash != null && this . device ?. certHash !== '' )
167- this . getPowerState ( )
167+ this . loadPowerState ( )
168168 this . loadAMTFeatures ( )
169169 // react to AMT feature updates emitted by service
170170 this . devicesService
@@ -217,13 +217,48 @@ export class DeviceToolbarComponent implements OnInit {
217217 this . powerOptions . set ( options )
218218 }
219219
220- getPowerState ( ) : void {
220+ private loadPowerState ( ) : void {
221+ // Use cached to dedupe simultaneous requests (KVM deep-link + toolbar both loading).
221222 this . isLoading ( ) . set ( true )
222- // Bypass the cache so a manual refresh always fetches the latest state from
223- // the device rather than returning a previously cached value.
223+ this . devicesService
224+ . getPowerStateCached ( this . deviceId ( ) )
225+ . pipe (
226+ catchError ( ( ) => {
227+ // Network/backend failure; silently skip update and let UI show previous state.
228+ return EMPTY
229+ } ) ,
230+ finalize ( ( ) => {
231+ this . isLoading ( ) . set ( false )
232+ } ) ,
233+ takeUntilDestroyed ( this . destroyRef )
234+ )
235+ . subscribe ( ( powerState ) => {
236+ this . powerState . set (
237+ powerState . powerstate . toString ( ) === '2'
238+ ? 'deviceToolbar.power.on.value'
239+ : powerState . powerstate . toString ( ) === '3' || powerState . powerstate . toString ( ) === '4'
240+ ? 'deviceToolbar.power.sleep.value'
241+ : 'deviceToolbar.power.off.value'
242+ )
243+ } )
244+ }
245+
246+ refreshPowerState ( ) : void {
247+ this . isLoading ( ) . set ( true )
248+ // Bypass the cache so manual refresh always fetches the latest state from the
249+ // device rather than returning a previously cached value.
224250 this . devicesService
225251 . getPowerState ( this . deviceId ( ) )
226- . pipe ( takeUntilDestroyed ( this . destroyRef ) )
252+ . pipe (
253+ catchError ( ( ) => {
254+ // Network/backend failure; finalize will reset loading state.
255+ return EMPTY
256+ } ) ,
257+ finalize ( ( ) => {
258+ this . isLoading ( ) . set ( false )
259+ } ) ,
260+ takeUntilDestroyed ( this . destroyRef )
261+ )
227262 . subscribe ( ( powerState ) => {
228263 this . powerState . set (
229264 powerState . powerstate . toString ( ) === '2'
@@ -232,7 +267,6 @@ export class DeviceToolbarComponent implements OnInit {
232267 ? 'deviceToolbar.power.sleep.value'
233268 : 'deviceToolbar.power.off.value'
234269 )
235- this . isLoading ( ) . set ( false )
236270 } )
237271 }
238272
0 commit comments