{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "incident-cause-tracer",
  "title": "Incident Cause Tracer",
  "description": "Walks an incident backwards from what people noticed to the change that caused it, with a confidence score, ideas ruled out and the evidence behind each step.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/incident-cause-tracer/incident-cause-tracer.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type CauseKind = \"symptom\" | \"effect\" | \"root\";\n\nexport interface CauseEvidence {\n  label: string;\n  value: string;\n}\n\nexport interface CauseStep {\n  id: string;\n  /** What happened, in one plain sentence. */\n  title: string;\n  service: string;\n  /** Short time, e.g. \"14:06\". */\n  time: string;\n  kind: CauseKind;\n  /** How sure the trace is, from 0 to 100. */\n  confidence: number;\n  summary: string;\n  evidence: CauseEvidence[];\n  /** Ideas checked and dropped at this step. */\n  ruledOut?: string[];\n  /** The metric at this step. Drawn as a small area chart. */\n  series?: number[];\n  /** Which point in series the step happened at. */\n  markAt?: number;\n  seriesLabel?: string;\n  /** A button under the evidence, usually on the root cause. */\n  action?: { label: string; onClick?: () => void };\n}\n\nexport interface IncidentCauseTracerProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  /** Start from what people noticed and end on the root cause. */\n  steps?: CauseStep[];\n  /** Walks the chain step by step when it scrolls into view. */\n  autoTrace?: boolean;\n  /** Milliseconds between steps. */\n  stepMs?: number;\n  onSelectStep?: (step: CauseStep) => void;\n}\n\nconst FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst ACCENT = \"#5E6AD2\";\nconst RED = \"#EB5757\";\n\nconst PALETTES = {\n  dark: {\n    card: \"#0F1011\",\n    inset: \"#161718\",\n    border: \"rgba(255,255,255,0.08)\",\n    rail: \"rgba(255,255,255,0.1)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n    picked: \"rgba(255,255,255,0.05)\",\n    btn: \"#E5E5E6\",\n    btnText: \"#08090A\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    inset: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.1)\",\n    rail: \"rgba(8,9,10,0.12)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.05)\",\n    picked: \"rgba(8,9,10,0.04)\",\n    btn: \"#08090A\",\n    btnText: \"#F7F8F8\",\n  },\n};\n\nconst DEFAULT_STEPS: CauseStep[] = [\n  {\n    id: \"errors\",\n    title: \"Checkout errors reached 12%\",\n    service: \"web-checkout\",\n    time: \"14:06\",\n    kind: \"symptom\",\n    confidence: 100,\n    summary: \"Customers started seeing a failed payment screen.\",\n    seriesLabel: \"Error rate\",\n    series: [1, 1, 1, 2, 1, 2, 2, 3, 6, 9, 12, 12],\n    markAt: 8,\n    evidence: [\n      { label: \"5xx responses\", value: \"12.4%\" },\n      { label: \"Orders affected\", value: \"1,284\" },\n      { label: \"Slowest route\", value: \"POST /checkout\" },\n    ],\n  },\n  {\n    id: \"payments\",\n    title: \"Payment calls began timing out\",\n    service: \"payments-api\",\n    time: \"14:04\",\n    kind: \"effect\",\n    confidence: 94,\n    summary: \"Requests waited on the orders service until they gave up.\",\n    seriesLabel: \"p95 latency\",\n    series: [2, 2, 3, 2, 3, 3, 5, 9, 14, 16, 17, 17],\n    markAt: 6,\n    ruledOut: [\"Card provider outage\"],\n    evidence: [\n      { label: \"p95 latency\", value: \"8.2 s\" },\n      { label: \"Normal p95\", value: \"240 ms\" },\n      { label: \"Timeouts per minute\", value: \"312\" },\n    ],\n  },\n  {\n    id: \"database\",\n    title: \"Database connections ran out\",\n    service: \"orders-db\",\n    time: \"14:02\",\n    kind: \"effect\",\n    confidence: 88,\n    summary: \"Every connection was in use, so new work had to queue.\",\n    seriesLabel: \"Connections in use\",\n    series: [6, 6, 7, 7, 8, 14, 20, 20, 20, 20, 20, 20],\n    markAt: 5,\n    ruledOut: [\"Slow queries\", \"Disk pressure\"],\n    evidence: [\n      { label: \"Pool in use\", value: \"20 of 20\" },\n      { label: \"Requests queued\", value: \"412\" },\n      { label: \"CPU\", value: \"31%\" },\n    ],\n  },\n  {\n    id: \"deploy\",\n    title: \"Release 4821 cut the pool to 20\",\n    service: \"orders-api\",\n    time: \"13:58\",\n    kind: \"root\",\n    confidence: 91,\n    summary:\n      \"A config change lowered the limit four minutes before the first alert.\",\n    seriesLabel: \"Pool limit\",\n    series: [100, 100, 100, 100, 100, 20, 20, 20, 20, 20, 20, 20],\n    markAt: 5,\n    evidence: [\n      { label: \"Changed setting\", value: \"db.pool.max\" },\n      { label: \"Old and new value\", value: \"100 to 20\" },\n      { label: \"Time to first alert\", value: \"4 min\" },\n    ],\n    action: { label: \"Roll back release\" },\n  },\n];\n\n/** Drawn as SVG, because a tiny border-radius circle turns lumpy on 1x screens and fractional positions. */\nfunction Dot({ size, color }: { size: number; color: string }) {\n  const r = size / 2;\n  return (\n    <svg\n      aria-hidden=\"true\"\n      width={size}\n      height={size}\n      viewBox={`0 0 ${size} ${size}`}\n      className=\"block shrink-0\"\n    >\n      <circle\n        cx={r}\n        cy={r}\n        r={r}\n        className=\"motion-safe:transition-[fill] motion-safe:duration-200\"\n        style={{ fill: color }}\n      />\n    </svg>\n  );\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (notify) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", notify);\n      return () => query.removeEventListener(\"change\", notify);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nfunction Sparkline({\n  series,\n  markAt,\n  label,\n  color,\n  grid,\n  faint,\n}: {\n  series: number[];\n  markAt?: number;\n  label?: string;\n  color: string;\n  grid: string;\n  faint: string;\n}) {\n  const id = React.useId();\n  const max = Math.max(...series, 1);\n  const min = Math.min(...series, 0);\n  const span = max - min || 1;\n  const x = (i: number) => Math.round((i / (series.length - 1)) * 1000) / 10;\n  const y = (v: number) => Math.round((92 - ((v - min) / span) * 84) * 10) / 10;\n  const line = series\n    .map((v, i) => `${i ? \"L\" : \"M\"}${x(i)} ${y(v)}`)\n    .join(\" \");\n  return (\n    <div>\n      <p\n        className=\"m-0 mb-1.5 text-[12px] leading-[16px] tracking-[-0.005em]\"\n        style={{ color: faint }}\n      >\n        {label}\n      </p>\n      <div className=\"relative h-[72px] w-full\">\n        {/* Only the line and fill stretch. The marker is HTML, so it stays a true 1px. */}\n        <svg\n          aria-hidden=\"true\"\n          viewBox=\"0 0 100 100\"\n          preserveAspectRatio=\"none\"\n          className=\"absolute inset-0 block size-full\"\n        >\n          <defs>\n            <linearGradient id={id} x1=\"0\" x2=\"0\" y1=\"0\" y2=\"1\">\n              <stop offset=\"0\" stopColor={color} stopOpacity=\"0.22\" />\n              <stop offset=\"1\" stopColor={color} stopOpacity=\"0\" />\n            </linearGradient>\n          </defs>\n          <path d={`${line} L100 100 L0 100 Z`} fill={`url(#${id})`} />\n          <path\n            d={line}\n            fill=\"none\"\n            stroke={color}\n            strokeWidth=\"1.5\"\n            vectorEffect=\"non-scaling-stroke\"\n          />\n        </svg>\n        <div\n          aria-hidden=\"true\"\n          className=\"absolute inset-x-0 bottom-0 h-px\"\n          style={{ background: grid }}\n        />\n        {markAt !== undefined && (\n          <div\n            aria-hidden=\"true\"\n            className=\"absolute inset-y-0 w-px\"\n            style={{ left: `${x(markAt)}%`, background: faint }}\n          />\n        )}\n      </div>\n    </div>\n  );\n}\n\nexport function IncidentCauseTracer({\n  theme = \"dark\",\n  title = \"Checkout failures\",\n  subtitle = \"Traced back eight minutes across four services.\",\n  steps = DEFAULT_STEPS,\n  autoTrace = true,\n  stepMs = 520,\n  onSelectStep,\n  className,\n  style,\n  ...rest\n}: IncidentCauseTracerProps) {\n  const c = PALETTES[theme];\n  const reduce = useReducedMotion();\n  const last = steps.length - 1;\n  const rootRef = React.useRef<HTMLElement>(null);\n  // How far the trace has got. The server draws the first step, or all of them with autoTrace off.\n  const [reached, setReached] = React.useState(autoTrace ? 0 : last);\n  const [selected, setSelected] = React.useState(autoTrace ? 0 : last);\n  const [run, setRun] = React.useState(0);\n  const [visible, setVisible] = React.useState(false);\n\n  React.useEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        if (entry.isIntersecting) {\n          setVisible(true);\n          io.disconnect();\n        }\n      },\n      { threshold: 0.25 },\n    );\n    io.observe(el);\n    return () => io.disconnect();\n  }, []);\n\n  const tracing = reached < last;\n\n  // Each step schedules the next one, and clicking a step ends the walk by jumping to the end.\n  React.useEffect(() => {\n    if ((!autoTrace && run === 0) || !visible || reached >= last) return;\n    const id = window.setTimeout(\n      () => {\n        const next = reduce ? last : reached + 1;\n        setReached(next);\n        setSelected(next);\n      },\n      reduce ? 0 : stepMs,\n    );\n    return () => window.clearTimeout(id);\n  }, [autoTrace, last, reached, reduce, run, stepMs, visible]);\n\n  const select = (i: number) => {\n    setReached(last);\n    setSelected(i);\n    onSelectStep?.(steps[i]);\n  };\n\n  const retrace = () => {\n    setReached(0);\n    setSelected(0);\n    setRun((n) => n + 1);\n  };\n\n  return (\n    <section\n      ref={rootRef}\n      aria-label={title}\n      className={`@container w-full rounded-2xl border ${className ?? \"\"}`}\n      style={{\n        background: c.card,\n        borderColor: c.border,\n        color: c.text,\n        fontFamily: FONT,\n        ...style,\n      }}\n      {...rest}\n    >\n      <header\n        className=\"flex items-start justify-between gap-4 border-b p-5 @2xl:px-6\"\n        style={{ borderColor: c.border }}\n      >\n        <div className=\"min-w-0\">\n          <h3 className=\"m-0 text-[20px] leading-[26px] font-[590] tracking-[-0.018em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-1 text-[13px] leading-[20px] tracking-[-0.011em]\"\n            style={{ color: c.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <button\n          type=\"button\"\n          onClick={retrace}\n          disabled={tracing}\n          className=\"h-8 w-[88px] shrink-0 cursor-pointer rounded-full border-0 text-[13px] font-[510] tracking-[-0.011em] outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2] disabled:cursor-default\"\n          style={{ background: c.chip, color: tracing ? c.faint : c.soft }}\n        >\n          {tracing ? \"Tracing\" : \"Retrace\"}\n        </button>\n      </header>\n\n      <div className=\"grid @2xl:grid-cols-[minmax(0,1.1fr)_minmax(0,1fr)]\">\n        <ol className=\"m-0 list-none p-3 @2xl:p-4\">\n          {steps.map((step, i) => {\n            const lit = i <= reached;\n            const picked = selected === i;\n            const dot = !lit ? c.rail : step.kind === \"symptom\" ? RED : ACCENT;\n            return (\n              <li key={step.id} className=\"relative\">\n                {/* The dot and its line share one centred column, so they can't drift apart. */}\n                <span\n                  aria-hidden=\"true\"\n                  className=\"pointer-events-none absolute z-[1] flex flex-col items-center\"\n                  style={{\n                    top: 14,\n                    left: 10,\n                    width: 11,\n                    bottom: i < last ? -14 : undefined,\n                  }}\n                >\n                  <Dot size={11} color={dot} />\n                  {i < last && (\n                    <span\n                      className=\"my-1 w-px flex-1\"\n                      style={{ background: c.rail }}\n                    >\n                      <span\n                        className=\"block size-full origin-top motion-safe:transition-transform\"\n                        style={{\n                          background: ACCENT,\n                          transform: i < reached ? \"scaleY(1)\" : \"scaleY(0)\",\n                          transitionDuration: `${Math.round(stepMs * 0.8)}ms`,\n                        }}\n                      />\n                    </span>\n                  )}\n                </span>\n                <button\n                  type=\"button\"\n                  data-step={step.id}\n                  aria-pressed={picked}\n                  onClick={() => select(i)}\n                  className=\"relative flex w-full cursor-pointer items-start gap-3 rounded-lg border-0 bg-transparent p-2.5 text-left outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2] motion-safe:transition-colors motion-safe:duration-150\"\n                  style={{\n                    paddingLeft: 33,\n                    background: picked ? c.picked : \"transparent\",\n                  }}\n                >\n                  <span className=\"min-w-0 flex-1\">\n                    <span\n                      className=\"block text-[13.5px] leading-[20px] font-[510] tracking-[-0.011em] motion-safe:transition-colors motion-safe:duration-200\"\n                      style={{ color: lit ? c.text : c.faint }}\n                    >\n                      {step.title}\n                    </span>\n                    <span\n                      className=\"mt-0.5 flex flex-wrap items-center gap-x-1.5 text-[12px] leading-[16px] tracking-[-0.005em] tabular-nums\"\n                      style={{ color: c.faint }}\n                    >\n                      <span>{step.time}</span>\n                      <span aria-hidden=\"true\">·</span>\n                      <span>{step.service}</span>\n                      {step.kind === \"root\" && (\n                        <>\n                          <span aria-hidden=\"true\">·</span>\n                          <span style={{ color: ACCENT }}>Root cause</span>\n                        </>\n                      )}\n                    </span>\n                  </span>\n                  <span\n                    className=\"shrink-0 text-[12px] leading-[20px] tracking-[-0.005em] tabular-nums\"\n                    style={{ color: lit ? c.sub : c.faint }}\n                  >\n                    {step.confidence}%\n                  </span>\n                </button>\n                {step.ruledOut?.length ? (\n                  <p\n                    className=\"m-0 flex flex-wrap items-center gap-1.5 pr-2 pb-1.5 text-[12px] leading-[16px] tracking-[-0.005em]\"\n                    style={{ color: c.faint, paddingLeft: 33 }}\n                  >\n                    <span>Ruled out</span>\n                    {step.ruledOut.map((item) => (\n                      <span\n                        key={item}\n                        className=\"rounded-full border border-dashed px-2 py-px line-through\"\n                        style={{ borderColor: c.rail }}\n                      >\n                        {item}\n                      </span>\n                    ))}\n                  </p>\n                ) : null}\n              </li>\n            );\n          })}\n        </ol>\n\n        {/* All the panels share one cell, so picking a step never changes the card's height. */}\n        <div\n          className=\"grid border-t p-5 @2xl:border-t-0 @2xl:border-l @2xl:p-6\"\n          style={{ borderColor: c.border }}\n        >\n          {steps.map((step, i) => {\n            const on = selected === i;\n            return (\n              <div\n                key={step.id}\n                aria-hidden={!on}\n                className={`flex flex-col [grid-area:1/1] motion-safe:transition-[opacity,visibility] motion-safe:duration-200 ${on ? \"visible opacity-100\" : \"invisible opacity-0\"}`}\n              >\n                <p\n                  className=\"m-0 min-h-[40px] text-[13.5px] leading-[20px] tracking-[-0.011em]\"\n                  style={{ color: c.soft }}\n                >\n                  {step.summary}\n                </p>\n                {step.series && (\n                  <div className=\"mt-4\">\n                    <Sparkline\n                      series={step.series}\n                      markAt={step.markAt}\n                      label={step.seriesLabel}\n                      color={step.kind === \"symptom\" ? RED : ACCENT}\n                      grid={c.border}\n                      faint={c.faint}\n                    />\n                  </div>\n                )}\n                <dl className=\"m-0 mt-4\">\n                  {step.evidence.map((item) => (\n                    <div\n                      key={item.label}\n                      className=\"flex items-center justify-between gap-4 border-t py-2 text-[13px] leading-[20px] tracking-[-0.011em]\"\n                      style={{ borderColor: c.border }}\n                    >\n                      <dt style={{ color: c.sub }}>{item.label}</dt>\n                      <dd\n                        className=\"m-0 font-[510] tabular-nums\"\n                        style={{ color: c.text }}\n                      >\n                        {item.value}\n                      </dd>\n                    </div>\n                  ))}\n                </dl>\n                {step.action && (\n                  <button\n                    type=\"button\"\n                    tabIndex={on ? 0 : -1}\n                    onClick={step.action.onClick}\n                    className=\"mt-auto h-9 w-full cursor-pointer rounded-full border-0 text-[13px] font-[510] tracking-[-0.011em] outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2] focus-visible:ring-offset-2\"\n                    style={{ background: c.btn, color: c.btnText }}\n                  >\n                    {step.action.label}\n                  </button>\n                )}\n              </div>\n            );\n          })}\n        </div>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}