@veluai/velu 0.1.15 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +50 -23
- package/package.json +9 -3
- package/runtime/velu-ui/components/ApiClient.jsx +97 -11
- package/runtime/velu-ui/components/ApiReferencePage.jsx +384 -0
- package/runtime/velu-ui/components/ApiSamples.jsx +36 -0
- package/runtime/velu-ui/components/ContextMenu.jsx +272 -0
- package/runtime/velu-ui/components/NotFound.jsx +63 -0
- package/runtime/velu-ui/components/Sidebar.jsx +10 -2
- package/runtime/velu-ui/components/TryItBar.jsx +15 -3
- package/runtime/velu-ui/components/api-page.css +208 -0
- package/runtime/velu-ui/components/api.css +165 -9
- package/runtime/velu-ui/components/context-menu.css +170 -0
- package/runtime/velu-ui/components/docs-layout.css +18 -0
- package/runtime/velu-ui/components/not-found.css +94 -0
- package/runtime/velu-ui/components/powered-by.css +6 -0
- package/runtime/velu-ui/index.js +4 -0
- package/runtime/velu-ui/lib/api-send.js +92 -0
- package/runtime/velu-ui/lib/brand-icons.jsx +102 -0
- package/runtime/velu-ui/styles.css +3 -0
- package/schema/velu.schema.json +121 -0
- package/src/navigation.js +11 -2
- package/src/runtime/App.jsx +95 -6
- package/templates/starter/api-reference/introduction.mdx +29 -14
- package/templates/starter/openapi.json +160 -0
- package/templates/starter/velu.json +10 -2
- package/templates/starter/api-reference/endpoint/create.mdx +0 -24
- package/templates/starter/api-reference/endpoint/get.mdx +0 -27
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import React, { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import MethodBadge from './MethodBadge.jsx';
|
|
4
|
+
import ApiPath from './ApiPath.jsx';
|
|
5
|
+
import TryItBar from './TryItBar.jsx';
|
|
6
|
+
import ApiClient from './ApiClient.jsx';
|
|
7
|
+
import ApiField from './ApiField.jsx';
|
|
8
|
+
import Field from './Field.jsx';
|
|
9
|
+
import Accordion, { AccordionGroup } from './Accordion.jsx';
|
|
10
|
+
import CodeBlock, { CodeGroup } from './CodeBlock.jsx';
|
|
11
|
+
import ApiSamples from './ApiSamples.jsx';
|
|
12
|
+
import Callout from './Callout.jsx';
|
|
13
|
+
import { sendApiRequest } from '../lib/api-send.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* ApiReferencePage — the full, auto-generated reference page for one OpenAPI
|
|
17
|
+
* operation. Driven entirely by the normalized `operation` model (see the
|
|
18
|
+
* CLI's src/openapi/parse.js). Renders the doc body (auth, params, body,
|
|
19
|
+
* responses) and an interactive "Try It" playground that builds + sends the
|
|
20
|
+
* request (through the dev proxy by default — see lib/api-send.js).
|
|
21
|
+
*
|
|
22
|
+
* The right-rail code samples live in the page's right aside (the runtime
|
|
23
|
+
* renders <ApiSamples> there for API pages) — this component owns the main
|
|
24
|
+
* column + the playground only.
|
|
25
|
+
*
|
|
26
|
+
* @param {{ operation: object, api?: { proxy?: boolean, server?: string } }} props
|
|
27
|
+
*/
|
|
28
|
+
const LOCATION_LABEL = { path: 'Path Parameters', query: 'Query Parameters', header: 'Header Parameters' };
|
|
29
|
+
|
|
30
|
+
export default function ApiReferencePage({ operation, samples = [], apiOperations = [], api = {} }) {
|
|
31
|
+
const [open, setOpen] = useState(false);
|
|
32
|
+
// Which operation the playground is currently showing. Defaults to this
|
|
33
|
+
// page's own operation; the dropdown can switch it to any sibling.
|
|
34
|
+
const [activeId, setActiveId] = useState(operation.id);
|
|
35
|
+
|
|
36
|
+
// The switchable list — fall back to just this page's operation.
|
|
37
|
+
const ops =
|
|
38
|
+
apiOperations.length > 0
|
|
39
|
+
? apiOperations
|
|
40
|
+
: [{ id: operation.id, method: operation.method, title: operation.title, operation, samples, api }];
|
|
41
|
+
const current = ops.find((o) => o.id === activeId) || { operation, samples, api };
|
|
42
|
+
// Each operation carries its own (section-level) api config; fall back to
|
|
43
|
+
// this page's config.
|
|
44
|
+
const currentApi = current.api ?? api;
|
|
45
|
+
const server = currentApi.server || current.operation.servers?.[0] || '';
|
|
46
|
+
|
|
47
|
+
// While the playground modal is open, close on Escape and lock the
|
|
48
|
+
// page scroll so the dimmed backdrop reads as a true overlay.
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!open || typeof document === 'undefined') return undefined;
|
|
51
|
+
const onKey = (e) => {
|
|
52
|
+
if (e.key === 'Escape') setOpen(false);
|
|
53
|
+
};
|
|
54
|
+
document.addEventListener('keydown', onKey);
|
|
55
|
+
const prev = document.body.style.overflow;
|
|
56
|
+
document.body.style.overflow = 'hidden';
|
|
57
|
+
return () => {
|
|
58
|
+
document.removeEventListener('keydown', onKey);
|
|
59
|
+
document.body.style.overflow = prev;
|
|
60
|
+
};
|
|
61
|
+
}, [open]);
|
|
62
|
+
|
|
63
|
+
if (!operation) return null;
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div className="velu-api-page">
|
|
67
|
+
{/* The page title + description come from the page frontmatter heading
|
|
68
|
+
(rendered by the runtime), so this component starts at the Try-It bar
|
|
69
|
+
to avoid a duplicate title. */}
|
|
70
|
+
<TryItBar
|
|
71
|
+
method={operation.method}
|
|
72
|
+
path={operation.path}
|
|
73
|
+
cta="Try It"
|
|
74
|
+
onTry={() => {
|
|
75
|
+
setActiveId(operation.id);
|
|
76
|
+
setOpen(true);
|
|
77
|
+
}}
|
|
78
|
+
className="velu-api-page__tryit"
|
|
79
|
+
/>
|
|
80
|
+
|
|
81
|
+
{/* Authorizations */}
|
|
82
|
+
{operation.auth?.length > 0 && (
|
|
83
|
+
<section className="velu-api-page__section">
|
|
84
|
+
<h2>Authorizations</h2>
|
|
85
|
+
{operation.auth.map((a, i) => (
|
|
86
|
+
<Field key={i} name={a.name} pre={a.prefix} type={a.type} required={a.required} post={a.in}>
|
|
87
|
+
{a.description}
|
|
88
|
+
</Field>
|
|
89
|
+
))}
|
|
90
|
+
</section>
|
|
91
|
+
)}
|
|
92
|
+
|
|
93
|
+
{/* Parameters, by location */}
|
|
94
|
+
{['path', 'query', 'header'].map((loc) => {
|
|
95
|
+
const rows = operation.parameters?.[loc] || [];
|
|
96
|
+
if (!rows.length) return null;
|
|
97
|
+
return (
|
|
98
|
+
<section key={loc} className="velu-api-page__section">
|
|
99
|
+
<h2>{LOCATION_LABEL[loc]}</h2>
|
|
100
|
+
{rows.map((p, i) => (
|
|
101
|
+
<Field key={i} name={p.name} type={p.type} required={p.required}>
|
|
102
|
+
{p.description}
|
|
103
|
+
{p.enum && <EnumHint values={p.enum} />}
|
|
104
|
+
</Field>
|
|
105
|
+
))}
|
|
106
|
+
</section>
|
|
107
|
+
);
|
|
108
|
+
})}
|
|
109
|
+
|
|
110
|
+
{/* Request body */}
|
|
111
|
+
{operation.body?.fields?.length > 0 && (
|
|
112
|
+
<section className="velu-api-page__section">
|
|
113
|
+
<h2>Body</h2>
|
|
114
|
+
{operation.body.fields.map((f, i) => (
|
|
115
|
+
<Field key={i} name={f.name} type={f.type} required={f.required}>
|
|
116
|
+
{f.description}
|
|
117
|
+
{f.enum && <EnumHint values={f.enum} />}
|
|
118
|
+
</Field>
|
|
119
|
+
))}
|
|
120
|
+
</section>
|
|
121
|
+
)}
|
|
122
|
+
|
|
123
|
+
{/* Responses */}
|
|
124
|
+
{operation.responses?.length > 0 && (
|
|
125
|
+
<section className="velu-api-page__section">
|
|
126
|
+
<h2>Response</h2>
|
|
127
|
+
<ResponseSections responses={operation.responses} />
|
|
128
|
+
</section>
|
|
129
|
+
)}
|
|
130
|
+
|
|
131
|
+
{/* Try-It playground — rendered as an overlay above the page, with a
|
|
132
|
+
blurred backdrop, rather than inline. Portaled to <body> so the
|
|
133
|
+
docs layout's @container containment can't clip the fixed overlay. */}
|
|
134
|
+
{open && typeof document !== 'undefined' &&
|
|
135
|
+
createPortal(
|
|
136
|
+
<div
|
|
137
|
+
className="velu-api-modal"
|
|
138
|
+
role="presentation"
|
|
139
|
+
onClick={() => setOpen(false)}
|
|
140
|
+
>
|
|
141
|
+
<div
|
|
142
|
+
className="velu-api-modal__panel"
|
|
143
|
+
role="dialog"
|
|
144
|
+
aria-modal="true"
|
|
145
|
+
aria-label={`${operation.title} playground`}
|
|
146
|
+
onClick={(e) => e.stopPropagation()}
|
|
147
|
+
>
|
|
148
|
+
<Playground
|
|
149
|
+
key={current.operation.id}
|
|
150
|
+
operation={current.operation}
|
|
151
|
+
samples={current.samples}
|
|
152
|
+
operations={ops}
|
|
153
|
+
activeId={activeId}
|
|
154
|
+
onSelectOperation={setActiveId}
|
|
155
|
+
server={server}
|
|
156
|
+
proxy={currentApi.proxy !== false}
|
|
157
|
+
onClose={() => setOpen(false)}
|
|
158
|
+
/>
|
|
159
|
+
</div>
|
|
160
|
+
</div>,
|
|
161
|
+
document.body,
|
|
162
|
+
)}
|
|
163
|
+
</div>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function EnumHint({ values }) {
|
|
168
|
+
return (
|
|
169
|
+
<p className="velu-api-page__enum">
|
|
170
|
+
Allowed values:{' '}
|
|
171
|
+
{values.map((v, i) => (
|
|
172
|
+
<code key={i}>{String(v)}</code>
|
|
173
|
+
))}
|
|
174
|
+
</p>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function ResponseSections({ responses }) {
|
|
179
|
+
return (
|
|
180
|
+
<AccordionGroup className="velu-api-resp">
|
|
181
|
+
{responses.map((r, i) => {
|
|
182
|
+
return (
|
|
183
|
+
<Accordion
|
|
184
|
+
key={i}
|
|
185
|
+
defaultOpen
|
|
186
|
+
title={
|
|
187
|
+
<span className="velu-api-resp__head">
|
|
188
|
+
<span className="velu-api-resp__status">{r.status}</span>
|
|
189
|
+
{r.contentType && (
|
|
190
|
+
<span className="velu-api-resp__ctype">{r.contentType}</span>
|
|
191
|
+
)}
|
|
192
|
+
</span>
|
|
193
|
+
}
|
|
194
|
+
>
|
|
195
|
+
{r.description && <p className="velu-api-resp__desc">{r.description}</p>}
|
|
196
|
+
{r.fields?.map((f, j) => (
|
|
197
|
+
<Field key={j} name={f.name} type={f.type} required={f.required}>
|
|
198
|
+
{f.description}
|
|
199
|
+
</Field>
|
|
200
|
+
))}
|
|
201
|
+
</Accordion>
|
|
202
|
+
);
|
|
203
|
+
})}
|
|
204
|
+
</AccordionGroup>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ── The interactive playground ──────────────────────────────────────────────
|
|
209
|
+
|
|
210
|
+
function Playground({
|
|
211
|
+
operation,
|
|
212
|
+
samples = [],
|
|
213
|
+
operations = [],
|
|
214
|
+
activeId,
|
|
215
|
+
onSelectOperation,
|
|
216
|
+
server,
|
|
217
|
+
proxy,
|
|
218
|
+
onClose,
|
|
219
|
+
}) {
|
|
220
|
+
const [values, setValues] = useState({});
|
|
221
|
+
const [bodyText, setBodyText] = useState(
|
|
222
|
+
operation.body ? JSON.stringify(operation.body.example ?? {}, null, 2) : '',
|
|
223
|
+
);
|
|
224
|
+
const [busy, setBusy] = useState(false);
|
|
225
|
+
const [resp, setResp] = useState(null);
|
|
226
|
+
|
|
227
|
+
const set = (loc, name, v) =>
|
|
228
|
+
setValues((prev) => ({ ...prev, [loc]: { ...(prev[loc] || {}), [name]: v } }));
|
|
229
|
+
|
|
230
|
+
const onSend = useCallback(async () => {
|
|
231
|
+
setBusy(true);
|
|
232
|
+
setResp(null);
|
|
233
|
+
try {
|
|
234
|
+
const result = await sendApiRequest({ operation, server, proxy, values, bodyText });
|
|
235
|
+
setResp(result);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
setResp({ error: String(err?.message || err) });
|
|
238
|
+
} finally {
|
|
239
|
+
setBusy(false);
|
|
240
|
+
}
|
|
241
|
+
}, [operation, server, proxy, values, bodyText]);
|
|
242
|
+
|
|
243
|
+
const sections = [];
|
|
244
|
+
if (operation.auth?.length) sections.push(['auth', 'Authorization', operation.auth]);
|
|
245
|
+
for (const loc of ['path', 'query', 'header']) {
|
|
246
|
+
const rows = operation.parameters?.[loc] || [];
|
|
247
|
+
if (rows.length) sections.push([loc, LOCATION_LABEL[loc].replace(' Parameters', ''), rows]);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return (
|
|
251
|
+
<ApiClient
|
|
252
|
+
method={operation.method}
|
|
253
|
+
label={operation.title}
|
|
254
|
+
path={operation.path}
|
|
255
|
+
description={operation.description}
|
|
256
|
+
operations={operations}
|
|
257
|
+
activeId={activeId}
|
|
258
|
+
onSelectOperation={onSelectOperation}
|
|
259
|
+
onSend={onSend}
|
|
260
|
+
sending={busy}
|
|
261
|
+
onClose={onClose}
|
|
262
|
+
response={resp ? <ResponsePanel resp={resp} /> : null}
|
|
263
|
+
aside={<ApiSamples samples={samples} responses={operation.responses} />}
|
|
264
|
+
>
|
|
265
|
+
<AccordionGroup>
|
|
266
|
+
{sections.map(([loc, title, rows]) => (
|
|
267
|
+
<Accordion key={loc} title={title} defaultOpen>
|
|
268
|
+
{rows.map((f, i) => (
|
|
269
|
+
<ApiField
|
|
270
|
+
key={i}
|
|
271
|
+
name={f.name}
|
|
272
|
+
type={f.type}
|
|
273
|
+
required={f.required}
|
|
274
|
+
prefix={f.prefix}
|
|
275
|
+
value={values[loc]?.[f.name] ?? ''}
|
|
276
|
+
onChange={(e) => set(loc, f.name, e.target.value)}
|
|
277
|
+
>
|
|
278
|
+
{f.description}
|
|
279
|
+
</ApiField>
|
|
280
|
+
))}
|
|
281
|
+
</Accordion>
|
|
282
|
+
))}
|
|
283
|
+
{operation.body && (
|
|
284
|
+
<Accordion title="Body" defaultOpen>
|
|
285
|
+
<textarea
|
|
286
|
+
className="velu-api-body-input"
|
|
287
|
+
spellCheck={false}
|
|
288
|
+
rows={Math.min(14, bodyText.split('\n').length + 1)}
|
|
289
|
+
value={bodyText}
|
|
290
|
+
onChange={(e) => setBodyText(e.target.value)}
|
|
291
|
+
/>
|
|
292
|
+
</Accordion>
|
|
293
|
+
)}
|
|
294
|
+
</AccordionGroup>
|
|
295
|
+
</ApiClient>
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* ResponsePanel — the live-response viewer: a Body / Header tabbed card.
|
|
301
|
+
* - Body: a status-colored Callout (green for 2xx, red otherwise) plus the
|
|
302
|
+
* response body.
|
|
303
|
+
* - Header: the response headers as a key/value table.
|
|
304
|
+
*/
|
|
305
|
+
function ResponsePanel({ resp }) {
|
|
306
|
+
const [tab, setTab] = useState('body');
|
|
307
|
+
if (!resp) return null;
|
|
308
|
+
|
|
309
|
+
const isError = resp.error != null;
|
|
310
|
+
const ok = !isError && resp.status >= 200 && resp.status < 300;
|
|
311
|
+
const statusLabel = isError
|
|
312
|
+
? 'Request failed'
|
|
313
|
+
: `${resp.status}${resp.statusText ? ` - ${resp.statusText}` : ''}`;
|
|
314
|
+
const bodyStr = isError
|
|
315
|
+
? resp.error
|
|
316
|
+
: typeof resp.body === 'string'
|
|
317
|
+
? resp.body
|
|
318
|
+
: JSON.stringify(resp.body, null, 2);
|
|
319
|
+
const headerRows = Object.entries(resp.headers || {});
|
|
320
|
+
|
|
321
|
+
// Reuse the CodeGroup's tab chrome (velu-code-block__*) so the Body/Header
|
|
322
|
+
// switcher looks identical to the code-sample tabs.
|
|
323
|
+
const tabCls = (name) =>
|
|
324
|
+
`velu-code-block__tab${tab === name ? ' velu-code-block__tab--active' : ''}`;
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<div className="velu-code-block velu-api-resp-panel">
|
|
328
|
+
<div className="velu-code-block__header">
|
|
329
|
+
<div className="velu-code-block__tabs" role="tablist">
|
|
330
|
+
<button
|
|
331
|
+
type="button"
|
|
332
|
+
role="tab"
|
|
333
|
+
aria-selected={tab === 'body'}
|
|
334
|
+
className={tabCls('body')}
|
|
335
|
+
onClick={() => setTab('body')}
|
|
336
|
+
>
|
|
337
|
+
<span className="velu-code-block__tab-label">Body</span>
|
|
338
|
+
</button>
|
|
339
|
+
<button
|
|
340
|
+
type="button"
|
|
341
|
+
role="tab"
|
|
342
|
+
aria-selected={tab === 'header'}
|
|
343
|
+
className={tabCls('header')}
|
|
344
|
+
onClick={() => setTab('header')}
|
|
345
|
+
disabled={!headerRows.length || undefined}
|
|
346
|
+
>
|
|
347
|
+
<span className="velu-code-block__tab-label">Header</span>
|
|
348
|
+
</button>
|
|
349
|
+
</div>
|
|
350
|
+
</div>
|
|
351
|
+
|
|
352
|
+
<div className="velu-api-resp-panel__content">
|
|
353
|
+
{tab === 'body' ? (
|
|
354
|
+
<>
|
|
355
|
+
<Callout
|
|
356
|
+
type={ok ? 'check' : 'danger'}
|
|
357
|
+
className="velu-api-resp-panel__status"
|
|
358
|
+
>
|
|
359
|
+
{statusLabel}
|
|
360
|
+
</Callout>
|
|
361
|
+
{!isError && (
|
|
362
|
+
<div className="velu-api-resp-body__scroll velu-hide-scrollbar">
|
|
363
|
+
<CodeBlock language="json">{bodyStr || '(empty)'}</CodeBlock>
|
|
364
|
+
</div>
|
|
365
|
+
)}
|
|
366
|
+
</>
|
|
367
|
+
) : (
|
|
368
|
+
<div className="velu-api-resp-headers__scroll velu-hide-scrollbar">
|
|
369
|
+
<table className="velu-api-resp-headers">
|
|
370
|
+
<tbody>
|
|
371
|
+
{headerRows.map(([k, val]) => (
|
|
372
|
+
<tr key={k}>
|
|
373
|
+
<td className="velu-api-resp-headers__key">{k}</td>
|
|
374
|
+
<td className="velu-api-resp-headers__val">{val}</td>
|
|
375
|
+
</tr>
|
|
376
|
+
))}
|
|
377
|
+
</tbody>
|
|
378
|
+
</table>
|
|
379
|
+
</div>
|
|
380
|
+
)}
|
|
381
|
+
</div>
|
|
382
|
+
</div>
|
|
383
|
+
);
|
|
384
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import CodeBlock, { CodeGroup } from './CodeBlock.jsx';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ApiSamples — the right-rail of an API reference page: the request code
|
|
6
|
+
* snippets (curl / JS / Python / Ruby tabs) and the example responses
|
|
7
|
+
* (one tab per status). The runtime renders this in the page's right aside
|
|
8
|
+
* (where the TOC sits for normal pages).
|
|
9
|
+
*
|
|
10
|
+
* @param {{ samples: {key,label,language,code}[], responses: object[] }} props
|
|
11
|
+
*/
|
|
12
|
+
export default function ApiSamples({ samples = [], responses = [] }) {
|
|
13
|
+
const withExample = responses.filter((r) => r.example != null);
|
|
14
|
+
return (
|
|
15
|
+
<div className="velu-api-samples">
|
|
16
|
+
{samples.length > 0 && (
|
|
17
|
+
<CodeGroup className="velu-api-samples__req">
|
|
18
|
+
{samples.map((s) => (
|
|
19
|
+
<CodeBlock key={s.key} title={s.label} language={s.language}>
|
|
20
|
+
{s.code}
|
|
21
|
+
</CodeBlock>
|
|
22
|
+
))}
|
|
23
|
+
</CodeGroup>
|
|
24
|
+
)}
|
|
25
|
+
{withExample.length > 0 && (
|
|
26
|
+
<CodeGroup className="velu-api-samples__res">
|
|
27
|
+
{withExample.map((r) => (
|
|
28
|
+
<CodeBlock key={r.status} title={r.status} language="json">
|
|
29
|
+
{JSON.stringify(r.example, null, 2)}
|
|
30
|
+
</CodeBlock>
|
|
31
|
+
))}
|
|
32
|
+
</CodeGroup>
|
|
33
|
+
)}
|
|
34
|
+
</div>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Copy,
|
|
4
|
+
Check,
|
|
5
|
+
ChevronDown,
|
|
6
|
+
ArrowUpRight,
|
|
7
|
+
Sparkles,
|
|
8
|
+
Terminal,
|
|
9
|
+
Download,
|
|
10
|
+
Bot,
|
|
11
|
+
Wind,
|
|
12
|
+
} from 'lucide-react';
|
|
13
|
+
import Cluster from '../primitives/Cluster.jsx';
|
|
14
|
+
import {
|
|
15
|
+
MarkdownIcon,
|
|
16
|
+
OpenAIIcon,
|
|
17
|
+
ClaudeIcon,
|
|
18
|
+
PerplexityIcon,
|
|
19
|
+
CursorIcon,
|
|
20
|
+
VscodeIcon,
|
|
21
|
+
} from '../lib/brand-icons.jsx';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* ContextMenu — the per-page agent/IDE action bar shown at the top of every
|
|
25
|
+
* page: the section eyebrow on the left, and a "Copy Page" split-button with a
|
|
26
|
+
* dropdown on the right. The dropdown items are driven by the Mintlify-
|
|
27
|
+
* compatible `contextual.options` config.
|
|
28
|
+
*
|
|
29
|
+
* The primary button copies the page's Markdown (the `.md` twin). Items either
|
|
30
|
+
* copy, open the Markdown, download the spec, or open the page in an AI tool /
|
|
31
|
+
* IDE. All side effects happen in click handlers (SSR-safe — no window at
|
|
32
|
+
* render).
|
|
33
|
+
*
|
|
34
|
+
* @param {{
|
|
35
|
+
* eyebrow?: string, // section/group label
|
|
36
|
+
* pageUrl: string, // page path, e.g. "/quickstart"
|
|
37
|
+
* title?: string, // page title (for AI prompts / filenames)
|
|
38
|
+
* isApi?: boolean, // API page → enable download-spec
|
|
39
|
+
* siteName?: string, // for MCP deep-link labels
|
|
40
|
+
* options?: Array<string|{title,description,href,icon}>,
|
|
41
|
+
* onAssistant?: () => void, // 'assistant' option → in-site Ask AI
|
|
42
|
+
* }} props
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
// The Markdown URL for a page path (`/` → `/index.md`).
|
|
46
|
+
const mdUrlForPage = (url) => (url === '/' ? '/index.md' : `${url}.md`);
|
|
47
|
+
|
|
48
|
+
// key → { icon, label, desc, external?, apiOnly?, kind, url? }
|
|
49
|
+
const REGISTRY = {
|
|
50
|
+
copy: { icon: Copy, label: 'Copy page', desc: 'Copy page as Markdown for LLMs', kind: 'copy' },
|
|
51
|
+
view: { icon: MarkdownIcon, label: 'View as Markdown', desc: 'Open the raw Markdown', kind: 'view', external: true },
|
|
52
|
+
'download-spec': { icon: Download, label: 'Download OpenAPI spec', desc: 'Save this endpoint as YAML', kind: 'spec', apiOnly: true },
|
|
53
|
+
assistant: { icon: Sparkles, label: 'Ask AI', desc: 'Ask the docs assistant about this page', kind: 'assistant' },
|
|
54
|
+
chatgpt: { icon: OpenAIIcon, label: 'Open in ChatGPT', desc: 'Ask questions about this page', kind: 'ai', external: true, url: (q) => `https://chatgpt.com/?hints=search&q=${q}` },
|
|
55
|
+
claude: { icon: ClaudeIcon, label: 'Open in Claude', desc: 'Ask questions about this page', kind: 'ai', external: true, url: (q) => `https://claude.ai/new?q=${q}` },
|
|
56
|
+
perplexity: { icon: PerplexityIcon, label: 'Open in Perplexity', desc: 'Ask questions about this page', kind: 'ai', external: true, url: (q) => `https://www.perplexity.ai/search?q=${q}` },
|
|
57
|
+
grok: { icon: Bot, label: 'Open in Grok', desc: 'Ask questions about this page', kind: 'ai', external: true, url: (q) => `https://grok.com/?q=${q}` },
|
|
58
|
+
aistudio: { icon: Sparkles, label: 'Open in AI Studio', desc: 'Ask questions about this page', kind: 'ai', external: true, url: (q) => `https://aistudio.google.com/app/prompts/new_chat?prompt=${q}` },
|
|
59
|
+
devin: { icon: Bot, label: 'Open in Devin', desc: 'Ask questions about this page', kind: 'ai', external: true, url: (q) => `https://app.devin.ai/?prompt=${q}` },
|
|
60
|
+
windsurf: { icon: Wind, label: 'Open in Windsurf', desc: 'Ask questions about this page', kind: 'ai', external: true, url: (q) => `https://windsurf.com/?q=${q}` },
|
|
61
|
+
mcp: { icon: Terminal, label: 'Copy MCP install command', desc: 'Copy npx command to install MCP server', kind: 'mcp' },
|
|
62
|
+
'add-mcp': { icon: Terminal, label: 'Add MCP server', desc: 'Copy command to add the MCP server', kind: 'mcp' },
|
|
63
|
+
cursor: { icon: CursorIcon, label: 'Connect to Cursor', desc: 'Install MCP Server on Cursor', kind: 'cursor', external: true },
|
|
64
|
+
vscode: { icon: VscodeIcon, label: 'Connect to VS Code', desc: 'Install MCP Server on VS Code', kind: 'vscode', external: true },
|
|
65
|
+
'devin-mcp': { icon: Terminal, label: 'Connect to Devin', desc: 'Install MCP Server on Devin', kind: 'cursor', external: true },
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export default function ContextMenu({
|
|
69
|
+
eyebrow,
|
|
70
|
+
pageUrl,
|
|
71
|
+
title = '',
|
|
72
|
+
isApi = false,
|
|
73
|
+
siteName = 'docs',
|
|
74
|
+
options = [],
|
|
75
|
+
onAssistant,
|
|
76
|
+
}) {
|
|
77
|
+
const [open, setOpen] = React.useState(false);
|
|
78
|
+
const [copied, setCopied] = React.useState(false);
|
|
79
|
+
const rootRef = React.useRef(null);
|
|
80
|
+
const copiedTimer = React.useRef(null);
|
|
81
|
+
|
|
82
|
+
React.useEffect(() => {
|
|
83
|
+
if (!open) return;
|
|
84
|
+
const onDoc = (e) => {
|
|
85
|
+
if (!rootRef.current?.contains(e.target)) setOpen(false);
|
|
86
|
+
};
|
|
87
|
+
const onKey = (e) => {
|
|
88
|
+
if (e.key === 'Escape') setOpen(false);
|
|
89
|
+
};
|
|
90
|
+
document.addEventListener('mousedown', onDoc);
|
|
91
|
+
document.addEventListener('keydown', onKey);
|
|
92
|
+
return () => {
|
|
93
|
+
document.removeEventListener('mousedown', onDoc);
|
|
94
|
+
document.removeEventListener('keydown', onKey);
|
|
95
|
+
};
|
|
96
|
+
}, [open]);
|
|
97
|
+
|
|
98
|
+
React.useEffect(() => () => clearTimeout(copiedTimer.current), []);
|
|
99
|
+
|
|
100
|
+
const flashCopied = () => {
|
|
101
|
+
setCopied(true);
|
|
102
|
+
clearTimeout(copiedTimer.current);
|
|
103
|
+
copiedTimer.current = setTimeout(() => setCopied(false), 1600);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const mdUrl = mdUrlForPage(pageUrl);
|
|
107
|
+
|
|
108
|
+
// Build the descriptor list from the configured options (skip API-only items
|
|
109
|
+
// off API pages, and any unsupported keys).
|
|
110
|
+
const items = [];
|
|
111
|
+
for (const o of options) {
|
|
112
|
+
if (typeof o === 'object' && o) {
|
|
113
|
+
items.push({ icon: ArrowUpRight, label: o.title, desc: o.description, external: true, kind: 'custom', href: o.href });
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const def = REGISTRY[o];
|
|
117
|
+
if (!def) continue; // e.g. download-pdf — no static pipeline
|
|
118
|
+
if (def.apiOnly && !isApi) continue;
|
|
119
|
+
items.push({ ...def, key: o });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const copyText = async (text) => {
|
|
123
|
+
try {
|
|
124
|
+
await navigator.clipboard.writeText(text);
|
|
125
|
+
flashCopied();
|
|
126
|
+
} catch {
|
|
127
|
+
/* clipboard blocked — no-op */
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const copyPage = async () => {
|
|
132
|
+
try {
|
|
133
|
+
const md = await fetch(mdUrl).then((r) => r.text());
|
|
134
|
+
await copyText(md);
|
|
135
|
+
} catch {
|
|
136
|
+
/* fetch failed — no-op */
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const run = async (it) => {
|
|
141
|
+
setOpen(false);
|
|
142
|
+
const origin = window.location.origin;
|
|
143
|
+
const absMd = origin + mdUrl;
|
|
144
|
+
switch (it.kind) {
|
|
145
|
+
case 'copy':
|
|
146
|
+
return copyPage();
|
|
147
|
+
case 'view':
|
|
148
|
+
return void window.open(mdUrl, '_blank', 'noopener');
|
|
149
|
+
case 'spec': {
|
|
150
|
+
try {
|
|
151
|
+
const text = await fetch(mdUrl).then((r) => r.text());
|
|
152
|
+
const blob = new Blob([text], { type: 'text/yaml' });
|
|
153
|
+
const a = document.createElement('a');
|
|
154
|
+
a.href = URL.createObjectURL(blob);
|
|
155
|
+
a.download = `${(title || 'openapi').replace(/[^a-z0-9]+/gi, '-').toLowerCase()}.yaml`;
|
|
156
|
+
a.click();
|
|
157
|
+
URL.revokeObjectURL(a.href);
|
|
158
|
+
} catch {
|
|
159
|
+
/* no-op */
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
case 'assistant':
|
|
164
|
+
return onAssistant?.();
|
|
165
|
+
case 'ai': {
|
|
166
|
+
const q = encodeURIComponent(`Read ${absMd} and answer my questions about this page.`);
|
|
167
|
+
return void window.open(it.url(q), '_blank', 'noopener');
|
|
168
|
+
}
|
|
169
|
+
case 'mcp':
|
|
170
|
+
// Stubbed: the Velu MCP server isn't hosted yet — this is the intended
|
|
171
|
+
// command, wired to <origin>/mcp once it exists.
|
|
172
|
+
return copyText(`npx -y @veluai/mcp@latest ${origin}/mcp`);
|
|
173
|
+
case 'cursor': {
|
|
174
|
+
const cfg =
|
|
175
|
+
typeof btoa === 'function' ? btoa(JSON.stringify({ url: `${origin}/mcp` })) : '';
|
|
176
|
+
return void window.open(
|
|
177
|
+
`cursor://anysphere.cursor-deeplink/mcp/install?name=${encodeURIComponent(siteName)}&config=${cfg}`,
|
|
178
|
+
'_blank',
|
|
179
|
+
'noopener',
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
case 'vscode': {
|
|
183
|
+
const cfg = encodeURIComponent(JSON.stringify({ name: siteName, url: `${origin}/mcp` }));
|
|
184
|
+
return void window.open(
|
|
185
|
+
`https://insiders.vscode.dev/redirect/mcp/install?${cfg}`,
|
|
186
|
+
'_blank',
|
|
187
|
+
'noopener',
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
case 'custom':
|
|
191
|
+
return void window.open(it.href, '_blank', 'noopener');
|
|
192
|
+
default:
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Nothing to show → render nothing (keeps the page top clean).
|
|
198
|
+
if (!eyebrow && !items.length) return null;
|
|
199
|
+
|
|
200
|
+
return (
|
|
201
|
+
<Cluster
|
|
202
|
+
space="var(--s-2)"
|
|
203
|
+
justify="space-between"
|
|
204
|
+
align="flex-end"
|
|
205
|
+
className="velu-context-bar"
|
|
206
|
+
>
|
|
207
|
+
{eyebrow ? <span className="velu-context-bar__eyebrow">{eyebrow}</span> : <span />}
|
|
208
|
+
|
|
209
|
+
{items.length > 0 && (
|
|
210
|
+
<div ref={rootRef} className="velu-context-menu" data-open={open ? 'true' : 'false'}>
|
|
211
|
+
<div className="velu-context-menu__split">
|
|
212
|
+
<button
|
|
213
|
+
type="button"
|
|
214
|
+
className="velu-context-menu__copy"
|
|
215
|
+
onClick={copyPage}
|
|
216
|
+
aria-label="Copy page as Markdown"
|
|
217
|
+
>
|
|
218
|
+
<span className="velu-context-menu__copy-icon" aria-hidden="true">
|
|
219
|
+
{copied ? <Check size="1em" /> : <Copy size="1em" />}
|
|
220
|
+
</span>
|
|
221
|
+
<span>{copied ? 'Copied' : 'Copy Page'}</span>
|
|
222
|
+
</button>
|
|
223
|
+
<button
|
|
224
|
+
type="button"
|
|
225
|
+
className="velu-context-menu__toggle"
|
|
226
|
+
aria-haspopup="menu"
|
|
227
|
+
aria-expanded={open}
|
|
228
|
+
aria-label="More actions"
|
|
229
|
+
onClick={() => setOpen((v) => !v)}
|
|
230
|
+
>
|
|
231
|
+
<ChevronDown size="1em" aria-hidden="true" focusable="false" />
|
|
232
|
+
</button>
|
|
233
|
+
</div>
|
|
234
|
+
|
|
235
|
+
<ul className="velu-context-menu__menu" role="menu" aria-hidden={!open}>
|
|
236
|
+
{items.map((it, i) => {
|
|
237
|
+
const Icon = it.icon;
|
|
238
|
+
return (
|
|
239
|
+
<li key={it.key ?? it.href ?? i} role="none">
|
|
240
|
+
<button
|
|
241
|
+
type="button"
|
|
242
|
+
role="menuitem"
|
|
243
|
+
className="velu-context-menu__item"
|
|
244
|
+
tabIndex={open ? 0 : -1}
|
|
245
|
+
onClick={() => run(it)}
|
|
246
|
+
>
|
|
247
|
+
<span className="velu-context-menu__item-icon" aria-hidden="true">
|
|
248
|
+
<Icon size="1.1em" />
|
|
249
|
+
</span>
|
|
250
|
+
<span className="velu-context-menu__item-text">
|
|
251
|
+
<span className="velu-context-menu__item-title">
|
|
252
|
+
{it.label}
|
|
253
|
+
{it.external && (
|
|
254
|
+
<ArrowUpRight
|
|
255
|
+
className="velu-context-menu__item-ext"
|
|
256
|
+
size="0.85em"
|
|
257
|
+
aria-hidden="true"
|
|
258
|
+
/>
|
|
259
|
+
)}
|
|
260
|
+
</span>
|
|
261
|
+
{it.desc && <span className="velu-context-menu__item-desc">{it.desc}</span>}
|
|
262
|
+
</span>
|
|
263
|
+
</button>
|
|
264
|
+
</li>
|
|
265
|
+
);
|
|
266
|
+
})}
|
|
267
|
+
</ul>
|
|
268
|
+
</div>
|
|
269
|
+
)}
|
|
270
|
+
</Cluster>
|
|
271
|
+
);
|
|
272
|
+
}
|