Skip to content

Commit d151e6d

Browse files
athul-rsclaude
andcommitted
refactor: Fix CodeRabbit major issues - state carryover and useCallback pattern
Fixed two major issues identified by CodeRabbit review: 1. Fixed state carryover bug in custom-tool-store.js - When switching tools, deploymentUsageInfo and lastExportedAt now properly reset to null instead of carrying over from previous tool - Prevents incorrect export reminders showing for wrong projects 2. Fixed useCallback pattern issue in ToolIde.jsx - Replaced isCheckingUsage state in useCallback deps with useRef - Prevents unnecessary callback recreations and potential race conditions - Simplified useEffect dependencies to only depend on the callback - Removed unused isCheckingUsage state variable These changes improve code quality and prevent potential bugs without affecting functionality. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent afde3a8 commit d151e6d

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

frontend/src/components/custom-tools/tool-ide/ToolIde.jsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Col, Row } from "antd";
2-
import { useState, useEffect, useCallback } from "react";
2+
import { useState, useEffect, useCallback, useRef } from "react";
33

44
import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";
55
import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
@@ -63,8 +63,8 @@ function ToolIde() {
6363
const [openShareModal, setOpenShareModal] = useState(false);
6464
const [openCloneModal, setOpenCloneModal] = useState(false);
6565
const [showExportReminder, setShowExportReminder] = useState(false);
66-
const [isCheckingUsage, setIsCheckingUsage] = useState(false);
6766
const [isExporting, setIsExporting] = useState(false);
67+
const isCheckingUsageRef = useRef(false);
6868

6969
useEffect(() => {
7070
if (openShareModal) {
@@ -87,11 +87,11 @@ function ToolIde() {
8787

8888
// Check deployment usage when there are unsaved changes
8989
const checkDeploymentUsage = useCallback(async () => {
90-
if (!details?.tool_id || !hasUnsavedChanges || isCheckingUsage) {
90+
if (!details?.tool_id || !hasUnsavedChanges || isCheckingUsageRef.current) {
9191
return;
9292
}
9393

94-
setIsCheckingUsage(true);
94+
isCheckingUsageRef.current = true;
9595
try {
9696
const response = await axiosPrivate.get(
9797
`/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/${details?.tool_id}/check_deployment_usage/`
@@ -110,20 +110,19 @@ function ToolIde() {
110110
console.error("Error checking deployment usage:", error);
111111
setShowExportReminder(false);
112112
} finally {
113-
setIsCheckingUsage(false);
113+
isCheckingUsageRef.current = false;
114114
}
115115
}, [
116116
details?.tool_id,
117117
hasUnsavedChanges,
118-
isCheckingUsage,
119118
axiosPrivate,
120119
sessionDetails?.orgId,
121120
setDeploymentUsageInfo,
122121
]);
123122

124123
useEffect(() => {
125124
checkDeploymentUsage();
126-
}, [checkDeploymentUsage, hasUnsavedChanges, details?.tool_id]);
125+
}, [checkDeploymentUsage]);
127126

128127
// Handle export from reminder bar
129128
const handleExportFromReminder = useCallback(async () => {

frontend/src/store/custom-tool-store.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ const useCustomToolStore = create((setState, getState) => ({
5353
},
5454
setCustomTool: (entireState) => {
5555
// Reset unsaved changes when loading a new tool
56-
setState({ ...entireState, hasUnsavedChanges: false });
56+
setState({
57+
...entireState,
58+
hasUnsavedChanges: false,
59+
deploymentUsageInfo: entireState?.deploymentUsageInfo ?? null,
60+
lastExportedAt: entireState?.lastExportedAt ?? null,
61+
});
5762
},
5863
updateCustomTool: (entireState) => {
5964
setState((state) => ({ state, ...entireState }));

0 commit comments

Comments
 (0)