import { For, Show, createEffect, createSignal } from "solid-js"; import { postInstanceWeeklyPlanMaterialize } from "@/api/instances"; import { formatApiError, type MaterializeReport } from "@/api/client"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { DatePicker } from "@/components/ui/date-picker"; import { Label } from "@/components/ui/label"; import { SidePanel } from "@/components/ui/side-panel"; import { dateInputToEndOfDayMs, dateTimeInputToMs, isoDateToInput } from "@/lib/datetime-input"; import { useT } from "@/stores/preferences-context"; /** * Turns a section's resolved weekly plan into dated lessons. Always previews * first (a dry run writes nothing), then the same range is applied — the call * is additive and idempotent, so a retry never doubles a lesson. */ export function MaterializePanel(props: { instanceId: string; open: boolean; onOpenChange: (open: boolean) => void; /** limits.weekly_plan.max_materialize_days */ maxDays?: number; onApplied: (report: MaterializeReport) => void | Promise; }) { const t = useT(); const [from, setFrom] = createSignal(""); const [to, setTo] = createSignal(""); const [preview, setPreview] = createSignal(null); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); createEffect(() => { if (!props.open) return; setFrom(""); setTo(""); setPreview(null); setError(""); }); const range = () => { const fromMs = dateTimeInputToMs(from(), "00:00"); const toMs = dateInputToEndOfDayMs(to()); if (fromMs === null || toMs === null || toMs < fromMs) return null; return { from: fromMs, to: toMs }; }; const run = async (apply: boolean) => { const body = range(); if (!body) { setError(t("weeklyPlan.rangeInvalid")); return; } const days = Math.round((body.to - body.from) / 86_400_000); if (props.maxDays && days > props.maxDays) { setError(t("weeklyPlan.rangeTooLong", { days: props.maxDays })); return; } setError(""); setPending(true); try { const report = await postInstanceWeeklyPlanMaterialize(props.instanceId, { ...body, apply }); if (apply) { props.onOpenChange(false); await props.onApplied(report); } else { setPreview(report); } } catch (err) { setError(formatApiError(err)); } finally { setPending(false); } }; return (
{ setFrom(value); setPreview(null); }} />
{ setTo(value); setPreview(null); }} />
{(report) => (

{t("weeklyPlan.previewSummary", { candidates: report().candidates, existing: report().skipped_existing, holiday: report().skipped_holiday, })}

{t("weeklyPlan.nothingToCreate")}

0}>

{t("weeklyPlan.blockedDays")}

    {(day) =>
  • {isoDateToInput(day.date)}
  • }
)}
{error()}
0} fallback={} >
); }