# Add the "Uptime Status" block to this P1 project

Add a Puck component from P1 Blocks (https://p1-blocks.prolificdigital.io/blocks/uptime-status) to this Pantheon Content Publisher (P1) Next.js project. A status-page widget: overall banner, per-service status and uptime, and a bar for each day of history.

## Steps

1. Create `components/puck/uptime-status-block.tsx` with the exact file contents below. If `components/puck/` doesn't exist, find the folder where this project keeps its Puck components (look at the imports in `puck.config.tsx`) and put it there.
2. In `puck.config.tsx`, import it: `import { uptimeStatusBlock } from "./components/puck/uptime-status-block";`
3. Register it under `components` as `UptimeStatusBlock: uptimeStatusBlock`.
4. Add `"UptimeStatusBlock"` to the best-fitting entry in `categories` (this block's library category is "Dashboards"). Create a category only if none fits.
5. Dashboard widgets are designed to sit inside the Dashboard Grid block (https://p1-blocks.prolificdigital.io/r/dashboard-grid.md). If the project doesn't have it yet, offer to add it too.
6. Run the project's type check (usually `pnpm typecheck`) and resolve anything project-specific.
7. If `package.json` has a `sync:registry` script, tell the user it must run (or that CI will run it) before the new component appears in the P1 editor.

## Rules

- Do not add dependencies. The block is self-contained by design: one type-only import from `@puckeditor/core`, Tailwind v4 classes, no client JavaScript.
- Keep the files as-is unless the user asks for changes. The accent color is `indigo-*`; if the project has brand colors, offer to swap them with a find-and-replace on `indigo-`.
- Data values are text fields on purpose: P1 data-source tokens (`{{ source.field }}`) only bind in text fields. Don't convert them to number fields.
- Don't change prop names in `defaultProps`. Content editors' saved pages depend on them.

## File: `components/puck/uptime-status-block.tsx`

```tsx
import type { ComponentConfig } from "@puckeditor/core";

/**
 * Uptime Status — a status-page widget: an overall banner computed from the services, then
 * one row per service with its current status, uptime and a bar per day of history.
 * Every value is a text field, so it can be bound to a P1 data source. Place inside a Dashboard Grid.
 * Register: `UptimeStatusBlock: uptimeStatusBlock` in puck.config.tsx.
 */
// spec: status-colors

type Heading = "h2" | "h3" | "h4" | "p";
type Status = "operational" | "degraded" | "outage" | "maintenance";

export type UptimeStatusProps = {
  title: string;
  headingLevel: Heading;
  subtitle: string;
  services: {
    name: string;
    status: Status;
    uptime: string;
    history: string;
  }[];
  footnote: string;
};

/** "1,234.5", "$12k" or "-3.2%" → number; anything unparseable → null. */
function toNumber(raw: string | undefined): number | null {
  if (raw == null) return null;
  const cleaned = String(raw).replace(/[^0-9.\-]/g, "");
  if (cleaned === "" || cleaned === "-" || cleaned === ".") return null;
  const n = Number(cleaned);
  return Number.isFinite(n) ? n : null;
}

const STATUS: Record<Status, { label: string; text: string; icon: string; rank: number }> = {
  operational: {
    label: "Operational",
    text: "text-emerald-700",
    icon: "M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm3.857-9.809a.75.75 0 0 0-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 1 0-1.06 1.061l2.5 2.5a.75.75 0 0 0 1.137-.089l4-5.5Z",
    rank: 0,
  },
  maintenance: {
    label: "Maintenance",
    text: "text-indigo-700",
    icon: "M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm.75-13a.75.75 0 0 0-1.5 0v5c0 .414.336.75.75.75h4a.75.75 0 0 0 0-1.5h-3.25V5Z",
    rank: 1,
  },
  degraded: {
    label: "Degraded",
    text: "text-amber-800",
    icon: "M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495ZM10 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 10 5Zm0 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z",
    rank: 2,
  },
  outage: {
    label: "Outage",
    text: "text-red-700",
    icon: "M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16ZM8.28 7.22a.75.75 0 0 0-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 1 0 1.06 1.06L10 11.06l1.72 1.72a.75.75 0 1 0 1.06-1.06L11.06 10l1.72-1.72a.75.75 0 0 0-1.06-1.06L10 8.94 8.28 7.22Z",
    rank: 3,
  },
};

const BANNER = {
  ok: { text: "All systems operational", tone: "bg-emerald-50 text-emerald-900 ring-emerald-600/20", icon: STATUS.operational.icon },
  maintenance: { text: "Scheduled maintenance in progress", tone: "bg-indigo-50 text-indigo-900 ring-indigo-600/20", icon: STATUS.maintenance.icon },
  degraded: { text: "Degraded performance", tone: "bg-amber-50 text-amber-900 ring-amber-600/25", icon: STATUS.degraded.icon },
  partial: { text: "Partial outage", tone: "bg-red-50 text-red-900 ring-red-600/20", icon: STATUS.outage.icon },
  major: { text: "Major outage", tone: "bg-red-50 text-red-900 ring-red-600/20", icon: STATUS.outage.icon },
} as const;

type Day = "up" | "degraded" | "down" | "none";

const DAY: Record<Day, { label: string; fill: string }> = {
  up: { label: "No incidents", fill: "fill-emerald-500" },
  degraded: { label: "Degraded", fill: "fill-amber-400" },
  down: { label: "Outage", fill: "fill-red-600" },
  none: { label: "No data", fill: "fill-neutral-200" },
};

/** "1, 1, 0.5, 0" → day states. 1 = up, 0.5 (anything between) = degraded, 0 = down. */
function parseHistory(raw: string | undefined): Day[] {
  return (raw ?? "")
    .split(",")
    .filter((v) => v.trim() !== "")
    .slice(-90)
    .map((v) => {
      const n = toNumber(v);
      if (n === null) return "none";
      if (n >= 1) return "up";
      if (n <= 0) return "down";
      return "degraded";
    });
}

/** Never rounds up to 100%: 99.996 shows as 99.99. */
function formatPct(n: number) {
  const clamped = Math.min(100, Math.max(0, n));
  return String(Math.floor(clamped * 100 + 1e-6) / 100);
}

function dayAgo(daysAgo: number) {
  return daysAgo === 0 ? "Today" : daysAgo === 1 ? "Yesterday" : `${daysAgo} days ago`;
}

function Bars({ days, className }: { days: Day[]; className: string }) {
  const n = days.length;
  const counts = { up: 0, degraded: 0, down: 0, none: 0 };
  days.forEach((d) => counts[d]++);
  const summary = `Last ${n} days: ${counts.up} without incidents, ${counts.degraded} degraded, ${counts.down} with an outage${counts.none ? `, ${counts.none} without data` : ""}.`;
  return (
    <div className={className}>
      <svg viewBox={`0 0 ${n * 4 - 1} 28`} preserveAspectRatio="none" role="img" aria-label={summary} className="block h-7 w-full">
        {days.map((d, i) => (
          <rect key={i} x={i * 4} y="0" width="3" height="28" rx="1" className={DAY[d].fill}>
            <title>{`${dayAgo(n - 1 - i)}: ${DAY[d].label}`}</title>
          </rect>
        ))}
      </svg>
      <div className="mt-1.5 flex items-center justify-between gap-3 text-xs text-neutral-500 tabular-nums">
        <span>{n} days ago</span>
        {counts.degraded || counts.down ? (
          <span className="truncate text-center">
            {[counts.down ? `${counts.down} outage` : "", counts.degraded ? `${counts.degraded} degraded` : ""].filter(Boolean).join(" · ")}
          </span>
        ) : null}
        <span>Today</span>
      </div>
    </div>
  );
}

/** Builds a sample history string: 90 good days, with a few marked degraded (0.5) or down (0). */
function sample(marks: Record<number, number>) {
  return Array.from({ length: 90 }, (_, i) => marks[i] ?? 1).join(", ");
}

export const uptimeStatusBlock: ComponentConfig<UptimeStatusProps> = {
  label: "Uptime Status",
  fields: {
    title: { type: "text", label: "Title" },
    headingLevel: {
      type: "select",
      label: "Heading level",
      options: [
        { label: "H2", value: "h2" },
        { label: "H3 (recommended inside a dashboard)", value: "h3" },
        { label: "H4", value: "h4" },
        { label: "Not a heading", value: "p" },
      ],
      metadata: { help: "Sets the page outline for screen readers and search. The look doesn't change." },
    },
    subtitle: { type: "text", label: "Subtitle", metadata: { help: "e.g. Last 90 days. Leave blank to hide." } },
    services: {
      type: "array",
      label: "Services",
      min: 1,
      max: 12,
      getItemSummary: (item) => (item?.name ? `${STATUS[item.status]?.label ?? "Operational"} · ${item.name}` : "Service"),
      defaultItemProps: { name: "New service", status: "operational", uptime: "100", history: "" },
      arrayFields: {
        name: { type: "text", label: "Service" },
        status: {
          type: "select",
          label: "Current status",
          options: [
            { label: "Operational", value: "operational" },
            { label: "Degraded", value: "degraded" },
            { label: "Outage", value: "outage" },
            { label: "Maintenance", value: "maintenance" },
          ],
          metadata: { help: "The banner at the top is worked out from these." },
        },
        uptime: {
          type: "text",
          label: "Uptime (%)",
          metadata: { help: "e.g. 99.98, or bind it: {{ monitor.uptime_90d }}. Leave blank to work it out from the history." },
        },
        history: {
          type: "textarea",
          label: "Daily history",
          metadata: { help: "Comma-separated, oldest first, one value per day (30 to 90): 1 = up, 0.5 = degraded, 0 = down. Leave blank to hide the bars." },
        },
      },
    },
    footnote: { type: "text", label: "Footnote", metadata: { help: "Where the data comes from. Leave blank to hide." } },
  },
  defaultProps: {
    title: "System status",
    headingLevel: "h3",
    subtitle: "Uptime over the last 90 days",
    services: [
      { name: "Online donations", status: "operational", uptime: "99.98", history: sample({ 41: 0.5 }) },
      { name: "Volunteer portal", status: "degraded", uptime: "99.71", history: sample({ 12: 0.5, 58: 0, 88: 0.5, 89: 0.5 }) },
      { name: "Event registration", status: "operational", uptime: "100", history: sample({}) },
      { name: "Member email", status: "maintenance", uptime: "99.94", history: sample({ 23: 0.5, 89: 0.5 }) },
    ],
    footnote: "Sample data · Source: site uptime monitor, checked every 60 seconds",
  },
  render: ({ title, headingLevel, subtitle, services, footnote }) => {
    const H = headingLevel || "h3";
    const list = (services ?? []).filter((s) => s?.name);
    const states = list.map((s) => (STATUS[s.status] ? s.status : "operational"));
    const outages = states.filter((s) => s === "outage").length;
    const worst = Math.max(0, ...states.map((s) => STATUS[s].rank));
    const banner =
      outages && outages === list.length && list.length > 1
        ? BANNER.major
        : outages
          ? BANNER.partial
          : worst === 2
            ? BANNER.degraded
            : worst === 1
              ? BANNER.maintenance
              : BANNER.ok;

    return (
      <div className="@container flex h-full flex-col rounded-xl bg-white p-5 shadow-xs ring-1 ring-neutral-950/5 sm:p-6 not-in-data-[dashboard-grid]:mx-4 not-in-data-[dashboard-grid]:my-8 sm:not-in-data-[dashboard-grid]:mx-auto sm:not-in-data-[dashboard-grid]:max-w-2xl">
        {title || subtitle ? (
          <div>
            {title ? <H className="text-base font-semibold text-neutral-950">{title}</H> : null}
            {subtitle ? <p className="mt-1 text-sm text-neutral-600">{subtitle}</p> : null}
          </div>
        ) : null}

        {list.length ? (
          <>
            <p className={`flex items-center gap-2.5 rounded-lg px-3.5 py-3 text-sm font-semibold ring-1 ring-inset ${banner.tone} ${title || subtitle ? "mt-4" : ""}`}>
              <svg aria-hidden="true" viewBox="0 0 20 20" fill="currentColor" className="size-5 shrink-0">
                <path fillRule="evenodd" d={banner.icon} clipRule="evenodd" />
              </svg>
              {banner.text}
            </p>

            <ul role="list" className="mt-2 divide-y divide-neutral-100">
              {list.map((service, i) => {
                const s = STATUS[states[i]];
                const days = parseHistory(service.history);
                const stated = toNumber(service.uptime);
                const known = days.filter((d) => d !== "none");
                const derived = known.length
                  ? (known.reduce((sum, d) => sum + (d === "up" ? 1 : d === "degraded" ? 0.5 : 0), 0) / known.length) * 100
                  : null;
                const uptime = stated ?? derived;
                return (
                  <li key={`${service.name}-${i}`} className="py-4 last:pb-0">
                    <div className="flex flex-wrap items-baseline justify-between gap-x-4 gap-y-1">
                      <p className="min-w-0 text-sm font-medium text-neutral-950">{service.name}</p>
                      <p className="flex items-center gap-3 text-sm">
                        {uptime !== null ? (
                          <span className="text-neutral-600 tabular-nums">
                            {formatPct(uptime)}%
                            <span className="sr-only"> uptime</span>
                          </span>
                        ) : null}
                        <span className={`inline-flex items-center gap-1 font-semibold ${s.text}`}>
                          <svg aria-hidden="true" viewBox="0 0 20 20" fill="currentColor" className="size-4 shrink-0">
                            <path fillRule="evenodd" d={s.icon} clipRule="evenodd" />
                          </svg>
                          <span className="sr-only">Status: </span>
                          {s.label}
                        </span>
                      </p>
                    </div>
                    {days.length ? (
                      <>
                        {days.length > 30 ? <Bars days={days.slice(-30)} className="mt-3 @md:hidden" /> : null}
                        <Bars days={days} className={`mt-3 ${days.length > 30 ? "hidden @md:block" : ""}`} />
                      </>
                    ) : null}
                  </li>
                );
              })}
            </ul>

            <ul role="list" aria-label="Legend" className="mt-5 flex flex-wrap gap-x-4 gap-y-1.5 border-t border-neutral-100 pt-4 text-xs text-neutral-600">
              {(["up", "degraded", "down", "none"] as const).map((d) => (
                <li key={d} className="inline-flex items-center gap-1.5">
                  <svg aria-hidden="true" viewBox="0 0 6 12" className="h-3 w-1.5">
                    <rect width="6" height="12" rx="1.5" className={DAY[d].fill} />
                  </svg>
                  {DAY[d].label}
                </li>
              ))}
            </ul>
          </>
        ) : (
          <p className="mt-4 text-sm text-neutral-500">No services to show yet.</p>
        )}

        {footnote ? <p className="mt-auto pt-4 text-xs text-neutral-500">{footnote}</p> : null}
      </div>
    );
  },
};
```
