{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chat-01",
  "type": "registry:block",
  "title": "Chat Basic",
  "description": "A chat basic block.",
  "author": "ephraim duncan <https://ephraimduncan.com>",
  "registryDependencies": [
    "bubble",
    "button",
    "dropdown-menu",
    "input-group",
    "message",
    "message-scroller",
    "textarea"
  ],
  "dependencies": [
    "@tabler/icons-react"
  ],
  "files": [
    {
      "path": "content/components/chat/chat-01.tsx",
      "type": "registry:component",
      "target": "components/chat-01.tsx",
      "content": "'use client';\n\nimport {\n  IconArrowUp,\n  IconCopy,\n  IconMicrophone,\n  IconPaperclip,\n  IconPhoto,\n  IconPlayerStopFilled,\n  IconPlus,\n  IconRefresh,\n  IconThumbDown,\n  IconThumbUp,\n  IconWorldSearch,\n} from '@tabler/icons-react';\nimport type React from 'react';\nimport { useState } from 'react';\nimport { Bubble, BubbleContent } from '@/components/ui/bubble';\nimport { Button } from '@/components/ui/button';\nimport {\n  DropdownMenu,\n  DropdownMenuContent,\n  DropdownMenuGroup,\n  DropdownMenuItem,\n  DropdownMenuTrigger,\n} from '@/components/ui/dropdown-menu';\nimport {\n  InputGroup,\n  InputGroupAddon,\n  InputGroupButton,\n  InputGroupInput,\n} from '@/components/ui/input-group';\nimport {\n  Message,\n  MessageContent,\n  MessageFooter,\n} from '@/components/ui/message';\nimport {\n  MessageScroller,\n  MessageScrollerButton,\n  MessageScrollerContent,\n  MessageScrollerItem,\n  MessageScrollerProvider,\n  MessageScrollerViewport,\n} from '@/components/ui/message-scroller';\nimport { Textarea } from '@/components/ui/textarea';\nimport { cn } from '@/lib/utils';\n\ntype Turn = {\n  id: string;\n  role: 'user' | 'assistant';\n  text: string;\n  streaming?: boolean;\n};\n\nconst thread: Turn[] = [\n  {\n    id: '1',\n    role: 'user',\n    text: 'Is it true that a day on Venus is longer than its year?',\n  },\n  {\n    id: '2',\n    role: 'assistant',\n    text: 'Yes. Venus takes 243 Earth days to rotate once, but only 225 days to orbit the Sun, so its day is longer than its year.\\n\\nIt also spins in the opposite direction to most planets, which means the Sun rises in the west there.',\n  },\n  { id: '3', role: 'user', text: 'How hot does it get on the surface?' },\n  {\n    id: '4',\n    role: 'assistant',\n    text: 'The surface sits around 465°C, hot enough to melt lead. The atmosphere traps so much heat that Venus stays hotter than Mercury, even though Mercury is much closer to the Sun.',\n  },\n];\n\nconst replies = [\n  'Good question. Venus has no moons and no rings, which makes it unusual among the inner planets. Most explanations point to its slow, retrograde spin: any moon it once had would have drifted inward and been pulled apart by tidal forces.\\n\\nMercury is the only other moonless planet in the solar system.',\n  'The clouds are mostly sulfuric acid droplets, sitting about 50 to 70 km above the surface. They reflect roughly 75% of incoming sunlight, which is why Venus is the brightest object in our sky after the Moon.\\n\\nBelow the clouds the air is almost entirely carbon dioxide, with pressure about 92 times that of Earth at sea level.',\n  'Several landers have made it down. The Soviet Venera 7 was the first to transmit from the surface in 1970, and Venera 13 survived for 127 minutes in 1982, long enough to send back the first color photos.\\n\\nNothing has lasted more than a couple of hours; the heat and pressure destroy electronics quickly.',\n];\n\nconst thinkDelay = 600;\nconst wordDelay = 35;\n\nconst easeOut = 'ease-[cubic-bezier(0.23,1,0.32,1)]';\nconst press = `${easeOut} transition-[scale,background-color] duration-150 active:scale-[0.96]`;\nconst enter = `${easeOut} motion-reduce:slide-in-from-bottom-0 fade-in slide-in-from-bottom-1 animate-in duration-200`;\nconst swap = `${easeOut} fade-in zoom-in-95 animate-in duration-150`;\n\nexport default function Chat01() {\n  const [turns, setTurns] = useState(thread);\n  const [draft, setDraft] = useState('');\n  const [listening, setListening] = useState(false);\n  const [replyIndex, setReplyIndex] = useState(0);\n  const [edit, setEdit] = useState<{ id: string; text: string } | null>(null);\n  const streaming = turns.some((t) => t.streaming);\n\n  const stream = (replyId: string) => {\n    const words = replies[replyIndex % replies.length].split(' ');\n    setReplyIndex(replyIndex + 1);\n    setTurns((prev) =>\n      prev.map((t) =>\n        t.id === replyId ? { ...t, text: '', streaming: true } : t\n      )\n    );\n    let shown = 0;\n    setTimeout(() => {\n      const timer = setInterval(() => {\n        shown += 1;\n        const done = shown >= words.length;\n        setTurns((prev) =>\n          prev.map((t) =>\n            t.id === replyId\n              ? {\n                  ...t,\n                  text: words.slice(0, shown).join(' '),\n                  streaming: !done,\n                }\n              : t\n          )\n        );\n        if (done) {\n          clearInterval(timer);\n        }\n      }, wordDelay);\n    }, thinkDelay);\n  };\n\n  const send = (e: React.FormEvent) => {\n    e.preventDefault();\n    const text = draft.trim();\n    if (!text || streaming) {\n      return;\n    }\n    const replyId = crypto.randomUUID();\n    setTurns([\n      ...turns,\n      { id: crypto.randomUUID(), role: 'user', text },\n      { id: replyId, role: 'assistant', text: '' },\n    ]);\n    setDraft('');\n    stream(replyId);\n  };\n\n  const resend = () => {\n    const text = edit?.text.trim();\n    if (!(edit && text) || streaming) {\n      return;\n    }\n    const at = turns.findIndex((t) => t.id === edit.id);\n    const replyId = crypto.randomUUID();\n    setTurns([\n      ...turns.slice(0, at),\n      { id: edit.id, role: 'user', text },\n      { id: replyId, role: 'assistant', text: '' },\n    ]);\n    setEdit(null);\n    stream(replyId);\n  };\n\n  const editLast = (e: React.KeyboardEvent) => {\n    const last = turns.findLast((t) => t.role === 'user');\n    if (e.key === 'ArrowUp' && !draft && last) {\n      e.preventDefault();\n      setEdit({ id: last.id, text: last.text });\n    }\n  };\n\n  const body = (turn: Turn) => {\n    const mine = turn.role === 'user';\n    if (edit?.id === turn.id) {\n      return (\n        <>\n          <Textarea\n            aria-label=\"Edit message\"\n            autoFocus\n            className={cn(\n              enter,\n              'min-h-0 w-md max-w-full resize-none rounded-[22px] border-0 bg-muted px-4 py-3 font-medium text-[15px]/6 focus-visible:ring-0 md:text-[15px]/6'\n            )}\n            onChange={(e) => setEdit({ ...edit, text: e.target.value })}\n            onKeyDown={(e) => {\n              if (e.key === 'Escape') {\n                setEdit(null);\n              }\n              if (e.key === 'Enter' && !e.shiftKey) {\n                e.preventDefault();\n                resend();\n              }\n            }}\n            value={edit.text}\n          />\n          <div className={cn(enter, 'flex items-center gap-1.5 self-end')}>\n            <Button\n              aria-label=\"Copy\"\n              className={cn(press, 'rounded-full')}\n              onClick={() => navigator.clipboard.writeText(turn.text)}\n              size=\"icon-sm\"\n              variant=\"ghost\"\n            >\n              <IconCopy stroke={1.6} />\n            </Button>\n            <Button\n              className={cn(press, 'rounded-full')}\n              onClick={() => setEdit(null)}\n              size=\"sm\"\n              variant=\"secondary\"\n            >\n              Cancel\n            </Button>\n            <Button\n              className={cn(press, 'rounded-full')}\n              disabled={!edit.text.trim() || streaming}\n              onClick={resend}\n              size=\"sm\"\n            >\n              Send\n            </Button>\n          </div>\n        </>\n      );\n    }\n    if (turn.streaming && !turn.text) {\n      return (\n        <span className=\"shimmer text-[15px]/6 text-muted-foreground\">\n          Thinking…\n        </span>\n      );\n    }\n    return turn.text.split('\\n\\n').map((paragraph) => (\n      <Bubble\n        align={mine ? 'end' : 'start'}\n        className={\n          mine\n            ? '-mx-2.5 -my-1 box-content max-w-md cursor-pointer rounded-xl px-2.5 py-1 text-right font-medium transition-colors duration-150 ease-out hover:bg-muted'\n            : undefined\n        }\n        key={paragraph}\n        onDoubleClick={\n          mine ? () => setEdit({ id: turn.id, text: turn.text }) : undefined\n        }\n        title={mine ? 'Double-click to edit' : undefined}\n        variant=\"ghost\"\n      >\n        <BubbleContent className=\"text-[15px]/6\">{paragraph}</BubbleContent>\n      </Bubble>\n    ));\n  };\n\n  return (\n    <div className=\"flex h-dvh w-full flex-col bg-background\">\n      <MessageScrollerProvider autoScroll>\n        <MessageScroller>\n          <MessageScrollerViewport>\n            <MessageScrollerContent className=\"mx-auto w-full max-w-2xl gap-3.5 px-1.5 pt-12 pb-4\">\n              {turns.map((turn) => {\n                const mine = turn.role === 'user';\n                return (\n                  <MessageScrollerItem\n                    className={cn(\n                      'px-2.5',\n                      mine && 'pt-5.5 pb-1 first:pt-1',\n                      !thread.some((t) => t.id === turn.id) && enter\n                    )}\n                    key={turn.id}\n                    messageId={turn.id}\n                  >\n                    <Message align={mine ? 'end' : 'start'}>\n                      <MessageContent className=\"gap-2\">\n                        {body(turn)}\n                        {!(mine || turn.streaming) && (\n                          <MessageFooter className={cn(enter, 'gap-0.5')}>\n                            <Button\n                              aria-label=\"Copy\"\n                              className={press}\n                              onClick={() =>\n                                navigator.clipboard.writeText(turn.text)\n                              }\n                              size=\"icon-sm\"\n                              variant=\"ghost\"\n                            >\n                              <IconCopy stroke={1.6} />\n                            </Button>\n                            <Button\n                              aria-label=\"Good response\"\n                              className={press}\n                              size=\"icon-sm\"\n                              variant=\"ghost\"\n                            >\n                              <IconThumbUp stroke={1.6} />\n                            </Button>\n                            <Button\n                              aria-label=\"Bad response\"\n                              className={press}\n                              size=\"icon-sm\"\n                              variant=\"ghost\"\n                            >\n                              <IconThumbDown stroke={1.6} />\n                            </Button>\n                            <Button\n                              aria-label=\"Regenerate\"\n                              className={press}\n                              onClick={() => stream(turn.id)}\n                              size=\"icon-sm\"\n                              variant=\"ghost\"\n                            >\n                              <IconRefresh stroke={1.6} />\n                            </Button>\n                          </MessageFooter>\n                        )}\n                      </MessageContent>\n                    </Message>\n                  </MessageScrollerItem>\n                );\n              })}\n            </MessageScrollerContent>\n          </MessageScrollerViewport>\n          <MessageScrollerButton className={easeOut} />\n        </MessageScroller>\n      </MessageScrollerProvider>\n\n      <form\n        className=\"mx-auto flex w-full max-w-2xl flex-col items-center gap-2.5 px-4 pt-3 pb-4\"\n        onSubmit={send}\n      >\n        <InputGroup className=\"h-13 rounded-full border-0 bg-muted px-2.5 has-[[data-slot=input-group-control]:focus-visible]:ring-0\">\n          <InputGroupAddon>\n            <DropdownMenu>\n              <DropdownMenuTrigger\n                render={\n                  <InputGroupButton\n                    aria-label=\"Add attachment\"\n                    className={cn(press, 'rounded-full hover:bg-foreground/10')}\n                    size=\"icon-sm\"\n                  />\n                }\n              >\n                <IconPlus stroke={1.8} />\n              </DropdownMenuTrigger>\n              <DropdownMenuContent\n                align=\"start\"\n                className=\"w-60 rounded-[20px] p-2 shadow-[0_0_0_1px_oklch(0_0_0/0.06),0_8px_24px_-6px_oklch(0_0_0/0.14)]\"\n                sideOffset={10}\n              >\n                <DropdownMenuGroup className=\"flex flex-col gap-0.5\">\n                  <DropdownMenuItem className=\"gap-3 rounded-xl px-3 py-2.5 text-[15px] [&>svg]:text-muted-foreground\">\n                    <IconPaperclip className=\"size-4.5\" stroke={1.6} />\n                    Add files\n                  </DropdownMenuItem>\n                  <DropdownMenuItem className=\"gap-3 rounded-xl px-3 py-2.5 text-[15px] [&>svg]:text-muted-foreground\">\n                    <IconPhoto className=\"size-4.5\" stroke={1.6} />\n                    Add photos\n                  </DropdownMenuItem>\n                  <DropdownMenuItem className=\"gap-3 rounded-xl px-3 py-2.5 text-[15px] [&>svg]:text-muted-foreground\">\n                    <IconWorldSearch className=\"size-4.5\" stroke={1.6} />\n                    Search the web\n                  </DropdownMenuItem>\n                </DropdownMenuGroup>\n              </DropdownMenuContent>\n            </DropdownMenu>\n          </InputGroupAddon>\n          <InputGroupInput\n            className=\"text-[15px] md:text-[15px]\"\n            onChange={(e) => setDraft(e.target.value)}\n            onKeyDown={editLast}\n            placeholder={listening ? 'Listening…' : 'Ask anything'}\n            value={draft}\n          />\n          <InputGroupAddon align=\"inline-end\">\n            {draft.trim() ? (\n              <InputGroupButton\n                aria-label=\"Send\"\n                className={cn(swap, press, 'rounded-full')}\n                disabled={streaming}\n                size=\"icon-sm\"\n                type=\"submit\"\n                variant=\"default\"\n              >\n                <IconArrowUp stroke={2} />\n              </InputGroupButton>\n            ) : (\n              <InputGroupButton\n                aria-label={listening ? 'Stop listening' : 'Voice input'}\n                aria-pressed={listening}\n                className={cn(\n                  swap,\n                  press,\n                  'rounded-full',\n                  listening\n                    ? 'bg-destructive/15 text-destructive hover:bg-destructive/25'\n                    : 'hover:bg-foreground/10'\n                )}\n                onClick={() => setListening(!listening)}\n                size=\"icon-sm\"\n              >\n                {listening ? (\n                  <IconPlayerStopFilled className=\"animate-pulse motion-reduce:animate-none\" />\n                ) : (\n                  <IconMicrophone stroke={1.8} />\n                )}\n              </InputGroupButton>\n            )}\n          </InputGroupAddon>\n        </InputGroup>\n        <p className=\"text-muted-foreground text-xs\">\n          AI can make mistakes. Check important info.\n        </p>\n      </form>\n    </div>\n  );\n}\n"
    }
  ],
  "categories": [
    "chat"
  ]
}