Skip to content

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

12.6 KB
Raw Blame
import { useEffect, useMemo, useRef, useState } from "react";
import { useLocation } from "react-router-dom";

import { deploymentApiTypes, displayURL } from "../../../helpers/GetStaticData";
import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate.js";
import { useCoOwnerManagement } from "../../../hooks/useCoOwnerManagement.jsx";
import { useExceptionHandler } from "../../../hooks/useExceptionHandler.jsx";
import { useExecutionLogs } from "../../../hooks/useExecutionLogs";
import {
  applyPagedResponse,
  usePaginatedList,
} from "../../../hooks/usePaginatedList";
import usePipelineHelper from "../../../hooks/usePipelineHelper.js";
import {
  useInitialFetchCount,
  usePromptStudioModal,
} from "../../../hooks/usePromptStudioFetchCount";
import { useScrollRestoration } from "../../../hooks/useScrollRestoration";
import { useShareModal } from "../../../hooks/useShareModal";
import { useAlertStore } from "../../../store/alert-store";
import { usePromptStudioStore } from "../../../store/prompt-studio-store";
import { useSessionStore } from "../../../store/session-store";
import { usePromptStudioService } from "../../api/prompt-studio-service";
import { PromptStudioModal } from "../../common/PromptStudioModal";
import { groupsService } from "../../groups/groups-service.js";
import { LogsModal } from "../../pipelines-or-deployments/log-modal/LogsModal.jsx";
import { NotificationModal } from "../../pipelines-or-deployments/notification-modal/NotificationModal.jsx";
import { CoOwnerManagement } from "../../widgets/co-owner-management/CoOwnerManagement";
import { SharePermission } from "../../widgets/share-permission/SharePermission";
import { workflowService } from "../../workflows/workflow/workflow-service.js";
import { CreateApiDeploymentModal } from "../create-api-deployment-modal/CreateApiDeploymentModal";
import { DisplayCode } from "../display-code/DisplayCode";
import { Layout } from "../layout/Layout";
import { ManageKeys } from "../manage-keys/ManageKeys";
import { createApiDeploymentCardConfig } from "./ApiDeploymentCardConfig.jsx";
import { apiDeploymentsService } from "./api-deployments-service";

