"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 (
{month.label}