{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comments-02",
  "description": "A composer that previews how mentions will look and who will be notified, above the latest updates.",
  "registryDependencies": [
    "card",
    "button",
    "tabs",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/comments.json",
    "https://www.dashboardblocks.com/r/radix/format.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/comments/comments-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  CommentAvatar,\n  type CommentAuthor,\n  CommentComposer,\n  CommentItem,\n  getMentions,\n  type ThreadComment,\n  toggleReaction,\n  updateComment,\n} from '@/registry/components/dashboardblocks/comments'\nimport { formatList } from '@/registry/lib/dashboardblocks/format'\nimport { startTransition, useId, useState } from 'react'\n\nimport { AvatarGroup } from '@/components/ui/avatar'\nimport { Button } from '@/components/ui/button'\nimport { Card, CardContent } from '@/components/ui/card'\nimport { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'\n\ntype Tab = 'write' | 'preview'\n\ninterface Comments2Props extends BlockStateProps {\n  currentUser: CommentAuthor\n  /** Pass a fixed date, so times render the same on the server and in the browser. */\n  now: Date\n  onReact?: (updateId: string, emoji: string) => void\n  /** Posts an update and notifies the people it mentions. */\n  onSubmit?: (body: string, options: { mentions: string[] }) => Promise<void>\n  people: CommentAuthor[]\n  project: string\n  title: string\n  /** Newest first. Leave out, or pass none, to show the empty state. */\n  updates?: ThreadComment[]\n}\n\nconst exampleNow = new Date(Date.UTC(2026, 8, 26, 12))\nconst hoursAgo = (hours: number) => new Date(exampleNow.getTime() - hours * 3_600_000)\n\nconst exampleProps: Comments2Props = {\n  currentUser: { name: 'Amara Okafor' },\n  now: exampleNow,\n  people: [\n    { name: 'Priya Nair' },\n    { name: 'Lena Fischer' },\n    { name: 'Mateo Silva' },\n    { name: 'Jonas Weber' },\n    { avatar: '/images/avatar-01.webp', name: 'Sofia Rossi' },\n  ],\n  project: 'Website relaunch',\n  title: 'Post an update',\n  updates: [\n    {\n      at: hoursAgo(5),\n      author: { avatar: '/images/avatar-01.webp', name: 'Sofia Rossi' },\n      body: 'New pricing page is on staging. @Priya Nair can you check the annual toggle copy before Thursday?',\n      id: 'u1',\n      reactions: [\n        { count: 2, emoji: '👍', reacted: false },\n        { count: 1, emoji: '👀', reacted: true },\n      ],\n    },\n    {\n      at: hoursAgo(29),\n      author: { name: 'Mateo Silva' },\n      body: 'Lighthouse score is up from 71 to 94 on mobile after moving the hero video to a poster image.',\n      id: 'u2',\n      reactions: [{ count: 5, emoji: '🚀', reacted: false }],\n    },\n  ],\n}\n\nconst noUpdates: ThreadComment[] = []\n\nasync function saveUpdate() {\n  await new Promise((resolve) => setTimeout(resolve, 500))\n}\n\nconst describeUpdates = (updates: ThreadComment[], now: Date) => {\n  if (updates.length === 0) return 'No updates yet. The first one shows here.'\n  const latest = Math.max(...updates.map((update) => update.at.getTime()))\n  const hours = Math.max(0, Math.round((now.getTime() - latest) / 3_600_000))\n  const when =\n    hours < 1 ? 'under an hour ago' : `${hours} ${hours === 1 ? 'hour' : 'hours'} ago`\n  return `${updates.length} ${updates.length === 1 ? 'update' : 'updates'}, the latest ${when}.`\n}\n\nconst Comments2 = (props: Comments2Props) => {\n  const {\n    currentUser,\n    now,\n    onReact = () => {},\n    onSubmit = saveUpdate,\n    people,\n    project,\n    errorMessage,\n    status,\n    title,\n    updates: initialUpdates = noUpdates,\n  } = props\n  const headingId = useId()\n  const [updates, setUpdates] = useState(initialUpdates)\n  const [loaded, setLoaded] = useState(initialUpdates)\n  if (initialUpdates !== loaded) {\n    setLoaded(initialUpdates)\n    setUpdates(initialUpdates)\n  }\n  const state = getBlockState(status, updates)\n  const [tab, setTab] = useState<Tab>('write')\n  const [draft, setDraft] = useState('')\n  const [isPosting, setIsPosting] = useState(false)\n  const names = people.map((person) => person.name)\n  const mentioned = getMentions(draft, names).flatMap((name) =>\n    people.filter((person) => person.name === name),\n  )\n\n  async function post(body: string) {\n    await onSubmit(body, { mentions: getMentions(body, names) })\n    startTransition(() => {\n      setUpdates((current) => [\n        { at: now, author: currentUser, body, id: `${Date.now()}` },\n        ...current,\n      ])\n      setDraft('')\n      setTab('write')\n    })\n  }\n\n  async function postFromPreview() {\n    setIsPosting(true)\n    try {\n      await post(draft.trim())\n    } finally {\n      startTransition(() => setIsPosting(false))\n    }\n  }\n\n  const notifies =\n    mentioned.length > 0 ? (\n      <span className='flex min-w-0 items-center gap-2'>\n        <AvatarGroup className='shrink-0'>\n          {mentioned.slice(0, 3).map((person) => (\n            <CommentAvatar key={person.name} author={person} size='sm' />\n          ))}\n        </AvatarGroup>\n        <span className='truncate'>\n          Notifies {formatList(mentioned.map((person) => person.name))}\n        </span>\n      </span>\n    ) : (\n      'Type @ to notify a teammate'\n    )\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <p className='text-muted-foreground text-sm'>\n                {project}. Everyone on the project sees it, and anyone you mention is\n                notified.\n              </p>\n            </header>\n            <Tabs value={tab} onValueChange={(value) => setTab(value as Tab)}>\n              <TabsList>\n                <TabsTrigger value='write'>Write</TabsTrigger>\n                <TabsTrigger value='preview'>Preview</TabsTrigger>\n              </TabsList>\n              <TabsContent value='write'>\n                <CommentComposer\n                  label='Update'\n                  placeholder='What changed, and who needs to know?'\n                  submitLabel='Post update'\n                  people={people}\n                  value={draft}\n                  footer={<span className='text-muted-foreground'>{notifies}</span>}\n                  onValueChange={setDraft}\n                  onSubmit={post}\n                />\n              </TabsContent>\n              <TabsContent value='preview' className='flex flex-col gap-3'>\n                <div className='min-h-24 rounded-md border p-3'>\n                  {draft.trim() ? (\n                    <CommentItem\n                      comment={{\n                        at: now,\n                        author: currentUser,\n                        body: draft,\n                        id: 'preview',\n                      }}\n                      now={now}\n                      people={people}\n                    />\n                  ) : (\n                    <p className='text-muted-foreground text-sm'>\n                      Nothing to preview yet.\n                    </p>\n                  )}\n                </div>\n                <div className='flex items-center justify-between gap-3'>\n                  <span className='text-muted-foreground min-w-0 text-xs'>\n                    {notifies}\n                  </span>\n                  <Button\n                    size='sm'\n                    disabled={isPosting || !draft.trim()}\n                    onClick={() => void postFromPreview()}\n                  >\n                    {isPosting ? 'Posting' : 'Post update'}\n                  </Button>\n                </div>\n              </TabsContent>\n            </Tabs>\n            <section\n              aria-labelledby={headingId}\n              className='flex flex-col gap-1 border-t pt-4'\n            >\n              <h3 id={headingId} className='text-sm font-medium'>\n                Recent updates\n              </h3>\n              <BlockReady className='text-sm'>\n                <p className='text-muted-foreground text-sm'>\n                  {describeUpdates(updates, now)}\n                </p>\n              </BlockReady>\n              <div className='mt-2 flex flex-col divide-y'>\n                <BlockStateRows rows={3} />\n                {!state &&\n                  updates.map((update) => (\n                    <CommentItem\n                      key={update.id}\n                      className='py-4 first:pt-0 last:pb-0'\n                      comment={update}\n                      now={now}\n                      people={people}\n                      onReact={(emoji) => {\n                        onReact(update.id, emoji)\n                        setUpdates((current) =>\n                          updateComment(current, update.id, (item) => ({\n                            ...item,\n                            reactions: toggleReaction(item.reactions, emoji),\n                          })),\n                        )\n                      }}\n                    />\n                  ))}\n              </div>\n            </section>\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Comments2, exampleProps as comments2ExampleProps, type Comments2Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/comments/comments-02.tsx"
    }
  ],
  "meta": {
    "shows": "A composer that previews how mentions will look and who will be notified, above the latest updates.",
    "size": "half",
    "usedWith": [
      "comments-01",
      "team-01"
    ],
    "component": "Comments2",
    "props": "{ currentUser: { avatar?: string; name: string }; now: Date; onReact?: (updateId: string, emoji: string) => void; onSubmit?: (body: string, options: { mentions: string[] }) => Promise<void>; people: { avatar?: string; name: string }[]; project: string; title: string; updates?: { at: Date; author: { avatar?: string; name: string }; body: string; edited?: boolean; id: string; reactions?: { count: number; emoji: string; reacted: boolean }[]; replies?: ThreadComment[]; resolved?: boolean }[]; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
