revise url format

This commit is contained in:
0x464e
2026-03-19 19:55:45 +02:00
parent ec3fc782a8
commit 05d29ebc93
3 changed files with 45 additions and 15 deletions
+33 -9
View File
@@ -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)}`;
}