// Home card: today's verse and Spurgeon's Morning & Evening reading, both from IQ Bible. import { useEffect, useState } from 'react'; import { useApp } from '../../context/AppContext.js'; import { useIq, useIqAvailable } from '../../hooks/useIq.js'; import VerseShareImage, { BACKGROUND_KEY } from '../VerseShareImage.jsx'; import { SCENES, loadPhotoList, resolveBackground, sceneDataUrl } from '../../utils/verseBackgrounds.js'; import { bookOptions } from '../../utils/bibleUtils.jsx'; import { READER_TRANSLATIONS, fetchChapter } from '../../utils/bibleText.js'; export default function IqDailyCard() { const on = useIqAvailable(); if (!on) return null; return ; } function Card() { const { jumpToReaderVerse, openBibleReader, readerTranslation, readerTheme } = useApp(); // Today's verse is chosen by IQ Bible (the same for everyone); its words come from the Bible you pick. const votd = useIq('bibles/eng_kjv/verse-of-the-day'); const [chosen, setChosen] = useState(() => { try { return localStorage.getItem('votd-translation') || ''; } catch { return ''; } }); const translation = READER_TRANSLATIONS.some((t) => t.id === chosen) ? chosen : (readerTranslation || 'BSB'); const pick = (id) => { setChosen(id); try { localStorage.setItem('votd-translation', id); } catch { /* storage unavailable */ } }; const ref = votd.data?.data; const [text, setText] = useState({ key: '', value: '', error: false }); const [sharing, setSharing] = useState(false); // The picture behind the verse: a different one each day by default, or the one you chose. const [bgChoice, setBgChoice] = useState(() => { try { return localStorage.getItem(BACKGROUND_KEY) || 'auto'; } catch { return 'auto'; } }); const [photos, setPhotos] = useState([]); useEffect(() => { let live = true; loadPhotoList().then((list) => { if (live) setPhotos(list); }); return () => { live = false; }; }, []); const background = resolveBackground(bgChoice, photos); const backgroundUrl = background?.kind === 'photo' ? background.url : background ? sceneDataUrl(background.id) : ''; const pickBackground = (id) => { setBgChoice(id); try { localStorage.setItem(BACKGROUND_KEY, id); } catch { /* storage unavailable */ } }; const key = ref ? `${translation}/${ref.book_usfm}/${ref.chapter_number}/${ref.verse_number}` : ''; useEffect(() => { if (!ref) return undefined; const controller = new AbortController(); fetchChapter(translation, ref.book_usfm, ref.chapter_number, { signal: controller.signal }) .then(({ verses }) => { const found = verses.find((v) => v.number === ref.verse_number)?.text; setText({ key, value: found ?? '', error: !found }); }) .catch(() => { if (!controller.signal.aborted) setText({ key, value: '', error: true }); }); return () => controller.abort(); }, [key]); // eslint-disable-line react-hooks/exhaustive-deps const dev = useIq('devotionals/today'); const [part, setPart] = useState(() => (new Date().getHours() < 15 ? 'morning' : 'evening')); const verse = votd.data?.data; const reading = dev.data?.data?.[part]; const loaded = text.key === key; const shown = loaded && text.value ? text.value : loaded && text.error ? ref?.text ?? '' : '…'; const shownTranslation = loaded && text.error ? 'KJV' : translation; const name = (abbrev) => bookOptions.find((b) => b.abbrev === abbrev)?.name ?? abbrev; if (!verse && !reading) return null; return (
{verse && (

Verse of the day

{/* the translation's text; the KJV from IQ Bible is the fallback if that Bible can't be loaded right now */} {backgroundUrl ? (
{shown}
{name(verse.book_usfm)} {verse.chapter_number}:{verse.verse_number} · {shownTranslation}
) : (
{shown}
)} {loaded && text.error && translation !== 'KJV' &&

Couldn’t load {translation} just now, so this is the KJV.

}
{sharing && loaded && shown && ( t.id === translation)?.notice ?? '' : ''} defaultStyle={readerTheme || 'paper'} defaultBackground={bgChoice} onClose={() => setSharing(false)} /> )} {text.key === key && text.value && READER_TRANSLATIONS.find((t) => t.id === translation)?.notice && (

{READER_TRANSLATIONS.find((t) => t.id === translation).notice}

)}
)} {reading && (
Spurgeon, {part === 'morning' ? 'Morning' : 'Evening'}: “{reading.heading_text}”
{['morning', 'evening'].map((p) => ( ))}

{reading.heading_citation}

{reading.body}

Charles Spurgeon, Morning and Evening (1866), via IQ Bible.

)}
); }