{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"chat-thread-01","title":"Chat Thread","description":"A conversation with an assistant: your messages and attachments on the right, answers across the full width with folded reasoning, lists and bold text, copy / feedback / share / retry actions, and a thinking state.","dependencies":["cn","lucide-react"],"registryDependencies":["@sortui/attachment","@sortui/bubble","@sortui/button","@sortui/collapsible","@sortui/message","@sortui/message-scroller","@sortui/tooltip"],"files":[{"path":"registry/blocks/chat-thread-01/components/chat-thread.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport {\n  CheckIcon,\n  ChevronRightIcon,\n  CopyIcon,\n  FileTextIcon,\n  PencilIcon,\n  RotateCcwIcon,\n  ShareIcon,\n  ThumbsDownIcon,\n  ThumbsUpIcon,\n} from \"lucide-react\"\nimport { cn } from \"cn\"\n\nimport {\n  Attachment,\n  AttachmentContent,\n  AttachmentDescription,\n  AttachmentMedia,\n  AttachmentTitle,\n} from \"@/components/ui/attachment\"\nimport { Bubble, BubbleContent } from \"@/components/ui/bubble\"\nimport { Button } from \"@/components/ui/button\"\nimport {\n  Collapsible,\n  CollapsibleContent,\n  CollapsibleTrigger,\n} from \"@/components/ui/collapsible\"\nimport { Message, MessageContent, MessageFooter } from \"@/components/ui/message\"\nimport {\n  MessageScroller,\n  MessageScrollerButton,\n  MessageScrollerContent,\n  MessageScrollerItem,\n  MessageScrollerProvider,\n  MessageScrollerViewport,\n} from \"@/components/ui/message-scroller\"\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipTrigger,\n} from \"@/components/ui/tooltip\"\n\nexport type ChatAttachment = {\n  name: string\n  description: string\n}\n\nexport type ChatMessage = {\n  id: string\n  role: \"user\" | \"assistant\"\n  content: string\n  attachments?: ChatAttachment[]\n  /** What the model weighed before answering, shown folded above the reply. */\n  reasoning?: { seconds: number; steps: string[] }\n  /** `thinking` before the first word, `streaming` while words arrive. */\n  status?: \"thinking\" | \"streaming\" | \"done\"\n}\n\nexport function ChatThread({\n  messages,\n  onRetry,\n  onEdit,\n  onShare,\n  onFeedback,\n  className,\n}: {\n  messages: ChatMessage[]\n  onRetry?: (message: ChatMessage) => void\n  onEdit?: (message: ChatMessage) => void\n  onShare?: (message: ChatMessage) => void\n  /** Called with the new rating, or null when it is turned off. */\n  onFeedback?: (message: ChatMessage, value: \"good\" | \"bad\" | null) => void\n  className?: string\n}) {\n  return (\n    <MessageScrollerProvider>\n      <MessageScroller className={className}>\n        <MessageScrollerViewport>\n          <MessageScrollerContent className=\"mx-auto w-full max-w-3xl gap-8 px-4 py-8\">\n            {messages.map((message) => (\n              <MessageScrollerItem\n                key={message.id}\n                messageId={message.id}\n                scrollAnchor={message.role === \"user\"}\n              >\n                {message.role === \"user\" ? (\n                  <UserMessage message={message} onEdit={onEdit} />\n                ) : (\n                  <AssistantMessage\n                    message={message}\n                    onRetry={onRetry}\n                    onShare={onShare}\n                    onFeedback={onFeedback}\n                  />\n                )}\n              </MessageScrollerItem>\n            ))}\n          </MessageScrollerContent>\n        </MessageScrollerViewport>\n        <MessageScrollerButton />\n      </MessageScroller>\n    </MessageScrollerProvider>\n  )\n}\n\nfunction UserMessage({\n  message,\n  onEdit,\n}: {\n  message: ChatMessage\n  onEdit?: (message: ChatMessage) => void\n}) {\n  return (\n    <Message align=\"end\">\n      <MessageContent>\n        {message.attachments?.map((file, index) => (\n          <Attachment key={index} className=\"w-64\">\n            <AttachmentMedia>\n              <FileTextIcon />\n            </AttachmentMedia>\n            <AttachmentContent>\n              <AttachmentTitle>{file.name}</AttachmentTitle>\n              <AttachmentDescription>{file.description}</AttachmentDescription>\n            </AttachmentContent>\n          </Attachment>\n        ))}\n        {message.content && (\n          <Bubble variant=\"muted\" align=\"end\">\n            <BubbleContent className=\"whitespace-pre-wrap\">\n              {message.content}\n            </BubbleContent>\n          </Bubble>\n        )}\n        <MessageFooter className=\"transition-opacity has-focus-visible:opacity-100 [@media(hover:hover)]:opacity-0 [@media(hover:hover)]:group-hover/message:opacity-100\">\n          {message.content && <CopyAction text={message.content} />}\n          {onEdit && (\n            <Action label=\"Edit message\" onClick={() => onEdit(message)}>\n              <PencilIcon />\n            </Action>\n          )}\n        </MessageFooter>\n      </MessageContent>\n    </Message>\n  )\n}\n\nfunction AssistantMessage({\n  message,\n  onRetry,\n  onShare,\n  onFeedback,\n}: {\n  message: ChatMessage\n  onRetry?: (message: ChatMessage) => void\n  onShare?: (message: ChatMessage) => void\n  onFeedback?: (message: ChatMessage, value: \"good\" | \"bad\" | null) => void\n}) {\n  const [feedback, setFeedback] = React.useState<\"good\" | \"bad\" | null>(null)\n\n  function toggleFeedback(value: \"good\" | \"bad\") {\n    const next = feedback === value ? null : value\n    setFeedback(next)\n    onFeedback?.(message, next)\n  }\n\n  if (message.status === \"thinking\") {\n    return (\n      <Message>\n        <MessageContent>\n          <p className=\"shimmer text-sm text-muted-foreground\">Thinking…</p>\n        </MessageContent>\n      </Message>\n    )\n  }\n\n  return (\n    <Message>\n      <MessageContent>\n        {message.reasoning && <Reasoning {...message.reasoning} />}\n        <Bubble variant=\"ghost\">\n          <BubbleContent>\n            <RichText text={message.content} />\n          </BubbleContent>\n        </Bubble>\n        {message.status !== \"streaming\" && (\n          <MessageFooter className=\"gap-0\">\n            <CopyAction text={message.content} />\n            <Action\n              label=\"Good response\"\n              pressed={feedback === \"good\"}\n              onClick={() => toggleFeedback(\"good\")}\n            >\n              <ThumbsUpIcon />\n            </Action>\n            <Action\n              label=\"Bad response\"\n              pressed={feedback === \"bad\"}\n              onClick={() => toggleFeedback(\"bad\")}\n            >\n              <ThumbsDownIcon />\n            </Action>\n            {onShare && (\n              <Action label=\"Share\" onClick={() => onShare(message)}>\n                <ShareIcon />\n              </Action>\n            )}\n            {onRetry && (\n              <Action label=\"Try again\" onClick={() => onRetry(message)}>\n                <RotateCcwIcon />\n              </Action>\n            )}\n          </MessageFooter>\n        )}\n      </MessageContent>\n    </Message>\n  )\n}\n\nfunction Reasoning({ seconds, steps }: { seconds: number; steps: string[] }) {\n  return (\n    <Collapsible className=\"flex flex-col gap-2\">\n      <CollapsibleTrigger className=\"group/reasoning flex w-fit items-center gap-1 rounded-xs text-sm text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/30\">\n        Thought for {seconds} {seconds === 1 ? \"second\" : \"seconds\"}\n        <ChevronRightIcon className=\"size-4 transition-transform group-data-panel-open/reasoning:rotate-90\" />\n      </CollapsibleTrigger>\n      <CollapsibleContent>\n        <ul className=\"flex flex-col gap-2 border-l-2 border-border pl-3 text-sm text-muted-foreground\">\n          {steps.map((step, index) => (\n            <li key={index}>{step}</li>\n          ))}\n        </ul>\n      </CollapsibleContent>\n    </Collapsible>\n  )\n}\n\nfunction Action({\n  label,\n  pressed,\n  onClick,\n  children,\n}: {\n  label: string\n  pressed?: boolean\n  onClick?: () => void\n  children: React.ReactNode\n}) {\n  return (\n    <Tooltip>\n      <TooltipTrigger\n        render={\n          <Button\n            variant=\"ghost\"\n            size=\"icon-xs\"\n            aria-label={label}\n            aria-pressed={pressed}\n            onClick={onClick}\n            className={cn(\n              \"text-muted-foreground hover:text-foreground\",\n              pressed && \"text-foreground\"\n            )}\n          />\n        }\n      >\n        {children}\n      </TooltipTrigger>\n      <TooltipContent>{label}</TooltipContent>\n    </Tooltip>\n  )\n}\n\nfunction CopyAction({ text }: { text: string }) {\n  const [copied, setCopied] = React.useState(false)\n\n  React.useEffect(() => {\n    if (!copied) return\n    const timeout = setTimeout(() => setCopied(false), 1500)\n    return () => clearTimeout(timeout)\n  }, [copied])\n\n  return (\n    <>\n      <Action\n        label={copied ? \"Copied\" : \"Copy\"}\n        onClick={() => {\n          navigator.clipboard?.writeText(text).then(\n            () => setCopied(true),\n            () => {}\n          )\n        }}\n      >\n        {copied ? <CheckIcon /> : <CopyIcon />}\n      </Action>\n      <span role=\"status\" className=\"sr-only\">\n        {copied && \"Copied\"}\n      </span>\n    </>\n  )\n}\n\nfunction RichText({ text }: { text: string }) {\n  const blocks = text.split(/\\n\\s*\\n/).filter((block) => block.trim())\n\n  return (\n    <div className=\"flex flex-col gap-3 leading-6\">\n      {blocks.map((block, index) => {\n        const lines = block.trim().split(\"\\n\")\n        if (lines.every((line) => /^- /.test(line))) {\n          return (\n            <ul key={index} className=\"flex list-disc flex-col gap-1 pl-5\">\n              {lines.map((line, i) => (\n                <li key={i}>{inline(line.slice(2))}</li>\n              ))}\n            </ul>\n          )\n        }\n        if (lines.every((line) => /^\\d+\\. /.test(line))) {\n          return (\n            <ol key={index} className=\"flex list-decimal flex-col gap-1 pl-5\">\n              {lines.map((line, i) => (\n                <li key={i}>{inline(line.replace(/^\\d+\\. /, \"\"))}</li>\n              ))}\n            </ol>\n          )\n        }\n        return (\n          <p key={index} className=\"whitespace-pre-wrap\">\n            {inline(block.trim())}\n          </p>\n        )\n      })}\n    </div>\n  )\n}\n\nfunction inline(text: string) {\n  return text.split(/(\\*\\*[^*]+\\*\\*)/g).map((part, index) =>\n    part.startsWith(\"**\") && part.endsWith(\"**\") ? (\n      <strong key={index} className=\"font-semibold\">\n        {part.slice(2, -2)}\n      </strong>\n    ) : (\n      part\n    )\n  )\n}\n","type":"registry:component"}],"cssVars":{"theme":{"text-sm--letter-spacing":"-0.005em"}},"meta":{"kind":"block","pro":false,"iframeHeight":720},"categories":["ai"],"type":"registry:block"}