From 8c9513a1a1ab9f86d1a231f269b8fe20185b3117 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Tue, 24 May 2022 13:42:19 -0300 Subject: [PATCH] Fallback to supported view even when action --- src/card.ts | 24 +++++++++++------------- src/view.ts | 9 +++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/card.ts b/src/card.ts index 1c7559f1..027088b9 100644 --- a/src/card.ts +++ b/src/card.ts @@ -1246,21 +1246,19 @@ export class FrigateCard extends LitElement { case 'camera_select': const camera = frigateCardAction.camera; if (this._cameras?.has(camera) && this._view) { + let targetView = + this._getConfig().view.camera_select === 'current' + ? this._view.view + : (this._getConfig().view.camera_select as FrigateCardView); + // Fallback to supported views for non-Frigate cameras. + if (!this._cameras?.get(camera)?.camera_name) { + targetView = View.supportsNonFrigateCameras(targetView) + ? targetView + : 'live'; + } this._changeView({ view: new View({ - view: - this._getConfig().view.camera_select === 'current' - ? // When switching to a Frigate camera, - this._cameras?.get(camera)?.camera_name - ? // always retain the current view. - this._view.view - : // But when switching to a non-Frigate camera, - // only retain the current view if it's supported - ['timeline', 'image', 'live'].includes(this._view.view) - ? this._view.view - : // And fallback to the live view otherwise. - 'live' - : (this._getConfig().view.camera_select as FrigateCardView), + view: targetView, camera: camera, }), }); diff --git a/src/view.ts b/src/view.ts index e73577cb..bb01afc4 100644 --- a/src/view.ts +++ b/src/view.ts @@ -35,6 +35,15 @@ export class View { this.context = params.context ?? null; } + /** + * Determines whether a view is supported on non-Frigate cameras. + * @param view The view to determine support for. + * @returns Whether the view is supported by non-Frigate cameras. + */ + public static supportsNonFrigateCameras(view: FrigateCardView): boolean { + return ['timeline', 'image', 'live'].includes(view); + } + /** * Clone a view. */