Study - New Outline stage (hierarchical I./A./1. points, linked to chunks, in all exports); the chunk stage is now "Chunks" - Home cards: passages shown as ranges, tags capped with "+N more" Reader - Side-by-side translations and a Compare tab per verse - Typeface, line spacing, verse-number settings; text and column scale with the screen - Highlights/bookmarks sheet with named colours (synced); BSB footnote spacing fix - Cmd-K palette (passages, studies, notes, marks), keyboard shortcuts and "?" help Flow and safety - Toasts with Undo replace most confirm()/alert() - Back/Forward move between pages - Server refuses saves over a newer copy (409); keep-mine / use-other banner; opening a study no longer re-stamps it - Latest chapter request wins when flipping quickly Offline and data - Save books, plan days or a whole BSB/NET for offline - Service worker fixes: helloao /api/ URLs were skipped and responses cloned too late - Settings: download/restore a full backup Accessibility - Sheets trap focus and close on Escape, skip link, focus ring, reduced motion, theme contrast raised to 4.5:1 (with a test) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
53 lines
2.4 KiB
JavaScript
53 lines
2.4 KiB
JavaScript
// Drives "Save for offline": one download at a time, with progress for the button to show,
|
|
// a cancel, and a toast when it finishes.
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { useToast } from '../components/Toasts.jsx';
|
|
import { bibleRefs, bookRefs, downloadChapters, loadSavedBooks, markBookSaved } from '../utils/offline.js';
|
|
|
|
export function useOnline() {
|
|
const [online, setOnline] = useState(() => (typeof navigator === 'undefined' ? true : navigator.onLine !== false));
|
|
useEffect(() => {
|
|
const up = () => setOnline(true);
|
|
const down = () => setOnline(false);
|
|
window.addEventListener('online', up);
|
|
window.addEventListener('offline', down);
|
|
return () => { window.removeEventListener('online', up); window.removeEventListener('offline', down); };
|
|
}, []);
|
|
return online;
|
|
}
|
|
|
|
export function useOfflineSave() {
|
|
const { toast } = useToast();
|
|
const [saved, setSaved] = useState(loadSavedBooks);
|
|
const [job, setJob] = useState(null); // { label, done, total } while downloading
|
|
const controller = useRef(null);
|
|
useEffect(() => () => controller.current?.abort(), []);
|
|
|
|
const run = useCallback(async (label, translation, refs, { bookKeys = [] } = {}) => {
|
|
if (controller.current) return; // one at a time
|
|
const abort = new AbortController();
|
|
controller.current = abort;
|
|
setJob({ label, done: 0, total: refs.length });
|
|
const result = await downloadChapters(translation, refs, { signal: abort.signal, onProgress: (done, total) => setJob({ label, done, total }) });
|
|
controller.current = null;
|
|
setJob(null);
|
|
if (abort.signal.aborted) { toast('Download stopped'); return; }
|
|
if (result.failed === 0) {
|
|
bookKeys.forEach((b) => markBookSaved(translation, b));
|
|
setSaved(loadSavedBooks());
|
|
toast(`Saved ${label} for offline`);
|
|
} else {
|
|
toast(`Saved ${result.done} of ${refs.length} chapters — check your connection and try again`, { tone: 'error' });
|
|
}
|
|
}, [toast]);
|
|
|
|
return {
|
|
saved, job,
|
|
isSaved: (translation, book) => (saved[translation] ?? []).includes(book),
|
|
saveBook: (translation, book, name) => run(name, translation, bookRefs(book), { bookKeys: [book] }),
|
|
saveBible: (translation) => run(`The whole ${translation}`, translation, bibleRefs(), { bookKeys: [...new Set(bibleRefs().map((r) => r.book))] }),
|
|
saveRefs: (label, translation, refs) => run(label, translation, refs),
|
|
cancel: () => controller.current?.abort(),
|
|
};
|
|
}
|