AI Checkpoints

Going back safely: a checkpoint timeline with a snapshot peek and a rewind sweep, a filmstrip scrubber over a run, and a branch graph whose head moves between branches.

Installation

Installation

One command adds ai-checkpoint and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-checkpoint.json
ai-checkpoint-demo
components/ui/ai-checkpoint-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { useAiLoop, useIdleHands } from "@/lib/ai-kit"
import {
  AiBranches,
  AiCheckpoints,
  AiRewind,
  type AiBranch,
  type AiCheckpoint,
} from "@/registry/tween/ai-checkpoint"

const POINTS: AiCheckpoint[] = [
  {
    id: "c5",
    label: "Yearly toggle added",
    time: "2:41 pm",
    files: 2,
    added: 38,
    removed: 4,
    step: 4,
  },
  {
    id: "c4",
    label: "Pricing cards written",
    time: "2:36 pm",
    files: 3,
    added: 126,
    removed: 8,
    step: 3,
  },
  {
    id: "c3",
    label: "Hero image generated",
    time: "2:29 pm",
    files: 1,
    added: 12,
    removed: 0,
    step: 2,
  },
  {
    id: "c2",
    label: "Headline reveal",
    time: "2:20 pm",
    files: 1,
    added: 24,
    removed: 11,
    step: 1,
  },
  {
    id: "c1",
    label: "Start of session",
    time: "2:04 pm",
    files: 0,
    added: 0,
    removed: 0,
    step: 0,
  },
]

const WRAP: React.CSSProperties = { display: "grid", width: "min(100%, 560px)" }

function Timeline() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [current, setCurrent] = React.useState("c5")
  const [peek, setPeek] = React.useState<string | undefined>("c4")

  useAiLoop(
    root,
    async ({ sleep }) => {
      setCurrent("c5")
      setPeek(undefined)
      await sleep(1300)
      for (const id of ["c4", "c3", "c2"]) {
        setPeek(id)
        await sleep(1300)
      }
      setCurrent("c2")
      await sleep(2600)
      setPeek("c4")
      await sleep(1100)
      setCurrent("c4")
      await sleep(2200)
    },
    { gap: 500, enabled: !hands, still: () => setPeek("c3") }
  )

  return (
    <div ref={root} style={WRAP} onPointerDown={touch} onKeyDown={touch}>
      <AiCheckpoints
        checkpoints={POINTS}
        current={current}
        peek={hands ? undefined : peek}
        onRestore={setCurrent}
      />
    </div>
  )
}

const STEPS: AiCheckpoint[] = [...POINTS].reverse()

function Rewind() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [value, setValue] = React.useState(3)
  const [head, setHead] = React.useState(4)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setHead(4)
      setValue(4)
      await sleep(1300)
      for (const at of [3, 2, 1, 2]) {
        setValue(at)
        await sleep(1250)
      }
      await sleep(500)
      setHead(2)
      await sleep(2400)
    },
    { gap: 500, enabled: !hands, still: () => setValue(2) }
  )

  return (
    <div ref={root} style={WRAP} onPointerDown={touch} onKeyDown={touch}>
      <AiRewind
        steps={STEPS}
        value={value}
        onValueChange={setValue}
        head={head}
        onCommit={setHead}
      />
    </div>
  )
}

const TREE: AiBranch[] = [
  { id: "main", name: "main", tone: "blue", length: 5, note: "the first run" },
  {
    id: "copy",
    name: "tighter-copy",
    tone: "violet",
    parent: "main",
    from: 2,
    length: 3,
    note: "from the hero image",
  },
]

/** The last column a branch reaches, following its parents. */
function endColumn(tree: AiBranch[], id: string): number {
  const branch = tree.find((item) => item.id === id)
  if (!branch) return 0
  const start = branch.parent === undefined ? 0 : (branch.from ?? 0) + 1
  return start + branch.length - 1
}

const FORK_NAMES = ["dark-hero", "bolder-type", "two-column"]
const FORK_TONES = ["amber", "green", "rose"] as const

