"use client";
import type { ReactNode } from "react";
import type { FC } from "react";
import type { CALENDARS } from "@calcom/platform-constants";
import { Button } from "@calcom/ui";
import type { OnCheckErrorType, UseCheckProps } from "../hooks/connect/useCheck";
import { useCheck } from "../hooks/connect/useCheck";
import { useConnect } from "../hooks/connect/useConnect";
import { useConnectedCalendars } from "../hooks/useConnectedCalendars";
import { AtomsWrapper } from "../src/components/atoms-wrapper";
import { cn } from "../src/lib/utils";
export type OAuthConnectProps = {
className?: string;
label: string;
alreadyConnectedLabel: string;
loadingLabel: string;
onCheckError?: OnCheckErrorType;
redir?: string;
initialData: UseCheckProps["initialData"];
isMultiCalendar?: boolean;
tooltip?: ReactNode;
tooltipSide?: "top" | "bottom" | "left" | "right";
isClickable?: boolean;
onSuccess?: () => void;
};
export const OAuthConnect: FC<
OAuthConnectProps & {
calendar: (typeof CALENDARS)[number];
}
> = ({
label,
alreadyConnectedLabel,
loadingLabel,
className,
onCheckError,
calendar,
redir,
initialData,
isMultiCalendar = false,
tooltip,
tooltipSide = "bottom",
isClickable,
onSuccess,
}) => {
const { connect } = useConnect(calendar, redir);
const { allowConnect, checked } = useCheck({
onCheckError,
calendar: calendar,
initialData,
});
const isChecking = !checked;
const isDisabled = isChecking || !allowConnect;
let displayedLabel = label;
if (isChecking) {
displayedLabel = loadingLabel;
} else if (!allowConnect) {
displayedLabel = alreadyConnectedLabel;
}
if (isMultiCalendar) {
return (