function ApiDeployment() {
  const { sessionDetails } = useSessionStore();
  const { setAlertDetails } = useAlertStore();
  const location = useLocation();
  const apiDeploymentsApiService = apiDeploymentsService();
  const workflowApiService = workflowService();
  const [isTableLoading, setIsTableLoading] = useState(true);
  const [openAddApiModal, setOpenAddApiModal] = useState(false);
  const [openCodeModal, setOpenCodeModal] = useState(false);
  const [openManageKeysModal, setOpenManageKeysModal] = useState(false);
  const [selectedRow, setSelectedRow] = useState({});
  const [tableData, setTableData] = useState([]);
  // Monotonic request token so a stale response can't overwrite a newer one.
  const seqRef = useRef(0);
  const [filteredData, setFilteredData] = useState([]);
  const [apiKeys, setApiKeys] = useState([]);
  const [isEdit, setIsEdit] = useState(false);
  const [workflowEndpointList, setWorkflowEndpointList] = useState([]);
  const handleException = useExceptionHandler();
  const axiosPrivate = useAxiosPrivate();
  const { getApiKeys, downloadPostmanCollection } = usePipelineHelper();
  const [openNotificationModal, setOpenNotificationModal] = useState(false);
  const {
    coOwnerOpen,
    setCoOwnerOpen,
    coOwnerData,
    coOwnerLoading,
    coOwnerAllUsers,
    coOwnerResourceId,
    handleCoOwner: handleCoOwnerAction,
    onApplyCoOwners,
  } = useCoOwnerManagement({
    service: apiDeploymentsApiService,
    setAlertDetails,
    onListRefresh: () =>
      getApiDeploymentList(pagination.current, pagination.pageSize, searchTerm),
  });
  const { count, isLoading, fetchCount } = usePromptStudioStore();
  const { getPromptStudioCount } = usePromptStudioService();

  const {
    pagination,
    setPagination,
    searchTerm,
    setSearchTerm,
    // The hook owns the fetch ref; assigned below (avoids declaration ordering).
    fetchRef,
    handlePaginationChange,
    handleSearch,
    handleListRefresh,
  } = usePaginatedList();

  const { scrollRestoreId, activateScrollRestore, clearPendingScroll } =
    useScrollRestoration({
      location,
      setSearchTerm,
      setPagination,
      fetchData: (...args) => fetchRef.current?.(...args),
    });

  const {
    openLogsModal,
    setOpenLogsModal,
    executionLogs,
    executionLogsTotalCount,
    isFetchingLogs,
    handleFetchLogs,
    handleViewLogs,
  } = useExecutionLogs({
    axiosPrivate,
    handleException,
    sessionDetails,
    setAlertDetails,
  });

  const groupsApi = groupsService();
  const {
    openShareModal,
    setOpenShareModal,
    allUsers,
    allGroups,
    isLoadingShare,
    handleShare,
    onShare,
  } = useShareModal({
    apiService: apiDeploymentsApiService,
    groupsApi,
    setSelectedItem: setSelectedRow,
    setAlertDetails,
    handleException,
    refreshList: () =>
      getApiDeploymentList(pagination.current, pagination.pageSize, searchTerm),
  });

  const initialFetchComplete = useInitialFetchCount(
    fetchCount,
    getPromptStudioCount,
  );

  useEffect(() => {
    getApiDeploymentList();
    getWorkflows();
  }, []);

  useEffect(() => {
    setFilteredData(tableData);
  }, [tableData]);

  const getWorkflows = () => {
    workflowApiService
      .getWorkflowEndpointList("SOURCE", "API")
      .then((res) => {
        setWorkflowEndpointList(res?.data);
      })
      .catch(() => {
        setAlertDetails({
          type: "error",
          content: "Unable to get workflow list.",
        });
      });
  };

  const getApiDeploymentList = (page = 1, pageSize = 10, search = "") => {
    setIsTableLoading(true);

    const seq = ++seqRef.current;
    return apiDeploymentsApiService
      .getApiDeploymentsList(page, pageSize, search)
      .then((res) => {
        const stepback = applyPagedResponse({
          data: res?.data,
          page,
          pageSize,
          seq,
          latestSeqRef: seqRef,
          setList: setTableData,
          setPagination,
          refetchPrevPage: () =>
            getApiDeploymentList(page - 1, pageSize, search),
        });
        if (seq === seqRef.current) {
          activateScrollRestore();
        }
        return stepback;
      })
      .catch((err) => {
        // A newer request superseded this one — don't surface its error.
        if (seq !== seqRef.current) {
          return;
        }
        setAlertDetails(handleException(err));
        clearPendingScroll();
      })
      .finally(() => {
        // Only the newest request owns the shared loading state.
        if (seq === seqRef.current) {
          setIsTableLoading(false);
        }
      });
  };

  fetchRef.current = getApiDeploymentList;

  const deleteApiDeployment = (item) => {
    const id = item?.id || selectedRow.id;
    apiDeploymentsApiService
      .deleteApiDeployment(id)
      .then(() => {
        getApiDeploymentList(
          pagination.current,
          pagination.pageSize,
          searchTerm,
        );
        setAlertDetails({
          type: "success",
          content: "API Deployment Deleted Successfully",
        });
      })
      .catch((err) => {
        setAlertDetails(handleException(err));
      });
  };

  const updateItemStatus = (itemId, isActive) => {
    setTableData((prev) =>
      prev.map((item) =>
        item.id === itemId ? { ...item, is_active: isActive } : item,
      ),
    );
  };

  const updateStatus = (record) => {
    const newStatus = !record?.is_active;
    // Optimistic update - no loading spinner
    updateItemStatus(record.id, newStatus);

    apiDeploymentsApiService
      .setApiDeploymentActive(record.id, newStatus)
      .catch((err) => {
        updateItemStatus(record.id, !newStatus);
        setAlertDetails(handleException(err));
      });
  };

  const openAddModal = (edit) => {
    setIsEdit(edit);
    setOpenAddApiModal(true);
  };

  // Handlers for card view actions
  const handleEditDeployment = () => {
    openAddModal(true);
  };

  const handleDeleteDeployment = (item) => {
    deleteApiDeployment(item);
  };

  const handleViewLogsDeployment = (deployment) => {
    handleViewLogs(deployment, setSelectedRow);
  };

  const handleManageKeysDeployment = (deployment) => {
    setSelectedRow(deployment);
    getApiKeys(
      apiDeploymentsApiService,
      deployment.id,
      setApiKeys,
      setOpenManageKeysModal,
    );
  };

  const handleSetupNotificationsDeployment = (deployment) => {
    setSelectedRow(deployment);
    setOpenNotificationModal(true);
  };

  const handleCodeSnippetsDeployment = (deployment) => {
    setSelectedRow(deployment);
    setOpenCodeModal(true);
  };

  const handleDownloadPostmanDeployment = (deployment) => {
    downloadPostmanCollection(apiDeploymentsApiService, deployment.id);
  };

  const handleManageCoOwners = (deployment) => {
    if (!deployment?.id) return;
    handleCoOwnerAction(deployment.id);
  };

  // Card view configuration
  const apiDeploymentCardConfig = useMemo(
    () =>
      createApiDeploymentCardConfig({
        setSelectedRow,
        updateStatus,
        sessionDetails,
        location,
        onEdit: handleEditDeployment,
        onShare: handleShare,
        onDelete: handleDeleteDeployment,
        onViewLogs: handleViewLogsDeployment,
        onManageKeys: handleManageKeysDeployment,
        onSetupNotifications: handleSetupNotificationsDeployment,
        onCodeSnippets: handleCodeSnippetsDeployment,
        onDownloadPostman: handleDownloadPostmanDeployment,
        onManageCoOwners: handleManageCoOwners,
        listContext: {
          page: pagination.current,
          pageSize: pagination.pageSize,
          searchTerm,
        },
      }),
    [
      sessionDetails,
      location,
      pagination.current,
      pagination.pageSize,
      searchTerm,
    ],
  );

  // Using the custom hook to manage modal state
  const { showModal, handleModalClose } = usePromptStudioModal(
    initialFetchComplete,
    isLoading,
    count,
  );

  return (
    <>
      {showModal && (
        <PromptStudioModal onClose={handleModalClose} showModal={showModal} />
      )}
      <Layout
        type="api"
        tableData={filteredData}
        isTableLoading={isTableLoading}
        openAddModal={openAddModal}
        cardConfig={apiDeploymentCardConfig}
        listMode={true}
        scrollToId={scrollRestoreId}
        enableSearch={true}
        onSearch={handleSearch}
        setSearchList={setFilteredData}
        searchTerm={searchTerm}
        pagination={{
          current: pagination.current,
          pageSize: pagination.pageSize,
          total: pagination.total,
          onChange: handlePaginationChange,
          itemLabel: "APIs",
        }}
      />
      {openAddApiModal && (
        <CreateApiDeploymentModal
          open={openAddApiModal}
          setOpen={setOpenAddApiModal}
          setTableData={setTableData}
          isEdit={isEdit}
          selectedRow={selectedRow}
          openCodeModal={setOpenCodeModal}
          setSelectedRow={setSelectedRow}
          workflowEndpointList={workflowEndpointList}
          refreshList={handleListRefresh}
        />
      )}
      <ManageKeys
        isDialogOpen={openManageKeysModal}
        setDialogOpen={setOpenManageKeysModal}
        apiKeys={apiKeys}
        setApiKeys={setApiKeys}
        selectedApiRow={selectedRow}
        apiService={apiDeploymentsApiService}
        type={deploymentApiTypes.api}
      />
      <DisplayCode
        isDialogOpen={openCodeModal}
        setDialogOpen={setOpenCodeModal}
        url={displayURL(selectedRow?.api_endpoint)}
      />
      <LogsModal
        open={openLogsModal}
        setOpen={setOpenLogsModal}
        logRecord={executionLogs}
        totalLogs={executionLogsTotalCount}
        fetchExecutionLogs={handleFetchLogs}
        loading={isFetchingLogs}
      />
      <NotificationModal
        open={openNotificationModal}
        setOpen={setOpenNotificationModal}
        type={deploymentApiTypes.api}
        id={selectedRow?.id}
      />
      <SharePermission
        open={openShareModal}
        setOpen={setOpenShareModal}
        adapter={selectedRow}
        permissionEdit={true}
        loading={isLoadingShare}
        allUsers={allUsers}
        allGroups={Array.isArray(allGroups) ? allGroups : []}
        onApply={onShare}
        isSharableToOrg={true}
      />
      <CoOwnerManagement
        open={coOwnerOpen}
        setOpen={setCoOwnerOpen}
        resourceId={coOwnerResourceId}
        resourceType="API Deployment"
        allUsers={coOwnerAllUsers}
        coOwners={coOwnerData.coOwners}
        loading={coOwnerLoading}
        onApplyCoOwners={onApplyCoOwners}
      />
    </>
  );
}

export { ApiDeployment };