"use client"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import { Download, Plus, Upload } from "lucide-react"; import { api, errorText } from "@/lib/api"; import { FormError, Loading, Modal, ModalButtons, PageMessage, PageTitle, Stat, useSubmit } from "@/components/ui"; type Msg = { type: "ok" | "err"; text: string } | null; type Translation = { id: string; name: string; language: string; copyright: string; acknowledgement: string }; type Passage = { reference: string; translation: string; text: string; location: unknown }; type Devotional = { id: string; date: string; title: string; passage: Passage; body: string; prayer: string | null; author: string | null; updatedAt: string }; type Overview = { today: string; latest: string; aheadMonths: number; ahead: string; appFetchDays: number; devotionals: Devotional[]; empty: string[]; translations: Translation[]; csv: { columns: string[]; template: string }; }; type CsvIssue = { column: string | null; message: string }; type CsvRow = { line: number; devotional: { date: string; title: string; passage: Passage } | null; errors: CsvIssue[]; warnings: CsvIssue[]; taken: boolean }; type CsvResult = { fileError: string | null; rows: CsvRow[] }; const MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; const DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const longDate = (d: string) => new Date(`${d}T12:00:00Z`).toLocaleDateString("en-ZA", { weekday: "short", day: "numeric", month: "long", year: "numeric", timeZone: "UTC" }); /** The months from today's to the last day's, each as its days (null before the 1st, Monday first). */ function months(from: string, to: string) { const out: { label: string; days: (string | null)[] }[] = []; let [y, m] = from.split("-").map(Number); const [ty, tm] = to.split("-").map(Number); while (y < ty || (y === ty && m <= tm)) { const first = new Date(Date.UTC(y, m - 1, 1)); const count = new Date(Date.UTC(y, m, 0)).getUTCDate(); const pad = (first.getUTCDay() + 6) % 7; const days: (string | null)[] = Array(pad).fill(null); for (let d = 1; d <= count; d++) days.push(`${y}-${String(m).padStart(2, "0")}-${String(d).padStart(2, "0")}`); out.push({ label: `${MONTHS[m - 1]} ${y}`, days }); m++; if (m > 12) { m = 1; y++; } } return out; } /** The days from today to two years ahead (2.26.1): filled ones open their devotional, empty ones a new one. */ function Calendar({ o, onOpen }: { o: Overview; onOpen: (date: string, d: Devotional | null) => void }) { const byDate = useMemo(() => new Map(o.devotionals.map(d => [d.date, d])), [o]); return (
{months(o.today, o.latest).map(month => (

{month.label}

{DAYS.map(d => {d})} {month.days.map((day, i) => { if (!day) return ; const inRange = day >= o.today && day <= o.latest; const d = byDate.get(day) || null; const label = `${longDate(day)}: ${d ? d.title : inRange ? "no devotional yet" : "outside the dates allowed"}`; return ( ); })}
))}
); } type Form = { date: string; title: string; reference: string; translation: string; passageText: string; body: string; prayer: string; author: string }; /** Write or edit one devotional. The passage is quoted as written, never by AI (BIBLE-PERMISSIONS.md). */ function DevotionalModal({ o, date, existing, onClose, onDone }: { o: Overview; date: string; existing: Devotional | null; onClose: () => void; onDone: (m: Msg) => void }) { const first: Form = existing ? { date: existing.date, title: existing.title, reference: existing.passage.reference, translation: existing.passage.translation, passageText: existing.passage.text, body: existing.body, prayer: existing.prayer || "", author: existing.author || "" } : { date, title: "", reference: "", translation: o.translations[0]?.id || "BSB", passageText: "", body: "", prayer: "", author: "Cross Code" }; const [form, setForm] = useState
(first); const { busy, error, run } = useSubmit(); const f = (k: keyof Form) => ({ value: form[k], onChange: (e: React.ChangeEvent) => setForm({ ...form, [k]: e.target.value }) }); const translation = o.translations.find(t => t.id === form.translation); const dirty = (Object.keys(first) as (keyof Form)[]).some(k => form[k] !== first[k]); const save = (e: React.FormEvent) => { e.preventDefault(); run(async () => { const r = await api<{ devotional: Devotional; warnings: string[] }>(existing ? `/api/admin/devotionals/${existing.id}` : "/api/admin/devotionals", { method: existing ? "PUT" : "POST", body: form }); onDone({ type: "ok", text: `${existing ? "Saved" : "Added"}: ${r.devotional.title} on ${longDate(r.devotional.date)}.${r.warnings.length ? ` ${r.warnings.join(" ")}` : ""}` }); }); }; const remove = () => { if (!existing || !window.confirm(`Delete "${existing.title}" (${longDate(existing.date)})? Apps drop it at their next nightly fetch.`)) return; run(async () => { await api(`/api/admin/devotionals/${existing.id}`, { method: "DELETE" }); onDone({ type: "ok", text: `Deleted: ${existing.title}.` }); }); }; return (

The passage it's about, not whole chapters: more than 25 verses gets a warning.

Only the free translations until a publisher gives written permission.