Spaces:
Sleeping
Sleeping
File size: 11,411 Bytes
0ef5c60 |
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
import React, { useState, useRef, useEffect } from 'react';
import { Button } from './ui/button';
import { Input } from './ui/input';
import { Label } from './ui/label';
import { Card } from './ui/card';
import { Separator } from './ui/separator';
import { Textarea } from './ui/textarea';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select';
import {
LogIn,
LogOut,
Download,
Sparkles,
Bookmark,
Copy
} from 'lucide-react';
import { Document, HeadingLevel, Packer, Paragraph, TextRun } from 'docx';
import type { User, SavedItem } from '../App';
import { toast } from 'sonner';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
DialogFooter,
} from './ui/dialog';
interface RightPanelProps {
user: User | null;
onLogin: (user: User) => void;
onLogout: () => void;
isLoggedIn: boolean;
onClose?: () => void;
exportResult: string;
setExportResult: (result: string) => void;
resultType: 'export' | 'quiz' | 'summary' | null;
setResultType: (type: 'export' | 'quiz' | 'summary' | null) => void;
onExport: () => void;
onSummary: () => void;
onSave: (content: string, type: 'export' | 'quiz' | 'summary') => void;
savedItems: SavedItem[];
}
export function RightPanel({ user, onLogin, onLogout, isLoggedIn, onClose, exportResult, setExportResult, resultType, setResultType, onExport, onSummary, onSave, savedItems }: RightPanelProps) {
const [showLoginForm, setShowLoginForm] = useState(false);
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [isExpanded, setIsExpanded] = useState(true);
const [isDownloading, setIsDownloading] = useState(false);
const [copied, setCopied] = useState(false);
// Check if current result is already saved
const isSaved = exportResult && resultType
? savedItems.some(item => item.content === exportResult && item.type === resultType)
: false;
const handleLogin = () => {
if (!name.trim() || !email.trim()) {
toast.error('Please fill in all fields');
return;
}
onLogin({ name: name.trim(), email: email.trim() });
setShowLoginForm(false);
setName('');
setEmail('');
toast.success(`Welcome, ${name}!`);
};
const handleLogout = () => {
onLogout();
setShowLoginForm(false);
toast.success('Logged out successfully');
};
const scrollContainerRef = useRef<HTMLDivElement>(null);
// Use native event listeners to prevent scroll propagation
useEffect(() => {
const container = scrollContainerRef.current;
if (!container) return;
const handleWheel = (e: WheelEvent) => {
// Always stop propagation to prevent scrolling other panels
e.stopPropagation();
e.stopImmediatePropagation();
// Only prevent default if we're at the boundaries
const { scrollTop, scrollHeight, clientHeight } = container;
const isScrollable = scrollHeight > clientHeight;
const isAtTop = scrollTop === 0;
const isAtBottom = scrollTop + clientHeight >= scrollHeight - 1;
// If scrolling up at top or down at bottom, prevent default to stop propagation
if (isScrollable && ((isAtTop && e.deltaY < 0) || (isAtBottom && e.deltaY > 0))) {
e.preventDefault();
}
};
container.addEventListener('wheel', handleWheel, { passive: false, capture: true });
return () => {
container.removeEventListener('wheel', handleWheel, { capture: true });
};
}, []);
const downloadBlob = (blob: Blob, filename: string) => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
};
const formatDateStamp = () => {
const d = new Date();
const yyyy = d.getFullYear();
const mm = String(d.getMonth() + 1).padStart(2, '0');
const dd = String(d.getDate()).padStart(2, '0');
return `${yyyy}-${mm}-${dd}`;
};
const getDefaultFilenameBase = () => {
const kind =
resultType === 'export' ? 'export' :
resultType === 'summary' ? 'summary' :
'result';
return `clare-${kind}-${formatDateStamp()}`;
};
const handleDownloadMd = async () => {
if (!exportResult) return;
try {
setIsDownloading(true);
toast.message('Preparing .md…');
const blob = new Blob([exportResult], { type: 'text/markdown;charset=utf-8' });
downloadBlob(blob, `${getDefaultFilenameBase()}.md`);
toast.success('Downloaded .md');
} catch (e) {
console.error(e);
toast.error('Failed to download .md');
} finally {
setIsDownloading(false);
}
};
const handleDownloadDocx = async () => {
if (!exportResult) return;
try {
setIsDownloading(true);
toast.message('Preparing .docx…');
const lines = exportResult.split('\n');
const paragraphs: Paragraph[] = lines.map((line) => {
const trimmed = line.trim();
if (!trimmed) return new Paragraph({ text: '' });
// Basic markdown-ish heading support
if (trimmed.startsWith('### ')) {
return new Paragraph({ text: trimmed.replace(/^###\s+/, ''), heading: HeadingLevel.HEADING_3 });
}
if (trimmed.startsWith('## ')) {
return new Paragraph({ text: trimmed.replace(/^##\s+/, ''), heading: HeadingLevel.HEADING_2 });
}
if (trimmed.startsWith('# ')) {
return new Paragraph({ text: trimmed.replace(/^#\s+/, ''), heading: HeadingLevel.HEADING_1 });
}
return new Paragraph({ children: [new TextRun({ text: line })] });
});
const doc = new Document({
sections: [{ properties: {}, children: paragraphs }],
});
const blob = await Packer.toBlob(doc);
downloadBlob(blob, `${getDefaultFilenameBase()}.docx`);
toast.success('Downloaded .docx');
} catch (e) {
console.error(e);
toast.error('Failed to download .docx');
} finally {
setIsDownloading(false);
}
};
return (
<div
ref={scrollContainerRef}
className="flex-1 overflow-auto overscroll-contain flex flex-col"
style={{ overscrollBehavior: 'contain' }}
>
<div className="p-4 space-y-4">
{isExpanded && (
<>
{/* Actions Section with Results */}
<div className="space-y-3">
<h3 className="text-base font-medium">Export / Summarize Conversation</h3>
<Card className="p-4 bg-muted/30">
<div className="flex flex-col gap-3">
<Button
variant="outline"
className="w-full h-12 rounded-lg justify-start gap-3"
onClick={onExport}
disabled={!isLoggedIn}
>
<Download className="h-5 w-5" />
<span>Export</span>
</Button>
<Button
variant="outline"
className="w-full h-12 rounded-lg justify-start gap-3"
onClick={onSummary}
disabled={!isLoggedIn}
>
<Sparkles className="h-5 w-5" />
<span>Summarize</span>
</Button>
{/* Results - Expanded from buttons */}
{exportResult && (
<>
<Separator className="my-2" />
<div className="space-y-3">
<div className="flex items-center justify-between gap-2">
<h4 className="text-base font-bold">
{resultType === 'export' && 'Exported Conversation'}
{resultType === 'quiz' && 'Micro-Quiz'}
{resultType === 'summary' && 'Summarization'}
</h4>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
disabled={isDownloading}
onClick={handleDownloadMd}
title="Download as .md"
className="h-7 px-2 text-xs gap-1.5"
>
<Download className="h-3 w-3" />
.md
</Button>
<Button
variant="outline"
size="sm"
disabled={isDownloading}
onClick={handleDownloadDocx}
title="Download as .docx"
className="h-7 px-2 text-xs gap-1.5"
>
<Download className="h-3 w-3" />
.docx
</Button>
<Button
variant="outline"
size="sm"
onClick={async () => {
await navigator.clipboard.writeText(exportResult);
setCopied(true);
toast.success('Copied to clipboard!');
setTimeout(() => setCopied(false), 2000);
}}
disabled={isDownloading}
className="h-7 px-2 text-xs gap-1.5"
title="Copy"
>
<Copy className="h-3 w-3" />
</Button>
{resultType && (
<Button
variant="outline"
size="sm"
onClick={() => {
if (resultType) {
onSave(exportResult, resultType);
}
}}
disabled={isDownloading || !resultType}
className={`h-7 px-2 text-xs gap-1.5 ${isSaved ? 'bg-red-50 dark:bg-red-950/20 border-red-300 dark:border-red-800' : ''}`}
title={isSaved ? 'Unsave' : 'Save for later'}
>
<Bookmark className={`h-3 w-3 ${isSaved ? 'fill-red-600 text-red-600' : ''}`} />
</Button>
)}
</div>
</div>
<div className={`text-sm whitespace-pre-wrap text-foreground p-3 rounded-lg ${
isSaved ? 'bg-red-50/50 dark:bg-red-950/10' : ''
}`}>
{exportResult}
</div>
</div>
</>
)}
</div>
</Card>
</div>
</>
)}
</div>
</div>
);
} |