# Add the "Rating Summary" block to this P1 project

Add a Puck component from P1 Blocks (https://p1-blocks.prolificdigital.io/blocks/rating-summary) to this Pantheon Content Publisher (P1) Next.js project. Overall star rating with a text equivalent, per-star distribution bars, review count, source and highlighted reviews.

## Steps

1. Create `components/puck/rating-summary-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 { ratingSummaryBlock } from "./components/puck/rating-summary-block";`
3. Register it under `components` as `RatingSummaryBlock: ratingSummaryBlock`.
4. Add `"RatingSummaryBlock"` to the best-fitting entry in `categories` (this block's library category is "Social proof"). Create a category only if none fits.
5. Run the project's type check (usually `pnpm typecheck`) and resolve anything project-specific.
6. 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-`.
- Images are plain `imageUrl` + `imageAlt` text fields. If this project defines a P1 media-library field (search for `"p1-media"`), offer to switch to it.
- Don't change prop names in `defaultProps`. Content editors' saved pages depend on them.

## File: `components/puck/rating-summary-block.tsx`

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

/**
 * Rating Summary — overall score with stars (and a plain-text "4.8 out of 5"), a bar per star
 * level, the review count and its source, plus a few highlighted reviews.
 * Register: `RatingSummaryBlock: ratingSummaryBlock` in puck.config.tsx.
 */

type Heading = "h1" | "h2" | "h3" | "h4";
type Stars = "5" | "4" | "3" | "2" | "1";

type Review = { rating: Stars; title: string; body: string; name: string; date: string };

export type RatingSummaryProps = {
  heading: string;
  headingLevel: Heading;
  intro: string;
  rating: string;
  fiveStar: string;
  fourStar: string;
  threeStar: string;
  twoStar: string;
  oneStar: string;
  source: string;
  reviews: Review[];
};

const subLevel = { h1: "h2", h2: "h3", h3: "h4", h4: "h5" } as const;

const num = (v?: string) => {
  const n = parseFloat(String(v ?? "").replace(/[^0-9.-]/g, ""));
  return Number.isFinite(n) ? n : NaN;
};

const STAR = "M10 1.5l2.6 5.3 5.9.9-4.3 4.1 1 5.8L10 14.9l-5.2 2.7 1-5.8L1.5 7.7l5.9-.9L10 1.5Z";

const StarRow = ({ value, size }: { value: number; size: string }) => {
  const pct = Math.max(0, Math.min(100, (value / 5) * 100));
  const row = (cls: string) => (
    <span className={`flex shrink-0 gap-0.5 ${cls}`}>
      {[0, 1, 2, 3, 4].map((i) => (
        <svg key={i} aria-hidden="true" viewBox="0 0 20 20" fill="currentColor" className={`${size} shrink-0`}>
          <path d={STAR} />
        </svg>
      ))}
    </span>
  );
  return (
    <span aria-hidden="true" className="relative inline-flex">
      {row("text-neutral-200")}
      <span className="absolute inset-y-0 left-0 overflow-hidden" style={{ width: `${pct}%` }}>
        {row("text-indigo-600")}
      </span>
    </span>
  );
};

const starOptions = [
  { label: "5 stars", value: "5" },
  { label: "4 stars", value: "4" },
  { label: "3 stars", value: "3" },
  { label: "2 stars", value: "2" },
  { label: "1 star", value: "1" },
];

const countHelp = { help: "Number of reviews at this level. The bar lengths are calculated from these." };

export const ratingSummaryBlock: ComponentConfig<RatingSummaryProps> = {
  label: "Rating Summary",
  fields: {
    heading: { type: "text", label: "Heading" },
    headingLevel: {
      type: "select",
      label: "Heading level",
      options: [
        { label: "H1", value: "h1" },
        { label: "H2", value: "h2" },
        { label: "H3", value: "h3" },
        { label: "H4", value: "h4" },
      ],
      metadata: { help: "Sets the page outline for screen readers and search. The look doesn't change." },
    },
    intro: { type: "textarea", label: "Intro", metadata: { help: "Optional sentence under the heading. Leave blank to hide it." } },
    rating: {
      type: "text",
      label: "Overall rating",
      metadata: { help: "Out of 5, like 4.8. Leave blank to calculate it from the counts below." },
    },
    fiveStar: { type: "text", label: "5-star reviews", metadata: countHelp },
    fourStar: { type: "text", label: "4-star reviews", metadata: countHelp },
    threeStar: { type: "text", label: "3-star reviews", metadata: countHelp },
    twoStar: { type: "text", label: "2-star reviews", metadata: countHelp },
    oneStar: { type: "text", label: "1-star reviews", metadata: countHelp },
    source: {
      type: "text",
      label: "Source",
      metadata: { help: "Where the ratings come from and when, such as \"Participant survey, spring 2025\". Shown under the score." },
    },
    reviews: {
      type: "array",
      label: "Highlighted reviews",
      min: 0,
      max: 6,
      getItemSummary: (item) => [item?.rating ? `${item.rating}★` : "", item?.title || item?.name].filter(Boolean).join(" · ").slice(0, 48) || "Review",
      defaultItemProps: { rating: "5", title: "Short headline", body: "What the reviewer said, in their words.", name: "First name, last initial", date: "" },
      arrayFields: {
        rating: { type: "select", label: "Rating", options: starOptions },
        title: { type: "text", label: "Headline" },
        body: { type: "textarea", label: "Review" },
        name: { type: "text", label: "Reviewer" },
        date: { type: "text", label: "Date", metadata: { help: "Shown as written, such as \"May 2025\"." } },
      },
    },
  },
  defaultProps: {
    heading: "How families rate our summer camps",
    headingLevel: "h2",
    intro: "Every family gets a short survey on the last day of camp. Here's the honest tally, including the critiques we're working on.",
    rating: "4.8",
    fiveStar: "180",
    fourStar: "24",
    threeStar: "5",
    twoStar: "2",
    oneStar: "1",
    source: "Sample data · End-of-camp family survey, summer 2025",
    reviews: [
      {
        rating: "5",
        title: "She came home muddy and happy",
        body: "Counselors were patient, the creek days were the highlight, and the daily photo email gave me peace of mind at work.",
        name: "Keisha W.",
        date: "August 2025",
      },
      {
        rating: "5",
        title: "Worth every penny of the sliding scale",
        body: "We paid the lowest tier and never once felt like it. Same gear, same field trips, same welcome.",
        name: "Daniel R.",
        date: "July 2025",
      },
      {
        rating: "4",
        title: "Great program, tight pickup window",
        body: "My twins loved it. The 4:30 pickup is hard for working parents, so I'm glad to see aftercare is coming next year.",
        name: "Priya S.",
        date: "July 2025",
      },
    ],
  },
  render: ({ id, heading, headingLevel, intro, rating, fiveStar, fourStar, threeStar, twoStar, oneStar, source, reviews }) => {
    const H = headingLevel || "h2";
    const Sub = subLevel[headingLevel || "h2"];
    const counts = [
      { stars: 5, n: num(fiveStar) },
      { stars: 4, n: num(fourStar) },
      { stars: 3, n: num(threeStar) },
      { stars: 2, n: num(twoStar) },
      { stars: 1, n: num(oneStar) },
    ].map((c) => ({ ...c, n: Number.isFinite(c.n) && c.n > 0 ? c.n : 0 }));
    const total = counts.reduce((sum, c) => sum + c.n, 0);
    const computed = total ? counts.reduce((sum, c) => sum + c.stars * c.n, 0) / total : NaN;
    const typed = num(rating);
    const score = Number.isFinite(typed) ? Math.max(0, Math.min(5, typed)) : computed;
    const hasScore = Number.isFinite(score);
    const scoreText = hasScore ? (Math.round(score * 10) / 10).toFixed(1) : "—";
    const list = (reviews ?? []).filter((r) => r?.body || r?.title);
    const hasHeader = Boolean(heading || intro);
    const distId = `${id || "rating"}-dist`;

    return (
      <section className="bg-white px-4 py-16 sm:px-6 sm:py-20 lg:px-8 lg:py-24">
        <div className="mx-auto max-w-7xl">
          {hasHeader ? (
            <div className="max-w-2xl">
              {heading ? <H className="text-3xl font-semibold tracking-tight text-balance text-neutral-950 sm:text-4xl">{heading}</H> : null}
              {intro ? <p className="mt-4 text-lg leading-8 text-pretty text-neutral-600">{intro}</p> : null}
            </div>
          ) : null}

          <div className={`${hasHeader ? "mt-12 sm:mt-16" : ""} grid grid-cols-1 gap-12 lg:grid-cols-12 lg:gap-16`}>
            <div className="lg:col-span-5 xl:col-span-4">
              <div className="rounded-2xl bg-neutral-50 p-6 ring-1 ring-neutral-950/5 sm:p-8">
                <div className="flex flex-wrap items-end gap-x-4 gap-y-2">
                  <p className="text-6xl font-semibold tracking-tight text-neutral-950 tabular-nums">
                    {scoreText}
                    <span className="sr-only"> out of 5 stars</span>
                  </p>
                  {hasScore ? (
                    <div className="pb-2">
                      <StarRow value={score} size="size-5" />
                      <p aria-hidden="true" className="mt-1 text-sm text-neutral-600">
                        {scoreText} out of 5
                      </p>
                    </div>
                  ) : null}
                </div>
                {total > 0 ? (
                  <p className="mt-3 text-sm font-medium text-neutral-800">
                    Based on <span className="tabular-nums">{total.toLocaleString("en-US")}</span> {total === 1 ? "review" : "reviews"}
                  </p>
                ) : null}

                {total > 0 ? (
                  <>
                    <p id={distId} className="sr-only">
                      Rating breakdown
                    </p>
                    <ul role="list" aria-labelledby={distId} className="mt-6 space-y-3">
                      {counts.map((c) => {
                        const pct = Math.round((c.n / total) * 100);
                        return (
                          <li key={c.stars} className="flex items-center gap-3 text-sm">
                            <span className="w-14 shrink-0 font-medium text-neutral-800">
                              {c.stars} {c.stars === 1 ? "star" : "stars"}
                            </span>
                            <span aria-hidden="true" className="relative h-2 min-w-0 flex-1 overflow-hidden rounded-full bg-neutral-200">
                              <span className="absolute inset-y-0 left-0 rounded-full bg-indigo-600" style={{ width: `${pct}%` }} />
                            </span>
                            <span className="w-16 shrink-0 text-right text-neutral-600 tabular-nums">
                              {pct}%<span className="sr-only"> ({c.n} reviews)</span>
                            </span>
                          </li>
                        );
                      })}
                    </ul>
                  </>
                ) : null}

                {source ? <p className="mt-6 border-t border-neutral-200 pt-4 text-xs leading-5 text-neutral-600">{source}</p> : null}
              </div>
            </div>

            {list.length > 0 ? (
              <ul role="list" className="divide-y divide-neutral-200 lg:col-span-7 xl:col-span-8">
                {list.map((r, i) => {
                  const stars = Number(r.rating) || 5;
                  return (
                    <li key={`${r.title}-${i}`} className="py-8 first:pt-0 last:pb-0">
                      <article>
                        <div className="flex flex-wrap items-center gap-x-3 gap-y-1">
                          <StarRow value={stars} size="size-4" />
                          <p className="sr-only">Rated {stars} out of 5</p>
                          {r.date ? <p className="text-sm text-neutral-600">{r.date}</p> : null}
                        </div>
                        {r.title ? <Sub className="mt-3 text-lg font-semibold text-pretty text-neutral-950">{r.title}</Sub> : null}
                        {r.body ? <p className="mt-2 max-w-2xl text-base leading-7 text-pretty text-neutral-700">{r.body}</p> : null}
                        {r.name ? <p className="mt-3 text-sm font-medium text-neutral-950">{r.name}</p> : null}
                      </article>
                    </li>
                  );
                })}
              </ul>
            ) : null}
          </div>
        </div>
      </section>
    );
  },
};
```
