|
1
|
"use client" |
|
2
|
|
|
3
|
import React from "react" |
|
4
|
import type { DocEvent } from "types" |
|
5
|
import { Table } from "@/components/Table" |
|
6
|
import { Badge } from "@/components/Badge" |
|
7
|
import { Tooltip } from "@/components/Tooltip" |
|
8
|
import { CodeBlock } from "@/components/CodeBlock" |
|
9
|
import { CopyGeneratedSnippetButton } from "@/components/CopyGeneratedSnippetButton" |
|
10
|
import { MarkdownContent } from "@/components/MarkdownContent" |
|
11
|
import { Mdx, DocHeading } from "./shared" |
|
12
|
|
|
13
|
export type ReferenceEventsListingProps = { |
|
14
|
categories: { title?: string; events: DocEvent[] }[] |
|
15
|
} |
|
16
|
|
|
17
|
const FENCE_RE = /```(\w+)?\n?([\s\S]*?)```/ |
|
18
|
|
|
19
|
function EventBadges({ event }: { event: DocEvent }) { |
|
20
|
return ( |
|
21
|
<> |
|
22
|
{event.deprecated ? ( |
|
23
|
<Tooltip text={event.deprecatedMessage || "Deprecated"}> |
|
24
|
<Badge variant="orange">Deprecated</Badge> |
|
25
|
</Tooltip> |
|
26
|
) : null} |
|
27
|
{event.since ? ( |
|
28
|
<Tooltip text={`This event was added in version v${event.since}`}> |
|
29
|
<Badge variant="blue">v{event.since}</Badge> |
|
30
|
</Tooltip> |
|
31
|
) : null} |
|
32
|
</> |
|
33
|
) |
|
34
|
} |
|
35
|
|
|
36
|
function EventDetail({ event, level }: { event: DocEvent; level: number }) { |
|
37
|
const fence = event.payload?.match(FENCE_RE) |
|
38
|
const { ul: Ul, li: Li, a: Anchor } = Mdx |
|
39
|
|
|
40
|
return ( |
|
41
|
<> |
|
42
|
<div className="flex items-center justify-between flex-wrap"> |
|
43
|
<DocHeading |
|
44
|
level={level} |
|
45
|
id={event.id} |
|
46
|
className="flex flex-wrap justify-center gap-docs_0.25" |
|
47
|
> |
|
48
|
{event.name} |
|
49
|
<EventBadges event={event} /> |
|
50
|
</DocHeading> |
|
51
|
<CopyGeneratedSnippetButton |
|
52
|
tooltipText="Copy subscriber for event" |
|
53
|
type="subscriber" |
|
54
|
options={{ event: event.name, payload: event.payload || "" }} |
|
55
|
/> |
|
56
|
</div> |
|
57
|
{event.description ? ( |
|
58
|
<MarkdownContent>{event.description}</MarkdownContent> |
|
59
|
) : null} |
|
60
|
{event.payload ? ( |
|
61
|
<> |
|
62
|
<DocHeading level={level + 1} id={`${event.id}-payload`}> |
|
63
|
Payload |
|
64
|
</DocHeading> |
|
65
|
<CodeBlock |
|
66
|
source={(fence?.[2] ?? event.payload).trim()} |
|
67
|
lang={fence?.[1] || "ts"} |
|
68
|
/> |
|
69
|
</> |
|
70
|
) : null} |
|
71
|
{event.workflows?.length ? ( |
|
72
|
<> |
|
73
|
<DocHeading level={level + 1} id={`${event.id}-workflows`}> |
|
74
|
Workflows Emitting this Event |
|
75
|
</DocHeading> |
|
76
|
<Ul> |
|
77
|
{event.workflows.map((workflow, index) => ( |
|
78
|
<Li key={`${workflow.name}-${index}`}> |
|
79
|
<Anchor href={workflow.href}>{workflow.name}</Anchor> |
|
80
|
</Li> |
|
81
|
))} |
|
82
|
</Ul> |
|
83
|
</> |
|
84
|
) : null} |
|
85
|
</> |
|
86
|
) |
|
87
|
} |
|
88
|
|
|
89
|
/** |
|
90
|
* Renders the full events reference (the `eventsListing` block): a per-category |
|
91
|
* summary table followed by each event's details (payload, emitting workflows). |
|
92
|
*/ |
|
93
|
export const ReferenceEventsListing = ({ |
|
94
|
categories, |
|
95
|
}: ReferenceEventsListingProps) => { |
|
96
|
const multi = categories.length > 1 |
|
97
|
const summaryLevel = multi ? 3 : 2 |
|
98
|
const eventLevel = multi ? 3 : 2 |
|
99
|
const { code: Code, a: Anchor } = Mdx |
|
100
|
|
|
101
|
return ( |
|
102
|
<> |
|
103
|
{categories.map((category, index) => { |
|
104
|
const categorySlug = |
|
105
|
(category.title || `category-${index}`) |
|
106
|
.toLowerCase() |
|
107
|
.replace(/[^a-z0-9]+/g, "-") |
|
108
|
.replace(/^-+|-+$/g, "") || `category-${index}` |
|
109
|
return ( |
|
110
|
<React.Fragment key={index}> |
|
111
|
{multi && category.title ? ( |
|
112
|
<DocHeading level={2} id={`${categorySlug}-events`}> |
|
113
|
{category.title} Events |
|
114
|
</DocHeading> |
|
115
|
) : null} |
|
116
|
<DocHeading level={summaryLevel} id={`${categorySlug}-summary`}> |
|
117
|
Summary |
|
118
|
</DocHeading> |
|
119
|
<Table> |
|
120
|
<Table.Header> |
|
121
|
<Table.Row> |
|
122
|
<Table.HeaderCell>Event</Table.HeaderCell> |
|
123
|
<Table.HeaderCell>Description</Table.HeaderCell> |
|
124
|
</Table.Row> |
|
125
|
</Table.Header> |
|
126
|
<Table.Body> |
|
127
|
{category.events.map((event) => ( |
|
128
|
<Table.Row key={event.id}> |
|
129
|
<Table.Cell> |
|
130
|
<Anchor href={`#${event.id}`}> |
|
131
|
<Code>{event.name}</Code> |
|
132
|
</Anchor>{" "} |
|
133
|
<EventBadges event={event} /> |
|
134
|
</Table.Cell> |
|
135
|
<Table.Cell> |
|
136
|
{event.description ? ( |
|
137
|
<MarkdownContent>{event.description}</MarkdownContent> |
|
138
|
) : null} |
|
139
|
</Table.Cell> |
|
140
|
</Table.Row> |
|
141
|
))} |
|
142
|
</Table.Body> |
|
143
|
</Table> |
|
144
|
{category.events.map((event) => ( |
|
145
|
<EventDetail key={event.id} event={event} level={eventLevel} /> |
|
146
|
))} |
|
147
|
</React.Fragment> |
|
148
|
) |
|
149
|
})} |
|
150
|
</> |
|
151
|
) |
|
152
|
} |