{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "network-guard",
  "title": "Network Guard",
  "description": "Wrapper that shows a 'This works on <chain>. Switch to <chain>' notice when the connected wallet is on an unsupported network, as a banner above the content or replacing it. Renders children unchanged when disconnected or on a supported chain. A declined switch shows as cancelled. Exports useNetworkGuard.",
  "dependencies": [
    "wagmi",
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "@0xui/use-mounted",
    "@0xui/0xui-utils"
  ],
  "files": [
    {
      "path": "registry/default/components/network-guard.tsx",
      "content": "// Docs: https://0xui.io/components/network-guard.md\nimport { Loader2Icon, TriangleAlertIcon } from \"lucide-react\";\nimport type { ReactNode } from \"react\";\nimport { useChains, useConnection, useSwitchChain } from \"wagmi\";\nimport { Button } from \"@/components/ui/button\";\nimport { useMounted } from \"@/hooks/use-mounted\";\nimport { errorCode, isUserRejection } from \"@/lib/0xui-utils\";\nimport { cn } from \"@/lib/utils\";\n\nexport type NetworkGuardPhase = \"ok\" | \"wrong-network\" | \"switching\";\n\n/** Whether the connected wallet is on one of `chainIds`, and a way to switch to the first. */\nexport function useNetworkGuard(chainIds: number | readonly number[]) {\n  const allowed = typeof chainIds === \"number\" ? [chainIds] : chainIds;\n  const mounted = useMounted();\n  const connection = useConnection();\n  const chains = useChains();\n  const switchChain = useSwitchChain();\n\n  const target = chains.find((c) => c.id === allowed[0]);\n  const wrongNetwork =\n    mounted &&\n    connection.status === \"connected\" &&\n    connection.chainId !== undefined &&\n    !allowed.includes(connection.chainId);\n\n  const phase: NetworkGuardPhase = !wrongNetwork\n    ? \"ok\"\n    : switchChain.isPending\n      ? \"switching\"\n      : \"wrong-network\";\n\n  return {\n    phase,\n    wrongNetwork,\n    allowedChains: chains.filter((c) => allowed.includes(c.id)),\n    currentChainId: connection.chainId,\n    target,\n    switchToTarget: () => {\n      if (!target) return;\n      switchChain.mutate({ chainId: target.id });\n    },\n    cancelled: isUserRejection(switchChain.error),\n    error: isUserRejection(switchChain.error) ? null : switchChain.error,\n  };\n}\n\nexport type NetworkGuardProps = {\n  /** Chain or chains the content works on. The first one is offered as the switch target. */\n  chainId: number | readonly number[];\n  /**\n   * \"banner\" shows a notice above the content. \"replace\" shows only the\n   * notice until the wallet is on a supported chain.\n   */\n  mode?: \"banner\" | \"replace\";\n  children: ReactNode;\n  className?: string;\n};\n\n/**\n * Shows a \"Switch to <chain>\" notice when the connected wallet is on a\n * network the content doesn't support. Renders children as-is when the\n * network is right or no wallet is connected.\n */\nexport function NetworkGuard({ chainId, mode = \"banner\", children, className }: NetworkGuardProps) {\n  const guard = useNetworkGuard(chainId);\n  if (!guard.wrongNetwork) return <>{children}</>;\n\n  const names = guard.allowedChains.map((c) => c.name);\n  const supported =\n    names.length <= 1\n      ? (names[0] ?? \"a different network\")\n      : `${names.slice(0, -1).join(\", \")} or ${names.at(-1)}`;\n\n  const notice = (\n    <div\n      role=\"region\"\n      aria-label=\"Wrong network\"\n      data-slot=\"network-guard\"\n      data-phase={guard.phase}\n      data-error={\n        guard.cancelled\n          ? \"user-rejected\"\n          : guard.error\n            ? errorCode(\"error\", guard.error)\n            : undefined\n      }\n      className={cn(\n        \"flex flex-wrap items-center gap-3 rounded-xl border border-amber-500/40 bg-amber-500/10 p-3 text-sm\",\n        className,\n      )}\n    >\n      <TriangleAlertIcon\n        aria-hidden\n        className=\"size-4 shrink-0 text-amber-600 dark:text-amber-400\"\n      />\n      <div className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n        <p className=\"font-medium\">This works on {supported}.</p>\n        <p className=\"text-muted-foreground\" aria-live=\"polite\">\n          {guard.phase === \"switching\"\n            ? \"Approve the switch in your wallet.\"\n            : guard.cancelled\n              ? \"Switch cancelled in wallet.\"\n              : \"Your wallet is on another network.\"}\n        </p>\n        {guard.error && (\n          <p role=\"alert\" className=\"text-destructive\">\n            {(guard.error as { shortMessage?: string }).shortMessage ?? guard.error.message}\n          </p>\n        )}\n      </div>\n      {guard.target && (\n        <Button\n          size=\"sm\"\n          onClick={guard.switchToTarget}\n          disabled={guard.phase === \"switching\"}\n          aria-busy={guard.phase === \"switching\" || undefined}\n        >\n          {guard.phase === \"switching\" && <Loader2Icon aria-hidden className=\"animate-spin\" />}\n          Switch to {guard.target.name}\n        </Button>\n      )}\n    </div>\n  );\n\n  if (mode === \"replace\") return notice;\n  return (\n    <>\n      {notice}\n      {children}\n    </>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/network-guard.md",
  "categories": [
    "network"
  ],
  "type": "registry:component"
}