File size: 13,188 Bytes
99191cb a2befa6 4afc549 99191cb a2dfa54 99191cb 407ba4e b438982 4afc549 99191cb a2dfa54 407ba4e a2befa6 b438982 4afc549 0c51808 9c1858c 407ba4e 70c6694 4afc549 407ba4e ec4e9ef 407ba4e 4afc549 ec4e9ef 407ba4e 4afc549 407ba4e 4afc549 407ba4e ec4e9ef 4afc549 ec4e9ef 407ba4e ec4e9ef 407ba4e 744336f 407ba4e ec4e9ef 407ba4e 70c6694 407ba4e 70c6694 407ba4e 99191cb a2befa6 9c1858c a2befa6 99191cb a2dfa54 9c1858c 4afc549 99191cb a2dfa54 99191cb 4afc549 99191cb 4afc549 99191cb a2dfa54 4afc549 a2dfa54 4afc549 a2dfa54 4afc549 a2dfa54 99191cb a2dfa54 99191cb a2dfa54 4afc549 a2dfa54 99191cb a2dfa54 99191cb 164f198 99191cb 164f198 99191cb a2dfa54 99191cb 407ba4e 4afc549 407ba4e 70c6694 4afc549 70c6694 407ba4e 70c6694 407ba4e 56e9e05 70c6694 99191cb 407ba4e 4afc549 407ba4e 99191cb a2dfa54 99191cb 4afc549 99191cb a2dfa54 99191cb 4afc549 a2dfa54 99191cb 407ba4e a2dfa54 4afc549 a2dfa54 407ba4e 99191cb 407ba4e a2dfa54 99191cb a2dfa54 99191cb a2dfa54 99191cb a2dfa54 99191cb 407ba4e 99191cb a2dfa54 4afc549 a2dfa54 164f198 a2dfa54 4afc549 a2dfa54 99191cb a2dfa54 99191cb 4afc549 a2dfa54 164f198 a2dfa54 99191cb 4afc549 |
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
import { useEffect, useState, useRef } from 'react';
import { useParams, useNavigate, useLocation } from 'react-router-dom';
import './ComicReader.css';
interface ComicImage {
index: number;
imageUrl: string;
}
interface ChapterInfo {
slug: string;
chapter: string;
}
interface ReaderData {
slug: string;
title: string;
images: ComicImage[];
totalImages: number;
prevChapter?: ChapterInfo | null;
nextChapter?: ChapterInfo | null;
allChapters?: ChapterInfo[];
}
export function ComicReader() {
const { sessionId } = useParams();
const navigate = useNavigate();
const location = useLocation();
const [data, setData] = useState<ReaderData | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const [currentPage, setCurrentPage] = useState(1);
const [showMenu, setShowMenu] = useState(false);
const [showControls, setShowControls] = useState(true);
const [showPageJump, setShowPageJump] = useState(false);
const [jumpPage, setJumpPage] = useState('');
const [loadingChapter, setLoadingChapter] = useState(false);
const [password, setPassword] = useState<string>('');
const [originalSessionId, setOriginalSessionId] = useState<string>('');
const imageRefs = useRef<{ [key: number]: HTMLImageElement | null }>({});
const controlsTimeout = useRef<number | null>(null);
const scrollContainerRef = useRef<HTMLDivElement | null>(null);
const wsRef = useRef<WebSocket | null>(null);
const chapterData = location.state?.chapterData;
useEffect(() => {
const statePassword = location.state?.password;
const stateSessionId = location.state?.sessionId;
if (statePassword) {
setPassword(statePassword);
}
if (stateSessionId) {
setOriginalSessionId(stateSessionId);
}
}, [location.state]);
useEffect(() => {
if (!password || !originalSessionId) return;
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${window.location.host}/ws`;
const ws = new WebSocket(wsUrl);
let isConnected = false;
wsRef.current = ws;
ws.onopen = () => {
isConnected = true;
};
ws.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
if (message.type === 'chapter_data_response') {
setLoadingChapter(false);
if (message.success && message.data) {
const newData = {
...message.data,
allChapters: message.data.allChapters || data?.allChapters || []
};
setData(newData);
setCurrentPage(1);
scrollContainerRef.current?.scrollTo({ top: 0 });
} else {
setError(message.error || 'Failed to load chapter');
setTimeout(() => setError(''), 3000);
}
}
if (message.type === 'session_force_closed') {
setError('Session dihapus oleh admin. Silakan buat session baru.');
sessionStorage.removeItem(`comic_${sessionId}`);
setTimeout(() => {
navigate('/');
}, 3000);
}
} catch (err) {
console.error('[Reader WS] Parse error:', err);
}
};
ws.onerror = (err) => {
console.error('[Reader WS] Error:', err);
setLoadingChapter(false);
if (!isConnected) {
setError('Failed to connect to server');
}
};
ws.onclose = () => {
wsRef.current = null;
};
return () => {
if (ws.readyState === WebSocket.OPEN) {
ws.close();
}
};
}, [password, originalSessionId]);
useEffect(() => {
if (chapterData) {
setData(chapterData);
setLoading(false);
return;
}
if (!password || !originalSessionId) {
setError('Unauthorized access - please open from comic page');
setLoading(false);
return;
}
setError('Please open chapter from comic page');
setLoading(false);
}, [sessionId, password, chapterData, originalSessionId]);
useEffect(() => {
const resetTimeout = () => {
if (controlsTimeout.current) {
clearTimeout(controlsTimeout.current);
}
setShowControls(true);
controlsTimeout.current = window.setTimeout(() => {
setShowControls(false);
setShowMenu(false);
}, 3000);
};
const handleMove = () => resetTimeout();
window.addEventListener('mousemove', handleMove);
window.addEventListener('touchstart', handleMove);
return () => {
window.removeEventListener('mousemove', handleMove);
window.removeEventListener('touchstart', handleMove);
if (controlsTimeout.current) {
clearTimeout(controlsTimeout.current);
}
};
}, []);
useEffect(() => {
const handleScroll = () => {
if (!scrollContainerRef.current) return;
const container = scrollContainerRef.current;
const windowHeight = container.clientHeight;
let currentIdx = 1;
for (let i = 1; i <= (data?.totalImages || 0); i++) {
const img = imageRefs.current[i];
if (img) {
const rect = img.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
const imgCenter = rect.top + rect.height / 2 - containerRect.top;
if (imgCenter > 0 && imgCenter < windowHeight) {
currentIdx = i;
break;
}
}
}
setCurrentPage(currentIdx);
};
const container = scrollContainerRef.current;
if (container) {
container.addEventListener('scroll', handleScroll);
return () => container.removeEventListener('scroll', handleScroll);
}
}, [data]);
const scrollToPage = (page: number) => {
const img = imageRefs.current[page];
if (img && scrollContainerRef.current) {
const container = scrollContainerRef.current;
const containerRect = container.getBoundingClientRect();
const imgRect = img.getBoundingClientRect();
const scrollTo = container.scrollTop + (imgRect.top - containerRect.top);
container.scrollTo({ top: scrollTo, behavior: 'smooth' });
}
};
const nextChapter = () => {
if (data?.nextChapter) {
handleChapterNavigation(data.nextChapter.slug);
}
};
const prevChapter = () => {
if (data?.prevChapter) {
handleChapterNavigation(data.prevChapter.slug);
}
};
const handlePageJump = () => {
const page = parseInt(jumpPage);
if (page >= 1 && page <= (data?.totalImages || 1)) {
scrollToPage(page);
setShowPageJump(false);
setJumpPage('');
}
};
const handleChapterNavigation = (chapterSlug: string) => {
if (!password || !originalSessionId) {
setError('Missing authentication data');
return;
}
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) {
if (wsRef.current?.readyState === WebSocket.CONNECTING) {
setTimeout(() => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
handleChapterNavigation(chapterSlug);
} else {
setError('Connection not ready. Please try again.');
}
}, 1000);
return;
}
setError('Connection lost. Please refresh the page.');
console.error('[Reader] WebSocket not connected. State:', wsRef.current?.readyState);
return;
}
setLoadingChapter(true);
setShowMenu(false);
const requestData = {
type: 'request_chapter_data',
sessionId: originalSessionId,
password: password,
chapterSlug: chapterSlug,
allChapters: data?.allChapters || []
};
wsRef.current.send(JSON.stringify(requestData));
};
if (loading) {
return (
<div className="reader-loading">
<div className="spinner"></div>
<p>Loading chapter...</p>
</div>
);
}
if (loadingChapter) {
return (
<div className="reader-loading">
<div className="spinner"></div>
<p>Loading chapter...</p>
</div>
);
}
if (error || !data) {
return (
<div className="reader-error">
<div className="error-icon">⚠️</div>
<h2>Error</h2>
<p>{error || 'Failed to load chapter'}</p>
<button onClick={() => navigate(-1)}>Go Back</button>
</div>
);
}
return (
<div className={`comic-reader ${!showControls ? 'hide-controls' : ''}`}>
<div className="reader-header">
<button className="back-btn" onClick={() => navigate(-1)}>
← Back
</button>
<div className="reader-title">
<h1>{data.title}</h1>
<span className="page-indicator">
{currentPage} / {data.totalImages}
</span>
</div>
<button
className="menu-btn"
onClick={() => setShowMenu(!showMenu)}
>
☰
</button>
</div>
{showMenu && (
<div className="hamburger-menu" onClick={() => setShowMenu(false)}>
<div className="menu-content" onClick={(e) => e.stopPropagation()}>
<button onClick={() => setShowMenu(false)} className="close-menu">✕</button>
<div className="menu-section">
<h3>Navigation</h3>
<button onClick={() => { scrollToPage(1); setShowMenu(false); }}>
⏮️ First Page
</button>
<button onClick={() => { scrollToPage(data.totalImages); setShowMenu(false); }}>
⏭️ Last Page
</button>
<button onClick={() => { setShowPageJump(true); setShowMenu(false); }}>
📍 Jump to Page
</button>
</div>
{(data.prevChapter || data.nextChapter) && (
<div className="menu-section">
<h3>Chapters</h3>
{data.prevChapter && (
<button onClick={() => handleChapterNavigation(data.prevChapter!.slug)}>
⬅️ {data.prevChapter.chapter}
</button>
)}
{data.nextChapter && (
<button onClick={() => handleChapterNavigation(data.nextChapter!.slug)}>
➡️ {data.nextChapter.chapter}
</button>
)}
</div>
)}
<div className="menu-section">
<h3>Info</h3>
<div className="menu-info">
<p>Total Pages: {data.totalImages}</p>
<p>Current: Page {currentPage}</p>
</div>
</div>
</div>
</div>
)}
{showPageJump && (
<div className="page-jump-modal" onClick={() => setShowPageJump(false)}>
<div className="page-jump-content" onClick={(e) => e.stopPropagation()}>
<h3>Jump to Page</h3>
<input
type="number"
min="1"
max={data.totalImages}
value={jumpPage}
onChange={(e) => setJumpPage(e.target.value)}
placeholder={`1-${data.totalImages}`}
autoFocus
onKeyPress={(e) => e.key === 'Enter' && handlePageJump()}
/>
<div className="page-jump-actions">
<button onClick={handlePageJump}>Go</button>
<button onClick={() => setShowPageJump(false)}>Cancel</button>
</div>
</div>
</div>
)}
<div className="reader-content" ref={scrollContainerRef}>
<div className="scroll-container">
{data.images.map((img) => (
<img
key={img.index}
ref={(el) => (imageRefs.current[img.index] = el)}
src={img.imageUrl}
alt={`Page ${img.index}`}
className="comic-page"
loading="lazy"
onError={(e) => {
(e.target as HTMLImageElement).src = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="800" height="1200"><rect width="800" height="1200" fill="%23222"/><text x="50%" y="50%" text-anchor="middle" fill="%23666" font-size="20">Failed to load page ' + img.index + '</text></svg>';
}}
/>
))}
</div>
</div>
<div className="reader-footer">
<div className="page-controls">
<button
className="nav-control-btn"
onClick={prevChapter}
disabled={!data.prevChapter}
>
← Prev
</button>
<div className="page-info">
<span>{currentPage} / {data.totalImages}</span>
<input
type="range"
min="1"
max={data.totalImages}
value={currentPage}
onChange={(e) => scrollToPage(parseInt(e.target.value))}
className="page-slider"
/>
</div>
<button
className="nav-control-btn"
onClick={nextChapter}
disabled={!data.nextChapter}
>
Next →
</button>
</div>
</div>
</div>
);
}
|