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
Fullscreenimport { 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}
/>
</>
)
}// data/integrations.ts — sample data, relative to the current week or month.
import type { SchedulerEvent, SchedulerResource } from "@andreisaac/pro-scheduler"
const palette = ["#2563eb", "#059669", "#d97706", "#7c3aed", "#db2777", "#0891b2"]
const jobs = ["Install", "Survey", "Repair", "Audit", "Deliver", "Inspect", "Train"]
/** Resources `from`..`from + count - 1`, named and coloured by index. */
export function makeResources(from: number, count: number): SchedulerResource[] {
return Array.from({ length: count }, (_, i) => {
const n = from + i
return { id: `r${n}`, name: `Crew ${String(n + 1).padStart(4, "0")}`, color: palette[n % palette.length] }
})
}
/** A tiny seeded hash, so a row and day always get the same events. */
function hash(...parts: number[]) {
let h = 2166136261
for (const part of parts) h = Math.imul(h ^ part, 16777619)
return (h >>> 0) / 4294967296
}
/** The events of these resources between two dates, after a simulated network delay. */
export async function fetchEvents(
resources: SchedulerResource[],
from: Date,
to: Date,
latencyMs = 450,
): Promise<SchedulerEvent[]> {
await new Promise((resolve) => setTimeout(resolve, latencyMs))
const events: SchedulerEvent[] = []
const firstDay = new Date(from.getFullYear(), from.getMonth(), from.getDate())
for (const resource of resources) {
const row = Number(String(resource.id).slice(1))
for (let day = new Date(firstDay); day < to; day.setDate(day.getDate() + 1)) {
const seed = hash(row, day.getFullYear(), day.getMonth(), day.getDate())
if (seed < 0.45) continue // a free day
const hour = 7 + Math.floor(seed * 9)
const start = new Date(day.getFullYear(), day.getMonth(), day.getDate(), hour)
events.push({
id: `${resource.id}-${day.toDateString()}`,
title: `${jobs[Math.floor(seed * 1000) % jobs.length]} #${row + 1}`,
start,
end: new Date(start.getTime() + (2 + Math.floor(seed * 7)) * 3_600_000),
resource: resource.id,
})
}
}
return events
}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.