{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "room-clock",
  "type": "registry:ui",
  "title": "Room Clock",
  "description": "A countdown to a host timestamp, in minutes and seconds (\"7:42\"), for the timers that run for minutes: a breakout, a round of pair talk. (`TimerRing` counts the seconds of a quiz round; a ring that says 600 is a poor way to say ten minutes.) The deadline…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "games",
    "game"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/live-room.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/games/room-clock.tsx",
      "target": "components/ui/games/room-clock.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/room-clock v1.1.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/room-clock\n// What changed since: https://ds.interlace.tools/c/room-clock#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — RoomClock\n *\n * A countdown to a host timestamp, in minutes and seconds (\"7:42\"), for the timers that run for\n * minutes: a breakout, a round of pair talk. (`TimerRing` counts the seconds of a quiz round; a\n * ring that says 600 is a poor way to say ten minutes.) The deadline is a timestamp from the\n * host's clock, so a phone that opens late, or reloads, shows the same time left as every other\n * screen; pass `useRoom`'s `now` so a phone with the wrong clock agrees too.\n *\n * ## Anatomy\n *\n *   RoomClock                     (div role=\"timer\" — data-slot=\"room-clock\", data-state=idle|running|low|done)\n *     ├─ span                     (data-slot=\"room-clock-value\": \"7:42\", or the \"time is up\" words)\n *     └─ span aria-live           (data-slot=\"room-clock-announce\": a minute left, ten seconds, time up)\n *\n * ## Screen readers\n *\n * The number is not read every second (a `timer` role is silent by default). The clock speaks when\n * it crosses a minute left, ten seconds left and zero, and only when it sees the crossing: one\n * that opens already past a mark says nothing, so a phone that joins or reloads is not read a\n * warning it missed. Time up is also written out, so it never depends on colour.\n *\n * ## MIN_VIEWPORT — 320\n *\n * | Rule | Concept                   | Where in this file                                   |\n * | ---- | ------------------------- | ---------------------------------------------------- |\n * | R4   | Extends native el         | `React.ComponentProps<'div'>`                        |\n * | R6   | data-slot per part        | room-clock / -value / -announce                      |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`                    |\n * | R8   | Enums, not booleans       | `size`                                               |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const                 |\n * | R26  | A11y                      | role=\"timer\", labelled; sparse announcements          |\n */\n\nimport { cn } from '@/lib/utils';\nimport { formatClock } from '@/components/ui/games/agenda-model';\nimport { useSecondsLeft } from '@/components/ui/games/live-room';\n\nexport const MIN_VIEWPORT = 320 as const;\n\nexport interface RoomClockLabels {\n  /** Names the timer. @default 'Time left' */\n  name?: string;\n  /** Written in place of the number at 0. @default \"Time's up\" */\n  done?: string;\n  /** Spoken at the marks. @default (seconds) => seconds >= 60 ? `${seconds / 60} minute left` : `${seconds} seconds left` */\n  remaining?: (seconds: number) => string;\n  /** Shown when there is no timer. @default 'No timer' */\n  idle?: string;\n}\n\ntype RoomClockProps = Omit<React.ComponentProps<'div'>, 'children'> & {\n  /** When time is up, in ms (host clock). null: no timer. */\n  endsAt: number | null;\n  /** Your clock, corrected to the host's (`useRoom`'s `now`). @default Date.now */\n  now?: () => number;\n  /** Called once when it reaches 0 (while this clock is watching). */\n  onExpire?: () => void;\n  /** @default 'default' */\n  size?: 'default' | 'stage';\n  labels?: RoomClockLabels;\n};\n\nconst SIZE = { default: 'text-2xl', stage: 'text-6xl' } as const;\n/** Seconds left at which the clock speaks: a minute, then ten seconds. */\nconst MARKS = [60, 10] as const;\nconst REMAINING = (s: number) => (s >= 60 ? `${s / 60} minute left` : `${s} seconds left`);\n\nfunction RoomClock({ endsAt, now = Date.now, onExpire, size = 'default', labels, className, ...props }: RoomClockProps) {\n  const left = useSecondsLeft(endsAt, now);\n  const state = left === null ? 'idle' : left === 0 ? 'done' : left <= 10 ? 'low' : 'running';\n  const last = React.useRef<{ at: number | null; left: number | null }>({ at: null, left: null });\n  const [announce, setAnnounce] = React.useState('');\n  const latest = React.useRef({ onExpire, labels });\n  latest.current = { onExpire, labels };\n\n  // Speak a mark when the clock crosses it, and say when it ends; never for a mark already behind it.\n  React.useEffect(() => {\n    const before = last.current;\n    last.current = { at: endsAt, left };\n    if (left === null || before.at !== endsAt || before.left === null || before.left === left) return;\n    const words = latest.current.labels;\n    if (left === 0) {\n      setAnnounce(words?.done ?? \"Time's up\");\n      latest.current.onExpire?.();\n      return;\n    }\n    for (const mark of MARKS) {\n      if (before.left > mark && left <= mark) {\n        setAnnounce((words?.remaining ?? REMAINING)(mark));\n        return;\n      }\n    }\n  }, [endsAt, left]);\n\n  return (\n    <div\n      role=\"timer\"\n      aria-label={labels?.name ?? 'Time left'}\n      data-slot=\"room-clock\"\n      data-state={state}\n      data-min-viewport={String(MIN_VIEWPORT)}\n      className={cn(\n        'inline-flex items-center font-semibold tabular-nums',\n        SIZE[size],\n        state === 'low' || state === 'done' ? 'text-destructive' : 'text-foreground',\n        className,\n      )}\n      {...props}\n    >\n      <span data-slot=\"room-clock-value\">\n        {left === null ? (labels?.idle ?? 'No timer') : left === 0 ? (labels?.done ?? \"Time's up\") : formatClock(left * 1000)}\n      </span>\n      <span data-slot=\"room-clock-announce\" aria-live=\"polite\" className=\"sr-only\">\n        {announce}\n      </span>\n    </div>\n  );\n}\nRoomClock.displayName = 'RoomClock';\n\nexport { RoomClock };\nexport type { RoomClockProps };\n"
    },
    {
      "path": "registry/interlace-ui/games/agenda-model.ts",
      "target": "components/ui/games/agenda-model.ts",
      "type": "registry:ui",
      "content": "export interface AgendaItem {\n  id: string;\n  title: string;\n  /** Planned length, in minutes. */\n  minutes: number;\n  /** Presenter notes for this item. Never shown to the room. */\n  notes?: string;\n}\n\nexport interface AgendaState {\n  /** -1 before the start; `items.length` once finished. */\n  current: number;\n  /** When the current item began, ms, moved forward by any pause. */\n  startedAt: number | null;\n  /** Set while paused. */\n  pausedAt: number | null;\n  /** Minutes added to an item, by id. */\n  extra: Readonly<Record<string, number>>;\n  /** How long each finished item really ran, ms, by id. */\n  spent: Readonly<Record<string, number>>;\n}\n\nexport type AgendaEvent =\n  | { type: 'start'; at: number }\n  | { type: 'next'; from: number; at: number }\n  | { type: 'back'; from: number; at: number }\n  | { type: 'pause'; at: number }\n  | { type: 'resume'; at: number }\n  /** Adds minutes (or takes them away, down to zero extra) to the current item. */\n  | { type: 'extend'; minutes: number }\n  | { type: 'reset' };\n\nexport type AgendaPhase = 'before' | 'running' | 'paused' | 'over' | 'finished';\n\nexport interface AgendaStatus {\n  phase: AgendaPhase;\n  index: number;\n  item: AgendaItem | null;\n  next: AgendaItem | null;\n  /** The current item's length with any extension, ms. */\n  plannedMs: number;\n  elapsedMs: number;\n  /** Negative once the item runs over. */\n  leftMs: number;\n  /** Positive when behind the plan, negative when ahead, ms. */\n  driftMs: number;\n  /** When the session will end at this pace, ms; null before the start. */\n  endsAt: number | null;\n  /** Planned length of the whole session, ms, with extensions. */\n  totalMs: number;\n}\n\nconst MIN = 60_000;\n\nexport function createAgenda(): AgendaState {\n  return { current: -1, startedAt: null, pausedAt: null, extra: {}, spent: {} };\n}\n\nfunction plannedOf(item: AgendaItem, state: AgendaState): number {\n  return Math.max(0, item.minutes + (state.extra[item.id] ?? 0)) * MIN;\n}\n\nfunction elapsedOf(state: AgendaState, now: number): number {\n  if (state.startedAt === null) return 0;\n  return Math.max(0, (state.pausedAt ?? now) - state.startedAt);\n}\n\n/** Applies one event. Events that change nothing return the same state. */\nexport function applyAgendaEvent(items: readonly AgendaItem[], state: AgendaState, event: AgendaEvent): AgendaState {\n  const live = state.current >= 0 && state.current < items.length;\n  switch (event.type) {\n    case 'start':\n      if (state.current !== -1 || items.length === 0) return state;\n      return { ...state, current: 0, startedAt: event.at, pausedAt: null };\n    case 'next': {\n      if (!live || state.current !== event.from) return state;\n      const item = items[state.current] as AgendaItem;\n      const spent = { ...state.spent, [item.id]: elapsedOf(state, event.at) };\n      const to = state.current + 1;\n      if (to >= items.length) return { ...state, current: items.length, startedAt: null, pausedAt: null, spent };\n      return { ...state, current: to, startedAt: event.at, pausedAt: null, spent };\n    }\n    case 'back': {\n      // Works from the finished state too, to reopen the last item.\n      if (state.current !== event.from || state.current < 1 || state.current > items.length) return state;\n      const leaving = items[state.current];\n      const spent = leaving ? { ...state.spent, [leaving.id]: elapsedOf(state, event.at) } : { ...state.spent };\n      const prev = items[state.current - 1] as AgendaItem;\n      // The earlier item picks up where it stopped.\n      const { [prev.id]: before = 0, ...rest } = spent;\n      return { ...state, current: state.current - 1, startedAt: event.at - before, pausedAt: null, spent: rest };\n    }\n    case 'pause':\n      if (!live || state.pausedAt !== null) return state;\n      return { ...state, pausedAt: event.at };\n    case 'resume': {\n      if (!live || state.pausedAt === null || state.startedAt === null) return state;\n      return { ...state, startedAt: state.startedAt + (event.at - state.pausedAt), pausedAt: null };\n    }\n    case 'extend': {\n      if (!live) return state;\n      const id = (items[state.current] as AgendaItem).id;\n      const extra = Math.max(0, (state.extra[id] ?? 0) + event.minutes);\n      return { ...state, extra: { ...state.extra, [id]: extra } };\n    }\n    case 'reset':\n      return createAgenda();\n  }\n}\n\n/** Where the session is at `now`. */\nexport function agendaStatus(items: readonly AgendaItem[], state: AgendaState, now: number): AgendaStatus {\n  const totalMs = items.reduce((sum, it) => sum + plannedOf(it, state), 0);\n  const index = state.current;\n  const item = items[index] ?? null;\n  const base = { index, item, next: items[index + 1] ?? null, totalMs };\n\n  // Time over or under plan on items already finished.\n  let driftMs = 0;\n  for (const it of items) {\n    const spent = state.spent[it.id];\n    if (spent !== undefined) driftMs += spent - plannedOf(it, state);\n  }\n\n  if (index < 0) {\n    return {\n      ...base,\n      phase: 'before',\n      next: items[0] ?? null,\n      plannedMs: 0,\n      elapsedMs: 0,\n      leftMs: 0,\n      driftMs: 0,\n      endsAt: null,\n    };\n  }\n  if (!item) {\n    return { ...base, phase: 'finished', plannedMs: 0, elapsedMs: 0, leftMs: 0, driftMs, endsAt: null };\n  }\n\n  const plannedMs = plannedOf(item, state);\n  const elapsedMs = elapsedOf(state, now);\n  const leftMs = plannedMs - elapsedMs;\n  const overMs = Math.max(0, -leftMs);\n  const later = items.slice(index + 1).reduce((sum, it) => sum + plannedOf(it, state), 0);\n  const phase: AgendaPhase = state.pausedAt !== null ? 'paused' : leftMs < 0 ? 'over' : 'running';\n  return {\n    ...base,\n    phase,\n    plannedMs,\n    elapsedMs,\n    leftMs,\n    driftMs: driftMs + overMs,\n    endsAt: now + Math.max(0, leftMs) + later,\n  };\n}\n\n/** `m:ss` for a length of time (its absolute value), rounded up to the second. */\nexport function formatClock(ms: number): string {\n  const s = Math.ceil(Math.abs(ms) / 1000);\n  const m = Math.floor(s / 60);\n  return `${m}:${String(s % 60).padStart(2, '0')}`;\n}\n\n/**\n * @interlace/ui — agenda-model\n *\n * The running order of a session, and where the presenter is in it. Each\n * item has planned minutes; the state records when the current item began,\n * pauses, extensions and how long finished items really took. Everything is\n * a timestamp, so a refreshed presenter tab, a second device, or a shared\n * screen that joins late all read the same clock from the same state.\n *\n * ## Events\n *\n * `next` and `back` name the item they move from (`from`), so an event that\n * arrives twice after a reconnect moves once, not twice. `pause` while\n * paused, `resume` while running and `start` once started change nothing.\n *\n * ## Status\n *\n * `agendaStatus(items, state, now)` says which item is on, how long is left\n * (negative once it runs over), how far the whole session has drifted from\n * the plan, and when it will end at this pace.\n */\n"
    }
  ],
  "meta": {
    "tier": "game",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.1.0",
    "since": "2.0.0"
  },
  "docs": "## @interlace/room-clock\n\nInstalled to `components/ui/games/room-clock.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/games/room-clock';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/room-clock\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
