ProSchedulerSign in

Timeline · Loading events

Virtual loading

Two thousand crews, but events are fetched only for the rows in view: onVirtualLoading reports the rendered rows and dates as you scroll, and the demo asks a (fake) API for just those.

Live demo

Scroll down fast and watch the request count · Page to next month

Fullscreen
import { useCallback, useRef, useState } from "react"
import { ProScheduler, type PschVirtualLoading, type SchedulerEvent, type SchedulerResource } from "@andreisaac/pro-scheduler"
import { fetchEvents, makeResources } from "./data/integrations"
import { Toolbar } from "./kit"

const ROWS = 2000

/** Only the rows and days in view are fetched; each window once. */
export function VirtualLoadingDemo() {
  const [resources] = useState(() => makeResources(0, ROWS))
  const [events, setEvents] = useState<SchedulerEvent[]>([])
  const [loading, setLoading] = useState(0)
  const [requests, setRequests] = useState(0)
  const loaded = useRef(new Set<string>())

  const onVirtualLoading = useCallback(({ resources: rows, firstDay, lastDay }: PschVirtualLoading) => {
    // Fetch per row and month, skipping what's already here.
    const key = (r: SchedulerResource) => `${r.id}:${firstDay.getFullYear()}-${firstDay.getMonth()}`
    const missing = rows.filter((r) => !loaded.current.has(key(r)))
    if (missing.length === 0) return
    missing.forEach((r) => loaded.current.add(key(r)))
    setLoading((n) => n + 1)
    setRequests((n) => n + 1)
    fetchEvents(missing, firstDay, lastDay).then((batch) => {
      setEvents((list) => [...list, ...batch])
      setLoading((n) => n - 1)
    })
  }, [])

  return (
    <>
      <Toolbar label="Loading">
        <span className="text-sm text-muted-foreground" aria-live="polite">
          {loading > 0 ? "Loading…" : "Up to date"} · {requests} requests · {events.length.toLocaleString()} events
          for {ROWS.toLocaleString()} crews
        </span>
      </Toolbar>
      <ProScheduler
        data={events}
        resources={resources}
        view={{ timeline: { type: "month" } }}
        styleConfig={{ unitWidthPx: 46, fitToWidth: false }}
        onVirtualLoading={onVirtualLoading}
        height={520}
      />
    </>
  )
}

About this demo

Two thousand crews, but events are fetched only for the rows in view: onVirtualLoading reports the rendered rows and dates as you scroll, and the demo asks a (fake) API for just those.

Things to try

  • Scroll down fast and watch the request count
  • Page to next month

The props behind it are explained in Output and scale.

Use ProScheduler in your project