import React, { useEffect, useRef, useState } from "react"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import showToast from "@calcom/lib/notification"; import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry"; import { trpc } from "@calcom/trpc/react"; import { Alert } from "@calcom/ui/Alert"; import Badge from "@calcom/ui/Badge"; import Button from "@calcom/ui/Button"; import ConfirmationDialogContent from "@calcom/ui/ConfirmationDialogContent"; import { Dialog, DialogTrigger } from "@calcom/ui/Dialog"; import { TextArea } from "@calcom/ui/form/fields"; import LicenseRequired from "./LicenseRequired"; export default function SAMLConfiguration({ teamsView, teamId, }: { teamsView: boolean; teamId: null | undefined | number; }) { const [isSAMLLoginEnabled, setIsSAMLLoginEnabled] = useState(false); const [samlConfig, setSAMLConfig] = useState(null); const query = trpc.useQuery(["viewer.showSAMLView", { teamsView, teamId }]); const telemetry = useTelemetry(); useEffect(() => { const data = query.data; setIsSAMLLoginEnabled(data?.isSAMLLoginEnabled ?? false); setSAMLConfig(data?.provider ?? null); }, [query.data]); const mutation = trpc.useMutation("viewer.updateSAMLConfig", { onSuccess: (data: { provider: string | undefined }) => { showToast(t("saml_config_updated_successfully"), "success"); setHasErrors(false); // dismiss any open errors setSAMLConfig(data?.provider ?? null); samlConfigRef.current.value = ""; }, onError: () => { setHasErrors(true); setErrorMessage(t("saml_configuration_update_failed")); document?.getElementsByTagName("main")[0]?.scrollTo({ top: 0, behavior: "smooth" }); }, }); const deleteMutation = trpc.useMutation("viewer.deleteSAMLConfig", { onSuccess: () => { showToast(t("saml_config_deleted_successfully"), "success"); setHasErrors(false); // dismiss any open errors setSAMLConfig(null); samlConfigRef.current.value = ""; }, onError: () => { setHasErrors(true); setErrorMessage(t("saml_configuration_delete_failed")); document?.getElementsByTagName("main")[0]?.scrollTo({ top: 0, behavior: "smooth" }); }, }); const samlConfigRef = useRef() as React.MutableRefObject; const [hasErrors, setHasErrors] = useState(false); const [errorMessage, setErrorMessage] = useState(""); async function updateSAMLConfigHandler(event: React.FormEvent) { event.preventDefault(); const rawMetadata = samlConfigRef.current.value; // track Google logins. Without personal data/payload telemetry.event(telemetryEventTypes.samlConfig, collectPageParameters()); mutation.mutate({ encodedRawMetadata: Buffer.from(rawMetadata).toString("base64"), teamId, }); } async function deleteSAMLConfigHandler(event: React.MouseEvent) { event.preventDefault(); deleteMutation.mutate({ teamId, }); } const { t } = useLocale(); return ( <> {isSAMLLoginEnabled ? (

{t("saml_configuration")} {samlConfig ? t("enabled") : t("disabled")} {samlConfig ? ( <> {samlConfig ? samlConfig : ""} ) : null}

{samlConfig ? (
{t("delete_saml_configuration_confirmation_message")}
) : (

{!samlConfig ? t("saml_not_configured_yet") : ""}

)}

{t("saml_configuration_description")}

{hasErrors && }