From 0f4452c19cd0c14ec57543d91d33cc222ba36a36 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Thu, 12 May 2022 21:13:24 -0700 Subject: [PATCH] Remove frigate.js util function. --- src/components/thumbnail.ts | 2 +- src/utils/frigate.ts | 33 -------------------------------- src/utils/ha/browse-media.ts | 37 ++++++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 src/utils/frigate.ts diff --git a/src/components/thumbnail.ts b/src/components/thumbnail.ts index e7265e4b..d5315c9c 100644 --- a/src/components/thumbnail.ts +++ b/src/components/thumbnail.ts @@ -7,7 +7,7 @@ import thumbnailStyle from '../scss/thumbnail.scss'; import type { FrigateBrowseMediaSource, FrigateEvent } from '../types.js'; import { stopEventFromActivatingCardWideActions } from '../utils/action.js'; import { prettifyTitle } from '../utils/basic.js'; -import { getEventDurationString } from '../utils/frigate.js'; +import { getEventDurationString } from '../utils/ha/browse-media.js'; import { View } from '../view.js'; // The minimum width of a thumbnail with details enabled. diff --git a/src/utils/frigate.ts b/src/utils/frigate.ts deleted file mode 100644 index fdb309b3..00000000 --- a/src/utils/frigate.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { - differenceInHours, differenceInMinutes, differenceInSeconds, fromUnixTime -} from 'date-fns'; -import { localize } from '../localize/localize.js'; -import { FrigateEvent } from '../types.js'; - -/** - * Convenience function to convert a timestamp to hours, minutes and seconds - * string. Heavily inspired by, and returning the same format as, the Frigate - * UI: https://github.com/blakeblackshear/frigate/blob/master/web/src/components/RecordingPlaylist.jsx#L97 - * @param event The Frigate event. - * @returns A duration string. - */ -export function getEventDurationString(event: FrigateEvent): string { - if (!event.end_time) { - return localize('event.in_progress'); - } - const start = fromUnixTime(event.start_time); - const end = fromUnixTime(event.end_time); - const hours = differenceInHours(end, start); - const minutes = differenceInMinutes(end, start) - hours * 60; - const seconds = differenceInSeconds(end, start) - hours * 60 * 60 - minutes * 60; - let duration = ''; - - if (hours) { - duration += `${hours}h `; - } - if (minutes) { - duration += `${minutes}m `; - } - duration += `${seconds}s`; - return duration; -} diff --git a/src/utils/ha/browse-media.ts b/src/utils/ha/browse-media.ts index cf7a7fb8..743fe125 100644 --- a/src/utils/ha/browse-media.ts +++ b/src/utils/ha/browse-media.ts @@ -1,4 +1,10 @@ import { HomeAssistant } from 'custom-card-helpers'; +import { + differenceInHours, + differenceInMinutes, + differenceInSeconds, + fromUnixTime +} from 'date-fns'; import { homeAssistantWSRequest } from '.'; import { dispatchErrorMessageEvent, @@ -9,8 +15,7 @@ import { BrowseMediaQueryParameters, CameraConfig, FrigateBrowseMediaSource, - frigateBrowseMediaSourceSchema, - MEDIA_CLASS_PLAYLIST, + frigateBrowseMediaSourceSchema, FrigateEvent, MEDIA_CLASS_PLAYLIST, MEDIA_TYPE_PLAYLIST } from '../../types.js'; import { View } from '../../view.js'; @@ -403,3 +408,31 @@ export const createEventParentForChildren = ( children: children, }; }; + +/** + * Convenience function to convert a timestamp to hours, minutes and seconds + * string. Heavily inspired by, and returning the same format as, the Frigate + * UI: https://github.com/blakeblackshear/frigate/blob/master/web/src/components/RecordingPlaylist.jsx#L97 + * @param event The Frigate event. + * @returns A duration string. + */ +export function getEventDurationString(event: FrigateEvent): string { + if (!event.end_time) { + return localize('event.in_progress'); + } + const start = fromUnixTime(event.start_time); + const end = fromUnixTime(event.end_time); + const hours = differenceInHours(end, start); + const minutes = differenceInMinutes(end, start) - hours * 60; + const seconds = differenceInSeconds(end, start) - hours * 60 * 60 - minutes * 60; + let duration = ''; + + if (hours) { + duration += `${hours}h `; + } + if (minutes) { + duration += `${minutes}m `; + } + duration += `${seconds}s`; + return duration; +}