{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "realtime",
  "title": "Realtime",
  "description": "Primitives for live data: an interval hook, a seeded random generator for demo streams, a rolling window, a pulsing live label that stays still for reduced motion, a number that counts to each new value, rolling bars on recharts and a short time-ago format.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/status-label.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/realtime.tsx",
      "content": "'use client'\n\nimport {\n  type ChartValueFormatter,\n  TinyBarChart,\n} from '@/registry/components/dashboardblocks/chart'\nimport { StatusLabel } from '@/registry/components/dashboardblocks/status-label'\nimport { formatNumber, formatRelativeTime } from '@/registry/lib/dashboardblocks/format'\nimport { type ReactNode, useEffect, useRef, useState } from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/** Calls `callback` every `delay` ms; `null` pauses it. */\nfunction useInterval(callback: () => void, delay: number | null) {\n  const saved = useRef(callback)\n\n  useEffect(() => {\n    saved.current = callback\n  }, [callback])\n\n  useEffect(() => {\n    if (delay === null) return\n    const id = setInterval(() => saved.current(), delay)\n    return () => clearInterval(id)\n  }, [delay])\n}\n\nfunction createRandom(seed: number) {\n  let state = seed >>> 0\n  return () => {\n    state = (state + 0x6d2b79f5) >>> 0\n    let t = state\n    t = Math.imul(t ^ (t >>> 15), t | 1)\n    t ^= t + Math.imul(t ^ (t >>> 7), t | 61)\n    return ((t ^ (t >>> 14)) >>> 0) / 4_294_967_296\n  }\n}\n\nfunction pushWindow<T>(values: T[], value: T, size: number) {\n  const next = [...values, value]\n  return next.length > size ? next.slice(next.length - size) : next\n}\n\ninterface LiveBadgeProps {\n  className?: string\n  /** @default 'Live' */\n  label?: string\n  paused?: boolean\n}\n\n/** A dot that pulses while something is live, in the text colour. */\nfunction LiveDot({\n  className,\n  paused = false,\n}: {\n  className?: string\n  paused?: boolean\n}) {\n  return (\n    <span aria-hidden className={cn('relative flex size-1.5 shrink-0', className)}>\n      {!paused && (\n        <span className='absolute inline-flex size-full rounded-full bg-current opacity-60 motion-safe:animate-ping' />\n      )}\n      <span className='relative inline-flex size-1.5 rounded-full bg-current' />\n    </span>\n  )\n}\n\nfunction LiveBadge({ className, label = 'Live', paused = false }: LiveBadgeProps) {\n  return (\n    <StatusLabel\n      tone={paused ? 'neutral' : 'good'}\n      className={className}\n      indicator={<LiveDot paused={paused} />}\n    >\n      {paused ? 'Paused' : label}\n    </StatusLabel>\n  )\n}\n\ninterface LiveNumberProps {\n  className?: string\n  /** @default 600 */\n  duration?: number\n  format?: (value: number) => ReactNode\n  value: number\n}\n\nfunction LiveNumber({\n  className,\n  duration = 600,\n  format = (value) => formatNumber(Math.round(value)),\n  value,\n}: LiveNumberProps) {\n  const [shown, setShown] = useState(value)\n  const from = useRef(value)\n\n  useEffect(() => {\n    const start = from.current\n    if (start === value) return\n    const length = window.matchMedia('(prefers-reduced-motion: reduce)').matches\n      ? 0\n      : duration\n    let frame = 0\n    const began = performance.now()\n    const step = (time: number) => {\n      const t = length > 0 ? Math.min(1, (time - began) / length) : 1\n      const eased = 1 - (1 - t) ** 3\n      const current = start + (value - start) * eased\n      from.current = current\n      setShown(current)\n      if (t < 1) frame = requestAnimationFrame(step)\n    }\n    frame = requestAnimationFrame(step)\n    return () => cancelAnimationFrame(frame)\n  }, [duration, value])\n\n  return <span className={cn('tabular-nums', className)}>{format(shown)}</span>\n}\n\ninterface RollingBarsProps {\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  formatter?: ChartValueFormatter\n  /** @default 64 */\n  height?: number\n  /** Draws the newest bar in full colour and the rest faded. @default true */\n  highlightLatest?: boolean\n  /** Names each bar in its tooltip, in the same order as `values`. */\n  labels?: string[]\n  /** Oldest first; the newest bar is on the right. */\n  values: number[]\n}\n\nfunction RollingBars({\n  className,\n  color = 'var(--color-chart-2)',\n  formatter,\n  height = 64,\n  highlightLatest = true,\n  labels,\n  values,\n}: RollingBarsProps) {\n  return (\n    <div aria-hidden className={cn('w-full', className)}>\n      <TinyBarChart\n        accessibilityLayer={false}\n        bars={[\n          {\n            dataKey: 'value',\n            fill: color,\n            isAnimationActive: false,\n            radius: [2, 2, 0, 0],\n          },\n        ]}\n        data={values.map((value, index) => ({ label: labels?.[index], value }))}\n        formatter={formatter}\n        height={height}\n        highlightIndex={highlightLatest ? values.length - 1 : undefined}\n        highlightLabel={false}\n      />\n    </div>\n  )\n}\n\nfunction formatAgo(date: Date, now: Date) {\n  const seconds = Math.max(0, Math.round((now.getTime() - date.getTime()) / 1000))\n  if (seconds < 5) return 'now'\n  if (seconds < 60) return `${seconds}s ago`\n  return formatRelativeTime(date, now, { style: 'narrow' })\n}\n\nexport {\n  LiveDot,\n  LiveBadge,\n  LiveNumber,\n  RollingBars,\n  createRandom,\n  formatAgo,\n  pushWindow,\n  useInterval,\n}\n\nexport type { LiveBadgeProps, LiveNumberProps, RollingBarsProps }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/realtime.tsx"
    }
  ],
  "type": "registry:component"
}
