Skip to main content
⌘K

Search

Search pages, pieces and blocks.

Empty states/A list page in the App Shell (Strip) with nothing to show: first run, no results after a filter, and an error, each message in a box in the middle of the panel.

components/blocks/empty-states.tsx
"use client"
import * as React from "react"
import { AsteriskIcon, CalendarIcon, ChevronDownIcon, SearchIcon, TriangleAlertIcon } from "@/components/icons"import { AppShell, type AppShellNavItem } from "@/components/ui/app-shell"import { Badge } from "@/components/ui/badge"import { Breadcrumb, BreadcrumbLink, BreadcrumbList, BreadcrumbSection, BreadcrumbSwitcher } from "@/components/ui/breadcrumb"import { Button } from "@/components/ui/button"import {  DropdownMenu,  DropdownMenuCheckboxItem,  DropdownMenuContent,  DropdownMenuGroup,  DropdownMenuLabel,  DropdownMenuRadioGroup,  DropdownMenuRadioItem,  DropdownMenuTrigger,} from "@/components/ui/dropdown-menu"import { Empty, EmptyBox, EmptyContent, EmptyDescription, EmptyMedia, EmptyTitle } from "@/components/ui/empty"import {  ErrorState,  ErrorStateBox,  ErrorStateContent,  ErrorStateDescription,  ErrorStateMedia,  ErrorStateTitle,} from "@/components/ui/error-state"import { Field, FieldDescription, FieldGroup } from "@/components/ui/field"import { FilterAdd, FilterBar, FilterChip, FilterClear, FilterOptions } from "@/components/ui/filter-bar"import { InputGroup, InputGroupInput } from "@/components/ui/input-group"import { PageHeader, PageHeaderActions } from "@/components/ui/page-header"import { SearchInput } from "@/components/ui/search-input"import { Spinner } from "@/components/ui/spinner"import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
// Empty states: the Maré bookings page in the App Shell (Strip), in its// three empty moments. First run: no bookings yet, with the one action that// starts them. No results: a filter left nothing, with Clear filters. Error:// the bookings could not load, with Try again. Each message sits in the// middle of the space under the top row, its title and line in a box// (Empty's look box), the action under it at the box's width. The top// row's switcher has the views of Table with filters. Controlled by// `state` when given;// its own actions move it on and tell onStateChange ("list" is the page// with its rows, once filters are cleared, a retry works or a booking is// made). New booking and Open a lesson slot open the takeover, a focused// form over the shell, as on Table with filters.
type EmptyStatesState = "first-run" | "no-results" | "error" | "list"
type Status = "Confirmed" | "Pending" | "Cancelled"
const rows: { id: string; student: string; school: string; status: Status; when: string }[] = [  { id: "MR-2041", student: "Marta Silva", school: "Ericeira", status: "Confirmed", when: "Mon 30 Nov, 09:00" },  { id: "MR-2043", student: "Leonor Santos", school: "Costa da Caparica", status: "Pending", when: "Tue 1 Dec, 08:30" },  { id: "MR-2054", student: "Tomás Amaro", school: "Ericeira", status: "Confirmed", when: "Sun 6 Dec, 12:30" },]
const tones: Record<Status, "success" | "warning" | "destructive"> = {  Confirmed: "success",  Pending: "warning",  Cancelled: "destructive",}
const headings = ["Student", "School", "Status", "When"]
// The views in the top row's switcher, as on Table with filters.type View = "overview" | "upcoming" | "past" | "cancelled"
const views: { value: View; label: string }[] = [  { value: "overview", label: "Overview" },  { value: "upcoming", label: "Upcoming" },  { value: "past", label: "Past" },  { value: "cancelled", label: "Cancelled" },]
// The takeover's two forms: a new booking, or a lesson slot to open.type Task = "booking" | "slot"
const tasks: Record<Task, { title: string; action: string; fields: { id: string; label: string; value: string }[]; line: string }> = {  booking: {    title: "New booking",    action: "Book lesson",    fields: [      { id: "empty-new-student", label: "Student", value: "Tomás Amaro" },      { id: "empty-new-school", label: "School", value: "Ericeira" },      { id: "empty-new-when", label: "When", value: "Sat 12 Dec, 09:00" },      { id: "empty-new-instructor", label: "Instructor", value: "Miguel Costa" },    ],    line: "The student gets a confirmation by email once the lesson is booked.",  },  slot: {    title: "Open a lesson slot",    action: "Open slot",    fields: [      { id: "empty-slot-school", label: "School", value: "Ericeira" },      { id: "empty-slot-when", label: "When", value: "Sat 12 Dec, 09:00" },      { id: "empty-slot-level", label: "Level", value: "Beginner" },      { id: "empty-slot-places", label: "Places", value: "8" },    ],    line: "Students can book the slot as soon as it opens.",  },}
const stripNav = ["Overview", "Bookings", "Calendar", "Schools", "Instructors", "Students", "Lessons", "Payouts", "Reviews", "Messages", "Reports"]
function EmptyStatesBlock({  state,  onStateChange,}: {  state?: EmptyStatesState  onStateChange?: (state: EmptyStatesState) => void}) {  const [own, setOwn] = React.useState<EmptyStatesState>("first-run")  const current = state ?? own  const [retrying, setRetrying] = React.useState(false)  const [shown, setShown] = React.useState(headings)  const [view, setView] = React.useState<View>("overview")  const [task, setTask] = React.useState<Task>("booking")  const [taskOpen, setTaskOpen] = React.useState(false)
  function start(next: Task) {    setTask(next)    setTaskOpen(true)  }  const currentView = views.find((each) => each.value === view) ?? views[0]  const timer = React.useRef<number | undefined>(undefined)
  React.useEffect(() => () => window.clearTimeout(timer.current), [])
  function go(next: EmptyStatesState) {    setOwn(next)    onStateChange?.(next)  }
  function retry() {    setRetrying(true)    window.clearTimeout(timer.current)    timer.current = window.setTimeout(() => {      setRetrying(false)      go("list")    }, 900)  }
  const nav: AppShellNavItem[] = stripNav.map((label) => ({    label,    count: label === "Bookings" ? "104" : undefined,    active: label === "Bookings",  }))  const filtered = current === "no-results"  const columns = headings.filter((heading) => shown.includes(heading))
  return (    <AppShell      id="mare-empty"      brand={{ name: "Maré", logo: <AsteriskIcon /> }}      nav={nav}      settings={{ label: "Settings" }}      header={        <PageHeader look="toprow">          <Breadcrumb>            <BreadcrumbList>              <BreadcrumbSection>                <BreadcrumbLink href="#">Bookings</BreadcrumbLink>              </BreadcrumbSection>              <BreadcrumbSwitcher label={currentView.label} aria-label={`Bookings view: ${currentView.label}`}>                <DropdownMenuRadioGroup                  value={view}                  onValueChange={(value) => {                    const next = views.find((each) => each.value === value)                    if (next) setView(next.value)                  }}                >                  {views.map((each) => (                    <DropdownMenuRadioItem key={each.value} value={each.value} closeOnClick>                      {each.label}                    </DropdownMenuRadioItem>                  ))}                </DropdownMenuRadioGroup>              </BreadcrumbSwitcher>            </BreadcrumbList>          </Breadcrumb>          <SearchInput aria-label="Search bookings" placeholder="Search" />          <PageHeaderActions primary={{ label: "New booking", onClick: () => start("booking") }} />        </PageHeader>      }      takeover={{        open: taskOpen,        onOpenChange: setTaskOpen,        title: tasks[task].title,        actions: (          <Button            onClick={() => {              setTaskOpen(false)              if (task === "booking") go("list")            }}          >            {tasks[task].action}          </Button>        ),        children: (          <FieldGroup>            {tasks[task].fields.map(({ id, label, value }) => (              <Field key={id}>                <InputGroup>                  <InputGroupInput id={id} label={label} defaultValue={value} />                </InputGroup>              </Field>            ))}            <FieldDescription>{tasks[task].line}</FieldDescription>          </FieldGroup>        ),      }}    >      {(current === "no-results" || current === "list") && (        <div className="flex min-h-7 flex-wrap items-center justify-between gap-3">          <DropdownMenu>            <DropdownMenuTrigger render={<Button variant="secondary" />}>              <ChevronDownIcon data-icon="inline-start" />              Columns            </DropdownMenuTrigger>            <DropdownMenuContent align="start" className="w-44">              <DropdownMenuGroup>                <DropdownMenuLabel>Show columns</DropdownMenuLabel>                {headings.map((heading) => (                  <DropdownMenuCheckboxItem                    key={heading}                    checked={shown.includes(heading)}                    disabled={shown.includes(heading) && shown.length === 1}                    closeOnClick={false}                    onCheckedChange={(checked) =>                      setShown((old) => (checked ? headings.filter((each) => each === heading || old.includes(each)) : old.filter((each) => each !== heading)))                    }                  >                    {heading}                  </DropdownMenuCheckboxItem>                ))}              </DropdownMenuGroup>            </DropdownMenuContent>          </DropdownMenu>          <FilterBar size="default" aria-label="Filters for bookings">            {filtered && <FilterChip field="Status" value="Confirmed" onRemove={() => go("list")} />}            <FilterAdd>              <FilterOptions                field="Status"                options={["Confirmed", "Pending", "Cancelled"]}                selected={filtered ? ["Confirmed"] : []}                onToggle={() => {}}              />            </FilterAdd>            {filtered && <FilterClear onClick={() => go("list")} />}          </FilterBar>        </div>      )}
      {current === "first-run" && (        <Empty look="box">          <EmptyMedia>            <CalendarIcon />          </EmptyMedia>          <EmptyBox>            <EmptyTitle>No bookings yet</EmptyTitle>            <EmptyDescription>              Bookings show here once students book. Open a lesson slot at a school to take the first one.            </EmptyDescription>          </EmptyBox>          <EmptyContent>            <Button onClick={() => start("slot")}>Open a lesson slot</Button>          </EmptyContent>        </Empty>      )}
      {current === "no-results" && (        <Empty look="box">          <EmptyMedia>            <SearchIcon />          </EmptyMedia>          <EmptyBox>            <EmptyTitle>No booking matches</EmptyTitle>            <EmptyDescription>Nothing matches these filters this week.</EmptyDescription>          </EmptyBox>          <EmptyContent>            <Button variant="secondary" onClick={() => go("list")}>              Clear filters            </Button>          </EmptyContent>        </Empty>      )}
      {current === "error" && (        <ErrorState look="box">          <ErrorStateMedia variant="plain">            <TriangleAlertIcon />          </ErrorStateMedia>          <ErrorStateBox>            <ErrorStateTitle>Bookings could not load</ErrorStateTitle>            <ErrorStateDescription>The booking service did not answer. Nothing was lost.</ErrorStateDescription>          </ErrorStateBox>          <ErrorStateContent>            <Button variant="secondary" onClick={retry} disabled={retrying}>              {retrying && <Spinner data-icon="inline-start" />}              {retrying ? "Trying again" : "Try again"}            </Button>          </ErrorStateContent>        </ErrorState>      )}
      {current === "list" && (        <Table bordered>          <TableHeader>            <TableRow>              {columns.map((heading) => (                <TableHead key={heading}>{heading}</TableHead>              ))}            </TableRow>          </TableHeader>          <TableBody>            {rows.map((row) => (              <TableRow key={row.id}>                {shown.includes("Student") && <TableCell emphasis="strong">{row.student}</TableCell>}                {shown.includes("School") && <TableCell>{row.school}</TableCell>}                {shown.includes("Status") && (                  <TableCell>                    <Badge look="status" tone={tones[row.status]}>                      {row.status}                    </Badge>                  </TableCell>                )}                {shown.includes("When") && <TableCell numeric>{row.when}</TableCell>}              </TableRow>            ))}          </TableBody>        </Table>      )}    </AppShell>  )}
export { EmptyStatesBlock }export type { EmptyStatesState }