"use client"; import React, { useState } from "react"; import type { Release } from "@/lib/types"; import { FormError, Modal, ModalButtons, useSubmit } from "@/components/ui"; import { fromInput, toInput } from "@/components/servers"; import { ReleaseSelect } from "@/components/ReleaseSelect"; import { H, changed, post, productName, withWarnings, type Done, type Install } from "./shared"; /** * Schedule an update of one install: at a time at least 4 hours away (the church gets notice), or * an emergency with a reason and a two-factor code, at least 15 minutes away or as soon as possible. */ export function UpdateModal({ x, releases, onClose, onDone }: { x: Install; releases: Release[]; onClose: () => void; onDone: Done }) { const [form0] = useState(() => ({ version: "", at: toInput(new Date(Date.now() + 5 * H)), emergency: false, asap: true, reason: "", totp: "" })); const [upd, setUpd] = useState(form0); const { busy, error, run } = useSubmit(); const asap = upd.emergency && upd.asap; const minAt = upd.emergency ? 15 * 60 * 1000 : 4 * H; const mine = releases.filter(r => r.product === x.product); const save = (e: React.FormEvent) => { e.preventDefault(); run(async () => { const r = await post("/api/admin/updates", { deploymentIds: [x.id], product: x.product, version: upd.version || undefined, at: asap ? undefined : fromInput(upd.at), asap, emergency: upd.emergency, reason: upd.reason || null, totp: upd.totp || null }); onDone({ type: "ok", text: withWarnings(asap ? "Started: the agent picks it up within a minute. The site admin was emailed and sees a banner." : "Planned. The site admin was emailed and sees a banner.", r) }); }); }; return (
setUpd({ ...upd, version })} current={x.install?.version || x.version} />

Now on {x.install?.version || x.version || "an unknown version"}.

{asap ?
When

As soon as possible: the agent starts within a minute.

:
setUpd({ ...upd, at: e.target.value })} />

At least {upd.emergency ? "15 minutes" : "4 hours"} away, so the church gets notice.

}
{upd.emergency && (
When the emergency update runs
setUpd({ ...upd, reason: e.target.value })} />
setUpd({ ...upd, totp: e.target.value })} />
)}
); }