Skip to content

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

UN-2541 [FIX] Prevented Prompt Studio create modal from flashing for existing users (#1327)

Athuld3b8e907e834
diff --git a/frontend/src/components/deployments/api-deployment/ApiDeployment.jsx b/frontend/src/components/deployments/api-deployment/ApiDeployment.jsx
index d62210c12..d09e02de5 100644
--- a/frontend/src/components/deployments/api-deployment/ApiDeployment.jsx
+++ b/frontend/src/components/deployments/api-deployment/ApiDeployment.jsx
@@ -10,7 +10,7 @@ import {
NotificationOutlined,
} from "@ant-design/icons";
import { Button, Dropdown, Space, Switch, Tooltip, Typography } from "antd";
-import { useEffect, useState, useCallback } from "react";
+import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { deploymentApiTypes, displayURL } from "../../../helpers/GetStaticData";
@@ -32,6 +32,10 @@ import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate.js";
import usePipelineHelper from "../../../hooks/usePipelineHelper.js";
import { NotificationModal } from "../../pipelines-or-deployments/notification-modal/NotificationModal.jsx";
import { usePromptStudioService } from "../../api/prompt-studio-service";
+import {
+ useInitialFetchCount,
+ usePromptStudioModal,
+} from "../../../hooks/usePromptStudioFetchCount";
function ApiDeployment() {
const { sessionDetails } = useSessionStore();
@@ -58,13 +62,12 @@ function ApiDeployment() {
usePipelineHelper();
const [openNotificationModal, setOpenNotificationModal] = useState(false);
const { count, isLoading, fetchCount } = usePromptStudioStore();
- const [showModal, setShowModal] = useState(false);
- const [modalDismissed, setModalDismissed] = useState(false);
const { getPromptStudioCount } = usePromptStudioService();
- useEffect(() => {
- fetchCount(getPromptStudioCount);
- }, [fetchCount]);
+ const initialFetchComplete = useInitialFetchCount(
+ fetchCount,
+ getPromptStudioCount
+ );
const handleFetchLogs = (page, pageSize) => {
fetchExecutionLogs(
@@ -386,18 +389,12 @@ function ApiDeployment() {
},
];
- useEffect(() => {
- if (!isLoading && count === 0 && !modalDismissed) {
- setShowModal(true);
- } else if (!isLoading && count > 0) {
- setShowModal(false);
- }
- }, [isLoading, count, modalDismissed]);
-
- const handleModalClose = useCallback(() => {
- setShowModal(false);
- setModalDismissed(true); // Prevent modal reopen.
- }, []); // Prevents re-renders.
+ // Using the custom hook to manage modal state
+ const { showModal, handleModalClose } = usePromptStudioModal(
+ initialFetchComplete,
+ isLoading,
+ count
+ );
return (
<>
diff --git a/frontend/src/components/pipelines-or-deployments/etl-task-deploy/EtlTaskDeploy.jsx b/frontend/src/components/pipelines-or-deployments/etl-task-deploy/EtlTaskDeploy.jsx
index 812965813..255397496 100644
--- a/frontend/src/components/pipelines-or-deployments/etl-task-deploy/EtlTaskDeploy.jsx
+++ b/frontend/src/components/pipelines-or-deployments/etl-task-deploy/EtlTaskDeploy.jsx
@@ -16,7 +16,8 @@ import { useWorkflowStore } from "../../../store/workflow-store.js";
import { getBackendErrorDetail } from "../../../helpers/GetStaticData.js";
import usePostHogEvents from "../../../hooks/usePostHogEvents.js";
import { PromptStudioModal } from "../../common/PromptStudioModal";
-import { useCallback } from "react";
+import { usePromptStudioService } from "../../api/prompt-studio-service";
+import { useInitialFetchCount } from "../../../hooks/usePromptStudioFetchCount";
const defaultFromDetails = {
pipeline_name: "",
@@ -59,14 +60,11 @@ const EtlTaskDeploy = ({
const { count, isLoadingModal, fetchCount } = usePromptStudioStore();
const [showModal, setShowModal] = useState(false);
const [modalDismissed, setModalDismissed] = useState(false);
-
- const fetchPromptCount = useCallback(() => {
- fetchCount();
- }, [fetchCount]);
-
- useEffect(() => {
- fetchPromptCount();
- }, [fetchPromptCount]);
+ const { getPromptStudioCount } = usePromptStudioService();
+ const initialFetchComplete = useInitialFetchCount(
+ fetchCount,
+ getPromptStudioCount
+ );
useEffect(() => {
if (workflowId) {
@@ -268,15 +266,19 @@ const EtlTaskDeploy = ({
};
useEffect(() => {
- if (!isLoadingModal && count === 0 && !modalDismissed) {
+ if (
+ initialFetchComplete &&
+ !isLoadingModal &&
+ count === 0 &&
+ !modalDismissed
+ ) {
setShowModal(true);
}
- }, [isLoadingModal, count, modalDismissed]);
+ }, [initialFetchComplete, isLoadingModal, count, modalDismissed]);
const handleModalClose = () => {
setShowModal(false);
setModalDismissed(true);
- fetchPromptCount();
};
return (
diff --git a/frontend/src/components/pipelines-or-deployments/pipelines/Pipelines.jsx b/frontend/src/components/pipelines-or-deployments/pipelines/Pipelines.jsx
index a501b2305..98785a5c1 100644
--- a/frontend/src/components/pipelines-or-deployments/pipelines/Pipelines.jsx
+++ b/frontend/src/components/pipelines-or-deployments/pipelines/Pipelines.jsx
@@ -22,7 +22,7 @@ import {
Typography,
} from "antd";
import PropTypes from "prop-types";
-import { useEffect, useState, useCallback } from "react";
+import { useEffect, useState } from "react";
import cronstrue from "cronstrue";
import {
@@ -47,6 +47,10 @@ import { NotificationModal } from "../notification-modal/NotificationModal.jsx";
import { usePromptStudioStore } from "../../../store/prompt-studio-store";
import { PromptStudioModal } from "../../common/PromptStudioModal";
import { usePromptStudioService } from "../../api/prompt-studio-service";
+import {
+ useInitialFetchCount,
+ usePromptStudioModal,
+} from "../../../hooks/usePromptStudioFetchCount";
function Pipelines({ type }) {
const [tableData, setTableData] = useState([]);
@@ -70,13 +74,12 @@ function Pipelines({ type }) {
usePipelineHelper();
const [openNotificationModal, setOpenNotificationModal] = useState(false);
const { count, isLoading, fetchCount } = usePromptStudioStore();
- const [showModal, setShowModal] = useState(false);
- const [modalDismissed, setModalDismissed] = useState(false);
const { getPromptStudioCount } = usePromptStudioService();
- useEffect(() => {
- fetchCount(getPromptStudioCount);
- }, [fetchCount]);
+ const initialFetchComplete = useInitialFetchCount(
+ fetchCount,
+ getPromptStudioCount
+ );
const handleFetchLogs = (page, pageSize) => {
fetchExecutionLogs(
@@ -608,18 +611,12 @@ function Pipelines({ type }) {
},
];
- useEffect(() => {
- if (!isLoading && count === 0 && !modalDismissed) {
- setShowModal(true);
- } else if (!isLoading && count > 0) {
- setShowModal(false);
- }
- }, [isLoading, count, modalDismissed]);
-
- const handleModalClose = useCallback(() => {
- setShowModal(false);
- setModalDismissed(true); // Prevent modal reopen.
- }, []); // Prevents re-renders.
+ // Using the custom hook to manage modal state
+ const { showModal, handleModalClose } = usePromptStudioModal(
+ initialFetchComplete,
+ isLoading,
+ count
+ );
return (
<div className="p-or-d-layout">
diff --git a/frontend/src/components/workflows/workflow/Workflows.jsx b/frontend/src/components/workflows/workflow/Workflows.jsx
index 25c5fd16d..bac18dcfc 100644
--- a/frontend/src/components/workflows/workflow/Workflows.jsx
+++ b/frontend/src/components/workflows/workflow/Workflows.jsx
@@ -2,7 +2,7 @@ import { PlusOutlined, UserOutlined } from "@ant-design/icons";
import { Typography } from "antd";
import isEmpty from "lodash/isEmpty";
import PropTypes from "prop-types";
-import { useEffect, useRef, useState, useCallback } from "react";
+import { useEffect, useRef, useState } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { useAlertStore } from "../../../store/alert-store";
@@ -21,6 +21,10 @@ import { ViewTools } from "../../custom-tools/view-tools/ViewTools.jsx";
import usePostHogEvents from "../../../hooks/usePostHogEvents.js";
import { PromptStudioModal } from "../../common/PromptStudioModal";
import { usePromptStudioService } from "../../api/prompt-studio-service";
+import {
+ useInitialFetchCount,
+ usePromptStudioModal,
+} from "../../../hooks/usePromptStudioFetchCount";
const PROJECT_FILTER_OPTIONS = [
{ label: "My Workflows", value: "mine" },
@@ -36,13 +40,12 @@ function Workflows() {
const handleException = useExceptionHandler();
const { setPostHogCustomEvent } = usePostHogEvents();
const { count, isLoading, fetchCount } = usePromptStudioStore();
- const [showModal, setShowModal] = useState(false);
- const [modalDismissed, setModalDismissed] = useState(false);
const { getPromptStudioCount } = usePromptStudioService();
- useEffect(() => {
- fetchCount(getPromptStudioCount);
- }, [fetchCount]);
+ const initialFetchComplete = useInitialFetchCount(
+ fetchCount,
+ getPromptStudioCount
+ );
const [projectList, setProjectList] = useState();
const [editingProject, setEditProject] = useState();
@@ -200,18 +203,12 @@ function Workflows() {
);
};
- useEffect(() => {
- if (!isLoading && count === 0 && !modalDismissed) {
- setShowModal(true);
- } else if (!isLoading && count > 0) {
- setShowModal(false);
- }
- }, [isLoading, count, modalDismissed]);
-
- const handleModalClose = useCallback(() => {
- setShowModal(false);
- setModalDismissed(true); // Prevent modal reopen.
- }, []); // Prevents re-renders.
+ // Using the custom hook to manage modal state
+ const { showModal, handleModalClose } = usePromptStudioModal(
+ initialFetchComplete,
+ isLoading,
+ count
+ );
return (
<>
diff --git a/frontend/src/hooks/usePromptStudioFetchCount.js b/frontend/src/hooks/usePromptStudioFetchCount.js
new file mode 100644
index 000000000..e35c256ac
--- /dev/null
+++ b/frontend/src/hooks/usePromptStudioFetchCount.js
@@ -0,0 +1,54 @@
+import { useEffect, useState, useCallback, useRef } from "react";
+
+/**
+ * Custom hook to fetch count and track initial fetch completion.
+ * @param {Function} fetchCount - The fetchCount function from your store.
+ * @param {Function} getPromptStudioCount - The function to get the count.
+ * @return {boolean} initialFetchComplete
+ */
+export function useInitialFetchCount(fetchCount, getPromptStudioCount) {
+ const [initialFetchComplete, setInitialFetchComplete] = useState(false);
+ const hasFetchedRef = useRef(false);
+
+ useEffect(() => {
+ // Only fetch once and prevent re-fetching
+ if (!hasFetchedRef.current) {
+ hasFetchedRef.current = true;
+ fetchCount(getPromptStudioCount).finally(() => {
+ setInitialFetchComplete(true);
+ });
+ }
+ }, [fetchCount, getPromptStudioCount]);
+
+ return initialFetchComplete;
+}
+
+/**
+ * Custom hook to manage prompt studio modal visibility based on item count
+ * @param {boolean} initialFetchComplete - Whether the initial data fetch is complete
+ * @param {boolean} isLoading - Whether data is currently loading
+ * @param {number} count - The count of items (workflows, deployments, etc.)
+ * @return {Object} - Modal state and handlers
+ */
+export function usePromptStudioModal(initialFetchComplete, isLoading, count) {
+ const [showModal, setShowModal] = useState(false);
+ const [modalDismissed, setModalDismissed] = useState(false);
+
+ useEffect(() => {
+ if (initialFetchComplete && !isLoading && count === 0 && !modalDismissed) {
+ setShowModal(true);
+ } else if (!isLoading && count > 0) {
+ setShowModal(false);
+ }
+ }, [initialFetchComplete, isLoading, count, modalDismissed]);
+
+ const handleModalClose = useCallback(() => {
+ setShowModal(false);
+ setModalDismissed(true); // Prevent modal reopen
+ }, []);
+
+ return {
+ showModal,
+ handleModalClose,
+ };
+}
diff --git a/frontend/src/store/prompt-studio-store.js b/frontend/src/store/prompt-studio-store.js
index 7e545c3f1..2195586fa 100644
--- a/frontend/src/store/prompt-studio-store.js
+++ b/frontend/src/store/prompt-studio-store.js
@@ -10,7 +10,8 @@ const usePromptStudioStore = create((set, get) => {
return {
...STORE_VARIABLES,
fetchCount: async (getPromptStudioCount) => {
- if (get().isLoading) return;
+ // Prevent duplicate calls if already loading or if we already have a count
+ if (get().isLoading || (get().count > 0 && !get().error)) return;
set({ isLoading: true });
try {