ProSchedulerSign in

Timeline · Loading events

Load resources on scroll

The resource list grows as you scroll: when onVirtualLoading reports a row near the end, the next 50 crews are appended — up to a thousand.

Live demo

Scroll to the bottom · Keep going

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

const PAGE = 50

/** Resources arrive 50 at a time as you scroll down. */
export function ResourcesOnScrollDemo() {
  const [resources, setResources] = useState(() => makeResources(0, PAGE))
  const { events, setEvents, handlers } = useEvents<SchedulerEvent>([])
  const fetching = useRef(false)
  const loadedRows = useRef(new Set<string>())

  const onVirtualLoading = useCallback(
    ({ endIndex, resources: rows, firstDay, lastDay }: PschVirtualLoading) => {
      // Events for rows coming into view…
      const fresh = rows.filter((r) => !loadedRows.current.has(String(r.id)))
      fresh.forEach((r) => loadedRows.current.add(String(r.id)))
      if (fresh.length) fetchEvents(fresh, firstDay, lastDay, 250).then((batch) => setEvents((l) => [...l, ...batch]))
      // …and the next page of resources near the end.
      if (fetching.current || endIndex < resources.length - 10 || resources.length >= 1000) return
      fetching.current = true
      setTimeout(() => {
        setResources((list) => [...list, ...makeResources(list.length, PAGE)])
        fetching.current = false
      }, 400)
    },
    [resources.length, setEvents],
  )

  return (
    <>
      <Toolbar label="Resources">
        <span className="text-sm text-muted-foreground" aria-live="polite">
          {resources.length} of 1,000 crews loaded — scroll down for more
        </span>
      </Toolbar>
      <ProScheduler
        data={events}
        resources={resources}
        view={{ timeline: { type: "week", resolutionHorizontal: "day" } }}
        styleConfig={{ unitWidthPx: 120, fitToWidth: true }}
        onVirtualLoading={onVirtualLoading}
        height={480}
        {...handlers}
      />
    </>
  )
}

About this demo

The resource list grows as you scroll: when onVirtualLoading reports a row near the end, the next 50 crews are appended — up to a thousand.

Things to try

  • Scroll to the bottom
  • Keep going

The props behind it are explained in Output and scale.

Use ProScheduler in your project