RotNot / src /components /reading /reader.tsx
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
7.43 kB
"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
key={i}
className="text-xl font-bold mt-8 mb-3"
style={{ color: GREEN }}
>
{h2[1]}
</h2>
);
}
const h3 = trimmed.match(/^### (.+)/);
if (h3) {
return (
<h3
key={i}
className="text-lg font-semibold mt-6 mb-2"
style={{ color: "var(--text-primary)" }}
>
{h3[1]}
</h3>
);
}
// Unordered list
if (trimmed.startsWith("- ")) {
const items = trimmed
.split("\n")
.filter((l) => l.startsWith("- "))
.map((l) => l.slice(2));
return (
<ul key={i} className="list-disc list-inside space-y-1 mb-4 ml-2">
{items.map((item, j) => (
<li key={j} className="text-base leading-relaxed" style={{ color: "var(--text-secondary)" }}>
{renderInline(item)}
</li>
))}
</ul>
);
}
// Paragraph
return (
<p
key={i}
className="text-base leading-relaxed mb-4"
style={{ color: "var(--text-secondary)" }}
>
{renderInline(trimmed)}
</p>
);
});
}
function renderInline(text: string): React.ReactNode {
// Handle **bold**
const parts = text.split(/(\*\*[^*]+\*\*)/g);
return parts.map((part, i) => {
if (part.startsWith("**") && part.endsWith("**")) {
return (
<strong key={i} style={{ color: "var(--text-primary)", fontWeight: 600 }}>
{part.slice(2, -2)}
</strong>
);
}
return part;
});
}
export function Reader({ article, onFinished }: ReaderProps) {
const [elapsed, setElapsed] = useState(0);
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
// Initialise to 0; the effect below sets the real start time on mount.
const startRef = useRef<number>(0);
// Fullscreen support — the wrapper div is the fullscreen target
const containerRef = useRef<HTMLDivElement>(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 (
<div
ref={containerRef}
className={
isFullscreen
? "fixed inset-0 z-50 overflow-y-auto px-4 py-0"
: "max-w-2xl mx-auto"
}
style={
isFullscreen
? { background: "var(--background)" }
: undefined
}
>
<div className={isFullscreen ? "max-w-2xl mx-auto" : undefined}>
{/* Sticky header bar */}
<div
className="sticky top-0 z-10 py-3 mb-8 flex items-center justify-between border-b"
style={{
background: "var(--background)",
borderColor: "rgba(255,255,255,0.08)",
}}
>
<div>
<p className="text-xs uppercase tracking-wider" style={{ color: GREEN }}>
{article.domain} · {article.difficulty} · ~{article.estimatedMinutes} min
</p>
<h1 className="text-lg font-bold" style={{ color: "var(--text-primary)" }}>
{article.title}
</h1>
</div>
<div className="flex items-center gap-3">
<span
style={{
fontFamily: "var(--font-mono)",
color: "var(--text-secondary)",
fontSize: "0.85rem",
}}
>
{formatTime(elapsed)}
</span>
{/* Fullscreen toggle — hidden when API not supported */}
{isSupported && (
<button
onClick={toggleFullscreen}
title={isFullscreen ? "Exit fullscreen" : "Enter distraction-free mode"}
className="p-2 rounded-lg transition-colors hover:bg-white/10"
style={{ color: "var(--text-secondary)" }}
>
{isFullscreen ? (
<Minimize2 className="w-4 h-4" />
) : (
<Maximize2 className="w-4 h-4" />
)}
</button>
)}
<button
onClick={handleDone}
disabled={!canFinish}
className="px-4 py-2 rounded-xl font-bold text-sm transition-all hover:scale-105 disabled:opacity-40 disabled:cursor-not-allowed disabled:hover:scale-100"
style={{ background: GREEN, color: "#000" }}
>
{canFinish ? "I'm Done" : `Wait ${MIN_READ_SECONDS - elapsed}s`}
</button>
</div>
</div>
{/* Progress bar (fills during minimum read period) */}
{!canFinish && (
<div
className="w-full h-1 rounded-full mb-6"
style={{ background: "rgba(255,255,255,0.08)" }}
>
<motion.div
className="h-1 rounded-full"
style={{ background: GREEN }}
initial={{ width: 0 }}
animate={{ width: `${progress * 100}%` }}
transition={{ duration: 0.5 }}
/>
</div>
)}
{/* Article body — no copy-paste */}
<article
onCopy={(e) => e.preventDefault()}
onCut={(e) => e.preventDefault()}
className="select-none"
style={{ userSelect: "none" }}
>
{renderMarkdown(article.body)}
</article>
{/* Bottom done button */}
<div className="mt-12 pb-12 flex justify-center">
<button
onClick={handleDone}
disabled={!canFinish}
className="px-8 py-3 rounded-xl font-bold text-lg transition-all hover:scale-105 disabled:opacity-40 disabled:cursor-not-allowed disabled:hover:scale-100"
style={{ background: GREEN, color: "#000" }}
>
{canFinish ? "Finished — Take the Quiz" : `Read for ${MIN_READ_SECONDS - elapsed}s more`}
</button>
</div>
</div>
</div>
);
}