Dev Helpers
← Back

Clip service windows to a morning shift

Clip service windows to a morning shift. 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 = [
    { 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 = rows
    .filter((r) => r.start < 12 && r.end > 8)
    .map((r) => ({
      ...r,
      start: Math.max(8, r.start),
      end: Math.min(12, r.end),
    }));
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Clip service windows to a morning shift"}</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>
  );
}