{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "elements-thread-search",
  "type": "registry:component",
  "title": "Thread search",
  "description": "History you can actually get back into: pinned first, then grouped by when.",
  "registryDependencies": [
    "https://r.assistant-ui.com/elements-surfaces.json"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "files": [
    {
      "type": "registry:component",
      "path": "components/assistant-ui/elements/thread-search.tsx",
      "sourcePath": "packages/ui/src/components/react/assistant-ui/elements/thread-search.tsx",
      "content": "\"use client\";\n\nimport { useId, type ComponentProps } from \"react\";\nimport { PinIcon, SearchIcon } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\nimport { field, mono, paper } from \"./surfaces\";\n\nexport interface SearchableThread {\n  id: string;\n  title: string;\n  group: string;\n  preview: string;\n  pinned?: boolean;\n}\n\ntype ThreadSearchInteractionProps =\n  | {\n      onActiveChange: (id: string) => void;\n      onSelect: (id: string) => void;\n    }\n  | {\n      onActiveChange?: (id: string) => void;\n      onSelect?: undefined;\n    };\n\ntype ThreadSearchProps = Omit<\n  ComponentProps<\"div\">,\n  | \"children\"\n  | \"threads\"\n  | \"query\"\n  | \"activeId\"\n  | \"onQueryChange\"\n  | \"onActiveChange\"\n  | \"onSelect\"\n> & {\n  threads: readonly SearchableThread[];\n  query: string;\n  activeId: string;\n  onQueryChange?: (query: string) => void;\n} & ThreadSearchInteractionProps;\n\nexport function ThreadSearch({\n  threads,\n  query,\n  activeId,\n  onQueryChange,\n  onActiveChange,\n  onSelect,\n  className,\n  ...props\n}: ThreadSearchProps) {\n  const listId = useId();\n  const optionId = (id: string) => `${listId}-${id}`;\n  const matches = threads.filter((thread) =>\n    `${thread.title} ${thread.preview}`\n      .toLowerCase()\n      .includes(query.toLowerCase()),\n  );\n  const pinned = matches.filter((thread) => thread.pinned);\n  const groups = [\n    ...new Set(matches.filter((t) => !t.pinned).map((t) => t.group)),\n  ];\n\n  const ordered = [\n    ...pinned,\n    ...groups.flatMap((group) =>\n      matches.filter((thread) => !thread.pinned && thread.group === group),\n    ),\n  ];\n\n  const move = (delta: number) => {\n    if (ordered.length === 0 || !onActiveChange) return false;\n    const at = ordered.findIndex((thread) => thread.id === activeId);\n    // activeId can be filtered out by the query; start from the edge the key implies\n    const from = at === -1 ? (delta > 0 ? -1 : 0) : at;\n    const next = ordered[(from + delta + ordered.length) % ordered.length];\n    if (!next) return false;\n    onActiveChange(next.id);\n    document\n      .getElementById(optionId(next.id))\n      ?.scrollIntoView({ block: \"nearest\" });\n    return true;\n  };\n\n  const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {\n    if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229)\n      return;\n    if (event.key === \"ArrowDown\") {\n      if (move(1)) event.preventDefault();\n    } else if (event.key === \"ArrowUp\") {\n      if (move(-1)) event.preventDefault();\n    } else if (event.key === \"Enter\") {\n      const active = ordered.find((thread) => thread.id === activeId);\n      if (active && onSelect) {\n        event.preventDefault();\n        onSelect(active.id);\n      }\n    }\n  };\n\n  const row = (thread: SearchableThread) => {\n    const className = cn(\n      \"flex flex-col gap-0.5 rounded-xl px-2 py-1 text-start transition-colors\",\n      thread.id === activeId\n        ? \"bg-foreground/[0.05]\"\n        : onSelect\n          ? \"hover:bg-foreground/[0.03]\"\n          : undefined,\n    );\n    const content = (\n      <>\n        <span className=\"flex items-center gap-1.5\">\n          {thread.pinned && (\n            <PinIcon className=\"text-muted-foreground size-2.5 shrink-0\" />\n          )}\n          <span className=\"min-w-0 flex-1 truncate text-[13px]\">\n            {thread.title}\n          </span>\n        </span>\n        <span className=\"text-muted-foreground truncate text-xs\">\n          {thread.preview}\n        </span>\n      </>\n    );\n\n    return onSelect ? (\n      <button\n        key={thread.id}\n        id={optionId(thread.id)}\n        type=\"button\"\n        role=\"option\"\n        tabIndex={-1}\n        aria-selected={thread.id === activeId}\n        onMouseDown={(event) => event.preventDefault()}\n        onClick={() => {\n          onActiveChange?.(thread.id);\n          onSelect(thread.id);\n        }}\n        className={className}\n      >\n        {content}\n      </button>\n    ) : (\n      <div\n        key={thread.id}\n        id={optionId(thread.id)}\n        role=\"option\"\n        aria-selected={thread.id === activeId}\n        className={className}\n      >\n        {content}\n      </div>\n    );\n  };\n\n  return (\n    <div\n      data-slot=\"thread-search\"\n      className={cn(\n        paper,\n        \"flex w-full max-w-sm flex-col gap-1.5 rounded-2xl p-3\",\n        className,\n      )}\n\n      {...props}\n    >\n      <div\n        className={cn(\n          field,\n          \"flex items-center gap-2 rounded-xl px-2.5 py-1.5\",\n        )}\n      >\n        <SearchIcon className=\"text-muted-foreground size-3.5 shrink-0\" />\n        <input\n          value={query}\n          onChange={(event) => onQueryChange?.(event.target.value)}\n          onKeyDown={onKeyDown}\n          placeholder=\"Search threads\"\n          aria-label=\"Search threads\"\n          role=\"combobox\"\n          aria-expanded={true}\n          aria-controls={listId}\n          aria-autocomplete=\"list\"\n          aria-activedescendant={\n            ordered.some((thread) => thread.id === activeId)\n              ? optionId(activeId)\n              : undefined\n          }\n          className=\"text-foreground/85 placeholder:text-muted-foreground min-w-0 flex-1 bg-transparent text-[13px] outline-none\"\n        />\n      </div>\n\n      <div\n        id={listId}\n        role=\"listbox\"\n        aria-label=\"Threads\"\n        className=\"flex flex-col gap-1.5\"\n      >\n        {pinned.length > 0 && (\n          <div role=\"group\" aria-label=\"Pinned\" className=\"flex flex-col\">\n            <span\n              aria-hidden\n              className={cn(mono, \"text-muted-foreground px-2 pb-1\")}\n            >\n              pinned\n            </span>\n            {pinned.map(row)}\n          </div>\n        )}\n\n        {groups.map((group) => (\n          <div\n            key={group}\n            role=\"group\"\n            aria-label={group}\n            className=\"flex flex-col\"\n          >\n            <span\n              aria-hidden\n              className={cn(mono, \"text-muted-foreground px-2 pb-1\")}\n            >\n              {group}\n            </span>\n            {matches\n              .filter((thread) => !thread.pinned && thread.group === group)\n              .map(row)}\n          </div>\n        ))}\n      </div>\n\n      {matches.length === 0 && (\n        <span\n          role=\"status\"\n          className=\"text-muted-foreground px-2 py-4 text-center text-xs\"\n        >\n          {threads.length === 0\n            ? \"No threads yet\"\n            : `No thread matches “${query}”`}\n        </span>\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}