{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "reroute-demo",
  "type": "registry:block",
  "title": "Reroute: Model Failover",
  "description": "Animated Failover Diagram example, Model Failover: An inference gateway over six model providers that drills its own outages: the live provider drops, the wheel reroutes, and it turns back once the provider recovers. Take one down yourself. A take on the flow diagram (flowchart). Fits hero and features sections.",
  "author": "KeyframeUI <https://keyframeui.com>",
  "categories": [
    "hero",
    "features",
    "flow-diagram",
    "interaction",
    "ai",
    "diagram"
  ],
  "docs": "Live preview, examples and props: https://keyframeui.com/components/reroute#reroute-demo",
  "registryDependencies": [
    "https://keyframeui.com/r/reroute.json"
  ],
  "files": [
    {
      "path": "components/reroute-demo.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { Reroute, type RerouteItem, type RerouteReason } from \"@/components/ui/reroute\";\n\nfunction Mark({ children }: { children: React.ReactNode }) {\n  return (\n    <svg viewBox=\"0 0 24 24\" className=\"size-full\" aria-hidden>\n      {children}\n    </svg>\n  );\n}\n\n// Fernwood's inference gateway and the model providers it routes between. Every name and mark is made up.\nconst PROVIDERS: RerouteItem[] = [\n  {\n    id: \"halcyon\",\n    name: \"Halcyon 3 Turbo\",\n    detail: \"p50 182 ms · us-east\",\n    icon: (\n      <Mark>\n        <circle cx=\"12\" cy=\"12\" r=\"9\" fill=\"#2563eb\" />\n        <path d=\"M6.5 13.2c1.8-2.4 3.6-2.4 5.5 0s3.7 2.4 5.5 0\" fill=\"none\" stroke=\"#fff\" strokeWidth=\"1.9\" strokeLinecap=\"round\" />\n      </Mark>\n    ),\n  },\n  {\n    id: \"okto\",\n    name: \"Okto Large\",\n    detail: \"p50 211 ms · eu-west\",\n    icon: (\n      <Mark>\n        <path d=\"M8.3 3h7.4L21 8.3v7.4L15.7 21H8.3L3 15.7V8.3z\" fill=\"currentColor\" />\n        <circle cx=\"12\" cy=\"12\" r=\"3.2\" fill=\"none\" strokeWidth=\"1.8\" className=\"stroke-white dark:stroke-zinc-900\" />\n      </Mark>\n    ),\n  },\n  {\n    id: \"brightline\",\n    name: \"Brightline 2\",\n    detail: \"p50 164 ms · us-west\",\n    icon: (\n      <Mark>\n        <circle cx=\"12\" cy=\"12\" r=\"4.6\" fill=\"#f59e0b\" />\n        <g stroke=\"#f59e0b\" strokeWidth=\"2\" strokeLinecap=\"round\">\n          <path d=\"M12 2.5v2.4M12 19.1v2.4M2.5 12h2.4M19.1 12h2.4M5.3 5.3l1.7 1.7M17 17l1.7 1.7M5.3 18.7 7 17M17 7l1.7-1.7\" />\n        </g>\n      </Mark>\n    ),\n  },\n  {\n    id: \"vesper\",\n    name: \"Vesper Mini\",\n    detail: \"p50 96 ms · us-east\",\n    icon: (\n      <Mark>\n        <path d=\"M15.8 3.4A9 9 0 1 0 20.6 15 7 7 0 0 1 15.8 3.4z\" fill=\"#7c3aed\" />\n        <circle cx=\"17.6\" cy=\"6.4\" r=\"1.4\" fill=\"#c4b5fd\" />\n      </Mark>\n    ),\n  },\n  {\n    id: \"corvid\",\n    name: \"Corvid R1\",\n    detail: \"p50 240 ms · ap-south\",\n    icon: (\n      <Mark>\n        <path d=\"M4 18 12 4l8 14z\" fill=\"#0f766e\" />\n        <path d=\"M9.2 14.6h5.6\" stroke=\"#5eead4\" strokeWidth=\"1.8\" strokeLinecap=\"round\" />\n      </Mark>\n    ),\n  },\n  {\n    id: \"lumen\",\n    name: \"Lumen Edge\",\n    detail: \"p50 128 ms · eu-central\",\n    icon: (\n      <Mark>\n        <path d=\"M12 2.8c.9 4.9 4.3 8.3 9.2 9.2-4.9.9-8.3 4.3-9.2 9.2-.9-4.9-4.3-8.3-9.2-9.2 4.9-.9 8.3-4.3 9.2-9.2z\" fill=\"#e11d48\" />\n      </Mark>\n    ),\n  },\n];\n\ntype Entry = { id: number; time: string; text: string; tone: \"fail\" | \"ok\" };\n\nconst clock = () =>\n  new Date().toLocaleTimeString(\"en-GB\", { hour: \"2-digit\", minute: \"2-digit\", second: \"2-digit\" });\n\n// The outage drill: the live provider drops, comes back, then traffic moves on.\nconst DRILL = { down: 2400, recover: 3200, next: 2600, rest: 9000 }; // ms; `rest` is how long the drill waits after you take over\n\nexport function RerouteDemo() {\n  const [value, setValue] = React.useState(\"halcyon\");\n  const [down, setDown] = React.useState<string[]>([]);\n  const [live, setLive] = React.useState<string | null>(\"halcyon\");\n  const [log, setLog] = React.useState<Entry[]>([]);\n  const [touched, setTouched] = React.useState(0);\n  const seq = React.useRef(0);\n  const liveRef = React.useRef(live);\n  const prevLive = React.useRef(live);\n\n  const name = (id: string | null) => PROVIDERS.find((p) => p.id === id)?.name ?? \"nothing\";\n\n  const onRoute = React.useCallback((id: string | null, reason: RerouteReason) => {\n    const from = prevLive.current;\n    prevLive.current = id;\n    liveRef.current = id;\n    setLive(id);\n    const text =\n      reason === \"failover\"\n        ? `${name(from)} down · rerouted to ${name(id)}`\n        : reason === \"recover\"\n          ? `${name(id)} back · traffic returned`\n          : `Routing to ${name(id)}`;\n    seq.current += 1;\n    const entry: Entry = { id: seq.current, time: clock(), text, tone: reason === \"failover\" ? \"fail\" : \"ok\" };\n    setLog((l) => [entry, ...l].slice(0, 3));\n  }, []);\n\n  // The drill runs on its own until you press something, then waits a while.\n  React.useEffect(() => {\n    let alive = true;\n    const timers: number[] = [];\n    const wait = (ms: number) => new Promise<void>((r) => timers.push(window.setTimeout(r, ms)));\n    const run = async () => {\n      if (touched) await wait(DRILL.rest);\n      while (alive) {\n        await wait(DRILL.down);\n        if (!alive) return;\n        const victim = liveRef.current;\n        if (victim) setDown([victim]);\n        await wait(DRILL.recover);\n        if (!alive) return;\n        setDown([]);\n        await wait(DRILL.next);\n        if (!alive) return;\n        const at = PROVIDERS.findIndex((p) => p.id === liveRef.current);\n        setValue(PROVIDERS[(at + 1) % PROVIDERS.length].id);\n      }\n    };\n    run();\n    return () => {\n      alive = false;\n      timers.forEach((t) => window.clearTimeout(t));\n    };\n  }, [touched]);\n\n  const anyDown = down.length > 0;\n\n  return (\n    <div className=\"w-full max-w-xl overflow-hidden rounded-3xl border border-zinc-950/[0.08] bg-white bg-[radial-gradient(120%_55%_at_50%_0%,rgba(255,59,31,0.07),transparent)] shadow-[0_1px_2px_rgba(0,0,0,0.04)] dark:border-white/10 dark:bg-zinc-950 dark:bg-[radial-gradient(120%_55%_at_50%_0%,rgba(255,90,60,0.1),transparent)]\">\n      <div className=\"flex items-center justify-between gap-3 px-5 pt-5\">\n        <div className=\"min-w-0\">\n          <p className=\"text-[13px] font-medium tracking-tight text-zinc-950 dark:text-white\">Fernwood gateway</p>\n          <p className=\"text-xs text-zinc-500 dark:text-zinc-400\">One endpoint, six model providers</p>\n        </div>\n        <span className=\"inline-flex shrink-0 items-center gap-1.5 rounded-full border border-zinc-950/[0.08] bg-white px-2.5 py-1 text-[11px] font-medium text-zinc-600 dark:border-white/10 dark:bg-zinc-900 dark:text-zinc-300\">\n          <span className={anyDown ? \"size-1.5 rounded-full bg-zinc-950 dark:bg-zinc-200\" : \"size-1.5 rounded-full bg-[#ff3b1f]\"} />\n          {anyDown ? \"1 provider down\" : \"All providers up\"}\n        </span>\n      </div>\n\n      <Reroute\n        className=\"mt-2\"\n        label=\"Model provider\"\n        items={PROVIDERS}\n        value={value}\n        onValueChange={(id) => {\n          setValue(id);\n          setTouched((t) => t + 1);\n        }}\n        down={down}\n        onRoute={onRoute}\n        hub={\n          <svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden>\n            <path d=\"M5 7h9a4 4 0 0 1 0 8H9\" />\n            <path d=\"M8 12l-3 3 3 3\" />\n          </svg>\n        }\n      />\n\n      <div className=\"flex flex-col items-start gap-3 border-t border-zinc-950/[0.06] px-5 py-4 sm:flex-row sm:items-center sm:justify-between dark:border-white/[0.08]\">\n        <ol className=\"w-full min-w-0 flex-1 space-y-1\" aria-label=\"Routing log\">\n          {log.length === 0 ? (\n            <li className=\"text-xs text-zinc-400 dark:text-zinc-500\">Watching for outages…</li>\n          ) : (\n            log.map((e, i) => (\n              <li\n                key={e.id}\n                className=\"flex items-center gap-2 text-xs transition-opacity duration-300\"\n                style={{ opacity: 1 - i * 0.3 }}\n              >\n                <span className=\"font-mono tabular-nums text-zinc-400 dark:text-zinc-500\">{e.time}</span>\n                <span\n                  className={\n                    e.tone === \"fail\"\n                      ? \"truncate font-medium text-zinc-950 dark:text-white\"\n                      : \"truncate text-zinc-600 dark:text-zinc-300\"\n                  }\n                >\n                  {e.text}\n                </span>\n              </li>\n            ))\n          )}\n        </ol>\n        <button\n          type=\"button\"\n          onClick={() => {\n            setTouched((t) => t + 1);\n            setDown(anyDown ? [] : live ? [live] : []);\n          }}\n          className=\"h-9 shrink-0 rounded-full bg-zinc-950 px-4 text-[13px] font-medium text-white outline-offset-2 outline-[#ff3b1f] transition-transform duration-150 focus-visible:outline-2 active:scale-[0.97] dark:bg-white dark:text-zinc-950\"\n        >\n          {anyDown ? `Restore ${name(down[0]).split(\" \")[0]}` : `Take ${name(live).split(\" \")[0]} down`}\n        </button>\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}