"use client"; import { useEffect, useRef, useState } from "react"; import { motion } from "framer-motion"; import { Maximize2, Minimize2 } from "lucide-react"; import type { Article } from "@/lib/reading/articles"; import { useFullscreen } from "@/hooks/use-fullscreen"; const GREEN = "#10b981"; interface ReaderProps { article: Article; onFinished: (durationSecs: number) => void; } const MIN_READ_SECONDS = 60; // minimum time before "Done" is active function formatTime(s: number): string { const m = Math.floor(s / 60); const sec = s % 60; return `${m}:${sec.toString().padStart(2, "0")}`; } /** Render basic markdown: ## headers, **bold**, paragraph splits */ function renderMarkdown(text: string): React.ReactNode[] { const blocks = text.split(/\n{2,}/); return blocks.map((block, i) => { const trimmed = block.trim(); if (!trimmed) return null; // Heading const h2 = trimmed.match(/^## (.+)/); if (h2) { return (

{h2[1]}

); } const h3 = trimmed.match(/^### (.+)/); if (h3) { return (

{h3[1]}

); } // Unordered list if (trimmed.startsWith("- ")) { const items = trimmed .split("\n") .filter((l) => l.startsWith("- ")) .map((l) => l.slice(2)); return ( ); } // Paragraph return (

{renderInline(trimmed)}

); }); } function renderInline(text: string): React.ReactNode { // Handle **bold** const parts = text.split(/(\*\*[^*]+\*\*)/g); return parts.map((part, i) => { if (part.startsWith("**") && part.endsWith("**")) { return ( {part.slice(2, -2)} ); } return part; }); } export function Reader({ article, onFinished }: ReaderProps) { const [elapsed, setElapsed] = useState(0); const timerRef = useRef | null>(null); // Initialise to 0; the effect below sets the real start time on mount. const startRef = useRef(0); // Fullscreen support — the wrapper div is the fullscreen target const containerRef = useRef(null); const { isFullscreen, isSupported, toggleFullscreen } = useFullscreen(containerRef); useEffect(() => { startRef.current = Date.now(); timerRef.current = setInterval(() => setElapsed((e) => e + 1), 1000); return () => { if (timerRef.current) clearInterval(timerRef.current); }; }, []); const canFinish = elapsed >= MIN_READ_SECONDS; const progress = Math.min(elapsed / MIN_READ_SECONDS, 1); const handleDone = () => { if (!canFinish) return; if (timerRef.current) clearInterval(timerRef.current); const totalSecs = Math.round((Date.now() - startRef.current) / 1000); onFinished(totalSecs); }; return (
{/* Sticky header bar */}

{article.domain} · {article.difficulty} · ~{article.estimatedMinutes} min

{article.title}

{formatTime(elapsed)} {/* Fullscreen toggle — hidden when API not supported */} {isSupported && ( )}
{/* Progress bar (fills during minimum read period) */} {!canFinish && (
)} {/* Article body — no copy-paste */}
e.preventDefault()} onCut={(e) => e.preventDefault()} className="select-none" style={{ userSelect: "none" }} > {renderMarkdown(article.body)}
{/* Bottom done button */}
); }