{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "event-feed",
  "title": "Event Feed",
  "description": "Live feed of a contract's events (useWatchContractEvent), newest first, deduplicated, with an optional recent-history backfill (getContractEvents over N blocks), indexed-argument filters, default rows showing the event name and formatted args, and a transaction link per event. role=log for polite announcements. renderEvent for custom rows. initialEvents takes past events from an indexer instead of RPC history. Exports useEventFeed and formatEventArg.",
  "dependencies": [
    "wagmi",
    "viem",
    "@tanstack/react-query"
  ],
  "registryDependencies": [
    "@0xui/explorer-link",
    "@0xui/0xui-utils"
  ],
  "files": [
    {
      "path": "registry/default/components/event-feed.tsx",
      "content": "// Docs: https://0xui.io/components/event-feed.md\nimport { useQuery } from \"@tanstack/react-query\";\nimport { useMemo, useState, type ReactNode } from \"react\";\nimport { isAddress, type Abi, type Address, type Log } from \"viem\";\nimport { usePublicClient, useWatchContractEvent } from \"wagmi\";\nimport { ExplorerLink } from \"@/components/explorer-link\";\nimport { truncateAddress } from \"@/lib/0xui-utils\";\nimport { cn } from \"@/lib/utils\";\n\nexport type FeedEvent = Log & {\n  eventName?: string;\n  args?: Record<string, unknown> | readonly unknown[];\n};\n\nconst logId = (log: FeedEvent) => `${log.transactionHash}-${log.logIndex}`;\n\nfunction newestFirst(a: FeedEvent, b: FeedEvent) {\n  const block = Number((b.blockNumber ?? 0n) - (a.blockNumber ?? 0n));\n  return block || (b.logIndex ?? 0) - (a.logIndex ?? 0);\n}\n\nexport type UseEventFeedParameters = {\n  address: Address;\n  abi: Abi;\n  /** Event to follow. Omit for every event in the ABI. */\n  eventName?: string;\n  /** Indexed-argument filter, e.g. `{ to: myAddress }`. */\n  args?: Record<string, unknown>;\n  chainId?: number;\n  /** Also load events from this many recent blocks. Keep it small: public RPCs cap log ranges. */\n  historyBlocks?: number;\n  /** Keep at most this many events. */\n  max?: number;\n  /**\n   * Past events from your own source, e.g. an indexer. Shown alongside\n   * live events and de-duplicated. Prefer this over `historyBlocks` in\n   * production.\n   */\n  initialEvents?: readonly FeedEvent[];\n};\n\n/** Contract events, newest first: optional recent history plus live updates. */\nexport function useEventFeed({\n  address,\n  abi,\n  eventName,\n  args,\n  chainId,\n  historyBlocks = 0,\n  max = 20,\n  initialEvents,\n}: UseEventFeedParameters) {\n  const client = usePublicClient({ chainId });\n  const [live, setLive] = useState<FeedEvent[]>([]);\n\n  const history = useQuery({\n    queryKey: [\"0xui\", \"event-feed\", chainId, address, eventName, args, historyBlocks],\n    queryFn: async () => {\n      const latest = await client!.getBlockNumber();\n      const logs = await client!.getContractEvents({\n        address,\n        abi,\n        eventName,\n        args,\n        fromBlock: latest - BigInt(historyBlocks) + 1n,\n        toBlock: latest,\n      });\n      return logs as FeedEvent[];\n    },\n    enabled: Boolean(client) && historyBlocks > 0,\n    staleTime: Number.POSITIVE_INFINITY,\n    retry: false,\n  });\n\n  useWatchContractEvent({\n    address,\n    abi,\n    eventName,\n    args,\n    chainId,\n    onLogs: (logs: Log[]) => setLive((prev) => [...(logs as FeedEvent[]), ...prev].slice(0, max * 2)),\n  });\n\n  const events = useMemo(() => {\n    const seen = new Set<string>();\n    return [...live, ...(history.data ?? []), ...(initialEvents ?? [])]\n      .filter((log) => {\n        const id = logId(log);\n        if (seen.has(id)) return false;\n        seen.add(id);\n        return true;\n      })\n      .sort(newestFirst)\n      .slice(0, max);\n  }, [live, history.data, initialEvents, max]);\n\n  return { events, isLoadingHistory: history.isLoading, historyError: history.error };\n}\n\n/** Short display text for an event argument. */\nexport function formatEventArg(value: unknown): string {\n  if (typeof value === \"bigint\") return value.toLocaleString();\n  if (typeof value === \"string\" && isAddress(value)) return truncateAddress(value);\n  if (typeof value === \"string\" && value.startsWith(\"0x\") && value.length > 20)\n    return truncateAddress(value, { start: 10, end: 6 });\n  if (Array.isArray(value)) return `[${value.map(formatEventArg).join(\", \")}]`;\n  return String(value);\n}\n\nexport type EventFeedProps = UseEventFeedParameters & {\n  /** Custom row content. Defaults to the event name and its arguments. */\n  renderEvent?: (event: FeedEvent) => ReactNode;\n  title?: ReactNode;\n  className?: string;\n};\n\n/**\n * Live feed of a contract's events, e.g. Transfers or Swaps, newest first,\n * each linked to its transaction. Announced politely to screen readers as a\n * log.\n */\nexport function EventFeed({\n  renderEvent,\n  title = \"Live events\",\n  className,\n  ...params\n}: EventFeedProps) {\n  const { events, isLoadingHistory, historyError } = useEventFeed(params);\n\n  return (\n    <section\n      data-slot=\"event-feed\"\n      aria-label={typeof title === \"string\" ? title : \"Live events\"}\n      className={cn(\"flex flex-col gap-2 rounded-2xl border p-4\", className)}\n    >\n      <header className=\"flex items-center gap-2\">\n        <span aria-hidden className=\"size-2 animate-pulse rounded-full bg-emerald-500 motion-reduce:animate-none\" />\n        {title && <h3 className=\"font-semibold\">{title}</h3>}\n      </header>\n      {historyError && (\n        <p className=\"text-xs text-muted-foreground\">\n          Couldn't load past events. Showing new ones as they arrive.\n        </p>\n      )}\n      <ol role=\"log\" aria-live=\"polite\" className=\"flex flex-col divide-y\">\n        {events.length === 0 ? (\n          <li className=\"py-4 text-center text-sm text-muted-foreground\">\n            {isLoadingHistory ? \"Loading recent events…\" : \"Waiting for events…\"}\n          </li>\n        ) : (\n          events.map((event) => (\n            <li key={logId(event)} className=\"flex items-start gap-3 py-2 text-sm\">\n              <div className=\"min-w-0 flex-1\">\n                {renderEvent ? (\n                  renderEvent(event)\n                ) : (\n                  <>\n                    <span className=\"font-medium\">{event.eventName ?? \"Event\"}</span>\n                    {event.args && (\n                      <dl className=\"mt-0.5 flex flex-wrap gap-x-3 gap-y-0.5 text-xs text-muted-foreground\">\n                        {Object.entries(event.args).map(([key, value]) => (\n                          <div key={key} className=\"flex gap-1\">\n                            <dt>{key}</dt>\n                            <dd className=\"font-mono text-foreground\">{formatEventArg(value)}</dd>\n                          </div>\n                        ))}\n                      </dl>\n                    )}\n                  </>\n                )}\n              </div>\n              {event.transactionHash && (\n                <ExplorerLink\n                  tx={event.transactionHash}\n                  chainId={params.chainId}\n                  icon={false}\n                  className=\"shrink-0 text-xs text-muted-foreground\"\n                >\n                  {event.blockNumber !== null ? `#${event.blockNumber.toLocaleString()}` : \"View\"}\n                </ExplorerLink>\n              )}\n            </li>\n          ))\n        )}\n      </ol>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/event-feed.md",
  "categories": [
    "transactions"
  ],
  "type": "registry:component"
}