EventBuzz_Console/src/app/utils/publicFormUrl.ts

23 lines
898 B
TypeScript

import type { FormWithSubmissionCount } from "../../services/api/types";
export function formEventId(f: FormWithSubmissionCount): string | null {
const ev = f.event;
if (!ev) return null;
if (typeof ev === "string") return ev.trim().length === 24 ? ev.trim() : null;
const id = (ev as { _id?: string })._id;
return id && String(id).trim().length === 24 ? String(id).trim() : null;
}
export function publicFormPageUrl(
f: FormWithSubmissionCount,
fallbackEventId?: string | null,
): string {
const base =
(typeof import.meta.env.VITE_PUBLIC_WEB_BASE === "string" && import.meta.env.VITE_PUBLIC_WEB_BASE.trim()) ||
window.location.origin.replace(/\/$/, "");
const eventId = formEventId(f) ?? fallbackEventId ?? null;
const fid = f._id;
if (eventId) return `${base.replace(/\/$/, "")}/event/${eventId}/form/${fid}`;
return `${base.replace(/\/$/, "")}/form/${fid}`;
}