From 3aadfd22524e4a79917502a5685ccb1f05b8c5a1 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Fri, 7 Oct 2022 17:53:49 -0700 Subject: [PATCH] Don't collect infinite previous views. --- src/components/gallery.ts | 28 ++++++++++++++++++++++------ src/utils/ha/browse-media.ts | 3 +++ src/view.ts | 10 +--------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/components/gallery.ts b/src/components/gallery.ts index b00f613a..9a4c9816 100644 --- a/src/components/gallery.ts +++ b/src/components/gallery.ts @@ -30,6 +30,17 @@ import { renderProgressIndicator } from './message.js'; import './thumbnail.js'; import { THUMBNAIL_DETAILS_WIDTH_MIN } from './thumbnail.js'; +interface GalleryViewContext { + // Keep track of the previous view to allow returning to a higher-level folder. + previous?: View; +} + +declare module 'view' { + interface ViewContext { + gallery?: GalleryViewContext; + } +} + @customElement('frigate-card-gallery') export class FrigateCardGallery extends LitElement { @property({ attribute: false }) @@ -100,7 +111,7 @@ export class FrigateCardGallery extends LitElement { browseMediaQueryParameters, ); } - return renderProgressIndicator({cardWideConfig: this.cardWideConfig}); + return renderProgressIndicator({ cardWideConfig: this.cardWideConfig }); } return html` @@ -196,9 +207,9 @@ export class FrigateCardGalleryCore extends LitElement { */ protected _showBackArrow(): boolean { return ( - !!this.view?.previous && - !!this.view.previous.target && - this.view.previous.view === this.view.view + !!this.view?.context?.gallery?.previous && + !!this.view.context.gallery.previous.target && + this.view.context.gallery.previous.view === this.view.view ); } @@ -243,8 +254,8 @@ export class FrigateCardGalleryCore extends LitElement { ${this._showBackArrow() ? html` { - if (this.view && this.view.previous) { - this.view.previous.dispatchChangeEvent(this); + if (this.view && this.view.context?.gallery?.previous) { + this.view.context.gallery.previous.dispatchChangeEvent(this); } stopEventFromActivatingCardWideActions(ev); }} @@ -266,6 +277,11 @@ export class FrigateCardGalleryCore extends LitElement { this.hass, this.view, child, + { + gallery: { + previous: this.view, + }, + }, ); } stopEventFromActivatingCardWideActions(ev); diff --git a/src/utils/ha/browse-media.ts b/src/utils/ha/browse-media.ts index 8fce396d..ac72e964 100644 --- a/src/utils/ha/browse-media.ts +++ b/src/utils/ha/browse-media.ts @@ -1,4 +1,5 @@ import { HomeAssistant } from 'custom-card-helpers'; +import { ViewContext } from 'view'; import { homeAssistantWSRequest } from '.'; import { dispatchErrorMessageEvent, @@ -346,6 +347,7 @@ export const fetchChildMediaAndDispatchViewChange = async ( hass: HomeAssistant, view: Readonly, child: Readonly, + context?: ViewContext, ): Promise => { let parent: FrigateBrowseMediaSource; try { @@ -358,6 +360,7 @@ export const fetchChildMediaAndDispatchViewChange = async ( .evolve({ target: parent, }) + .mergeInContext(context) .dispatchChangeEvent(element); }; diff --git a/src/view.ts b/src/view.ts index 3d82ce5d..fcc5177f 100644 --- a/src/view.ts +++ b/src/view.ts @@ -13,7 +13,6 @@ export interface ViewEvolveParameters { camera?: string; target?: FrigateBrowseMediaSource | null; childIndex?: number | null; - previous?: View | null; context?: ViewContext | null; } @@ -27,7 +26,6 @@ export class View { public camera: string; public target: FrigateBrowseMediaSource | null; public childIndex: number | null; - public previous: View | null; public context: ViewContext | null; constructor(params: ViewParameters) { @@ -35,7 +33,6 @@ export class View { this.camera = params.camera; this.target = params.target ?? null; this.childIndex = params.childIndex ?? null; - this.previous = params.previous ?? null; this.context = params.context ?? null; } @@ -90,7 +87,6 @@ export class View { camera: this.camera, target: this.target, childIndex: this.childIndex, - previous: this.previous, context: this.context, }); } @@ -107,10 +103,6 @@ export class View { target: params.target !== undefined ? params.target : this.target, childIndex: params.childIndex !== undefined ? params.childIndex : this.childIndex, context: params.context !== undefined ? params.context : this.context, - - // Special case: Set the previous to this of the evolved view (rather than - // the previous of this). - previous: params.previous !== undefined ? params.previous : this, }); } @@ -119,7 +111,7 @@ export class View { * @param context The context to merge in. * @returns This view. */ - public mergeInContext(context: ViewContext): View { + public mergeInContext(context?: ViewContext): View { this.context = { ...this.context, ...context }; return this; }