frigate/web/src/utils/recordingReviewUrl.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-03-19 23:54:54 +03:00
export const RECORDING_REVIEW_LINK_PARAM = "timestamp";
export type RecordingReviewLinkState = {
camera: string;
timestamp: number;
};
export function parseRecordingReviewLink(
2026-03-19 23:54:54 +03:00
value: string | null,
): RecordingReviewLinkState | undefined {
2026-03-19 23:54:54 +03:00
if (!value) {
return undefined;
}
const separatorIndex = value.lastIndexOf("_");
if (separatorIndex <= 0 || separatorIndex == value.length - 1) {
return undefined;
}
const camera = value.slice(0, separatorIndex);
const timestamp = value.slice(separatorIndex + 1);
2026-03-19 23:54:54 +03:00
2026-03-20 00:19:33 +03:00
if (!camera || !timestamp) {
return undefined;
}
2026-03-20 00:19:33 +03:00
const parsedTimestamp = Number(timestamp);
2026-03-20 20:52:06 +03:00
const now = Math.floor(Date.now() / 1000);
if (!Number.isFinite(parsedTimestamp) || parsedTimestamp <= 0) {
return undefined;
}
return {
camera,
2026-03-20 20:52:06 +03:00
// clamp future timestamps to now
timestamp: Math.min(Math.floor(parsedTimestamp), now),
};
}
export function createRecordingReviewUrl(
pathname: string,
state: RecordingReviewLinkState,
): string {
2026-03-19 17:33:30 +03:00
const url = new URL(globalThis.location.href);
const normalizedPathname = pathname.startsWith("/")
? pathname
: `/${pathname}`;
const reviewLink = `${state.camera}_${Math.floor(state.timestamp)}`;
2026-03-19 23:54:54 +03:00
return `${url.origin}${normalizedPathname}?${RECORDING_REVIEW_LINK_PARAM}=${reviewLink}`;
}