Dev Helpers
← Back

Build per-document author histories

Build per-document author histories. Build the result in an App Router server page and send the rendered entries to the client.

Open this example in the interactive library

Source code

// app/build-per-document-author-histories/page.tsx
export const metadata = { title: "Build per-document author histories" };
export default function Page() {
  const rows = [
    { doc: "guide", rev: 1, author: "Ada" },
    { doc: "api", rev: 1, author: "Leo" },
    { doc: "guide", rev: 3, author: "Kim" },
    { doc: "guide", rev: 4, author: "Ada" },
  ];
  const result = Object.fromEntries(
    [...new Set(rows.map((r) => r.doc))].map((doc) => [
      doc,
      rows
        .filter((r) => r.doc === doc)
        .toSorted((a, b) => a.rev - b.rev)
        .map((r) => r.author),
    ]),
  );
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Build per-document author histories"}</h2>
      {entries.length ? (
        <dl>
          {entries.map(([key, value]) => (
            <div key={key}>
              <dt>{key}</dt>
              <dd>
                {typeof value === "string" ? value : JSON.stringify(value)}
              </dd>
            </div>
          ))}
        </dl>
      ) : (
        <p>No matching entries.</p>
      )}
    </section>
  );
}