|
| 1 | +import React, {useEffect, useState, useRef} from 'react'; |
| 2 | +import styles from './styles.module.css'; |
| 3 | + |
| 4 | +const STORAGE_PREFIX = 'borr:read:'; |
| 5 | + |
| 6 | +function normalizePath(path) { |
| 7 | + if (!path) return '/'; |
| 8 | + try { |
| 9 | + const url = new URL(path, typeof window !== 'undefined' ? window.location.origin : 'http://localhost'); |
| 10 | + path = url.pathname; |
| 11 | + } catch (e) { |
| 12 | + } |
| 13 | + if (path !== '/' && path.endsWith('/')) path = path.slice(0, -1); |
| 14 | + return path || '/'; |
| 15 | +} |
| 16 | + |
| 17 | +function getSidebarDocLinks() { |
| 18 | + const anchors = Array.from(document.querySelectorAll('.menu__list a')); |
| 19 | + const internal = anchors |
| 20 | + .map((a) => a.getAttribute('href')) |
| 21 | + .filter(Boolean) |
| 22 | + .filter((h) => h.startsWith('/')) |
| 23 | + .map((h) => normalizePath(h)); |
| 24 | + return Array.from(new Set(internal)); |
| 25 | +} |
| 26 | + |
| 27 | +const TRACK_SECTIONS = { |
| 28 | + '/computer-science': 'Computer Science', |
| 29 | + '/precollege-math': 'Pre-College Math', |
| 30 | + '/data-science': 'Data Science', |
| 31 | + '/math': 'Math', |
| 32 | +}; |
| 33 | + |
| 34 | +export default function ReadingProgress() { |
| 35 | + const [totalPages, setTotalPages] = useState(0); |
| 36 | + const [readCount, setReadCount] = useState(0); |
| 37 | + const [isRead, setIsRead] = useState(false); |
| 38 | + const [pagesList, setPagesList] = useState(null); |
| 39 | + const [currentSection, setCurrentSection] = useState(null); |
| 40 | + const fileInputRef = useRef(null); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (typeof window === 'undefined') return; |
| 44 | + |
| 45 | + const currentPath = normalizePath(window.location.pathname + window.location.search); |
| 46 | + |
| 47 | + const normalizedSections = Object.keys(TRACK_SECTIONS).map((p) => normalizePath(p)); |
| 48 | + const found = normalizedSections.find((s) => currentPath === s || currentPath.startsWith(s + '/')) || null; |
| 49 | + setCurrentSection(found); |
| 50 | + |
| 51 | + if (!found) { |
| 52 | + setTotalPages(0); |
| 53 | + setReadCount(0); |
| 54 | + setIsRead(false); |
| 55 | + setPagesList([]); |
| 56 | + return; |
| 57 | + } |
| 58 | + async function loadManifest() { |
| 59 | + try { |
| 60 | + const res = await fetch('/docs-manifest.json', {cache: 'no-store'}); |
| 61 | + if (res.ok) { |
| 62 | + const arr = await res.json(); |
| 63 | + const pages = arr.map((p) => normalizePath(p)); |
| 64 | + const sectionPages = pages.filter((p) => p === found || p.startsWith(found + '/')); |
| 65 | + setPagesList(sectionPages); |
| 66 | + setTotalPages(sectionPages.length); |
| 67 | + |
| 68 | + const counts = sectionPages.reduce((acc, p) => acc + (localStorage.getItem(STORAGE_PREFIX + p) === '1' ? 1 : 0), 0); |
| 69 | + setReadCount(counts); |
| 70 | + |
| 71 | + const currentKey = STORAGE_PREFIX + currentPath; |
| 72 | + setIsRead(localStorage.getItem(currentKey) === '1'); |
| 73 | + return; |
| 74 | + } |
| 75 | + } catch (e) { |
| 76 | + } |
| 77 | + |
| 78 | + const links = getSidebarDocLinks(); |
| 79 | + const sectionLinks = (links.length > 0 ? links : [currentPath]).filter((p) => p === found || p.startsWith(found + '/')); |
| 80 | + |
| 81 | + setPagesList(sectionLinks); |
| 82 | + setTotalPages(sectionLinks.length); |
| 83 | + |
| 84 | + const counts = sectionLinks.reduce((acc, p) => { |
| 85 | + const key = STORAGE_PREFIX + p; |
| 86 | + if (localStorage.getItem(key) === '1') acc++; |
| 87 | + return acc; |
| 88 | + }, 0); |
| 89 | + setReadCount(counts); |
| 90 | + |
| 91 | + const currentKey = STORAGE_PREFIX + currentPath; |
| 92 | + setIsRead(localStorage.getItem(currentKey) === '1'); |
| 93 | + } |
| 94 | + |
| 95 | + loadManifest(); |
| 96 | + |
| 97 | + function onStorage(ev) { |
| 98 | + if (!ev.key) return; |
| 99 | + if (!ev.key.startsWith(STORAGE_PREFIX)) return; |
| 100 | + let newCounts = 0; |
| 101 | + for (let i = 0; i < localStorage.length; i++) { |
| 102 | + const k = localStorage.key(i); |
| 103 | + if (!k || !k.startsWith(STORAGE_PREFIX)) continue; |
| 104 | + const p = k.slice(STORAGE_PREFIX.length); |
| 105 | + if (p === found || p.startsWith(found + '/')) newCounts++; |
| 106 | + } |
| 107 | + setReadCount(newCounts); |
| 108 | + setIsRead(localStorage.getItem(STORAGE_PREFIX + currentPath) === '1'); |
| 109 | + } |
| 110 | + |
| 111 | + window.addEventListener('storage', onStorage); |
| 112 | + return () => window.removeEventListener('storage', onStorage); |
| 113 | + }, []); |
| 114 | + |
| 115 | + if (typeof window === 'undefined') return null; |
| 116 | + |
| 117 | + if (!currentSection) return null; |
| 118 | + |
| 119 | + const percent = totalPages > 0 ? Math.round((readCount / totalPages) * 100) : 0; |
| 120 | + function toggleRead() { |
| 121 | + const currentPath = normalizePath(window.location.pathname + window.location.search); |
| 122 | + const key = STORAGE_PREFIX + currentPath; |
| 123 | + if (localStorage.getItem(key) === '1') { |
| 124 | + localStorage.removeItem(key); |
| 125 | + setIsRead(false); |
| 126 | + setReadCount((c) => Math.max(0, c - 1)); |
| 127 | + } else { |
| 128 | + localStorage.setItem(key, '1'); |
| 129 | + setIsRead(true); |
| 130 | + setReadCount((c) => c + 1); |
| 131 | + } |
| 132 | + try { |
| 133 | + window.dispatchEvent(new Event('storage')); |
| 134 | + } catch (e) {} |
| 135 | + } |
| 136 | + |
| 137 | + function exportProgress() { |
| 138 | + if (!currentSection) return; |
| 139 | + const entries = []; |
| 140 | + for (let i = 0; i < localStorage.length; i++) { |
| 141 | + const k = localStorage.key(i); |
| 142 | + if (!k || !k.startsWith(STORAGE_PREFIX)) continue; |
| 143 | + const p = k.slice(STORAGE_PREFIX.length); |
| 144 | + if (p === currentSection || p.startsWith(currentSection + '/')) entries.push(p); |
| 145 | + } |
| 146 | + if (entries.length === 0) { |
| 147 | + alert('No progress saved for this section.'); |
| 148 | + return; |
| 149 | + } |
| 150 | + const data = {version: 1, section: currentSection, entries}; |
| 151 | + const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'}); |
| 152 | + const url = URL.createObjectURL(blob); |
| 153 | + const a = document.createElement('a'); |
| 154 | + const safeSection = currentSection.replace(/[^a-z0-9]/gi, '_').replace(/^_+|_+$/g, ''); |
| 155 | + a.download = `borr-progress-${safeSection}.json`; |
| 156 | + a.href = url; |
| 157 | + document.body.appendChild(a); |
| 158 | + a.click(); |
| 159 | + a.remove(); |
| 160 | + URL.revokeObjectURL(url); |
| 161 | + } |
| 162 | + |
| 163 | + function openImportDialog() { |
| 164 | + if (fileInputRef.current) fileInputRef.current.click(); |
| 165 | + } |
| 166 | + |
| 167 | + async function handleImportFile(ev) { |
| 168 | + const f = ev.target.files && ev.target.files[0]; |
| 169 | + if (!f) return; |
| 170 | + try { |
| 171 | + const text = await f.text(); |
| 172 | + const json = JSON.parse(text); |
| 173 | + let entries = []; |
| 174 | + if (Array.isArray(json)) entries = json; |
| 175 | + else if (json && Array.isArray(json.entries)) entries = json.entries; |
| 176 | + else if (json && typeof json === 'object') { |
| 177 | + if (json.entries && typeof json.entries === 'object') entries = Object.keys(json.entries); |
| 178 | + } |
| 179 | + if (entries.length === 0) { |
| 180 | + alert('No entries found in import file. Expected an array of doc paths or {entries: [...]}.'); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + if (json.section && json.section !== currentSection) { |
| 185 | + const ok = window.confirm(`Import file is for "${json.section}" but you are on "${currentSection}". Import anyway (will merge)?`); |
| 186 | + if (!ok) return; |
| 187 | + } |
| 188 | + |
| 189 | + let added = 0; |
| 190 | + for (const p of entries) { |
| 191 | + const norm = normalizePath(p); |
| 192 | + if (norm === currentSection || norm.startsWith(currentSection + '/')) { |
| 193 | + const key = STORAGE_PREFIX + norm; |
| 194 | + if (localStorage.getItem(key) !== '1') { |
| 195 | + localStorage.setItem(key, '1'); |
| 196 | + added++; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + const newCounts = pagesList ? pagesList.reduce((acc, p) => acc + (localStorage.getItem(STORAGE_PREFIX + p) === '1' ? 1 : 0), 0) : 0; |
| 201 | + setReadCount(newCounts); |
| 202 | + setIsRead(localStorage.getItem(STORAGE_PREFIX + normalizePath(window.location.pathname)) === '1'); |
| 203 | + alert(`Imported ${added} entries for this section.`); |
| 204 | + } catch (e) { |
| 205 | + alert('Failed to import file: ' + (e && e.message ? e.message : String(e))); |
| 206 | + } finally { |
| 207 | + if (fileInputRef.current) fileInputRef.current.value = ''; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + function resetProgress() { |
| 212 | + if (!currentSection) return; |
| 213 | + const ok = window.confirm('Reset progress for this section? This will remove all saved "done" marks for this section.'); |
| 214 | + if (!ok) return; |
| 215 | + let removed = 0; |
| 216 | + const toRemove = []; |
| 217 | + for (let i = 0; i < localStorage.length; i++) { |
| 218 | + const k = localStorage.key(i); |
| 219 | + if (!k || !k.startsWith(STORAGE_PREFIX)) continue; |
| 220 | + const p = k.slice(STORAGE_PREFIX.length); |
| 221 | + if (p === currentSection || p.startsWith(currentSection + '/')) toRemove.push(k); |
| 222 | + } |
| 223 | + for (const k of toRemove) { |
| 224 | + localStorage.removeItem(k); |
| 225 | + removed++; |
| 226 | + } |
| 227 | + setReadCount(0); |
| 228 | + setIsRead(false); |
| 229 | + alert(`Removed ${removed} entries for this section.`); |
| 230 | + } |
| 231 | + |
| 232 | + return ( |
| 233 | + <div className={styles.container}> |
| 234 | + <div className={styles.header}> |
| 235 | + <div className={styles.summary}> |
| 236 | + <span className={styles.sectionName}>{TRACK_SECTIONS[currentSection] ?? ''}</span> |
| 237 | + <strong>{readCount}</strong> |
| 238 | + <span className={styles.sep}>/</span> |
| 239 | + <span>{totalPages}</span> |
| 240 | + |
| 241 | + </div> |
| 242 | + <div style={{display: 'flex', gap: '0.5rem', alignItems: 'center'}}> |
| 243 | + <button |
| 244 | + aria-pressed={isRead} |
| 245 | + className={`button button--primary borr-tick ${styles.tick} ${isRead ? styles.ticked : ''}`} |
| 246 | + onClick={toggleRead} |
| 247 | + title={isRead ? 'Mark page as not done' : 'Mark page as done'} |
| 248 | + > |
| 249 | + {isRead ? '✓ Done' : 'Mark done'} |
| 250 | + </button> |
| 251 | + </div> |
| 252 | + </div> |
| 253 | + |
| 254 | + <div className={styles.progressBar} aria-hidden> |
| 255 | + <div className={styles.filler} style={{width: `${percent}%`}} /> |
| 256 | + </div> |
| 257 | + <div className={styles.percent}>{percent}%</div> |
| 258 | + |
| 259 | + <div className={styles.controls}> |
| 260 | + <input ref={fileInputRef} className={styles.fileInput} type="file" accept="application/json" onChange={handleImportFile} /> |
| 261 | + <div style={{display: 'flex', gap: '0.5rem'}}> |
| 262 | + <button className="button button--secondary borr-export" onClick={exportProgress} title="Export progress for this section">Export</button> |
| 263 | + <button className="button button--secondary borr-import" onClick={openImportDialog} title="Import progress JSON">Import</button> |
| 264 | + <button className="button button--outline borr-reset" onClick={resetProgress} title="Reset progress for this section">Reset</button> |
| 265 | + </div> |
| 266 | + </div> |
| 267 | + </div> |
| 268 | + ); |
| 269 | +} |
0 commit comments