Files
calendar/packages/embeds/embed-react/src/Cal.tsx
T
Hariom BalharaandGitHub 5fceee27fe Embed: Strictly type the codebase and fixes a few bugs found (#7536)
* Add strict types

* Make it array

* Make api consistent and support calOrigin in floatingPopup and modal

* Support calOrigin everywhere and fix branding config issue

* Fix styles not being uniformly applied across components

* Fix unused code
2023-03-28 11:17:40 -07:00

50 lines
1.1 KiB
TypeScript

"use client";
import { useEffect, useRef } from "react";
import useEmbed from "./useEmbed";
type CalProps = {
calOrigin?: string;
calLink: string;
initConfig?: {
debug?: boolean;
uiDebug?: boolean;
};
config?: Record<string, string>;
embedJsUrl?: string;
} & React.HTMLAttributes<HTMLDivElement>;
const Cal = function Cal(props: CalProps) {
const { calLink, calOrigin, config, initConfig = {}, embedJsUrl, ...restProps } = props;
if (!calLink) {
throw new Error("calLink is required");
}
const initializedRef = useRef(false);
const Cal = useEmbed(embedJsUrl);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!Cal || initializedRef.current || !ref.current) {
return;
}
initializedRef.current = true;
const element = ref.current;
Cal("init", {
...initConfig,
origin: calOrigin,
});
Cal("inline", {
elementOrSelector: element,
calLink,
config,
});
}, [Cal, calLink, config, calOrigin, initConfig]);
if (!Cal) {
return null;
}
return <div ref={ref} {...restProps} />;
};
export default Cal;