"use client"; import React, { useEffect, useState } from "react"; import { Mail, MessageCircle } from "lucide-react"; import { useAuth } from "@/hooks/useAuth"; import { apiV2, type V2Result } from "@/lib/apiV2"; import { errorMessage } from "@/lib/utils"; type Overview = V2Result<"/settings/sign-in/messaging", "get">["data"]; type Answer = V2Result<"/settings/sign-in/messaging", "post">["data"]; type Result = { ok: boolean; error: string | null; saved: boolean } | null; /** * Email and WhatsApp for the church's Accounts (3.1.0): Accounts sends its own sign-in codes and * password links, with its own settings, so they're set up before sign-in moves there. This site's * own settings are offered to copy (its password and WAWP token stay on the server: leave the field * blank to use them), tested through Accounts, and saved there only once the test worked. */ export function AccountsMessaging({ onReady }: { onReady: (ready: { email: boolean; whatsapp: boolean }) => void }) { const { token, user } = useAuth(); const [data, setData] = useState(null); const [err, setErr] = useState(null); const [email, setEmail] = useState({ host: "", port: "587", secure: false, user: "", from: "", pass: "", to: "" }); const [wa, setWa] = useState({ instanceId: "", accessToken: "", to: "" }); const [emailResult, setEmailResult] = useState(null); const [waResult, setWaResult] = useState(null); const [testedEmail, setTestedEmail] = useState(null); const [testedWa, setTestedWa] = useState(null); const [busy, setBusy] = useState(null); const load = () => { if (!token) return; apiV2("get", "/settings/sign-in/messaging", { authToken: token }) .then(({ data: d }) => { setData(d); if (d.accounts) onReady({ email: d.accounts.email.ready, whatsapp: d.accounts.whatsapp.ready }); const e = d.events.email; if (e) setEmail((x) => (x.host ? x : { ...x, host: e.host, port: String(e.port), secure: e.secure, user: e.user, from: e.from })); if (d.events.whatsapp) setWa((x) => (x.instanceId ? x : { ...x, instanceId: d.events.whatsapp!.instanceId })); }) .catch((e) => setErr(errorMessage(e) || "Could not load the email settings.")); }; // eslint-disable-next-line react-hooks/exhaustive-deps -- once per sign-in useEffect(load, [token]); useEffect(() => { setEmail((x) => (x.to ? x : { ...x, to: user?.email || "" })); setWa((x) => (x.to ? x : { ...x, to: user?.phoneNumber || "" })); }, [user]); if (!token) return null; if (!data) return err ?

{err}

:

Loading email settings…

; const emailKey = JSON.stringify({ ...email, to: undefined }); const waKey = JSON.stringify({ ...wa, to: undefined }); const emailBody = () => ({ email: { host: email.host, port: parseInt(email.port, 10) || 587, secure: email.secure, user: email.user, from: email.from, pass: email.pass || null, to: email.to } }); const waBody = () => ({ whatsapp: { instanceId: wa.instanceId, accessToken: wa.accessToken || null, to: wa.to } }); const run = async (key: string, save: boolean, body: object, after: (a: Answer) => void) => { setBusy(key); setErr(null); try { const { data: a } = save ? await apiV2("post", "/settings/sign-in/messaging", { authToken: token, body: body as never }) : await apiV2("post", "/settings/sign-in/messaging/test", { authToken: token, body: body as never }); after(a); setData((d) => (d ? { ...d, accounts: { email: a.status.email, whatsapp: a.status.whatsapp }, accountsError: null } : d)); onReady({ email: a.status.email.ready, whatsapp: a.status.whatsapp.ready }); } catch (e) { setErr(errorMessage(e) || "That didn't work."); } setBusy(null); }; const acc = data.accounts; const input = "w-full rounded-lg border px-3 py-2 text-sm"; const line = (r: Result, what: string) => r && (

{r.ok ? (r.saved ? `Saved in Accounts: ${what} works there now.` : `The test ${what} went. Check it arrived, then save.`) : `The test didn't work: ${r.error}`}

); return (

Email and WhatsApp for the church's sign-in

Accounts sends sign-in codes and password links itself, with its own settings. Without email there, nobody can sign in after the move.

{data.accountsError ? (

Accounts can't be reached: {data.accountsError}

) : acc && (
  • Email: {acc.email.ready ? works{acc.email.from ? ` (from ${acc.email.from})` : ""} : not set up}
  • WhatsApp: {acc.whatsapp.ready ? works : not set up (WhatsApp codes then go by email)}
)} {err &&

{err}

}
{acc?.email.ready ? "Change Accounts' email" : "Set up Accounts' email"}{data.events.email ? " (copied from this site)" : ""}
{line(emailResult, "email")}
{acc?.whatsapp.ready ? "Change Accounts' WhatsApp (WAWP)" : "Set up Accounts' WhatsApp (WAWP), optional"}{data.events.whatsapp ? " (copied from this site)" : ""}
{line(waResult, "WhatsApp")}
); }