ProSchedulerSign in

Timeline · Resources

Dynamic resource sort

Row order is the order of resources: sort it by name, role, or hours booked.

Live demo

Sort busiest first · Move a long bar and watch the order change

Fullscreen
import { useMemo, useState } from "react"
import { ProScheduler } from "@andreisaac/pro-scheduler"
import { studioAssignments, studioResources } from "./data/studio"
import { Choice, Toolbar, useEvents } from "./kit"

type SortKey = "name" | "role" | "load"

export function ResourceSortDemo() {
  const { events, handlers } = useEvents(studioAssignments)
  const [sort, setSort] = useState<SortKey>("load")

  const resources = useMemo(() => {
    const load = new Map<string, number>()
    for (const e of events) {
      const hours = (new Date(e.end!).getTime() - new Date(e.start).getTime()) / 3_600_000
      load.set(String(e.resource), (load.get(String(e.resource)) ?? 0) + hours)
    }
    const list = [...studioResources]
    if (sort === "name") list.sort((a, b) => a.name.localeCompare(b.name))
    if (sort === "role") list.sort((a, b) => a.role.localeCompare(b.role) || a.name.localeCompare(b.name))
    if (sort === "load") list.sort((a, b) => (load.get(b.id) ?? 0) - (load.get(a.id) ?? 0))
    return list
  }, [events, sort])

  return (
    <div className="docs-demo__stack">
      <Toolbar label="Sort rows">
        <Choice active={sort === "load"} onClick={() => setSort("load")}>
          Busiest first
        </Choice>
        <Choice active={sort === "name"} onClick={() => setSort("name")}>
          Name
        </Choice>
        <Choice active={sort === "role"} onClick={() => setSort("role")}>
          Role
        </Choice>
      </Toolbar>
      <ProScheduler
        data={events}
        // Row order is simply the order of `resources`.
        resources={resources}
        view={{ timeline: { type: "month" } }}
        renderResource={(r) => (
          <div className="min-w-0">
            <div className="truncate text-sm font-medium">{r.name}</div>
            <div className="truncate text-xs text-muted-foreground">{String(r.role)}</div>
          </div>
        )}
        height={520}
        {...handlers}
      />
    </div>
  )
}

About this demo

Row order is the order of resources: sort it by name, role, or hours booked.

Things to try

  • Sort busiest first
  • Move a long bar and watch the order change

The props behind it are explained in Resources.

Use ProScheduler in your project