File size: 7,431 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"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>
  );
}