{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pages-casework-landing",
  "title": "Casework landing",
  "description": "What a session's work is, read from its Casework profile, where each kind lands, the seam a dashboard fills for a registrar, and the guard that sends a session without the right to its own work.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/dashboard-cells",
    "@registrystack/shell-loading"
  ],
  "files": [
    {
      "path": "pages/casework-landing.tsx",
      "content": "import { useEffect, type ReactNode } from \"react\";\nimport type {\n  AuthenticatedSession,\n  DashboardResponse,\n} from \"@registrystack/app-runtime\";\nimport { useAuthority } from \"@registrystack/app-runtime/react\";\nimport { dashboardRoute } from \"@/blocks/dashboard/dashboard-target\";\nimport { Loading } from \"@/blocks/shell/loading\";\n\n/**\n * What a session's work is. It is read from what the session can do, its\n * Casework profile, and never from the name of its role: a role name grants\n * nothing, and Casework decides what a profile may read.\n *\n * A registrar is any session without a Casework profile: it works the\n * register through BReg alone.\n */\nexport type WorkKind = \"reviewer\" | \"supervisor\" | \"administrator\" | \"registrar\";\n\nexport function workKind(session: AuthenticatedSession): WorkKind {\n  switch (session.caseworkProfile) {\n    case \"staff\":\n      return \"reviewer\";\n    case \"supervisor\":\n      return \"supervisor\";\n    case \"administrator\":\n      return \"administrator\";\n    default:\n      return \"registrar\";\n  }\n}\n\n/** What the session's dashboard read has told the browser so far. */\nexport interface DashboardRead {\n  isPending: boolean;\n  data?: DashboardResponse;\n}\n\n/**\n * The dashboard seam. A registrar lands on the dashboard where the host\n * serves one to the session. Undefined while the read is pending, so the\n * landing is decided once; null where the host serves none, where the read\n * failed (the registrar lands on the register's home, never on the\n * dashboard's error) and for every kind of work but a registrar's.\n */\nexport function dashboardLanding(\n  session: AuthenticatedSession,\n  dashboard: DashboardRead,\n): string | null | undefined {\n  if (workKind(session) !== \"registrar\") return null;\n  if (dashboard.isPending) return undefined;\n  return dashboard.data?.dashboard ? dashboardRoute : null;\n}\n\n/**\n * Where a session opens: the page of its own work. A registrar opens on the\n * root, which sends them to the dashboard where there is one\n * (`dashboardLanding`) and to the register's home otherwise.\n */\nexport function landingPath(session: AuthenticatedSession): string {\n  switch (workKind(session)) {\n    case \"reviewer\":\n      return \"/casework\";\n    case \"supervisor\":\n      return \"/casework/team-reviews\";\n    case \"administrator\":\n      return \"/casework/setup\";\n    case \"registrar\":\n      return \"/\";\n  }\n}\n\n/** The Casework profiles whose work a page serves. */\ntype Profile = NonNullable<AuthenticatedSession[\"caseworkProfile\"]>;\n\n/**\n * Draws its page for a session whose Casework profile is one of `profiles`.\n * Any other session, arriving by a shared link, is sent to the page of its\n * own work instead: it never meets an error page for a link it was handed.\n */\nexport function WhenProfile({\n  profiles,\n  children,\n}: {\n  profiles: readonly Profile[];\n  children: ReactNode;\n}) {\n  const session = useAuthority();\n  const allowed =\n    session.caseworkProfile !== undefined &&\n    profiles.includes(session.caseworkProfile);\n  const landing = landingPath(session);\n  useEffect(() => {\n    if (!allowed) location.replace(`#${landing}`);\n  }, [allowed, landing]);\n  return allowed ? <>{children}</> : <Loading />;\n}\n",
      "type": "registry:block",
      "target": "@components/pages/casework-landing.tsx"
    }
  ],
  "type": "registry:block"
}