Dev Helpers
← Back

Build the first departure board

Build the first departure board. 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-the-first-departure-board/page.tsx
export const metadata = { title: "Build the first departure board" };
export default function Page() {
  const rows = [
    { line: "Red", start: 8, end: 10 },
    { line: "Blue", start: 9, end: 11 },
    { line: "Red", start: 12, end: 15 },
    { line: "Gold", start: 15, end: 16 },
  ];
  const result = Object.fromEntries(
    rows.toSorted((a, b) => b.start - a.start).map((r) => [r.line, r.start]),
  );
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Build the first departure board"}</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>
  );
}