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
Fullscreenimport { 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}
/>
</>
)
}// 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
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.