fix/improve-snippets-for-multiple-embeds-scenario
Hariom Balhara 2023-09-25 18:53:43 +05:30
parent 437a44060b
commit 030e75587e
6 changed files with 81 additions and 22 deletions

View File

@ -318,6 +318,7 @@ function EventTypeSingleLayout({
StartIcon={Code} StartIcon={Code}
color="secondary" color="secondary"
variant="icon" variant="icon"
namespace={eventType.slug}
tooltip={t("embed")} tooltip={t("embed")}
eventId={eventType.id} eventId={eventType.id}
/> />

View File

@ -512,6 +512,7 @@ export const EventTypeList = ({ group, groupIndex, readOnly, types }: EventTypeL
{!isManagedEventType && ( {!isManagedEventType && (
<DropdownMenuItem className="outline-none"> <DropdownMenuItem className="outline-none">
<EventTypeEmbedButton <EventTypeEmbedButton
namespace={type.slug}
as={DropdownItem} as={DropdownItem}
type="button" type="button"
StartIcon={Code} StartIcon={Code}

View File

@ -13,12 +13,13 @@ type CalProps = {
debug?: boolean; debug?: boolean;
uiDebug?: boolean; uiDebug?: boolean;
}; };
namespace?: string;
config?: PrefillAndIframeAttrsConfig; config?: PrefillAndIframeAttrsConfig;
embedJsUrl?: string; embedJsUrl?: string;
} & React.HTMLAttributes<HTMLDivElement>; } & React.HTMLAttributes<HTMLDivElement>;
const Cal = function Cal(props: CalProps) { const Cal = function Cal(props: CalProps) {
const { calLink, calOrigin, config, initConfig = {}, embedJsUrl, ...restProps } = props; const { calLink, calOrigin, namespace = "", config, initConfig = {}, embedJsUrl, ...restProps } = props;
if (!calLink) { if (!calLink) {
throw new Error("calLink is required"); throw new Error("calLink is required");
} }
@ -31,16 +32,28 @@ const Cal = function Cal(props: CalProps) {
} }
initializedRef.current = true; initializedRef.current = true;
const element = ref.current; const element = ref.current;
Cal("init", { if (namespace) {
...initConfig, Cal("init", namespace, {
origin: calOrigin, ...initConfig,
}); origin: calOrigin,
Cal("inline", { });
elementOrSelector: element, Cal.ns[namespace]("inline", {
calLink, elementOrSelector: element,
config, calLink,
}); config,
}, [Cal, calLink, config, calOrigin, initConfig]); });
} else {
Cal("init", {
...initConfig,
origin: calOrigin,
});
Cal("inline", {
elementOrSelector: element,
calLink,
config,
});
}
}, [Cal, calLink, config, namespace, calOrigin, initConfig]);
if (!Cal) { if (!Cal) {
return null; return null;

View File

@ -503,12 +503,14 @@ const EmbedTypeCodeAndPreviewDialogContent = ({
embedType, embedType,
embedUrl, embedUrl,
tabs, tabs,
namespace,
eventTypeHideOptionDisabled, eventTypeHideOptionDisabled,
types, types,
}: { }: {
embedType: EmbedType; embedType: EmbedType;
embedUrl: string; embedUrl: string;
tabs: EmbedTabs; tabs: EmbedTabs;
namespace: string;
eventTypeHideOptionDisabled: boolean; eventTypeHideOptionDisabled: boolean;
types: EmbedTypes; types: EmbedTypes;
}) => { }) => {
@ -1013,6 +1015,7 @@ const EmbedTypeCodeAndPreviewDialogContent = ({
<div className="flex h-[55vh] flex-grow flex-col"> <div className="flex h-[55vh] flex-grow flex-col">
{tab.type === "code" ? ( {tab.type === "code" ? (
<tab.Component <tab.Component
namespace={namespace}
embedType={embedType} embedType={embedType}
calLink={calLink} calLink={calLink}
previewState={previewState} previewState={previewState}
@ -1020,6 +1023,7 @@ const EmbedTypeCodeAndPreviewDialogContent = ({
/> />
) : ( ) : (
<tab.Component <tab.Component
namespace={namespace}
embedType={embedType} embedType={embedType}
calLink={calLink} calLink={calLink}
previewState={previewState} previewState={previewState}
@ -1101,7 +1105,9 @@ export const EmbedDialog = ({
eventTypeHideOptionDisabled: boolean; eventTypeHideOptionDisabled: boolean;
}) => { }) => {
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const embedUrl = searchParams?.get("embedUrl") as string; const embedUrl = (searchParams?.get("embedUrl") || "") as string;
const namespace = (searchParams?.get("namespace") || "") as string;
return ( return (
<Dialog name="embed" clearQueryParamsOnClose={queryParamsForDialog}> <Dialog name="embed" clearQueryParamsOnClose={queryParamsForDialog}>
{!searchParams?.get("embedType") ? ( {!searchParams?.get("embedType") ? (
@ -1110,6 +1116,7 @@ export const EmbedDialog = ({
<EmbedTypeCodeAndPreviewDialogContent <EmbedTypeCodeAndPreviewDialogContent
embedType={searchParams?.get("embedType") as EmbedType} embedType={searchParams?.get("embedType") as EmbedType}
embedUrl={embedUrl} embedUrl={embedUrl}
namespace={namespace}
tabs={tabs} tabs={tabs}
types={types} types={types}
eventTypeHideOptionDisabled={eventTypeHideOptionDisabled} eventTypeHideOptionDisabled={eventTypeHideOptionDisabled}
@ -1121,6 +1128,7 @@ export const EmbedDialog = ({
type EmbedButtonProps<T> = { type EmbedButtonProps<T> = {
embedUrl: string; embedUrl: string;
namespace: string;
children?: React.ReactNode; children?: React.ReactNode;
className?: string; className?: string;
as?: T; as?: T;
@ -1133,6 +1141,7 @@ export const EmbedButton = <T extends React.ElementType>({
className = "", className = "",
as, as,
eventId, eventId,
namespace,
...props ...props
}: EmbedButtonProps<T> & React.ComponentPropsWithoutRef<T>) => { }: EmbedButtonProps<T> & React.ComponentPropsWithoutRef<T>) => {
const { goto } = useRouterHelpers(); const { goto } = useRouterHelpers();
@ -1141,6 +1150,7 @@ export const EmbedButton = <T extends React.ElementType>({
goto({ goto({
dialog: "embed", dialog: "embed",
eventId: eventId ? eventId.toString() : "", eventId: eventId ? eventId.toString() : "",
namespace,
embedUrl, embedUrl,
}); });
}; };

View File

@ -12,11 +12,13 @@ export const Codes = {
uiInstructionCode, uiInstructionCode,
previewState, previewState,
embedCalOrigin, embedCalOrigin,
namespace,
}: { }: {
calLink: string; calLink: string;
uiInstructionCode: string; uiInstructionCode: string;
previewState: PreviewState; previewState: PreviewState;
embedCalOrigin: string; embedCalOrigin: string;
namespace: string;
}) => { }) => {
const width = getDimension(previewState.inline.width); const width = getDimension(previewState.inline.width);
const height = getDimension(previewState.inline.height); const height = getDimension(previewState.inline.height);
@ -31,6 +33,7 @@ export const Codes = {
})(); })();
}, []) }, [])
return <Cal return <Cal
${namespace ? `namespace="${namespace}"` : ""}
calLink="${calLink}" calLink="${calLink}"
style={{width:"${width}",height:"${height}",overflow:"scroll"}} style={{width:"${width}",height:"${height}",overflow:"scroll"}}
${previewState.layout ? "config={{layout: '" + previewState.layout + "'}}" : ""}${ ${previewState.layout ? "config={{layout: '" + previewState.layout + "'}}" : ""}${
@ -46,9 +49,11 @@ export const Codes = {
"floating-popup": ({ "floating-popup": ({
floatingButtonArg, floatingButtonArg,
uiInstructionCode, uiInstructionCode,
namespace,
}: { }: {
floatingButtonArg: string; floatingButtonArg: string;
uiInstructionCode: string; uiInstructionCode: string;
namespace: string;
}) => { }) => {
return code` return code`
import { getCalApi } from "@calcom/embed-react"; import { getCalApi } from "@calcom/embed-react";
@ -57,7 +62,7 @@ export const Codes = {
useEffect(()=>{ useEffect(()=>{
(async function () { (async function () {
const cal = await getCalApi(${IS_SELF_HOSTED ? `"${embedLibUrl}"` : ""}); const cal = await getCalApi(${IS_SELF_HOSTED ? `"${embedLibUrl}"` : ""});
cal("floatingButton", ${floatingButtonArg}); cal.ns.${namespace}("floatingButton", ${floatingButtonArg});
${uiInstructionCode} ${uiInstructionCode}
})(); })();
}, []) }, [])
@ -68,11 +73,13 @@ export const Codes = {
uiInstructionCode, uiInstructionCode,
previewState, previewState,
embedCalOrigin, embedCalOrigin,
namespace,
}: { }: {
calLink: string; calLink: string;
uiInstructionCode: string; uiInstructionCode: string;
previewState: PreviewState; previewState: PreviewState;
embedCalOrigin: string; embedCalOrigin: string;
namespace: string;
}) => { }) => {
return code` return code`
import { getCalApi } from "@calcom/embed-react"; import { getCalApi } from "@calcom/embed-react";
@ -85,6 +92,7 @@ export const Codes = {
})(); })();
}, []) }, [])
return <button return <button
data-cal-namespace="${namespace}"
data-cal-link="${calLink}"${IS_SELF_HOSTED ? `\ndata-cal-origin="${embedCalOrigin}"` : ""} data-cal-link="${calLink}"${IS_SELF_HOSTED ? `\ndata-cal-origin="${embedCalOrigin}"` : ""}
${`data-cal-config='${JSON.stringify({ ${`data-cal-config='${JSON.stringify({
layout: previewState.layout, layout: previewState.layout,
@ -98,12 +106,14 @@ export const Codes = {
calLink, calLink,
uiInstructionCode, uiInstructionCode,
previewState, previewState,
namespace,
}: { }: {
calLink: string; calLink: string;
uiInstructionCode: string; uiInstructionCode: string;
previewState: PreviewState; previewState: PreviewState;
namespace: string;
}) => { }) => {
return code`Cal("inline", { return code`Cal.ns.${namespace}("inline", {
elementOrSelector:"#my-cal-inline", elementOrSelector:"#my-cal-inline",
calLink: "${calLink}", calLink: "${calLink}",
layout: "${previewState.layout}" layout: "${previewState.layout}"
@ -115,23 +125,28 @@ export const Codes = {
"floating-popup": ({ "floating-popup": ({
floatingButtonArg, floatingButtonArg,
uiInstructionCode, uiInstructionCode,
namespace,
}: { }: {
floatingButtonArg: string; floatingButtonArg: string;
uiInstructionCode: string; uiInstructionCode: string;
namespace: string;
}) => { }) => {
return code`Cal("floatingButton", ${floatingButtonArg}); return code`Cal.ns.${namespace}("floatingButton", ${floatingButtonArg});
${uiInstructionCode}`; ${uiInstructionCode}`;
}, },
"element-click": ({ "element-click": ({
calLink, calLink,
uiInstructionCode, uiInstructionCode,
previewState, previewState,
namespace,
}: { }: {
calLink: string; calLink: string;
uiInstructionCode: string; uiInstructionCode: string;
previewState: PreviewState; previewState: PreviewState;
namespace: string;
}) => { }) => {
return code` return code`
data-cal-namespace="${namespace}"
// Important: Please add following attributes to the element you want to open Cal on click // Important: Please add following attributes to the element you want to open Cal on click
// \`data-cal-link="${calLink}"\` // \`data-cal-link="${calLink}"\`
// \`data-cal-config='${JSON.stringify({ // \`data-cal-config='${JSON.stringify({

View File

@ -22,8 +22,8 @@ export const tabs = [
type: "code", type: "code",
Component: forwardRef< Component: forwardRef<
HTMLTextAreaElement | HTMLIFrameElement | null, HTMLTextAreaElement | HTMLIFrameElement | null,
{ embedType: EmbedType; calLink: string; previewState: PreviewState } { embedType: EmbedType; calLink: string; previewState: PreviewState; namespace: string }
>(function EmbedHtml({ embedType, calLink, previewState }, ref) { >(function EmbedHtml({ embedType, calLink, previewState, namespace }, ref) {
const { t } = useLocale(); const { t } = useLocale();
const embedSnippetString = useGetEmbedSnippetString(); const embedSnippetString = useGetEmbedSnippetString();
const embedCalOrigin = useEmbedCalOrigin(); const embedCalOrigin = useEmbedCalOrigin();
@ -56,7 +56,14 @@ export const tabs = [
: "") + : "") +
`<script type="text/javascript"> `<script type="text/javascript">
${embedSnippetString} ${embedSnippetString}
${getEmbedTypeSpecificString({ embedFramework: "HTML", embedType, calLink, previewState, embedCalOrigin })} ${getEmbedTypeSpecificString({
embedFramework: "HTML",
embedType,
calLink,
previewState,
embedCalOrigin,
namespace,
})}
</script> </script>
<!-- Cal ${embedType} embed code ends -->` <!-- Cal ${embedType} embed code ends -->`
} }
@ -73,8 +80,8 @@ export const tabs = [
type: "code", type: "code",
Component: forwardRef< Component: forwardRef<
HTMLTextAreaElement | HTMLIFrameElement | null, HTMLTextAreaElement | HTMLIFrameElement | null,
{ embedType: EmbedType; calLink: string; previewState: PreviewState } { embedType: EmbedType; calLink: string; previewState: PreviewState; namespace: string }
>(function EmbedReact({ embedType, calLink, previewState }, ref) { >(function EmbedReact({ embedType, calLink, previewState, namespace }, ref) {
const { t } = useLocale(); const { t } = useLocale();
const embedCalOrigin = useEmbedCalOrigin(); const embedCalOrigin = useEmbedCalOrigin();
@ -101,7 +108,14 @@ export const tabs = [
/* If you are using npm */ /* If you are using npm */
// npm install @calcom/embed-react // npm install @calcom/embed-react
${getEmbedTypeSpecificString({ embedFramework: "react", embedType, calLink, previewState, embedCalOrigin })} ${getEmbedTypeSpecificString({
embedFramework: "react",
embedType,
calLink,
previewState,
embedCalOrigin,
namespace,
})}
`} `}
/> />
</> </>
@ -115,7 +129,7 @@ export const tabs = [
type: "iframe", type: "iframe",
Component: forwardRef< Component: forwardRef<
HTMLIFrameElement | HTMLTextAreaElement | null, HTMLIFrameElement | HTMLTextAreaElement | null,
{ calLink: string; embedType: EmbedType; previewState: PreviewState } { calLink: string; embedType: EmbedType; previewState: PreviewState; namespace: string }
>(function Preview({ calLink, embedType }, ref) { >(function Preview({ calLink, embedType }, ref) {
const bookerUrl = useBookerUrl(); const bookerUrl = useBookerUrl();
if (ref instanceof Function || !ref) { if (ref instanceof Function || !ref) {
@ -144,12 +158,14 @@ const getEmbedTypeSpecificString = ({
calLink, calLink,
embedCalOrigin, embedCalOrigin,
previewState, previewState,
namespace,
}: { }: {
embedFramework: EmbedFramework; embedFramework: EmbedFramework;
embedType: EmbedType; embedType: EmbedType;
calLink: string; calLink: string;
previewState: PreviewState; previewState: PreviewState;
embedCalOrigin: string; embedCalOrigin: string;
namespace: string;
}) => { }) => {
const frameworkCodes = Codes[embedFramework]; const frameworkCodes = Codes[embedFramework];
if (!frameworkCodes) { if (!frameworkCodes) {
@ -189,6 +205,7 @@ const getEmbedTypeSpecificString = ({
uiInstructionCode: getEmbedUIInstructionString(uiInstructionStringArg), uiInstructionCode: getEmbedUIInstructionString(uiInstructionStringArg),
previewState, previewState,
embedCalOrigin, embedCalOrigin,
namespace,
}); });
} else if (embedType === "floating-popup") { } else if (embedType === "floating-popup") {
const floatingButtonArg = { const floatingButtonArg = {
@ -197,11 +214,13 @@ const getEmbedTypeSpecificString = ({
...previewState.floatingPopup, ...previewState.floatingPopup,
}; };
return frameworkCodes[embedType]({ return frameworkCodes[embedType]({
namespace,
floatingButtonArg: JSON.stringify(floatingButtonArg), floatingButtonArg: JSON.stringify(floatingButtonArg),
uiInstructionCode: getEmbedUIInstructionString(uiInstructionStringArg), uiInstructionCode: getEmbedUIInstructionString(uiInstructionStringArg),
}); });
} else if (embedType === "element-click") { } else if (embedType === "element-click") {
return frameworkCodes[embedType]({ return frameworkCodes[embedType]({
namespace,
calLink, calLink,
uiInstructionCode: getEmbedUIInstructionString(uiInstructionStringArg), uiInstructionCode: getEmbedUIInstructionString(uiInstructionStringArg),
previewState, previewState,