Spaces:
Sleeping
Sleeping
File size: 5,161 Bytes
a706099 f444dc0 8066d07 f444dc0 a706099 98d2bd7 a706099 98d2bd7 a706099 f444dc0 f1b7f9a f444dc0 f1b7f9a f444dc0 a706099 f444dc0 a706099 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 a706099 8d0b379 f444dc0 a706099 8d0b379 f444dc0 a706099 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import { useState } from 'react';
export const useChunkNavigation = (documentData, clearTypingAnimation) => {
const [chunkStates, setChunkStates] = useState({});
const [currentChunkIndex, setCurrentChunkIndex] = useState(0);
const [chunkExpanded, setChunkExpanded] = useState(true);
const [globalChatHistory, setGlobalChatHistory] = useState([]);
const [showChat, setShowChat] = useState(true);
const [isTransitioning, setIsTransitioning] = useState(false);
const [completedChunks, setCompletedChunks] = useState(new Set());
const goToNextChunk = () => {
if (documentData && currentChunkIndex < documentData.chunks.length - 1) {
if (clearTypingAnimation) {
clearTypingAnimation();
}
setCurrentChunkIndex(currentChunkIndex + 1);
setChunkExpanded(true);
}
};
const goToPrevChunk = () => {
if (currentChunkIndex > 0) {
if (clearTypingAnimation) {
clearTypingAnimation();
}
setCurrentChunkIndex(currentChunkIndex - 1);
setChunkExpanded(true);
}
};
const sendAutomatedMessage = async (action) => {
if (!documentData || currentChunkIndex >= documentData.chunks.length - 1) return;
setIsTransitioning(true);
const nextChunkIndex = currentChunkIndex + 1;
const nextChunk = documentData.chunks[nextChunkIndex];
// Mark current chunk as completed
setCompletedChunks(prev => new Set(prev).add(currentChunkIndex));
// Update chunk index immediately for UI feedback
setCurrentChunkIndex(nextChunkIndex);
setChunkExpanded(true);
// Check if we already have messages for this chunk
if (hasChunkMessages(nextChunkIndex)) {
// Don't generate new response, just navigate
setIsTransitioning(false);
return;
}
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: globalChatHistory,
currentChunk: documentData.chunks[currentChunkIndex]?.text || '',
nextChunk: nextChunk.text,
action: action,
document: documentData ? JSON.stringify(documentData) : ''
})
});
const data = await response.json();
addMessageToChunk(
{ role: 'assistant', content: data.content || 'Let\'s continue to the next section.' },
nextChunkIndex
);
// Clear any animations after successful response
if (clearTypingAnimation) {
clearTypingAnimation();
}
} catch (error) {
console.error('Error in automated transition:', error);
// Clear animations on error too
if (clearTypingAnimation) {
clearTypingAnimation();
}
} finally {
setIsTransitioning(false);
}
};
const skipChunk = () => {
return sendAutomatedMessage('skip');
};
const markChunkUnderstood = () => {
return sendAutomatedMessage('understood');
};
const startInteractiveLesson = (startChunkLessonFn) => {
setChunkStates(prev => ({
...prev,
[currentChunkIndex]: 'interactive'
}));
startChunkLessonFn(currentChunkIndex);
};
const setChunkAsInteractive = () => {
// No longer tracking status - this is just for compatibility
};
const updateGlobalChatHistory = (messages) => {
setGlobalChatHistory(messages);
};
const getGlobalChatHistory = () => {
return globalChatHistory;
};
const addMessageToChunk = (message, chunkIndex) => {
const messageWithChunk = { ...message, chunkIndex };
setGlobalChatHistory(prev => [...prev, messageWithChunk]);
};
const getCurrentChunkMessages = () => {
return globalChatHistory.filter(msg => msg.chunkIndex === currentChunkIndex);
};
const hasChunkMessages = (chunkIndex) => {
return globalChatHistory.some(msg => msg.chunkIndex === chunkIndex);
};
const isChunkCompleted = (chunkIndex) => {
return completedChunks.has(chunkIndex);
};
const canEditChunk = (chunkIndex) => {
return chunkIndex === currentChunkIndex && !isChunkCompleted(chunkIndex);
};
return {
chunkStates,
currentChunkIndex,
chunkExpanded,
showChat,
isTransitioning,
goToNextChunk,
goToPrevChunk,
skipChunk,
markChunkUnderstood,
startInteractiveLesson,
setChunkExpanded,
setShowChat,
setChunkAsInteractive,
updateGlobalChatHistory,
getGlobalChatHistory,
addMessageToChunk,
getCurrentChunkMessages,
hasChunkMessages,
isChunkCompleted,
canEditChunk
};
}; |