@@ -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,44 @@ export class DeviceToolbarComponent implements OnInit {
217217 this . powerOptions . set ( options )
218218 }
219219
220- getPowerState ( ) : void {
220+ loadPowerState ( ) : void {
221+ // Use cached to dedupe simultaneous requests (KVM deep-link + toolbar both loading).
222+ this . devicesService
223+ . getPowerStateCached ( this . deviceId ( ) )
224+ . pipe (
225+ catchError ( ( ) => {
226+ // Network/backend failure; silently skip update and let UI show previous state.
227+ return EMPTY
228+ } ) ,
229+ takeUntilDestroyed ( this . destroyRef )
230+ )
231+ . subscribe ( ( powerState ) => {
232+ this . powerState . set (
233+ powerState . powerstate . toString ( ) === '2'
234+ ? 'deviceToolbar.power.on.value'
235+ : powerState . powerstate . toString ( ) === '3' || powerState . powerstate . toString ( ) === '4'
236+ ? 'deviceToolbar.power.sleep.value'
237+ : 'deviceToolbar.power.off.value'
238+ )
239+ } )
240+ }
241+
242+ refreshPowerState ( ) : void {
221243 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.
244+ // Bypass the cache so manual refresh always fetches the latest state from the
245+ // device rather than returning a previously cached value.
224246 this . devicesService
225247 . getPowerState ( this . deviceId ( ) )
226- . pipe ( takeUntilDestroyed ( this . destroyRef ) )
248+ . pipe (
249+ catchError ( ( ) => {
250+ // Network/backend failure; finalize will reset loading state.
251+ return EMPTY
252+ } ) ,
253+ finalize ( ( ) => {
254+ this . isLoading ( ) . set ( false )
255+ } ) ,
256+ takeUntilDestroyed ( this . destroyRef )
257+ )
227258 . subscribe ( ( powerState ) => {
228259 this . powerState . set (
229260 powerState . powerstate . toString ( ) === '2'
@@ -232,7 +263,6 @@ export class DeviceToolbarComponent implements OnInit {
232263 ? 'deviceToolbar.power.sleep.value'
233264 : 'deviceToolbar.power.off.value'
234265 )
235- this . isLoading ( ) . set ( false )
236266 } )
237267 }
238268
0 commit comments