forked from programmablehq/PROGRAMMABLE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-code-preview.tsx
More file actions
252 lines (230 loc) · 7.11 KB
/
Copy pathdocs-code-preview.tsx
File metadata and controls
252 lines (230 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"use client";
import { Check, CircleAlert, Copy } from "lucide-react";
import {
useEffect,
useRef,
useState,
type KeyboardEvent,
} from "react";
import styles from "@/components/docs-experience.module.css";
type CopyState = "idle" | "copied" | "error";
type LaunchSample = {
id: string;
label: string;
caption: string;
record: Record<string, unknown>;
};
const sampleToken = "0x1111111111111111111111111111111111111111";
export const docsLaunchSamples: readonly LaunchSample[] = [
{
id: "classic",
label: "Classic",
caption: "Classic · live pool",
record: {
launchId: `eip155:1:${sampleToken}`,
category: "classic",
token: { address: sampleToken, symbol: "EXAMPLE" },
launch: { status: "live", finality: "finalized" },
markets: [{ kind: "uniswap-v4", status: "active" }],
},
},
{
id: "custom-pool",
label: "Custom pool",
caption: "Custom · planned pool",
record: {
launchId: `eip155:1:${sampleToken}`,
category: "custom",
token: { address: sampleToken, symbol: "EXAMPLE" },
launch: { status: "prelaunch", finality: null },
markets: [{ kind: "uniswap-v4", status: "planned" }],
},
},
{
id: "no-pool",
label: "No pool",
caption: "Custom · no registered market",
record: {
launchId: `eip155:1:${sampleToken}`,
category: "custom",
token: { address: sampleToken, symbol: "EXAMPLE" },
launch: { status: "prelaunch", finality: null },
markets: [],
},
},
{
id: "contract-market",
label: "Contract market",
caption: "Custom · contract-defined market",
record: {
launchId: `eip155:1:${sampleToken}`,
category: "custom",
token: { address: sampleToken, symbol: "EXAMPLE" },
launch: { status: "prelaunch", finality: null },
markets: [{ kind: "contract-market", status: "planned" }],
},
},
] as const;
export function getNextDocsSampleIndex(
currentIndex: number,
key: string,
sampleCount = docsLaunchSamples.length,
): number {
if (sampleCount <= 0) return -1;
if (key === "Home") return 0;
if (key === "End") return sampleCount - 1;
if (key === "ArrowRight" || key === "ArrowDown") {
return (currentIndex + 1) % sampleCount;
}
if (key === "ArrowLeft" || key === "ArrowUp") {
return (currentIndex - 1 + sampleCount) % sampleCount;
}
return currentIndex;
}
export function getDocsCopyStatus(label: string, state: CopyState): string {
if (state === "copied") return `${label} copied`;
if (state === "error") return `Could not copy ${label}`;
return "";
}
function CopyButton({ label, text }: { label: string; text: string }) {
const [state, setState] = useState<CopyState>("idle");
const resetTimerRef = useRef<number | null>(null);
useEffect(
() => () => {
if (resetTimerRef.current !== null) {
window.clearTimeout(resetTimerRef.current);
}
},
[],
);
async function copyText() {
if (resetTimerRef.current !== null) {
window.clearTimeout(resetTimerRef.current);
}
try {
await navigator.clipboard.writeText(text);
setState("copied");
resetTimerRef.current = window.setTimeout(() => setState("idle"), 1600);
} catch {
setState("error");
resetTimerRef.current = window.setTimeout(() => setState("idle"), 2400);
}
}
return (
<>
<button
aria-label={`Copy ${label}`}
className={styles.codeCopyButton}
data-state={state}
onClick={copyText}
type="button"
>
<span className={styles.codeCopyIcons} aria-hidden="true">
<Copy className={styles.codeCopyIdleIcon} strokeWidth={1.8} />
<Check className={styles.codeCopySuccessIcon} strokeWidth={2} />
<CircleAlert
className={styles.codeCopyErrorIcon}
strokeWidth={1.8}
/>
</span>
<span>{state === "copied" ? "Copied" : state === "error" ? "Retry" : "Copy"}</span>
</button>
<span className="sr-only" role="status" aria-live="polite">
{getDocsCopyStatus(label, state)}
</span>
</>
);
}
export function DocsLaunchInspector() {
const [activeIndex, setActiveIndex] = useState(0);
const tabRefs = useRef<Array<HTMLButtonElement | null>>([]);
const activeSample = docsLaunchSamples[activeIndex];
const code = JSON.stringify(activeSample.record, null, 2);
function moveToTab(index: number) {
setActiveIndex(index);
window.requestAnimationFrame(() => tabRefs.current[index]?.focus());
}
function handleTabKeyDown(event: KeyboardEvent<HTMLButtonElement>) {
const nextIndex = getNextDocsSampleIndex(activeIndex, event.key);
if (nextIndex === activeIndex) return;
event.preventDefault();
moveToTab(nextIndex);
}
return (
<div
className={`${styles.launchInspector} liquid-glass-surface`}
>
<div className={styles.inspectorHeader}>
<div>
<span>Normalized record preview</span>
<strong>Same envelope, different markets</strong>
</div>
<span className={styles.sampleBadge}>Simulated</span>
</div>
<div
className={styles.sampleTabs}
role="tablist"
aria-label="Launch record examples"
>
{docsLaunchSamples.map((sample, index) => {
const isActive = index === activeIndex;
return (
<button
aria-controls="docs-launch-sample-panel"
aria-selected={isActive}
className={styles.sampleTab}
id={`docs-launch-sample-tab-${sample.id}`}
key={sample.id}
onClick={() => setActiveIndex(index)}
onKeyDown={handleTabKeyDown}
ref={(element) => {
tabRefs.current[index] = element;
}}
role="tab"
tabIndex={isActive ? 0 : -1}
type="button"
>
{sample.label}
</button>
);
})}
</div>
<div
aria-labelledby={`docs-launch-sample-tab-${activeSample.id}`}
className={styles.codeWindow}
id="docs-launch-sample-panel"
role="tabpanel"
tabIndex={0}
>
<div className={styles.codeWindowBar}>
<span>{activeSample.caption}</span>
<CopyButton label={`${activeSample.label} example`} text={code} />
</div>
<pre className={styles.codePre}>
<code>{code}</code>
</pre>
</div>
<p className={styles.codeNote}>
Preview only. Use the complete schema and fixtures for validation.
</p>
</div>
);
}
const quickstartCommand =
"curl -fsSL https://developers.programmable.family/api/v1/launches";
export function DocsQuickstartCommand() {
return (
<div
className={`${styles.commandPanel} liquid-glass-surface`}
>
<div className={styles.codeWindowBar}>
<span className={styles.commandMethod}>GET</span>
<span>/api/v1/launches</span>
<CopyButton label="launch feed command" text={quickstartCommand} />
</div>
<pre className={styles.commandPre}>
<code>{quickstartCommand}</code>
</pre>
</div>
);
}