Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: schedules page says what a past start does, no longer claims a one-shot fired

A start in the past fires the latest missed time once, right away, so the
form says that instead of 'already passed'. A one-shot past its time reads
'past' / 'time passed' (the controller's clock, not a record of firing) and
keeps its pause button. The edit form compares against what it opened with,
so a schedule changing under it is not reverted on save.
This commit is contained in:
iris 2026-10-04 17:07:29 +02:00 • committed by mara
commit 0af7f53e88
4 changed files with 45 additions and 31 deletions

View file

@ -18,7 +18,7 @@ import "./ScheduleCard.css";
const STATE_TONE: Record<ScheduleState, BadgeTone> = {
active: "positive",
paused: "warning",
done: "neutral",
past: "neutral",
};
export function ScheduleCard({
@ -65,7 +65,7 @@ export function ScheduleCard({
"paused, will not fire"
) : (
<>
fired{" "}
time passed{" "}
<span title={fmtLocal(schedule.anchor_unix)}>
<RelativeTime epochMs={schedule.anchor_unix * 1000} />
</span>
@ -76,11 +76,9 @@ export function ScheduleCard({
<Button onClick={onEdit} disabled={pending}>
edit
</Button>
{state !== "done" ? (
<Button onClick={onTogglePaused} disabled={pending}>
{schedule.paused ? "resume" : "pause"}
</Button>
) : null}
<Button onClick={onTogglePaused} disabled={pending}>
{schedule.paused ? "resume" : "pause"}
</Button>
<Button onClick={onRemove} disabled={pending}>
remove
</Button>

View file

@ -61,17 +61,23 @@ export function ScheduleForm({
onSaved: () => void;
onCancel: () => void;
}) {
// What the form opened with. A refetch can change `schedule` under an open
// form; every "did the operator edit this" comparison is against this
// snapshot, so another edit landing meanwhile is not reverted on save.
const [original] = useState(schedule);
const [initialStart] = useState(() =>
toLocalInput(original?.anchor_unix ?? nextMinuteUnix()),
);
const initialInterval =
schedule?.interval_seconds != null
? splitInterval(schedule.interval_seconds)
original?.interval_seconds != null
? splitInterval(original.interval_seconds)
: { amount: 1, unit: "h" as IntervalUnit };
const initialStart = toLocalInput(schedule?.anchor_unix ?? nextMinuteUnix());
const [name, setName] = useState(schedule?.name ?? "");
const [body, setBody] = useState(schedule?.body ?? "");
const [agents, setAgents] = useState<string[]>(schedule?.agents ?? []);
const [name, setName] = useState(original?.name ?? "");
const [body, setBody] = useState(original?.body ?? "");
const [agents, setAgents] = useState<string[]>(original?.agents ?? []);
const [repeat, setRepeat] = useState(
schedule ? schedule.interval_seconds !== null : true,
original ? original.interval_seconds !== null : true,
);
const [amount, setAmount] = useState(String(initialInterval.amount));
const [unit, setUnit] = useState<IntervalUnit>(initialInterval.unit);
@ -79,7 +85,7 @@ export function ScheduleForm({
const [saving, setSaving] = useState(false);
const [error, setError] = useState<ProblemDetails | null>(null);
const idPrefix = `schedule-${schedule?.id ?? "new"}`;
const idPrefix = `schedule-${original?.id ?? "new"}`;
const amountNum = Number(amount);
const intervalSeconds =
repeat && Number.isInteger(amountNum) && amountNum > 0
@ -101,16 +107,18 @@ export function ScheduleForm({
// always look edited.
const startChanged = start !== initialStart;
const cadenceChanged =
schedule !== undefined &&
original !== undefined &&
(startChanged ||
(repeat ? intervalSeconds : null) !== schedule.interval_seconds);
(repeat ? intervalSeconds : null) !== original.interval_seconds);
const nowUnix = Math.floor(Date.now() / 1000);
const startInPast = startUnix !== null && startUnix <= nowUnix;
const fires =
cadenceValid && startUnix !== null
? upcomingFires(
startUnix,
repeat ? intervalSeconds : null,
Math.floor(Date.now() / 1000),
nowUnix,
PREVIEW_COUNT,
)
: [];
@ -122,8 +130,8 @@ export function ScheduleForm({
setError(null);
try {
const cadence = repeat ? intervalSeconds : null;
const patch = schedule
? patchBody(schedule, {
const patch = original
? patchBody(original, {
name,
body,
agents,
@ -136,8 +144,8 @@ export function ScheduleForm({
onSaved();
return;
}
const r = schedule
? await fetch(`/api/schedules/${encodeURIComponent(schedule.id)}`, {
const r = original
? await fetch(`/api/schedules/${encodeURIComponent(original.id)}`, {
method: "PATCH",
headers: { "content-type": "application/json" },
body: JSON.stringify(patch),
@ -238,8 +246,12 @@ export function ScheduleForm({
{repeat ? "next fires: " : "fires: "}
{fires.map(fmtLocal).join(" · ")}
</p>
) : cadenceValid ? (
<p class="ui-schedule-form-hint">that time has already passed</p>
) : null}
{startInPast && (original === undefined || startChanged) ? (
<p class="ui-schedule-form-hint">
That start is in the past: saving fires the latest missed time once,
right away, in every agent chosen.
</p>
) : null}
{cadenceChanged ? (
<p class="ui-schedule-form-warn">
@ -257,7 +269,7 @@ export function ScheduleForm({
type="submit"
disabled={saving || !cadenceValid || agents.length === 0}
>
{saving ? "saving…" : schedule ? "save" : "create"}
{saving ? "saving…" : original ? "save" : "create"}
</Button>
</div>
</form>

View file

@ -16,13 +16,14 @@ export interface Schedule {
next_fire_unix: number | null;
}
export type ScheduleState = "active" | "paused" | "done";
export type ScheduleState = "active" | "paused" | "past";
// A one-shot that has fired reads "done" rather than "active": nothing is
// left to wait for, but the card stays until the operator removes it.
// A one-shot whose time has passed reads "past" rather than "active": the
// controller has no next fire for it. That is only its clock against the
// anchor, not a record that an agent fired, so the page never says "fired".
export function scheduleState(s: Schedule): ScheduleState {
if (s.paused) return "paused";
if (s.interval_seconds === null && s.next_fire_unix === null) return "done";
if (s.interval_seconds === null && s.next_fire_unix === null) return "past";
return "active";
}