{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "acting-as-banner",
  "title": "Acting As Banner",
  "description": "Shows a sticky \"You're acting as…\" bar with who is really signed in, a countdown to when impersonation ends and a switch-back action, plus an org chip that makes each destructive button say where it acts.",
  "dependencies": [
    "lucide-react"
  ],
  "files": [
    {
      "path": "registry/default/acting-as-banner/acting-as-banner.tsx",
      "content": "\"use client\"\r\n\r\n// Bollard UI — acting-as-banner · MIT licence · https://bollard-ui.vercel.app/components/acting-as-banner\r\n\r\nimport * as React from \"react\"\r\nimport { Clock, LoaderCircle, Undo2, UserCog } from \"lucide-react\"\r\n\r\nimport { cn } from \"@/lib/utils\"\r\n\r\n/** Banner layout shared by every Bollard UI banner: one height, padding, icon size and action style. */\r\nconst bar = \"flex min-h-11 flex-wrap items-center gap-x-3 gap-y-1.5 px-3 py-1.5 text-sm\"\r\nconst action =\r\n  \"inline-flex h-8 shrink-0 items-center justify-center gap-1.5 rounded-md px-3 text-sm font-medium whitespace-nowrap outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-60\"\r\n/** Banner tones, identical across Bollard UI banners. */\r\nconst tones = {\r\n  warning: {\r\n    surface: \"border-amber-500/30 bg-background bg-linear-to-r from-amber-500/10 to-amber-500/10 text-amber-950 dark:text-amber-50\",\r\n    icon: \"text-amber-600 dark:text-amber-400\",\r\n    muted: \"text-amber-900/75 dark:text-amber-100/75\",\r\n    solid: \"bg-amber-700 text-white hover:bg-amber-800 dark:bg-amber-400 dark:text-amber-950 dark:hover:bg-amber-300\",\r\n    outline: \"border border-amber-700/25 hover:bg-amber-500/15 dark:border-amber-300/25\",\r\n  },\r\n} as const\r\n\r\nconst hues = [12, 36, 160, 200, 230, 262, 290, 330]\r\n\r\n/** A stable hue per org name, so \"Acme Corp\" is the same color in the banner and on every button. */\r\nexport function orgHue(name: string) {\r\n  let hash = 0\r\n  for (const char of name) hash = (hash * 31 + char.charCodeAt(0)) | 0\r\n  return hues[Math.abs(hash) % hues.length]\r\n}\r\n\r\nfunction OrgMark({ name, className }: { name: string; className?: string }) {\r\n  const initials = name\r\n    .split(/\\s+/)\r\n    .slice(0, 2)\r\n    .map((part) => part[0]?.toUpperCase())\r\n    .join(\"\")\r\n  return (\r\n    <span\r\n      aria-hidden\r\n      style={{ backgroundColor: `oklch(0.55 0.16 ${orgHue(name)})` }}\r\n      className={cn(\"grid shrink-0 place-items-center rounded-[4px] font-semibold text-white\", className)}\r\n    >\r\n      {initials}\r\n    </span>\r\n  )\r\n}\r\n\r\nfunction useMinutesLeft(expiresAt: Date | string | number | undefined, onExpire?: () => void) {\r\n  const [minutes, setMinutes] = React.useState<number>()\r\n  const expire = React.useRef(onExpire)\r\n  React.useEffect(() => {\r\n    expire.current = onExpire\r\n  })\r\n  React.useEffect(() => {\r\n    setMinutes(undefined)\r\n    if (expiresAt === undefined) return\r\n    const end = new Date(expiresAt).getTime()\r\n    let fired = false\r\n    const update = () => {\r\n      const left = Math.max(0, Math.ceil((end - Date.now()) / 60_000))\r\n      setMinutes(left)\r\n      if (left === 0 && !fired) {\r\n        fired = true\r\n        expire.current?.()\r\n      }\r\n    }\r\n    update()\r\n    const timer = setInterval(update, 5_000)\r\n    return () => clearInterval(timer)\r\n  }, [expiresAt])\r\n  return minutes\r\n}\r\n\r\nexport interface ActingAsBannerProps extends Omit<React.ComponentProps<\"div\">, \"children\"> {\r\n  /** The identity whose permissions are in effect right now. */\r\n  subject: { name: string; email?: string }\r\n  /** The organization or workspace actions apply to. */\r\n  org?: string\r\n  /** Who is really signed in, shown so the session stays accountable. */\r\n  actor?: string\r\n  /** When the session ends on its own. Shows a live countdown. */\r\n  expiresAt?: Date | string | number\r\n  /** Called once when `expiresAt` passes. */\r\n  onExpire?: () => void\r\n  /** Return to your own identity. Return a promise to show a pending state. */\r\n  onSwitchBack: () => void | Promise<void>\r\n  /** Stick to the top of the scroll container. */\r\n  sticky?: boolean\r\n  /** Templates: `{name}` and `{minutes}` are filled in. */\r\n  actingLabel?: string\r\n  /** Text naming who is really signed in. */\r\n  signedInLabel?: string\r\n  /** Text before the time left in the session. */\r\n  endsInLabel?: string\r\n  /** Text shown when the session has expired. */\r\n  expiredLabel?: string\r\n  /** Defaults to \"Switch back to {actor's first name}\". */\r\n  switchBackLabel?: string\r\n  /** Shown when switching back fails. */\r\n  switchBackErrorText?: string\r\n}\r\n\r\n/**\r\n * A persistent \"you're acting as someone else\" bar for impersonation and\r\n * multi-org sessions, so support staff never change a customer's data\r\n * believing it's their own. Shows who, where, who's really behind it, and a\r\n * one-tap way back. Pair it with `OrgContextChip` on destructive buttons.\r\n */\r\nexport function ActingAsBanner({\r\n  subject,\r\n  org,\r\n  actor,\r\n  expiresAt,\r\n  onExpire,\r\n  onSwitchBack,\r\n  sticky = true,\r\n  actingLabel = \"Acting as\",\r\n  signedInLabel = \"Signed in as {name}\",\r\n  endsInLabel = \"Ends in {minutes} min\",\r\n  expiredLabel = \"Session expired\",\r\n  switchBackLabel,\r\n  switchBackErrorText = \"Couldn't switch back. Try again.\",\r\n  className,\r\n  ...props\r\n}: ActingAsBannerProps) {\r\n  const [leaving, setLeaving] = React.useState(false)\r\n  const [failed, setFailed] = React.useState(false)\r\n  const minutes = useMinutesLeft(expiresAt, onExpire)\r\n  const tone = tones.warning\r\n  const backLabel = switchBackLabel ?? (actor ? `Switch back to ${actor.split(/[\\s@]/)[0]}` : \"Switch back\")\r\n\r\n  const switchBack = async () => {\r\n    setLeaving(true)\r\n    setFailed(false)\r\n    try {\r\n      await onSwitchBack()\r\n    } catch {\r\n      setFailed(true)\r\n    } finally {\r\n      setLeaving(false)\r\n    }\r\n  }\r\n\r\n  return (\r\n    <div\r\n      data-slot=\"acting-as-banner\"\r\n      role=\"region\"\r\n      aria-label={`${actingLabel} ${subject.name}${org ? `, ${org}` : \"\"}`}\r\n      className={cn(bar, \"z-40 w-full border-b px-4\", tone.surface, sticky && \"sticky top-0\", className)}\r\n      {...props}\r\n    >\r\n      <div className=\"flex min-w-0 flex-1 basis-60 items-center gap-3\">\r\n        <UserCog aria-hidden className={cn(\"size-4 shrink-0\", tone.icon)} />\r\n        <div className=\"min-w-0 flex-1\">\r\n          <p>\r\n            {actingLabel} <strong className=\"font-semibold\">{subject.name}</strong>\r\n            {subject.email && <span className={cn(\"hidden sm:inline\", tone.muted)}> ({subject.email})</span>}\r\n            {org && (\r\n              <span className=\"ml-1.5 inline-flex items-center gap-1 rounded-md border border-amber-700/20 bg-background/60 py-px pr-1.5 pl-0.5 align-middle text-xs font-medium dark:border-amber-300/20\">\r\n                <OrgMark name={org} className=\"size-4 text-[9px]\" />\r\n                {org}\r\n              </span>\r\n            )}\r\n          </p>\r\n          {(actor || minutes !== undefined) && (\r\n            <p className={cn(\"flex flex-wrap items-center gap-x-3 text-xs\", tone.muted)}>\r\n              {actor && <span>{signedInLabel.replace(\"{name}\", actor)}</span>}\r\n              {minutes !== undefined && (\r\n                <span className=\"inline-flex items-center gap-1 tabular-nums\">\r\n                  <Clock aria-hidden className=\"size-3.5\" />\r\n                  {minutes > 0 ? endsInLabel.replace(\"{minutes}\", String(minutes)) : expiredLabel}\r\n                </span>\r\n              )}\r\n            </p>\r\n          )}\r\n          {failed && (\r\n            <p role=\"alert\" className=\"text-xs font-medium text-red-700 dark:text-red-300\">\r\n              {switchBackErrorText}\r\n            </p>\r\n          )}\r\n        </div>\r\n      </div>\r\n      <button type=\"button\" onClick={() => void switchBack()} disabled={leaving} className={cn(action, tone.solid)}>\r\n        {leaving ? <LoaderCircle aria-hidden className=\"size-4 animate-spin\" /> : <Undo2 aria-hidden className=\"size-4\" />}\r\n        {backLabel}\r\n      </button>\r\n    </div>\r\n  )\r\n}\r\n\r\nexport interface OrgContextChipProps extends Omit<React.ComponentProps<\"span\">, \"children\"> {\r\n  /** The organization the surrounding action applies to. */\r\n  org: string\r\n  /** Also say whose identity the action runs as. */\r\n  actingAs?: string\r\n  /** Word joining the action to the org; \"in\" reads as \"Delete in Acme Corp\". */\r\n  preposition?: string\r\n}\r\n\r\n/**\r\n * Drop inside any destructive button so it always says where it acts:\r\n * `<button>Delete <OrgContextChip org=\"Acme Corp\" /></button>` reads, and is\r\n * announced, as \"Delete in Acme Corp\". Uses the same color as the banner.\r\n */\r\nexport function OrgContextChip({ org, actingAs, preposition = \"in\", className, ...props }: OrgContextChipProps) {\r\n  return (\r\n    <span data-slot=\"org-context-chip\" className={cn(\"inline-flex items-center gap-1 whitespace-nowrap\", className)} {...props}>\r\n      <span className=\"opacity-80\">{preposition}</span>\r\n      <span className=\"inline-flex items-center gap-1 rounded-[5px] bg-black/10 py-0.5 pr-1.5 pl-0.5 text-[0.85em] font-semibold dark:bg-white/15\">\r\n        <OrgMark name={org} className=\"size-3.5 text-[8px]\" />\r\n        {org}\r\n      </span>\r\n      {actingAs && <span className=\"opacity-80\">as {actingAs}</span>}\r\n    </span>\r\n  )\r\n}\r\n",
      "type": "registry:ui",
      "target": "@ui/acting-as-banner.tsx"
    }
  ],
  "meta": {
    "stops": "Edited a customer's data as myself",
    "mistakes": [
      "impersonation mode mistake",
      "support agent edited wrong account",
      "forgot i was logged in as user",
      "admin acting as customer",
      "wrong org selected"
    ],
    "aliases": [
      "impersonate",
      "login as user",
      "acting as",
      "support mode",
      "org switcher",
      "admin panel",
      "sudo mode",
      "workspace switcher"
    ],
    "flows": [
      "account-settings",
      "danger-zone"
    ],
    "fields": [
      "name"
    ],
    "summary": "A sticky bar that says whose account you are acting as, and labels destructive buttons with the org they act on, so support staff don't edit the wrong data.",
    "server": "Scope impersonation sessions on the server, log every action with both the actor and the subject, and expire sessions server-side.",
    "catches": [
      "Forgetting you are impersonating a customer",
      "Destructive buttons that don't say which org they act on (via the org chip)",
      "Impersonation sessions that have expired",
      "A failed switch back, shown as an error"
    ],
    "limits": [
      "Shows the session you describe in props; it doesn't create or end impersonation",
      "The org chip only appears where you add it"
    ],
    "a11y": [
      "A role=\"region\" with an aria-label naming who you act as and the org",
      "Switch-back errors use role=\"alert\"",
      "The org colour is decorative; the org is always named in text"
    ],
    "usage": "import { ActingAsBanner } from \"@/components/ui/acting-as-banner\"",
    "runtimeDependencies": [
      "lucide-react"
    ],
    "zeroRuntimeDependencies": false
  },
  "docs": "https://bollard-ui.vercel.app/components/acting-as-banner",
  "categories": [
    "Context and awareness"
  ],
  "type": "registry:ui"
}
