import { useState, useEffect } from "react"; import { LineNumberedText } from "@/components/reading/LineNumberedText"; import { TranslationSelector } from "@/components/reading/TranslationSelector"; import { ReadingControls } from "@/components/reading/ReadingControls"; import { useReadingSettings } from "@/hooks/use-reading-settings"; import { WorkWithDetails, TranslationWithDetails } from "@/lib/types"; import { AuthorChip } from "@/components/common/AuthorChip"; import { LanguageTag } from "@/components/common/LanguageTag"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { apiRequest } from "@/lib/queryClient"; interface ReadingViewProps { work: WorkWithDetails; translations: TranslationWithDetails[]; } export function ReadingView({ work, translations }: ReadingViewProps) { const { settings, increaseFontSize, decreaseFontSize, toggleZenMode } = useReadingSettings(); const [selectedTranslationId, setSelectedTranslationId] = useState( translations.length > 0 ? translations[0].id : undefined ); const [readingProgress, setReadingProgress] = useState(0); // Get the selected translation const selectedTranslation = translations.find(t => t.id === selectedTranslationId); // Content to display - either the translation or original work const contentToDisplay = selectedTranslation ? selectedTranslation.content : work.content; // Update reading progress as user scrolls useEffect(() => { const handleScroll = () => { const windowHeight = window.innerHeight; const documentHeight = document.documentElement.scrollHeight; const scrollTop = window.scrollY; // Calculate progress percentage const progress = Math.min( 100, Math.round((scrollTop / (documentHeight - windowHeight)) * 100) ); setReadingProgress(progress); // Update reading progress in backend (throttled to avoid too many requests) const debounced = setTimeout(() => { updateReadingProgress(progress); }, 2000); return () => clearTimeout(debounced); }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, [work.id, selectedTranslationId]); // Update reading progress in backend const updateReadingProgress = async (progress: number) => { try { // In a real app, this would use the logged-in user ID // For demo purposes, we'll use a hard-coded user ID of 1 await apiRequest('POST', '/api/reading-progress', { userId: 1, workId: work.id, translationId: selectedTranslationId, progress }); } catch (error) { console.error('Failed to update reading progress:', error); } }; return (
{/* Context sidebar (sticky on desktop) */} {/* Main reading area */}

{work.title}

{translations.length > 0 && ( )}
{/* Text content */}
{selectedTranslation && selectedTranslation.notes && (

Translation Notes

{selectedTranslation.notes}

)}
{/* Progress bar (fixed at bottom) */}
); }