Spaces:
Sleeping
Sleeping
File size: 9,841 Bytes
a706099 f444dc0 98d2bd7 f444dc0 8d0b379 a706099 8d0b379 f444dc0 8066d07 f444dc0 a706099 8d0b379 a706099 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 8066d07 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 8066d07 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 a706099 8066d07 f444dc0 a706099 f444dc0 a706099 98d2bd7 8066d07 98d2bd7 8066d07 f444dc0 8066d07 98d2bd7 8066d07 98d2bd7 8066d07 98d2bd7 8066d07 98d2bd7 8066d07 98d2bd7 8066d07 a706099 f444dc0 8d0b379 f444dc0 8d0b379 f444dc0 8d0b379 98d2bd7 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
import ReactMarkdown from 'react-markdown';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import rehypeRaw from 'rehype-raw';
import { getChatMarkdownComponents, getTitleMarkdownComponents } from '../utils/markdownComponents.jsx';
import SimpleChat from './SimpleChat.jsx';
import ChunkLoadingTips from './ChunkLoadingTips.jsx';
import React, { useState, useEffect } from 'react';
const ChunkPanel = ({
documentData,
currentChunkIndex,
showChat,
isTransitioning,
updateGlobalChatHistory,
getGlobalChatHistory,
addMessageToChunk,
getCurrentChunkMessages,
hasChunkMessages,
isChunkCompleted,
canEditChunk,
setWaitingForFirstResponse,
markChunkUnderstood,
skipChunk,
goToPrevChunk
}) => {
const chatMarkdownComponents = getChatMarkdownComponents();
const titleMarkdownComponents = getTitleMarkdownComponents();
const [isLoading, setIsLoading] = useState(false);
// Generate greeting for chunks that don't have messages yet
// Only for initial chunk (0) and when not transitioning
useEffect(() => {
if (documentData && showChat && !hasChunkMessages(currentChunkIndex) && currentChunkIndex === 0 && !isTransitioning) {
generateGreeting();
}
}, [currentChunkIndex, documentData, showChat, isTransitioning]);
const generateGreeting = async () => {
setIsLoading(true);
if (setWaitingForFirstResponse) {
setWaitingForFirstResponse(true);
}
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [],
currentChunk: documentData?.chunks?.[currentChunkIndex]?.text || '',
document: documentData ? JSON.stringify(documentData) : ''
})
});
const data = await response.json();
addMessageToChunk(
{
role: 'assistant',
content: data.content || 'Hi! Welcome to your learning session. Let\'s explore this document together!'
},
currentChunkIndex
);
} catch (error) {
console.error('Error generating greeting:', error);
addMessageToChunk(
{
role: 'assistant',
content: 'Hi! Welcome to your learning session. Let\'s explore this document together!'
},
currentChunkIndex
);
} finally {
setIsLoading(false);
if (setWaitingForFirstResponse) {
setWaitingForFirstResponse(false);
}
}
};
const handleSend = async (text) => {
const userMessage = { role: 'user', content: text, chunkIndex: currentChunkIndex };
addMessageToChunk(userMessage, currentChunkIndex);
setIsLoading(true);
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: getGlobalChatHistory(),
currentChunk: documentData?.chunks?.[currentChunkIndex]?.text || '',
document: documentData ? JSON.stringify(documentData) : ''
})
});
const data = await response.json();
addMessageToChunk(
{ role: 'assistant', content: data.content || 'Sorry, no response received.' },
currentChunkIndex
);
} catch (error) {
console.error('Error:', error);
addMessageToChunk(
{ role: 'assistant', content: 'Sorry, something went wrong. Please try again.' },
currentChunkIndex
);
} finally {
setIsLoading(false);
}
};
return (
<>
{/* Chunk Header */}
<div className="px-6 py-4 flex-shrink-0 bg-white rounded-t-lg border-b border-gray-200 z-10 flex items-center justify-between">
<div className="flex items-center flex-1">
{/* Previous Chunk Button */}
<button
onClick={goToPrevChunk}
disabled={currentChunkIndex === 0}
className="mr-3 p-2 rounded-full bg-gray-100 hover:bg-gray-200 text-gray-600 transition-colors duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
title="Go to previous chunk"
>
<svg
className="w-5 h-5"
fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z"
clipRule="evenodd"
/>
</svg>
</button>
{/* Chunk Title */}
<div className="font-semibold text-xl text-gray-900 text-left">
<ReactMarkdown
remarkPlugins={[remarkMath]}
rehypePlugins={[rehypeRaw, rehypeKatex]}
components={titleMarkdownComponents}
>
{documentData?.chunks?.[currentChunkIndex]?.topic || "Loading..."}
</ReactMarkdown>
</div>
</div>
<div className="flex items-center gap-2">
{/* Skip Button */}
<button
onClick={skipChunk}
className="p-2 rounded-full bg-gray-100 hover:bg-gray-200 text-gray-600 transition-colors duration-200"
title="Skip this chunk"
>
<svg
className="w-5 h-5"
fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clipRule="evenodd"
/>
<path
fillRule="evenodd"
d="M12.293 14.707a1 1 0 010-1.414L15.586 10l-3.293-3.293a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clipRule="evenodd"
/>
</svg>
</button>
{/* Understood Button */}
<button
onClick={markChunkUnderstood}
className="p-2 rounded-full bg-green-100 hover:bg-green-200 text-green-600 transition-colors duration-200"
title="Mark chunk as understood"
>
<svg
className="w-5 h-5"
fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
clipRule="evenodd"
/>
</svg>
</button>
</div>
</div>
{/* Chat Interface - Only shown when showChat is true and not transitioning */}
{showChat && !isLoading && !isTransitioning && (
<SimpleChat
messages={getGlobalChatHistory()}
currentChunkIndex={currentChunkIndex}
canEdit={canEditChunk(currentChunkIndex)}
onSend={handleSend}
isLoading={isLoading || isTransitioning}
/>
)}
{/* Loading Tips - Shown when generating greeting */}
{showChat && isLoading && !hasChunkMessages(currentChunkIndex) && (
<ChunkLoadingTips message="Preparing your lesson..." />
)}
{/* Transition Loading - Shown when moving between chunks */}
{showChat && isTransitioning && (
<ChunkLoadingTips message="Transitioning to next topic..." />
)}
</>
);
};
export default ChunkPanel; |