Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

14.1 KB
Raw Blame
import { Info } from "lucide-react";
import PropTypes from "prop-types";
import { createRef, useEffect, useState } from "react";
import { Col, Row } from "@/components/ui/shims/antd-layout";
import { Popover } from "@/components/ui/shims/antd-overlays";
import { saasOnlyErrorProps } from "../../../helpers/PostHogDeployment";
import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler.jsx";
import usePostHogEvents from "../../../hooks/usePostHogEvents.js";
import useRequestUrl from "../../../hooks/useRequestUrl";
import { RjsfFormLayout } from "../../../layouts/rjsf-form-layout/RjsfFormLayout.jsx";
import { useAlertStore } from "../../../store/alert-store";
import { useSessionStore } from "../../../store/session-store";
import { OAuthDs } from "../../oauth-ds/oauth-ds/OAuthDs.jsx";
import { CustomButton } from "../../widgets/custom-button/CustomButton.jsx";
import "./ConfigureDs.css";

function ConfigureDs({
  spec,
  formData,
  setFormData,
  setOpen,
  oAuthProvider,
  selectedSourceId,
  isLoading,
  addNewItem,
  type,
  editItemId,
  isConnector,
  metadata,
  selectedSourceName,
  selectedDocUrl,
}) {
  const formRef = createRef(null);
  const axiosPrivate = useAxiosPrivate();
  const [isTcSuccessful, setIsTcSuccessful] = useState(false);
  const [isTcLoading, setIsTcLoading] = useState(false);
  const [isSubmitApiLoading, setIsSubmitApiLoading] = useState(false);

  const [cacheKey, setCacheKey] = useState("");
  const [status, setStatus] = useState("");
  const { sessionDetails } = useSessionStore();
  const { setAlertDetails } = useAlertStore();
  const handleException = useExceptionHandler();
  const { updateSessionDetails } = useSessionStore();
  const {
    posthogTcEventText,
    posthogSubmitEventText,
    posthogConnectorAddedEventText,
    setPostHogCustomEvent,
  } = usePostHogEvents();
  const { getUrl } = useRequestUrl();

  const captureAdapterResult = (eventName, result, errorMessage) => {
    if (isConnector) {
      return;
    }
    setPostHogCustomEvent(eventName, {
      adapter_type: type,
      adapter_name: selectedSourceName,
      is_edit: Boolean(editItemId),
      result,
      ...saasOnlyErrorProps(errorMessage),
    });
  };

  const oauthCacheKey = `oauth-cachekey-${selectedSourceId}`;
  const oauthStatusKey = `oauth-status-${selectedSourceId}`;

  // Determine if this is a new or existing connector
  const hasOAuthCredentials =
    metadata && (metadata.access_token || (metadata.provider && metadata.uid));
  const isExistingConnector = Boolean(editItemId || hasOAuthCredentials);

  // Determine if OAuth authentication method is selected
  const isOAuthMethodSelected = () => {
    if (!oAuthProvider?.length) {
      return false;
    }
    // Check if auth_type is set to a non-OAuth value
    const data = formData || {};
    // If auth_type exists and is not "oauth", then OAuth is not selected
    if (data.auth_type && data.auth_type !== "oauth") {
      return false;
    }
    return true;
  };

  // Initialize OAuth state from localStorage after keys are available
  useEffect(() => {
    if (!oAuthProvider?.length) {
      return;
    }

    // Initialize cache key
    const storedCacheKey = localStorage.getItem(oauthCacheKey);
    if (storedCacheKey) {
      setCacheKey(storedCacheKey);
    }

    // Initialize status from connector-specific key only
    const storedStatus = localStorage.getItem(oauthStatusKey);
    if (storedStatus) {
      setStatus(storedStatus);
    }
  }, [oauthCacheKey, oauthStatusKey, oAuthProvider, selectedSourceId]);

  // Wrapper functions to persist OAuth state to localStorage
  const handleSetCacheKey = (key) => {
    // Only handle OAuth operations for OAuth-enabled connectors
    if (!oAuthProvider?.length) {
      return;
    }
    setCacheKey(key);
    localStorage.setItem(oauthCacheKey, key);
  };

  const handleSetStatus = (newStatus) => {
    // Only handle OAuth operations for OAuth-enabled connectors
    if (!oAuthProvider?.length) {
      return;
    }
    setStatus(newStatus);
    // Store only in connector-specific location to prevent contamination
    localStorage.setItem(oauthStatusKey, newStatus);
  };

  useEffect(() => {
    if (isTcSuccessful) {
      setIsTcSuccessful(false);
    }
  }, [formData]);

  useEffect(() => {
    if (!metadata) {
      setFormData({});
      return;
    }
    setFormData(metadata);
  }, [selectedSourceId, metadata, setFormData]);

  // Clear OAuth state when switching to a different connector
  useEffect(() => {
    // Reset test connection success when switching connectors
    setIsTcSuccessful(false);

    // Reset local state only if current connector doesn't have stored OAuth credentials
    const hasStoredOAuthState =
      localStorage.getItem(oauthStatusKey) ||
      localStorage.getItem(oauthCacheKey);
    if (oAuthProvider?.length > 0 && !hasStoredOAuthState) {
      setStatus("");
      setCacheKey("");
    }
  }, [selectedSourceId, oauthStatusKey, oauthCacheKey]);

  // Restore OAuth state when returning to a connector with stored credentials
  useEffect(() => {
    if (oAuthProvider?.length > 0) {
      const storedStatus = localStorage.getItem(oauthStatusKey);
      const storedCacheKey = localStorage.getItem(oauthCacheKey);

      // Always restore stored credentials when switching to this connector
      if (storedStatus) {
        setStatus(storedStatus);
      }
      if (storedCacheKey) {
        setCacheKey(storedCacheKey);
      }
    }
  }, [selectedSourceId, oAuthProvider, oauthStatusKey, oauthCacheKey]);

  // Cleanup OAuth localStorage when component unmounts (modal close)
  useEffect(() => {
    return () => {
      localStorage.removeItem(oauthCacheKey);
      localStorage.removeItem(oauthStatusKey);
    };
  }, [oauthCacheKey, oauthStatusKey]);

  const handleTestConnection = (updatedFormData) => {
    // Check if there any error in form proceed to test connection only there is no error.
    if (formRef && !formRef.current?.validateForm()) {
      return;
    }
    if (
      oAuthProvider?.length &&
      isOAuthMethodSelected() &&
      (status !== "success" || !cacheKey?.length)
    ) {
      const providerName =
        oAuthProvider === "google-oauth2" ? "Google" : "OAuth provider";
      setAlertDetails({
        type: "error",
        content: `OAuth authentication required. Please sign in with ${providerName} first.`,
      });
      return;
    }

    let body = {};
    let url;

    if (isConnector) {
      const connectorMetadata = { ...updatedFormData };
      delete connectorMetadata.connectorName;
      body = {
        connector_id: selectedSourceId,
        connector_metadata: connectorMetadata,
      };
      url = getUrl("test_connectors/");
    } else {
      const adapterMetadata = { ...updatedFormData };
      delete adapterMetadata.adapterName;
      body = {
        adapter_id: selectedSourceId,
        adapter_metadata: adapterMetadata,
        adapter_type: type?.toUpperCase(),
      };
      url = getUrl("test_adapters/");

      setPostHogCustomEvent(posthogTcEventText[type], {
        info: `Test connection was triggered: ${selectedSourceName}`,
      });
    }

    if (oAuthProvider?.length > 0 && isOAuthMethodSelected()) {
      body["connector_metadata"] = {
        ...body["connector_metadata"],
        ...{ "oauth-key": cacheKey },
      };
    }

    const requestOptions = {
      method: "POST",
      url,
      headers: {
        "X-CSRFToken": sessionDetails?.csrfToken,
        "Content-Type": "application/json",
      },
      data: body,
    };

    setIsTcLoading(true);
    axiosPrivate(requestOptions)
      .then((res) => {
        const isValid = res?.data?.is_valid;
        setIsTcSuccessful(isValid);
        captureAdapterResult(
          "adapter_test_connection_result",
          isValid ? "success" : "invalid",
        );
        if (!isValid) {
          setAlertDetails({
            type: "error",
            content: "Test connection failed",
          });
        } else {
          setAlertDetails({
            type: "success",
            content: "Test connection successful",
          });
        }
      })
      .catch((err) => {
        const TestErrorMessage =
          err?.response?.data?.message || "Test connection failed";
        const alert = handleException(err, TestErrorMessage);
        captureAdapterResult(
          "adapter_test_connection_result",
          "error",
          alert.content,
        );
        setAlertDetails(alert);
      })
      .finally(() => {
        setIsTcLoading(false);
      });
  };

  const handleSubmit = () => {
    if (!isTcSuccessful) {
      setAlertDetails({
        type: "error",
        content: "Please test the connection before submitting.",
      });
      return;
    }

    let body = {};
    let url;

    if (isConnector) {
      const connectorMetadata = { ...formData };
      const connectorName = connectorMetadata?.connectorName;
      delete connectorMetadata.connectorName;

      body = {
        connector_id: selectedSourceId,
        connector_metadata: connectorMetadata,
        connector_name: connectorName,
        created_by: sessionDetails?.id,
      };

      url = getUrl("connector/");

      const eventKey = type?.toUpperCase();
      if (eventKey && posthogConnectorAddedEventText[eventKey]) {
        setPostHogCustomEvent(posthogConnectorAddedEventText[eventKey], {
          info: `Clicked on 'Submit' button`,
          connector_name: selectedSourceName,
        });
      }
    } else {
      const adapterMetadata = { ...formData };
      const adapterName = adapterMetadata?.adapter_name;
      delete adapterMetadata.adapter_name;
      body = {
        adapter_id: selectedSourceId,
        adapter_metadata: adapterMetadata,
        adapter_type: type?.toUpperCase(),
        adapter_name: adapterName,
      };
      url = getUrl("adapter/");

      setPostHogCustomEvent(posthogSubmitEventText[type], {
        info: "Clicked on 'Submit' button",
        adpater_name: selectedSourceName,
      });
    }

    let method = "POST";
    if (editItemId?.length) {
      method = "PUT";
      url = `${url}${editItemId}/`;
    }

    if (oAuthProvider?.length > 0 && isOAuthMethodSelected()) {
      const encodedCacheKey = encodeURIComponent(cacheKey);
      url = url + `?oauth-key=${encodedCacheKey}`;
    }

    const requestOptions = {
      method,
      url,
      headers: {
        "Content-Type": "application/json",
        "X-CSRFToken": sessionDetails?.csrfToken,
      },
      data: body,
    };

    setIsSubmitApiLoading(true);
    axiosPrivate(requestOptions)
      .then((res) => {
        const data = res?.data;

        // For centralized connectors or adapters
        if (data) {
          addNewItem(data, !!editItemId);
        }
        if (!isConnector && method === "POST") {
          updateSession(type);
        }
        captureAdapterResult("adapter_save_result", "success");

        if (oAuthProvider?.length > 0 && isOAuthMethodSelected()) {
          localStorage.removeItem(oauthCacheKey);
          localStorage.removeItem(oauthStatusKey);
        }

        setOpen(false);
      })
      .catch((err) => {
        const alert = handleException(err);
        captureAdapterResult("adapter_save_result", "error", alert.content);
        setAlertDetails(alert);
      })
      .finally(() => {
        setIsSubmitApiLoading(false);
      });
  };

  const updateSession = (type) => {
    const adapterType = type.toLowerCase();
    const adaptersList = sessionDetails?.adapters;
    if (adaptersList && !adaptersList.includes(adapterType)) {
      adaptersList.push(adapterType);
      const adaptersListInSession = { adapters: adaptersList };
      updateSessionDetails(adaptersListInSession);
    }
  };

  return (
    <div className="config-layout">
      {selectedDocUrl && (
        <div className="config-doc-link">
          <Popover
            content={
              <div>
                Need help?{" "}
                <a
                  href={selectedDocUrl}
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  See documentation.
                </a>
              </div>
            }
            trigger="click"
            placement="bottomRight"
          >
            <Info className="config-doc-icon" />
          </Popover>
        </div>
      )}
      <RjsfFormLayout
        schema={spec}
        formData={formData}
        setFormData={setFormData}
        isLoading={isLoading}
        validateAndSubmit={handleTestConnection}
        formRef={formRef}
        isStateUpdateRequired={true}
      >
        {!isLoading && oAuthProvider?.length > 0 && isOAuthMethodSelected() && (
          <OAuthDs
            oAuthProvider={oAuthProvider}
            setCacheKey={handleSetCacheKey}
            setStatus={handleSetStatus}
            selectedSourceId={selectedSourceId}
            isExistingConnector={isExistingConnector}
          />
        )}
        <Row className="config-row">
          <Col span={12} className="config-col1">
            <CustomButton
              block
              type="primary"
              htmlType="submit"
              className="config-tc-btn"
              loading={isTcLoading}
            >
              Test Connection
            </CustomButton>
          </Col>
          <Col span={12} className="config-col2">
            <CustomButton
              block
              type="primary"
              onClick={handleSubmit}
              disabled={!isTcSuccessful}
              loading={isSubmitApiLoading}
            >
              Submit
            </CustomButton>
          </Col>
        </Row>
      </RjsfFormLayout>
    </div>
  );
}

ConfigureDs.propTypes = {
  spec: PropTypes.object,
  formData: PropTypes.object.isRequired,
  setFormData: PropTypes.func.isRequired,
  setOpen: PropTypes.func,
  oAuthProvider: PropTypes.string,
  selectedSourceId: PropTypes.string,
  isLoading: PropTypes.bool.isRequired,
  addNewItem: PropTypes.func,
  type: PropTypes.string,
  editItemId: PropTypes.string,
  isConnector: PropTypes.bool.isRequired,
  metadata: PropTypes.object,
  selectedSourceName: PropTypes.string.isRequired,
  selectedDocUrl: PropTypes.string,
};

export { ConfigureDs };