Skip to content

Commit d66c0b7

Browse files
authored
Merge pull request #1424 from stakwork/fix-refreshChatTitle
fix refreshChatTitle
2 parents a0e48dd + e1bada4 commit d66c0b7

File tree

2 files changed

+27
-59
lines changed

2 files changed

+27
-59
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"start:cypress": "PORT=3007 craco start",
1515
"build": "craco build",
1616
"eject": "craco eject",
17-
"lint": "eslint src --max-warnings 66 --ext .ts --ext .tsx --ignore-pattern *.spec.tsx --ignore-pattern *.spec.ts",
17+
"lint": "eslint src --max-warnings 67 --ext .ts --ext .tsx --ignore-pattern *.spec.tsx --ignore-pattern *.spec.ts",
1818
"lint:fix": "npm run lint -- --fix",
1919
"prettier": "npx prettier --config .prettierrc.json -w ./src ./cypress",
2020
"prettier:check": "npx prettier --config .prettierrc.json --check ./src",
@@ -219,4 +219,4 @@
219219
"@types/react-syntax-highlighter": "^15.5.13",
220220
"cypress": "^13.6.3"
221221
}
222-
}
222+
}

src/people/hiveChat/index.tsx

+25-57
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,6 @@ export const HiveChatView: React.FC = observer(() => {
628628
const lastRefreshTime = useRef<number>(Date.now()).current;
629629
const refreshIntervalRef = useRef<NodeJS.Timeout | null>(null);
630630
const [isRefreshingTitle, setIsRefreshingTitle] = useState(false);
631-
const [lastTitleRefreshTime, setLastTitleRefreshTime] = useState(Date.now());
632-
const titleRefreshIntervalRef = useRef<NodeJS.Timeout | null>(null);
633631
const [sseLogs, setSseLogs] = useState<SSEMessage[]>([]);
634632
useBrowserTabTitle('Hive Chat');
635633

@@ -908,7 +906,7 @@ export const HiveChatView: React.FC = observer(() => {
908906
setIsChainVisible(false);
909907
setIsActionSend(false);
910908

911-
if (data.artifacts.length === 0) {
909+
if (data.artifacts?.length === 0) {
912910
setLogs([]);
913911
setLastLogLine('');
914912
}
@@ -947,7 +945,7 @@ export const HiveChatView: React.FC = observer(() => {
947945
}, [projectId, chatId, isVerboseLoggingEnabled, isActionSend]);
948946

949947
useEffect(() => {
950-
if (logs.length > 0) {
948+
if (logs?.length > 0) {
951949
setLastLogLine(logs[logs.length - 1]?.message || '');
952950
}
953951
}, [logs]);
@@ -1089,7 +1087,7 @@ export const HiveChatView: React.FC = observer(() => {
10891087

10901088
useEffect(() => {
10911089
const checkForNewArtifacts = async () => {
1092-
if (!chatId || !isArtifactLoggingEnabled || messages.length === 0) return;
1090+
if (!chatId || !isArtifactLoggingEnabled || messages?.length === 0) return;
10931091

10941092
const latestMessage = messages[messages.length - 1];
10951093
if (latestMessage && latestMessage.id !== lastProcessedMessageId) {
@@ -1148,7 +1146,7 @@ export const HiveChatView: React.FC = observer(() => {
11481146
hasTextUpdates ? 'text' : null
11491147
].filter(Boolean) as ('visual' | 'code' | 'text' | 'logs')[];
11501148

1151-
if (availableTabs.length > 0 && !availableTabs.includes(artifactTab)) {
1149+
if (availableTabs?.length > 0 && !availableTabs.includes(artifactTab)) {
11521150
setArtifactTab(availableTabs[0]);
11531151
}
11541152
}
@@ -1344,64 +1342,38 @@ export const HiveChatView: React.FC = observer(() => {
13441342
dividerRef.current?.focus();
13451343
};
13461344

1347-
useEffect(() => {
1348-
const handleMouseMove = (e: MouseEvent) => {
1349-
if (!isDraggingRef.current) return;
1350-
1351-
const containerWidth = containerRef.current?.clientWidth || 1000;
1352-
const deltaX = e.clientX - startXRef.current;
1353-
const deltaPercentage = (deltaX / containerWidth) * 100;
1354-
1355-
let newChatWidth = startChatWidthRef.current + deltaPercentage;
1356-
1357-
newChatWidth = Math.max(20, Math.min(80, newChatWidth));
1358-
const newViewerWidth = 100 - newChatWidth;
1359-
1360-
setChatSectionWidth(`${newChatWidth}%`);
1361-
setViewerSectionWidth(`${newViewerWidth}%`);
1362-
};
1363-
1364-
const handleMouseUp = () => {
1365-
if (isDraggingRef.current) {
1366-
isDraggingRef.current = false;
1367-
document.body.style.cursor = '';
1368-
document.body.style.userSelect = '';
1369-
1370-
localStorage.setItem('hiveChatSectionWidth', chatSectionWidth);
1371-
localStorage.setItem('hiveViewerSectionWidth', viewerSectionWidth);
1372-
}
1373-
};
1374-
1375-
document.addEventListener('mousemove', handleMouseMove);
1376-
document.addEventListener('mouseup', handleMouseUp);
1377-
1378-
return () => {
1379-
document.removeEventListener('mousemove', handleMouseMove);
1380-
document.removeEventListener('mouseup', handleMouseUp);
1381-
};
1382-
}, [chatSectionWidth, viewerSectionWidth]);
1345+
// Initialize refs at the top of your component
1346+
const lastTitleRefreshTimeRef = useRef(Date.now());
1347+
const titleRefreshIntervalRef = useRef<NodeJS.Timeout | null>(null);
1348+
const isRefreshingTitleRef = useRef(false);
13831349

13841350
const refreshChatTitle = useCallback(async () => {
1385-
if (isEditingTitle || isRefreshingTitle) return;
1351+
// Use ref for internal state check to avoid dependency cycle
1352+
if (isEditingTitle || isRefreshingTitleRef.current) return;
13861353

13871354
try {
1355+
// Track refreshing state in ref
1356+
isRefreshingTitleRef.current = true;
1357+
// Also update state for UI purposes
13881358
setIsRefreshingTitle(true);
13891359

1390-
const currentChat = await chat.getWorkspaceChats(uuid as string);
1360+
const currentChat = await chat.getWorkspaceChats(uuid);
13911361
const updatedChat = currentChat?.find((c) => c.id === chatId);
13921362

13931363
if (updatedChat?.title && updatedChat.title !== title) {
13941364
setTitle(updatedChat.title);
13951365
chat.updateChat(chatId, { title: updatedChat.title });
13961366
}
13971367

1398-
setLastTitleRefreshTime(Date.now());
1368+
// Update timestamp ref
1369+
lastTitleRefreshTimeRef.current = Date.now();
13991370
} catch (error) {
14001371
console.error('Error refreshing chat title:', error);
14011372
} finally {
1373+
isRefreshingTitleRef.current = false;
14021374
setIsRefreshingTitle(false);
14031375
}
1404-
}, [chat, chatId, uuid, title, isEditingTitle, isRefreshingTitle]);
1376+
}, [chat, chatId, uuid, title, isEditingTitle]); // Removed isRefreshingTitle from deps
14051377

14061378
useEffect(() => {
14071379
if (!chatId || !uuid) return;
@@ -1415,7 +1387,7 @@ export const HiveChatView: React.FC = observer(() => {
14151387

14161388
titleRefreshIntervalRef.current = setInterval(() => {
14171389
if (document.visibilityState === 'visible' && !isEditingTitle) {
1418-
if (Date.now() - lastTitleRefreshTime > 1000) {
1390+
if (Date.now() - lastTitleRefreshTimeRef.current > 1000) {
14191391
refreshChatTitle();
14201392
}
14211393
}
@@ -1430,18 +1402,14 @@ export const HiveChatView: React.FC = observer(() => {
14301402
titleRefreshIntervalRef.current = null;
14311403
}
14321404
};
1433-
}, [chatId, uuid, refreshChatTitle, isEditingTitle, lastTitleRefreshTime]);
1405+
}, [chatId, uuid, refreshChatTitle, isEditingTitle]);
14341406

14351407
useEffect(() => {
1436-
const refreshTitleOnFocus = async () => {
1437-
try {
1438-
if (document.visibilityState === 'visible' && !isEditingTitle) {
1439-
if (Date.now() - lastTitleRefreshTime > 1000) {
1440-
await refreshChatTitle();
1441-
}
1408+
const refreshTitleOnFocus = () => {
1409+
if (document.visibilityState === 'visible' && !isEditingTitle) {
1410+
if (Date.now() - lastTitleRefreshTimeRef.current > 1000) {
1411+
refreshChatTitle();
14421412
}
1443-
} catch (error) {
1444-
console.error('Error refreshing chat title on focus:', error);
14451413
}
14461414
};
14471415

@@ -1452,7 +1420,7 @@ export const HiveChatView: React.FC = observer(() => {
14521420
window.removeEventListener('visibilitychange', refreshTitleOnFocus);
14531421
window.removeEventListener('focus', refreshTitleOnFocus);
14541422
};
1455-
}, [refreshChatTitle, isEditingTitle, lastTitleRefreshTime]);
1423+
}, [refreshChatTitle, isEditingTitle]);
14561424

14571425
const handleMinimizeToggle = () => {
14581426
setIsMinimized((prev) => {

0 commit comments

Comments
 (0)