function Graph() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [tree, setTree] = React.useState<AiBranch[]>(TREE)
  const [active, setActive] = React.useState("main")

  const fork = (from: string) => {
    const list = tree
    const made = list.length - TREE.length
    if (made >= FORK_NAMES.length) return
    const id = `fork-${made}`
    setActive(id)
    setTree([
      ...list,
      {
        id,
        name: FORK_NAMES[made] ?? id,
        tone: FORK_TONES[made] ?? "amber",
        parent: from,
        from: endColumn(list, from),
        length: 1,
        note: `from ${list.find((item) => item.id === from)?.name ?? from}`,
      },
    ])
  }

  useAiLoop(
    root,
    async ({ sleep }) => {
      setTree(TREE)
      setActive("main")
      await sleep(1700)
      setActive("copy")
      await sleep(2000)
      setTree([
        ...TREE,
        {
          id: "fork-0",
          name: "dark-hero",
          tone: "amber",
          parent: "copy",
          from: endColumn(TREE, "copy"),
          length: 1,
          note: "from tighter-copy",
        },
      ])
      setActive("fork-0")
      await sleep(2400)
      setActive("main")
      await sleep(2000)
    },
    { gap: 500, enabled: !hands, still: () => setActive("copy") }
  )

  return (
    <div ref={root} style={WRAP} onPointerDown={touch} onKeyDown={touch}>
      <AiBranches
        branches={tree}
        active={active}
        onSwitch={setActive}
        onFork={fork}
      />
    </div>
  )
}

export function AiCheckpointDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Timeline", node: <Timeline /> },
        { name: "Filmstrip", node: <Rewind /> },
        { name: "Branches", node: <Graph /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-checkpoint.json

Usage

import {
  AiBranches,
  AiCheckpoints,
  AiRewind,
  AiSnapshot,
} from "@/components/ui/ai-checkpoint"
<AiCheckpoints
  checkpoints={[
    { id: "c2", label: "Pricing cards written", time: "2:36 pm", files: 3, added: 126, removed: 8, step: 3 },
    { id: "c1", label: "Start of session", time: "2:04 pm", step: 0 },
  ]}
  current="c2"
  onRestore={(id) => restore(id)}
/>
 
<AiRewind steps={steps} value={at} onValueChange={setAt} onCommit={rewind} />
 
<AiBranches
  branches={[
    { id: "main", name: "main", length: 5 },
    { id: "copy", name: "tighter-copy", parent: "main", from: 2, length: 3, tone: "violet" },
  ]}
  active="main"
  onSwitch={setActive}
/>

Versions

  • AiCheckpoints is a timeline. Pointing at a checkpoint, by hover or focus, shows a picture of that state with what was added and removed. Restore asks first; then the blue rail rewinds to that point and the later rows dim.
  • AiRewind is a scrubber. Drag the thumb or use the arrow keys, Home and End. A large preview slides between states and the filmstrip thumbnail under the thumb grows. Rewind here commits.
  • AiBranches is a branch graph. Lanes draw in, a face marks where the work is and travels along the graph when you switch branch, and Fork from here adds a lane.

Each checkpoint draws an AiSnapshot miniature by default (step 0 to 4). Pass preview to show your own picture.

Props

AiCheckpoints

PropTypeDefaultDescription
checkpointsAiCheckpoint[]Newest first.
currentstringThe id the work is at now.
onRestore(id: string) => voidCalled after the visitor confirms.
peekstringControls the peek card. Omit to follow hover.
onPeekChange(id: string | undefined) => voidCalled as the pointed checkpoint changes.

AiCheckpoint

FieldTypeDescription
id, labelstringKey and title.
timestringAlready formatted.
filesnumberFiles changed at this point.
added, removednumberLine counts for the diff bar.
stepnumberWhich AiSnapshot drawing to use, 0 to 4.
previewReactNodeYour own picture in place of the snapshot.

AiRewind

PropTypeDefaultDescription
stepsAiCheckpoint[]Oldest first.
valuenumberThe index the thumb is on.
onValueChange(value: number) => voidCalled while scrubbing.
headnumberlast indexWhere the work actually is.
onCommit(value: number) => voidCalled by Rewind here.

AiBranches

PropTypeDefaultDescription
branches{ id, name, length, parent?, from?, tone?, note? }[]from is the parent's column it forks from.
activestringThe branch the work is on.
onSwitch(id: string) => voidCalled by a row's Switch button.
onFork(id: string) => voidCalled by Fork from here with the active id.