Skip to content

Commit 8f8c40e

Browse files
committed
fix: address copilot review comments
1 parent cca39b4 commit 8f8c40e

3 files changed

Lines changed: 48 additions & 8 deletions

File tree

src/app/devices/device-toolbar/device-toolbar.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<mat-divider style="height: 40px" vertical="true"></mat-divider>
5353
@if (!isLoading()()) {
5454
<mat-icon
55-
(click)="getPowerState()"
55+
(click)="refreshPowerState()"
5656
style="padding: 0 18px 0 12px; cursor: pointer"
5757
[style.color]="
5858
powerState() === 'deviceToolbar.power.on.value'

src/app/devices/device-toolbar/device-toolbar.component.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ describe('DeviceToolbarComponent', () => {
138138
expect(component.isLoading()()).toBeFalse()
139139
})
140140

141+
it('should fetch power state using non-cached API on refresh', () => {
142+
devicesService.getPowerState.calls.reset()
143+
devicesService.getPowerStateCached.calls.reset()
144+
145+
component.refreshPowerState()
146+
147+
expect(devicesService.getPowerState).toHaveBeenCalledWith('guid')
148+
expect(devicesService.getPowerStateCached).not.toHaveBeenCalled()
149+
})
150+
141151
it('should navigate to device', async () => {
142152
fixture.componentRef.setInput('deviceId', '12345-pokli-456772')
143153
const routerSpy = spyOn(component.router, 'navigate')

src/app/devices/device-toolbar/device-toolbar.component.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Component, OnInit, inject, signal, input, DestroyRef } from '@angular/c
77
import { catchError, finalize, switchMap } from 'rxjs/operators'
88
import { MatSnackBar } from '@angular/material/snack-bar'
99
import { Router } from '@angular/router'
10-
import { Observable, of, forkJoin } from 'rxjs'
10+
import { Observable, of, forkJoin, EMPTY } from 'rxjs'
1111
import { DevicesService } from '../devices.service'
1212
import SnackbarDefaults from '../../shared/config/snackBarDefault'
1313
import { 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

Comments
 (0)