Dev Helpers
← Back

Find terminal tasks in a dependency graph

Find terminal tasks in a dependency graph. Render the transformed entries in a reusable React component.

Open this example in the interactive library

Live preview

Source code

// CollectionView.tsx
export default function CollectionView() {
  const rows = [
    { task: "design", needs: [] },
    { task: "build", needs: ["design"] },
    { task: "test", needs: ["build"] },
    { task: "ship", needs: ["build", "test"] },
  ];
  const result = rows
    .filter((r) => !rows.some((x) => x.needs.includes(r.task)))
    .map((r) => r.task);
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Find terminal tasks in a dependency graph"}</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>
  );
}