Files
calendar/packages/core/event.ts
T
3d41c64d81 feat: error message for invalid variables in custom event name (#7426)
* feat: add custom validate name util

* refactor: separate custom event type modal into a
different component

* feat: add validation to zod

* chore: add i18n key

* feat: add dynamic imports

* fix: padding

* Omit cache-hit exit 1, assuming it'll fail regardless

---------

Co-authored-by: Alex van Andel <me@alexvanandel.com>
Co-authored-by: CarinaWolli <wollencarina@gmail.com>
2023-03-06 12:14:54 +00:00

60 lines
1.9 KiB
TypeScript

import type { TFunction } from "next-i18next";
import { guessEventLocationType } from "@calcom/app-store/locations";
export type EventNameObjectType = {
attendeeName: string;
eventType: string;
eventName?: string | null;
host: string;
location?: string;
t: TFunction;
};
export function getEventName(eventNameObj: EventNameObjectType, forAttendeeView = false) {
if (!eventNameObj.eventName)
return eventNameObj.t("event_between_users", {
eventName: eventNameObj.eventType,
host: eventNameObj.host,
attendeeName: eventNameObj.attendeeName,
});
let eventName = eventNameObj.eventName;
let locationString = eventNameObj.location || "";
if (eventNameObj.eventName.includes("{Location}") || eventNameObj.eventName.includes("{LOCATION}")) {
const eventLocationType = guessEventLocationType(eventNameObj.location);
if (eventLocationType) {
locationString = eventLocationType.label;
}
eventName = eventName.replace("{Location}", locationString);
eventName = eventName.replace("{LOCATION}", locationString);
}
return (
eventName
// Need this for compatibility with older event names
.replace("{Event type title}", eventNameObj.eventType)
.replace("{Scheduler}", eventNameObj.attendeeName)
.replace("{Organiser}", eventNameObj.host)
.replace("{USER}", eventNameObj.attendeeName)
.replace("{ATTENDEE}", eventNameObj.attendeeName)
.replace("{HOST}", eventNameObj.host)
.replace("{HOST/ATTENDEE}", forAttendeeView ? eventNameObj.host : eventNameObj.attendeeName)
);
}
export const validateCustomEventName = (value: string, message: string) => {
const validVariables = ["{Event type title}", "{Organiser}", "{Scheduler}", "{Location}"];
const matches = value.match(/\{([^}]+)\}/g);
if (matches?.length) {
for (const item of matches) {
if (!validVariables.includes(item)) {
return message;
}
}
}
return true;
};