{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-midi-monitor",
  "type": "registry:ui",
  "title": "MIDI Monitor",
  "description": "A diagnostic list of incoming MIDI messages, decoded and colour-coded by type. Controlled and presentational: the caller owns the buffer and supplies messages, so it drops into a lab page, a settings popover or a routing debugger without acquiring MIDI itself.",
  "dependencies": [
    "maruaudio@>=0.0.3 <0.1.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "components/ui/midi-monitor.tsx",
      "content": "/**\n * Source: https://maru.audio/ui-kit/midi-monitor\n * Copyright © [Maru](https://maru.audio)\n *\n * Maru UI Kit — https://maru.audio/ui-kit\n *\n * Installed from the Maru UI registry. This file is yours now: edit it\n * freely, it will not be overwritten unless you re-run the add command.\n *\n * Keep this item’s companion files and CSS imports together. Dependencies\n * declared by the registry are installed with the source.\n */\n/**\n * A diagnostic MIDI monitor: every incoming message, decoded and listed.\n *\n * Ported from the midimama monitor and reduced to the part that is actually\n * reusable. The original carried Next.js <Image>, recharts plots, shadcn Cards\n * and its own device store; none of that survives contact with another app.\n * What does survive is the decode table (now\n * `lib/midi-message-decode`) and this list.\n *\n * Presentational and controlled: it takes messages and renders them. Acquiring\n * MIDI stays with `useMidiAccess` / `useMidiInputs`, so this component can sit\n * in a lab page, a settings popover, or a routing debugger without dragging a\n * permission prompt along.\n *\n * Styling uses the kit's `--control-*` theme variables and plain utility\n * classes, so it inherits whatever surface it is dropped into.\n */\n\nimport { useMemo } from \"react\"\n\nimport {\n  decodeMidiMessage,\n  type DecodedMidiMessage,\n  type MidiMessageKind,\n} from \"maruaudio/ui/shared/midi-message-decode\"\n\nexport interface MidiMonitorEntry {\n  /** Monotonic id — use a counter, not a timestamp; messages collide at 1ms. */\n  id: number\n  /** Raw bytes as they arrived. */\n  raw: readonly number[]\n  /** performance.now() or the port timestamp. */\n  timestamp: number\n  /** Optional port label, shown when several inputs are merged. */\n  source?: string\n}\n\nexport interface MidiMonitorProps {\n  /** Newest first. The caller owns the buffer and its cap. */\n  entries: readonly MidiMonitorEntry[]\n  /** Hide the source column when only one port can ever appear. */\n  showSource?: boolean\n  /** Kinds to render; omit for everything. */\n  kinds?: readonly MidiMessageKind[]\n  /** Rendered when there is nothing yet. */\n  empty?: React.ReactNode\n  /** Extra cell appended to each row — e.g. a resolved semantic function. */\n  renderExtra?: (\n    entry: MidiMonitorEntry,\n    decoded: DecodedMidiMessage\n  ) => React.ReactNode\n  /** Header for the extra column. */\n  extraLabel?: string\n  className?: string\n}\n\nconst KIND_TONE: Record<MidiMessageKind, string> = {\n  noteOn: \"bg-emerald-500/15 text-emerald-600 dark:text-emerald-400\",\n  noteOff: \"bg-current/10\",\n  controlChange: \"bg-sky-500/15 text-sky-600 dark:text-sky-400\",\n  polyPressure: \"bg-purple-500/15 text-purple-600 dark:text-purple-400\",\n  channelPressure: \"bg-orange-500/15 text-orange-600 dark:text-orange-400\",\n  pitchBend: \"bg-amber-500/15 text-amber-600 dark:text-amber-400\",\n  programChange: \"bg-pink-500/15 text-pink-600 dark:text-pink-400\",\n  sysex: \"bg-current/10\",\n  timingClock: \"bg-current/5\",\n  systemRealTime: \"bg-current/5\",\n  unknown: \"bg-current/5\",\n}\n\nconst hex = (n: number) => n.toString(16).toUpperCase().padStart(2, \"0\")\n\nexport function MidiMonitor({\n  entries,\n  showSource = true,\n  kinds,\n  empty = \"Waiting for MIDI…\",\n  renderExtra,\n  extraLabel,\n  className = \"\",\n}: MidiMonitorProps) {\n  // Decode once per render pass rather than per cell: a full buffer is a few\n  // hundred rows and each one is read by three columns.\n  const rows = useMemo(() => {\n    const allow = kinds ? new Set(kinds) : null\n    return entries\n      .map((entry) => ({ entry, decoded: decodeMidiMessage(entry.raw) }))\n      .filter(({ decoded }) => !allow || allow.has(decoded.kind))\n  }, [entries, kinds])\n\n  return (\n    <div\n      className={`overflow-x-auto rounded-xl border border-current/10 ${className}`}\n    >\n      <table className=\"w-full min-w-[40rem] text-left text-sm\">\n        <thead className=\"border-b border-current/10 text-xs tracking-wide uppercase opacity-60\">\n          <tr>\n            {showSource && <th className=\"px-3 py-2\">Source</th>}\n            <th className=\"px-3 py-2\">Time (ms)</th>\n            <th className=\"px-3 py-2\">Raw</th>\n            <th className=\"px-3 py-2\">Ch</th>\n            <th className=\"px-3 py-2\">Type</th>\n            <th className=\"px-3 py-2\">Message</th>\n            {renderExtra && (\n              <th className=\"px-3 py-2\">{extraLabel ?? \"Extra\"}</th>\n            )}\n          </tr>\n        </thead>\n        <tbody>\n          {rows.length === 0 && (\n            <tr>\n              <td\n                colSpan={4 + (showSource ? 1 : 0) + (renderExtra ? 1 : 0) + 1}\n                className=\"px-3 py-8 text-center opacity-50\"\n              >\n                {empty}\n              </td>\n            </tr>\n          )}\n          {rows.map(({ entry, decoded }) => (\n            <tr key={entry.id} className=\"border-b border-current/5\">\n              {showSource && (\n                <td className=\"max-w-28 truncate px-3 py-1.5 text-xs opacity-60\">\n                  {entry.source ?? \"—\"}\n                </td>\n              )}\n              <td className=\"px-3 py-1.5 font-mono text-[10px] tabular-nums opacity-60\">\n                {Number.isFinite(entry.timestamp)\n                  ? entry.timestamp.toFixed(1)\n                  : \"—\"}\n              </td>\n              <td className=\"px-3 py-1.5 font-mono text-xs\">\n                {entry.raw.map(hex).join(\" \")}\n              </td>\n              <td className=\"px-3 py-1.5 text-xs tabular-nums opacity-70\">\n                {decoded.kind === \"sysex\" ||\n                decoded.kind === \"timingClock\" ||\n                decoded.kind === \"systemRealTime\"\n                  ? \"—\"\n                  : decoded.channel}\n              </td>\n              <td className=\"px-3 py-1.5\">\n                <span\n                  className={`rounded px-1.5 py-0.5 font-mono text-[11px] ${KIND_TONE[decoded.kind]}`}\n                >\n                  {decoded.kind}\n                </span>\n              </td>\n              <td className=\"px-3 py-1.5 text-xs\">{decoded.description}</td>\n              {renderExtra && (\n                <td className=\"px-3 py-1.5 text-xs\">\n                  {renderExtra(entry, decoded)}\n                </td>\n              )}\n            </tr>\n          ))}\n        </tbody>\n      </table>\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "@ui/midi-monitor.tsx"
    }
  ]
}
