{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "thread-list",
  "title": "Thread list",
  "description": "Conversation sidebar with search highlighting, pinned and date groups, a selection that glides between rows, hover actions on desktop and swipe-to-reveal on touch, inline rename, delete and arrow-key navigation.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/ai/thread-list.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, animate, motion, useMotionValue, useReducedMotion, type PanInfo } from \"motion/react\"\nimport { MessageSquare, Pencil, Pin, PinOff, Plus, Search, Trash2 } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens, spring } from \"@/lib/goodui-motion\"\n\n/*\n * Conversation sidebar: search with match highlighting, pinned and date groups (Today, Yesterday, Previous\n * 7 days…), rows with hover actions on desktop and swipe-to-reveal actions on touch, inline rename, delete,\n * a selection highlight that glides between rows and arrow-key navigation.\n */\n\nexport type Thread = {\n  id: string\n  title: string\n  /** Last message preview under the title. */\n  preview?: string\n  /** ISO string, timestamp or Date. */\n  updatedAt: string | number | Date\n  pinned?: boolean\n  unread?: boolean\n}\n\nexport type ThreadListLabels = {\n  search: string\n  newChat: string\n  pinned: string\n  today: string\n  yesterday: string\n  week: string\n  month: string\n  older: string\n  pin: string\n  unpin: string\n  rename: string\n  delete: string\n  empty: string\n  unread: string\n  actions: string\n  save: string\n}\nconst DEFAULT_LABELS: ThreadListLabels = {\n  search: \"Search chats\",\n  newChat: \"New chat\",\n  pinned: \"Pinned\",\n  today: \"Today\",\n  yesterday: \"Yesterday\",\n  week: \"Previous 7 days\",\n  month: \"Previous 30 days\",\n  older: \"Older\",\n  pin: \"Pin\",\n  unpin: \"Unpin\",\n  rename: \"Rename\",\n  delete: \"Delete\",\n  empty: \"No chats found\",\n  unread: \"Unread\",\n  actions: \"Chat actions\",\n  save: \"Save\",\n}\n\nexport type ThreadListProps = Omit<React.ComponentProps<\"nav\">, \"onChange\"> & {\n  threads: Thread[]\n  /** Selected thread id (controlled). */\n  value?: string\n  defaultValue?: string\n  onValueChange?: (id: string) => void\n  onNew?: () => void\n  onPin?: (id: string, pinned: boolean) => void\n  onRename?: (id: string, title: string) => void\n  onDelete?: (id: string) => void\n  /** Reference time for the date groups and relative times. Pass a fixed value for stable server rendering. */\n  now?: number | Date\n  search?: boolean\n  /** Group under Pinned / Today / Yesterday / … headings. */\n  groupByDate?: boolean\n  /** Swipe rows to reveal actions: on, off, or only on touch screens (auto). */\n  swipe?: boolean | \"auto\"\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<ThreadListLabels>\n}\n\nconst DAY = 86_400_000\nconst toMs = (d: string | number | Date) => (d instanceof Date ? d.getTime() : typeof d === \"number\" ? d : new Date(d).getTime())\n\nfunction groupKey(t: number, now: number): keyof Pick<ThreadListLabels, \"today\" | \"yesterday\" | \"week\" | \"month\" | \"older\"> {\n  const start = new Date(now)\n  start.setHours(0, 0, 0, 0)\n  const s = start.getTime()\n  if (t >= s) return \"today\"\n  if (t >= s - DAY) return \"yesterday\"\n  if (t >= s - 7 * DAY) return \"week\"\n  if (t >= s - 30 * DAY) return \"month\"\n  return \"older\"\n}\n\nfunction relative(t: number, now: number, locale: string) {\n  const diff = now - t\n  if (diff < 60_000) return \"now\"\n  if (diff < 3_600_000) return `${Math.floor(diff / 60_000)}m`\n  const d = new Date(t)\n  if (diff < DAY && groupKey(t, now) === \"today\") return `${d.getHours()}:${String(d.getMinutes()).padStart(2, \"0\")}`\n  if (diff < 7 * DAY) return d.toLocaleDateString(locale, { weekday: \"short\" })\n  return d.toLocaleDateString(locale, { month: \"short\", day: \"numeric\" })\n}\n\nfunction Highlight({ text, query }: { text: string; query: string }) {\n  if (!query) return <>{text}</>\n  const i = text.toLowerCase().indexOf(query.toLowerCase())\n  if (i < 0) return <>{text}</>\n  return (\n    <>\n      {text.slice(0, i)}\n      <mark className=\"rounded-sm bg-amber-300/50 text-foreground dark:bg-amber-400/30\">{text.slice(i, i + query.length)}</mark>\n      {text.slice(i + query.length)}\n    </>\n  )\n}\n\ntype RowProps = {\n  thread: Thread\n  selected: boolean\n  query: string\n  now: number\n  swipe: boolean\n  reduce: boolean\n  l: ThreadListLabels\n  uid: string\n  editing: boolean\n  onSelect: () => void\n  onStartRename: () => void\n  onRename: (title: string) => void\n  onCancelRename: () => void\n  onPin?: () => void\n  onDelete?: () => void\n}\n\nconst ACTIONS_W = 136\n\nfunction ThreadRow({ thread: t, selected, query, now, swipe, reduce, l, uid, editing, onSelect, onStartRename, onRename, onCancelRename, onPin, onDelete }: RowProps) {\n  const x = useMotionValue(0)\n  const [revealed, setRevealed] = React.useState(false)\n  const [draft, setDraft] = React.useState(t.title)\n  const inputRef = React.useRef<HTMLInputElement>(null)\n  React.useEffect(() => {\n    if (editing) {\n      setDraft(t.title)\n      inputRef.current?.focus()\n      inputRef.current?.select()\n    }\n  }, [editing, t.title])\n\n  const settle = (open: boolean) => {\n    setRevealed(open)\n    animate(x, open ? -ACTIONS_W : 0, reduce ? { duration: 0 } : spring.snap)\n  }\n  const onDragEnd = (_: unknown, info: PanInfo) => settle(info.offset.x < -ACTIONS_W / 2.5 || info.velocity.x < -400)\n  const actionCount = (onPin ? 1 : 0) + 1 + (onDelete ? 1 : 0)\n\n  const actions = (\n    <div className={cn(\"flex items-center gap-0.5\", swipe ? \"h-full justify-end pe-2\" : \"\")} role=\"group\" aria-label={l.actions}>\n      {onPin && (\n        <button\n          type=\"button\"\n          aria-label={t.pinned ? l.unpin : l.pin}\n          title={t.pinned ? l.unpin : l.pin}\n          onClick={() => {\n            onPin()\n            if (swipe) settle(false)\n          }}\n          className=\"pc-press grid size-7 place-items-center rounded-md text-muted-foreground outline-none hover:bg-background hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n        >\n          {t.pinned ? <PinOff className=\"size-3.5\" aria-hidden=\"true\" /> : <Pin className=\"size-3.5\" aria-hidden=\"true\" />}\n        </button>\n      )}\n      <button\n        type=\"button\"\n        aria-label={l.rename}\n        title={l.rename}\n        onClick={() => {\n          onStartRename()\n          if (swipe) settle(false)\n        }}\n        className=\"pc-press grid size-7 place-items-center rounded-md text-muted-foreground outline-none hover:bg-background hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n      >\n        <Pencil className=\"size-3.5\" aria-hidden=\"true\" />\n      </button>\n      {onDelete && (\n        <button\n          type=\"button\"\n          aria-label={l.delete}\n          title={l.delete}\n          onClick={onDelete}\n          className=\"pc-press grid size-7 place-items-center rounded-md text-muted-foreground outline-none hover:bg-rose-500/15 hover:text-rose-600 focus-visible:ring-3 focus-visible:ring-ring/50 dark:hover:text-rose-400\"\n        >\n          <Trash2 className=\"size-3.5\" aria-hidden=\"true\" />\n        </button>\n      )}\n    </div>\n  )\n\n  return (\n    <motion.li\n      layout=\"position\"\n      initial={reduce ? { opacity: 0 } : { opacity: 0, y: -6 }}\n      animate={{ opacity: 1, y: 0 }}\n      exit={reduce ? { opacity: 0, transition: { duration: 0.1 } } : { opacity: 0, height: 0, transition: { duration: 0.18 } }}\n      transition={reduce ? { duration: 0 } : spring.snap}\n      className=\"group/row relative overflow-hidden rounded-lg\"\n      data-selected={selected || undefined}\n    >\n      {selected && !editing && <motion.span layoutId={`${uid}-sel`} className=\"absolute inset-0 rounded-lg bg-muted\" transition={reduce ? { duration: 0 } : spring.snap} aria-hidden=\"true\" />}\n      {swipe && (\n        <div className=\"absolute inset-y-0 end-0 bg-muted\" style={{ width: ACTIONS_W }} inert={!revealed || undefined}>\n          {actions}\n        </div>\n      )}\n      <motion.div\n        drag={swipe && !editing ? \"x\" : false}\n        dragConstraints={{ left: -ACTIONS_W, right: 0 }}\n        dragElastic={0.1}\n        dragMomentum={false}\n        onDragEnd={onDragEnd}\n        style={{ x }}\n        className={cn(\"relative flex items-center\", swipe && \"bg-background touch-pan-y\", selected && swipe && \"bg-muted\")}\n      >\n        {editing ? (\n          <form\n            className=\"flex w-full items-center gap-1 px-2 py-1\"\n            onSubmit={(e) => {\n              e.preventDefault()\n              const v = draft.trim()\n              if (v) onRename(v)\n              else onCancelRename()\n            }}\n          >\n            <input\n              ref={inputRef}\n              value={draft}\n              aria-label={l.rename}\n              onChange={(e) => setDraft(e.target.value)}\n              onKeyDown={(e) => e.key === \"Escape\" && onCancelRename()}\n              onBlur={() => (draft.trim() && draft.trim() !== t.title ? onRename(draft.trim()) : onCancelRename())}\n              className=\"h-7 min-w-0 flex-1 rounded-md border bg-background px-2 text-sm outline-none focus-visible:ring-3 focus-visible:ring-ring/50\"\n            />\n            <button type=\"submit\" className=\"pc-press h-7 rounded-md px-2 text-xs font-medium outline-none hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50\">\n              {l.save}\n            </button>\n          </form>\n        ) : (\n          <>\n            <button\n              type=\"button\"\n              data-thread-row\n              tabIndex={selected ? 0 : -1}\n              aria-current={selected ? \"true\" : undefined}\n              onClick={onSelect}\n              onDoubleClick={onStartRename}\n              onKeyDown={(e) => {\n                if (e.key === \"F2\") {\n                  e.preventDefault()\n                  onStartRename()\n                } else if (e.key === \"Delete\" && onDelete) {\n                  e.preventDefault()\n                  onDelete()\n                }\n              }}\n              className={cn(\"pc-press pc-press-soft relative flex min-w-0 flex-1 items-center gap-2.5 rounded-lg px-2.5 py-2 text-start outline-none focus-visible:ring-3 focus-visible:ring-ring/50\", !selected && \"hover:bg-muted/60\")}\n            >\n              <span className=\"min-w-0 flex-1\">\n                <span className=\"flex items-center gap-1.5\">\n                  {t.unread && (\n                    <span className=\"size-1.5 shrink-0 rounded-full bg-[var(--chart-1)]\" aria-hidden=\"true\" />\n                  )}\n                  <span className={cn(\"truncate text-sm leading-5\", t.unread ? \"font-semibold\" : \"font-medium\")}>\n                    <Highlight text={t.title} query={query} />\n                  </span>\n                  {t.unread && <span className=\"sr-only\">({l.unread})</span>}\n                </span>\n                {t.preview && (\n                  <span className={cn(\"block truncate text-xs leading-4\", selected ? \"text-foreground/70\" : \"text-muted-foreground\")}>\n                    <Highlight text={t.preview} query={query} />\n                  </span>\n                )}\n              </span>\n              <span className=\"shrink-0 text-[11px] text-muted-foreground tabular-nums\" aria-hidden=\"true\">\n                {relative(toMs(t.updatedAt), now, \"en-US\")}\n              </span>\n            </button>\n            {!swipe && (\n              <div\n                className=\"pointer-events-none absolute inset-y-0 end-0 flex items-center rounded-e-lg bg-[linear-gradient(90deg,transparent,var(--muted)_30%)] ps-6 pe-1 opacity-0 transition-opacity group-hover/row:pointer-events-auto group-hover/row:opacity-100 group-focus-within/row:pointer-events-auto group-focus-within/row:opacity-100 rtl:bg-[linear-gradient(270deg,transparent,var(--muted)_30%)]\"\n                style={{ minWidth: actionCount * 30 }}\n              >\n                {actions}\n              </div>\n            )}\n          </>\n        )}\n      </motion.div>\n    </motion.li>\n  )\n}\n\n/** Conversation sidebar with search, pinning, rename, delete, date groups, hover and swipe actions. */\nexport function ThreadList({\n  threads,\n  value: valueProp,\n  defaultValue,\n  onValueChange,\n  onNew,\n  onPin,\n  onRename,\n  onDelete,\n  now: nowProp,\n  search = true,\n  groupByDate = true,\n  swipe = \"auto\",\n  labels,\n  className,\n  ...props\n}: ThreadListProps) {\n  const l = useLabels(\"thread-list\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const uid = React.useId()\n  const [inner, setInner] = React.useState(defaultValue ?? threads[0]?.id ?? \"\")\n  const value = valueProp ?? inner\n  const [query, setQuery] = React.useState(\"\")\n  const [editing, setEditing] = React.useState<string | null>(null)\n  const [touch, setTouch] = React.useState(false)\n  const [nowState, setNowState] = React.useState<number | null>(null)\n  const listRef = React.useRef<HTMLDivElement>(null)\n\n  React.useEffect(() => {\n    if (nowProp === undefined) setNowState(Date.now())\n    setTouch(window.matchMedia(\"(hover: none)\").matches)\n  }, [nowProp])\n  const now = nowProp !== undefined ? toMs(nowProp) : (nowState ?? 0)\n  const canSwipe = swipe === true || (swipe === \"auto\" && touch)\n\n  const select = (id: string) => {\n    if (valueProp === undefined) setInner(id)\n    onValueChange?.(id)\n  }\n\n  const q = query.trim().toLowerCase()\n  const filtered = React.useMemo(() => {\n    const list = q ? threads.filter((t) => t.title.toLowerCase().includes(q) || t.preview?.toLowerCase().includes(q)) : threads\n    return [...list].sort((a, b) => toMs(b.updatedAt) - toMs(a.updatedAt))\n  }, [threads, q])\n\n  const groups = React.useMemo(() => {\n    const out: { key: string; label: string; items: Thread[] }[] = []\n    const add = (key: string, label: string, t: Thread) => {\n      const g = out.find((x) => x.key === key)\n      if (g) g.items.push(t)\n      else out.push({ key, label, items: [t] })\n    }\n    for (const t of filtered) if (t.pinned) add(\"pinned\", l.pinned, t)\n    for (const t of filtered) {\n      if (t.pinned) continue\n      if (groupByDate && now) {\n        const k = groupKey(toMs(t.updatedAt), now)\n        add(k, l[k], t)\n      } else add(\"all\", \"\", t)\n    }\n    return out\n  }, [filtered, groupByDate, now, l])\n\n  const onListKey = (e: React.KeyboardEvent) => {\n    if (e.key !== \"ArrowDown\" && e.key !== \"ArrowUp\" && e.key !== \"Home\" && e.key !== \"End\") return\n    const rows = [...(listRef.current?.querySelectorAll<HTMLButtonElement>(\"[data-thread-row]\") ?? [])]\n    if (!rows.length) return\n    const i = rows.findIndex((r) => r === document.activeElement)\n    e.preventDefault()\n    const next = e.key === \"Home\" ? 0 : e.key === \"End\" ? rows.length - 1 : i < 0 ? 0 : (i + (e.key === \"ArrowDown\" ? 1 : -1) + rows.length) % rows.length\n    rows[next]?.focus()\n  }\n\n  return (\n    <nav className={cn(\"pc-threads flex flex-col gap-2\", className)} {...props}>\n      <GooduiTokens />\n      <div className=\"flex items-center gap-2\">\n        {search && (\n          <label className=\"relative min-w-0 flex-1\">\n            <Search className=\"pointer-events-none absolute start-2.5 top-1/2 size-4 -translate-y-1/2 text-muted-foreground\" aria-hidden=\"true\" />\n            <input\n              type=\"search\"\n              value={query}\n              onChange={(e) => setQuery(e.target.value)}\n              placeholder={l.search}\n              aria-label={l.search}\n              className=\"h-9 w-full rounded-lg border bg-background ps-8 pe-3 text-sm outline-none placeholder:text-muted-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n            />\n          </label>\n        )}\n        {onNew && (\n          <button\n            type=\"button\"\n            onClick={onNew}\n            aria-label={l.newChat}\n            title={l.newChat}\n            className=\"pc-press grid size-9 shrink-0 place-items-center rounded-lg border bg-background text-foreground outline-none transition-colors hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 active:scale-95\"\n          >\n            <Plus className=\"size-4\" aria-hidden=\"true\" />\n          </button>\n        )}\n      </div>\n      <div ref={listRef} onKeyDown={onListKey} className=\"flex flex-col gap-3\">\n        {groups.map((g) => (\n          <div key={g.key}>\n            {g.label && <div className=\"px-2.5 pb-1 text-[11px] font-medium tracking-wide text-muted-foreground uppercase\">{g.label}</div>}\n            <ul className=\"flex flex-col gap-0.5\">\n              <AnimatePresence initial={false}>\n                {g.items.map((t) => (\n                  <ThreadRow\n                    key={t.id}\n                    thread={t}\n                    selected={t.id === value}\n                    query={q}\n                    now={now}\n                    swipe={canSwipe}\n                    reduce={reduce}\n                    l={l}\n                    uid={uid}\n                    editing={editing === t.id}\n                    onSelect={() => select(t.id)}\n                    onStartRename={() => setEditing(t.id)}\n                    onRename={(title) => {\n                      setEditing(null)\n                      onRename?.(t.id, title)\n                    }}\n                    onCancelRename={() => setEditing(null)}\n                    onPin={onPin ? () => onPin(t.id, !t.pinned) : undefined}\n                    onDelete={onDelete ? () => onDelete(t.id) : undefined}\n                  />\n                ))}\n              </AnimatePresence>\n            </ul>\n          </div>\n        ))}\n        {groups.length === 0 && (\n          <div className=\"flex flex-col items-center gap-2 py-8 text-center text-sm text-muted-foreground\">\n            <MessageSquare className=\"size-5\" aria-hidden=\"true\" />\n            {l.empty}\n          </div>\n        )}\n      </div>\n    </nav>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/thread-list.tsx"
    }
  ],
  "categories": [
    "components",
    "ai"
  ],
  "type": "registry:component"
}