mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-16 10:33:00 +03:00
revise url format
This commit is contained in:
@@ -1,19 +1,39 @@
|
||||
import { formatInTimeZone, fromZonedTime } from "date-fns-tz";
|
||||
|
||||
export const RECORDING_REVIEW_START_PARAM = "start_time";
|
||||
export const RECORDING_REVIEW_TIMEZONE_PARAM = "timezone";
|
||||
|
||||
export type RecordingReviewLinkState = {
|
||||
camera: string;
|
||||
timestamp: number;
|
||||
};
|
||||
|
||||
function formatRecordingReviewTimestamp(
|
||||
timestamp: number,
|
||||
timezone: string | undefined,
|
||||
): string {
|
||||
const date = new Date(Math.floor(timestamp) * 1000);
|
||||
|
||||
if (timezone) {
|
||||
return formatInTimeZone(date, timezone, "yyyy-MM-dd'T'HH:mm:ss");
|
||||
}
|
||||
|
||||
return formatInTimeZone(date, "UTC", "yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
}
|
||||
|
||||
export function parseRecordingReviewLink(
|
||||
camera: string | null,
|
||||
start: string | null,
|
||||
timezone: string | null,
|
||||
): RecordingReviewLinkState | undefined {
|
||||
if (!camera || !start) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const parsedTimestamp = Number(start);
|
||||
const parsedDate = timezone
|
||||
? fromZonedTime(start, timezone)
|
||||
: new Date(start);
|
||||
const parsedTimestamp = parsedDate.getTime() / 1000;
|
||||
|
||||
if (!Number.isFinite(parsedTimestamp)) {
|
||||
return undefined;
|
||||
@@ -28,15 +48,19 @@ export function parseRecordingReviewLink(
|
||||
export function createRecordingReviewUrl(
|
||||
pathname: string,
|
||||
state: RecordingReviewLinkState,
|
||||
timezone?: string,
|
||||
): string {
|
||||
const url = new URL(window.location.href);
|
||||
url.pathname = pathname;
|
||||
url.hash = state.camera;
|
||||
url.search = "";
|
||||
url.searchParams.set(
|
||||
RECORDING_REVIEW_START_PARAM,
|
||||
`${Math.floor(state.timestamp)}`,
|
||||
const url = new URL(globalThis.location.href);
|
||||
const formattedTimestamp = formatRecordingReviewTimestamp(
|
||||
state.timestamp,
|
||||
timezone,
|
||||
);
|
||||
const normalizedPathname = pathname.startsWith("/")
|
||||
? pathname
|
||||
: `/${pathname}`;
|
||||
const timezoneParam = timezone
|
||||
? `&${RECORDING_REVIEW_TIMEZONE_PARAM}=${encodeURIComponent(timezone)}`
|
||||
: "";
|
||||
|
||||
return url.toString();
|
||||
return `${url.origin}${normalizedPathname}?${RECORDING_REVIEW_START_PARAM}=${formattedTimestamp}${timezoneParam}#${encodeURIComponent(state.camera)}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user