@polpo-ai/dashboard 0.1.3 → 0.15.8

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.
Files changed (184) hide show
  1. package/LICENSE +3 -1
  2. package/README.md +13 -40
  3. package/dist/components.d.ts +53 -0
  4. package/dist/components.d.ts.map +1 -0
  5. package/dist/components.js +81 -0
  6. package/dist/components.js.map +1 -0
  7. package/dist/host.d.ts +24 -0
  8. package/dist/host.d.ts.map +1 -0
  9. package/dist/host.js +18 -0
  10. package/dist/host.js.map +1 -0
  11. package/dist/index.d.ts +13 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +11 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/test/setup.d.ts +2 -0
  16. package/dist/test/setup.d.ts.map +1 -0
  17. package/dist/test/setup.js +2 -0
  18. package/dist/test/setup.js.map +1 -0
  19. package/dist/views/agent-detail.d.ts +4 -0
  20. package/dist/views/agent-detail.d.ts.map +1 -0
  21. package/dist/views/agent-detail.js +31 -0
  22. package/dist/views/agent-detail.js.map +1 -0
  23. package/dist/views/agents.d.ts +2 -0
  24. package/dist/views/agents.d.ts.map +1 -0
  25. package/dist/views/agents.js +81 -0
  26. package/dist/views/agents.js.map +1 -0
  27. package/dist/views/files.d.ts +2 -0
  28. package/dist/views/files.d.ts.map +1 -0
  29. package/dist/views/files.js +30 -0
  30. package/dist/views/files.js.map +1 -0
  31. package/dist/views/memory.d.ts +2 -0
  32. package/dist/views/memory.d.ts.map +1 -0
  33. package/dist/views/memory.js +19 -0
  34. package/dist/views/memory.js.map +1 -0
  35. package/dist/views/playground.d.ts +4 -0
  36. package/dist/views/playground.d.ts.map +1 -0
  37. package/dist/views/playground.js +43 -0
  38. package/dist/views/playground.js.map +1 -0
  39. package/dist/views/session-detail.d.ts +4 -0
  40. package/dist/views/session-detail.d.ts.map +1 -0
  41. package/dist/views/session-detail.js +23 -0
  42. package/dist/views/session-detail.js.map +1 -0
  43. package/dist/views/sessions.d.ts +2 -0
  44. package/dist/views/sessions.d.ts.map +1 -0
  45. package/dist/views/sessions.js +17 -0
  46. package/dist/views/sessions.js.map +1 -0
  47. package/dist/views/skills.d.ts +2 -0
  48. package/dist/views/skills.d.ts.map +1 -0
  49. package/dist/views/skills.js +16 -0
  50. package/dist/views/skills.js.map +1 -0
  51. package/package.json +47 -83
  52. package/src/styles.css +169 -0
  53. package/app/(dashboard)/error.tsx +0 -47
  54. package/app/(dashboard)/layout.tsx +0 -41
  55. package/app/(dashboard)/loading.tsx +0 -30
  56. package/app/(dashboard)/projects/[id]/agents/[name]/memory/view.tsx +0 -36
  57. package/app/(dashboard)/projects/[id]/agents/[name]/models-view.tsx +0 -30
  58. package/app/(dashboard)/projects/[id]/agents/[name]/page.tsx +0 -51
  59. package/app/(dashboard)/projects/[id]/agents/[name]/prompt/view.tsx +0 -211
  60. package/app/(dashboard)/projects/[id]/agents/[name]/skills/view.tsx +0 -84
  61. package/app/(dashboard)/projects/[id]/agents/[name]/tools/view.tsx +0 -525
  62. package/app/(dashboard)/projects/[id]/agents/[name]/vault/view.tsx +0 -61
  63. package/app/(dashboard)/projects/[id]/agents/page.tsx +0 -39
  64. package/app/(dashboard)/projects/[id]/agents/view.tsx +0 -283
  65. package/app/(dashboard)/projects/[id]/layout.tsx +0 -11
  66. package/app/(dashboard)/projects/[id]/logs/page.tsx +0 -27
  67. package/app/(dashboard)/projects/[id]/logs/view.tsx +0 -184
  68. package/app/(dashboard)/projects/[id]/memory/page.tsx +0 -40
  69. package/app/(dashboard)/projects/[id]/memory/view.tsx +0 -17
  70. package/app/(dashboard)/projects/[id]/missions/[missionId]/page.tsx +0 -88
  71. package/app/(dashboard)/projects/[id]/missions/[missionId]/view.tsx +0 -569
  72. package/app/(dashboard)/projects/[id]/missions/page.tsx +0 -32
  73. package/app/(dashboard)/projects/[id]/missions/view.tsx +0 -282
  74. package/app/(dashboard)/projects/[id]/onboarding-checklist.tsx +0 -359
  75. package/app/(dashboard)/projects/[id]/page.tsx +0 -68
  76. package/app/(dashboard)/projects/[id]/playbooks/[name]/page.tsx +0 -5
  77. package/app/(dashboard)/projects/[id]/playbooks/[name]/view.tsx +0 -433
  78. package/app/(dashboard)/projects/[id]/playbooks/page.tsx +0 -5
  79. package/app/(dashboard)/projects/[id]/playbooks/view.tsx +0 -222
  80. package/app/(dashboard)/projects/[id]/schedules/page.tsx +0 -44
  81. package/app/(dashboard)/projects/[id]/schedules/view.tsx +0 -385
  82. package/app/(dashboard)/projects/[id]/sessions/[sessionId]/page.tsx +0 -55
  83. package/app/(dashboard)/projects/[id]/sessions/[sessionId]/raw-view.tsx +0 -373
  84. package/app/(dashboard)/projects/[id]/sessions/[sessionId]/view.tsx +0 -124
  85. package/app/(dashboard)/projects/[id]/sessions/page.tsx +0 -31
  86. package/app/(dashboard)/projects/[id]/sessions/view.tsx +0 -242
  87. package/app/(dashboard)/projects/[id]/settings/page.tsx +0 -52
  88. package/app/(dashboard)/projects/[id]/skills/[name]/page.tsx +0 -65
  89. package/app/(dashboard)/projects/[id]/skills/[name]/view.tsx +0 -227
  90. package/app/(dashboard)/projects/[id]/skills/page.tsx +0 -31
  91. package/app/(dashboard)/projects/[id]/skills/view.tsx +0 -243
  92. package/app/(dashboard)/projects/[id]/storage/page.tsx +0 -5
  93. package/app/(dashboard)/projects/[id]/storage/view.tsx +0 -385
  94. package/app/(dashboard)/projects/[id]/tasks/[taskId]/activity/view.tsx +0 -336
  95. package/app/(dashboard)/projects/[id]/tasks/[taskId]/assessment/view.tsx +0 -113
  96. package/app/(dashboard)/projects/[id]/tasks/[taskId]/blueprint.ts +0 -72
  97. package/app/(dashboard)/projects/[id]/tasks/[taskId]/output/view.tsx +0 -90
  98. package/app/(dashboard)/projects/[id]/tasks/[taskId]/page.tsx +0 -63
  99. package/app/(dashboard)/projects/[id]/tasks/[taskId]/view.tsx +0 -137
  100. package/app/(dashboard)/projects/[id]/tasks/page.tsx +0 -40
  101. package/app/(dashboard)/projects/[id]/tasks/view.tsx +0 -421
  102. package/app/(dashboard)/projects/[id]/view.tsx +0 -421
  103. package/app/(dashboard)/projects/[id]/welcome-banner.tsx +0 -156
  104. package/app/(dashboard)/projects/projects-list.tsx +0 -126
  105. package/app/(playground)/error.tsx +0 -42
  106. package/app/(playground)/layout.tsx +0 -27
  107. package/app/(playground)/projects/[id]/playground/page.tsx +0 -57
  108. package/app/(playground)/projects/[id]/playground/view.tsx +0 -167
  109. package/app/(playground)/projects/[id]/playground-legacy/page.tsx +0 -47
  110. package/app/(playground)/projects/[id]/playground-legacy/skeleton.tsx +0 -63
  111. package/app/(playground)/projects/[id]/playground-legacy/view.tsx +0 -237
  112. package/app/favicon.ico +0 -0
  113. package/app/globals.css +0 -385
  114. package/app/icon.svg +0 -6
  115. package/app/layout.tsx +0 -39
  116. package/app/page.tsx +0 -11
  117. package/components/ai-elements/code-block.tsx +0 -562
  118. package/components/dashboard/agent-capabilities.tsx +0 -326
  119. package/components/dashboard/agent-identity-header.tsx +0 -50
  120. package/components/dashboard/agent-model-picker.tsx +0 -195
  121. package/components/dashboard/agent-model-selector.tsx +0 -121
  122. package/components/dashboard/agent-picker-card.tsx +0 -46
  123. package/components/dashboard/agent-studio.tsx +0 -242
  124. package/components/dashboard/agents-table.tsx +0 -48
  125. package/components/dashboard/breadcrumb.tsx +0 -70
  126. package/components/dashboard/builder-chat.tsx +0 -418
  127. package/components/dashboard/chat-shell.tsx +0 -40
  128. package/components/dashboard/client-picker.tsx +0 -63
  129. package/components/dashboard/command-snippet.tsx +0 -77
  130. package/components/dashboard/connect-dialog.tsx +0 -680
  131. package/components/dashboard/copy-button.tsx +0 -45
  132. package/components/dashboard/copy-card.tsx +0 -48
  133. package/components/dashboard/file-browser.tsx +0 -161
  134. package/components/dashboard/hint.tsx +0 -22
  135. package/components/dashboard/inference-mode.tsx +0 -15
  136. package/components/dashboard/manual-refresh-button.tsx +0 -52
  137. package/components/dashboard/markdown.tsx +0 -31
  138. package/components/dashboard/mcp-install-panel.tsx +0 -196
  139. package/components/dashboard/mission-graph.tsx +0 -262
  140. package/components/dashboard/nav-tabs.tsx +0 -86
  141. package/components/dashboard/polpo-chat.tsx +0 -468
  142. package/components/dashboard/project-copilot.tsx +0 -253
  143. package/components/dashboard/sdk-snippet-panel.tsx +0 -194
  144. package/components/dashboard/section-header.tsx +0 -37
  145. package/components/dashboard/settings-form.tsx +0 -2477
  146. package/components/dashboard/sidebar.tsx +0 -385
  147. package/components/dashboard/skeletons.tsx +0 -1054
  148. package/components/dashboard/skills-install-wizard.tsx +0 -294
  149. package/components/dashboard/swarm-runs.tsx +0 -316
  150. package/components/dashboard/tab-toggle.tsx +0 -35
  151. package/components/dashboard/task-studio.tsx +0 -224
  152. package/components/dashboard/top-header.tsx +0 -203
  153. package/components/dashboard/webhook-deliveries.tsx +0 -234
  154. package/components/icons/coding-agents.tsx +0 -151
  155. package/components/json-ld.tsx +0 -8
  156. package/components/providers.tsx +0 -25
  157. package/components/ui/badge.tsx +0 -52
  158. package/components/ui/button.tsx +0 -60
  159. package/components/ui/card.tsx +0 -103
  160. package/components/ui/chart.tsx +0 -356
  161. package/components/ui/command.tsx +0 -196
  162. package/components/ui/dialog.tsx +0 -157
  163. package/components/ui/input-group.tsx +0 -158
  164. package/components/ui/input.tsx +0 -20
  165. package/components/ui/multi-select.tsx +0 -161
  166. package/components/ui/popover.tsx +0 -90
  167. package/components/ui/select.tsx +0 -201
  168. package/components/ui/separator.tsx +0 -25
  169. package/components/ui/sheet.tsx +0 -135
  170. package/components/ui/skeleton.tsx +0 -13
  171. package/components/ui/tabs.tsx +0 -56
  172. package/components/ui/textarea.tsx +0 -18
  173. package/components/ui/tooltip.tsx +0 -66
  174. package/hooks/use-desktop-sidebar.tsx +0 -50
  175. package/hooks/use-mobile-sidebar.tsx +0 -37
  176. package/index.ts +0 -17
  177. package/lib/api.ts +0 -194
  178. package/lib/builder-context.ts +0 -60
  179. package/lib/data-client.ts +0 -68
  180. package/lib/get-query-client.ts +0 -25
  181. package/lib/polpo-client.tsx +0 -49
  182. package/lib/tool-catalog.ts +0 -234
  183. package/lib/use-event-catalog.ts +0 -28
  184. package/lib/utils.ts +0 -6
@@ -1,562 +0,0 @@
1
- "use client";
2
-
3
- import { Button } from "../../components/ui/button";
4
- import {
5
- Select,
6
- SelectContent,
7
- SelectItem,
8
- SelectTrigger,
9
- SelectValue,
10
- } from "../../components/ui/select";
11
- import { cn } from "../../lib/utils";
12
- import { CheckIcon, CopyIcon } from "lucide-react";
13
- import type { ComponentProps, CSSProperties, HTMLAttributes } from "react";
14
- import {
15
- createContext,
16
- memo,
17
- useCallback,
18
- useContext,
19
- useEffect,
20
- useMemo,
21
- useRef,
22
- useState,
23
- } from "react";
24
- import type {
25
- BundledLanguage,
26
- BundledTheme,
27
- HighlighterGeneric,
28
- ThemedToken,
29
- } from "shiki";
30
- import { createHighlighter } from "shiki";
31
-
32
- // Shiki uses bitflags for font styles: 1=italic, 2=bold, 4=underline
33
- // oxlint-disable-next-line eslint(no-bitwise)
34
- const isItalic = (fontStyle: number | undefined) => fontStyle && fontStyle & 1;
35
- // oxlint-disable-next-line eslint(no-bitwise)
36
- const isBold = (fontStyle: number | undefined) => fontStyle && fontStyle & 2;
37
- const isUnderline = (fontStyle: number | undefined) =>
38
- // oxlint-disable-next-line eslint(no-bitwise)
39
- fontStyle && fontStyle & 4;
40
-
41
- // Transform tokens to include pre-computed keys to avoid noArrayIndexKey lint
42
- interface KeyedToken {
43
- token: ThemedToken;
44
- key: string;
45
- }
46
- interface KeyedLine {
47
- tokens: KeyedToken[];
48
- key: string;
49
- }
50
-
51
- const addKeysToTokens = (lines: ThemedToken[][]): KeyedLine[] =>
52
- lines.map((line, lineIdx) => ({
53
- key: `line-${lineIdx}`,
54
- tokens: line.map((token, tokenIdx) => ({
55
- key: `line-${lineIdx}-${tokenIdx}`,
56
- token,
57
- })),
58
- }));
59
-
60
- // Token rendering component
61
- const TokenSpan = ({ token }: { token: ThemedToken }) => (
62
- <span
63
- className="dark:!bg-[var(--shiki-dark-bg)] dark:!text-[var(--shiki-dark)]"
64
- style={
65
- {
66
- backgroundColor: token.bgColor,
67
- color: token.color,
68
- fontStyle: isItalic(token.fontStyle) ? "italic" : undefined,
69
- fontWeight: isBold(token.fontStyle) ? "bold" : undefined,
70
- textDecoration: isUnderline(token.fontStyle) ? "underline" : undefined,
71
- ...token.htmlStyle,
72
- } as CSSProperties
73
- }
74
- >
75
- {token.content}
76
- </span>
77
- );
78
-
79
- // Line number styles using CSS counters
80
- const LINE_NUMBER_CLASSES = cn(
81
- "block",
82
- "before:content-[counter(line)]",
83
- "before:inline-block",
84
- "before:[counter-increment:line]",
85
- "before:w-8",
86
- "before:mr-4",
87
- "before:text-right",
88
- "before:text-muted-foreground/50",
89
- "before:font-mono",
90
- "before:select-none"
91
- );
92
-
93
- // Line rendering component
94
- const LineSpan = ({
95
- keyedLine,
96
- showLineNumbers,
97
- }: {
98
- keyedLine: KeyedLine;
99
- showLineNumbers: boolean;
100
- }) => (
101
- <span className={showLineNumbers ? LINE_NUMBER_CLASSES : "block"}>
102
- {keyedLine.tokens.length === 0
103
- ? "\n"
104
- : keyedLine.tokens.map(({ token, key }) => (
105
- <TokenSpan key={key} token={token} />
106
- ))}
107
- </span>
108
- );
109
-
110
- // Types
111
- type CodeBlockProps = HTMLAttributes<HTMLDivElement> & {
112
- code: string;
113
- language: BundledLanguage;
114
- showLineNumbers?: boolean;
115
- };
116
-
117
- interface TokenizedCode {
118
- tokens: ThemedToken[][];
119
- fg: string;
120
- bg: string;
121
- }
122
-
123
- interface CodeBlockContextType {
124
- code: string;
125
- }
126
-
127
- // Context
128
- const CodeBlockContext = createContext<CodeBlockContextType>({
129
- code: "",
130
- });
131
-
132
- // Highlighter cache (singleton per language)
133
- const highlighterCache = new Map<
134
- string,
135
- Promise<HighlighterGeneric<BundledLanguage, BundledTheme>>
136
- >();
137
-
138
- // Token cache
139
- const tokensCache = new Map<string, TokenizedCode>();
140
-
141
- // Subscribers for async token updates
142
- const subscribers = new Map<string, Set<(result: TokenizedCode) => void>>();
143
-
144
- const getTokensCacheKey = (code: string, language: BundledLanguage) => {
145
- const start = code.slice(0, 100);
146
- const end = code.length > 100 ? code.slice(-100) : "";
147
- return `${language}:${code.length}:${start}:${end}`;
148
- };
149
-
150
- const getHighlighter = (
151
- language: BundledLanguage
152
- ): Promise<HighlighterGeneric<BundledLanguage, BundledTheme>> => {
153
- const cached = highlighterCache.get(language);
154
- if (cached) {
155
- return cached;
156
- }
157
-
158
- const highlighterPromise = createHighlighter({
159
- langs: [language],
160
- themes: ["github-light", "github-dark"],
161
- });
162
-
163
- highlighterCache.set(language, highlighterPromise);
164
- return highlighterPromise;
165
- };
166
-
167
- // Create raw tokens for immediate display while highlighting loads
168
- const createRawTokens = (code: string): TokenizedCode => ({
169
- bg: "transparent",
170
- fg: "inherit",
171
- tokens: code.split("\n").map((line) =>
172
- line === ""
173
- ? []
174
- : [
175
- {
176
- color: "inherit",
177
- content: line,
178
- } as ThemedToken,
179
- ]
180
- ),
181
- });
182
-
183
- // Synchronous highlight with callback for async results
184
- export const highlightCode = (
185
- code: string,
186
- language: BundledLanguage,
187
- // oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-callbacks)
188
- callback?: (result: TokenizedCode) => void
189
- ): TokenizedCode | null => {
190
- const tokensCacheKey = getTokensCacheKey(code, language);
191
-
192
- // Return cached result if available
193
- const cached = tokensCache.get(tokensCacheKey);
194
- if (cached) {
195
- return cached;
196
- }
197
-
198
- // Subscribe callback if provided
199
- if (callback) {
200
- if (!subscribers.has(tokensCacheKey)) {
201
- subscribers.set(tokensCacheKey, new Set());
202
- }
203
- subscribers.get(tokensCacheKey)?.add(callback);
204
- }
205
-
206
- // Start highlighting in background - fire-and-forget async pattern
207
- getHighlighter(language)
208
- // oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-then)
209
- .then((highlighter) => {
210
- const availableLangs = highlighter.getLoadedLanguages();
211
- const langToUse = availableLangs.includes(language) ? language : "text";
212
-
213
- const result = highlighter.codeToTokens(code, {
214
- lang: langToUse,
215
- themes: {
216
- dark: "github-dark",
217
- light: "github-light",
218
- },
219
- });
220
-
221
- const tokenized: TokenizedCode = {
222
- bg: result.bg ?? "transparent",
223
- fg: result.fg ?? "inherit",
224
- tokens: result.tokens,
225
- };
226
-
227
- // Cache the result
228
- tokensCache.set(tokensCacheKey, tokenized);
229
-
230
- // Notify all subscribers
231
- const subs = subscribers.get(tokensCacheKey);
232
- if (subs) {
233
- for (const sub of subs) {
234
- sub(tokenized);
235
- }
236
- subscribers.delete(tokensCacheKey);
237
- }
238
- })
239
- // oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-then), eslint-plugin-promise(prefer-await-to-callbacks)
240
- .catch((error) => {
241
- console.error("Failed to highlight code:", error);
242
- subscribers.delete(tokensCacheKey);
243
- });
244
-
245
- return null;
246
- };
247
-
248
- const CodeBlockBody = memo(
249
- ({
250
- tokenized,
251
- showLineNumbers,
252
- className,
253
- }: {
254
- tokenized: TokenizedCode;
255
- showLineNumbers: boolean;
256
- className?: string;
257
- }) => {
258
- const preStyle = useMemo(
259
- () => ({
260
- backgroundColor: tokenized.bg,
261
- color: tokenized.fg,
262
- }),
263
- [tokenized.bg, tokenized.fg]
264
- );
265
-
266
- const keyedLines = useMemo(
267
- () => addKeysToTokens(tokenized.tokens),
268
- [tokenized.tokens]
269
- );
270
-
271
- return (
272
- <pre
273
- className={cn(
274
- "dark:!bg-[var(--shiki-dark-bg)] dark:!text-[var(--shiki-dark)] m-0 p-4 text-sm",
275
- className
276
- )}
277
- style={preStyle}
278
- >
279
- <code
280
- className={cn(
281
- "font-mono text-sm",
282
- showLineNumbers && "[counter-increment:line_0] [counter-reset:line]"
283
- )}
284
- >
285
- {keyedLines.map((keyedLine) => (
286
- <LineSpan
287
- key={keyedLine.key}
288
- keyedLine={keyedLine}
289
- showLineNumbers={showLineNumbers}
290
- />
291
- ))}
292
- </code>
293
- </pre>
294
- );
295
- },
296
- (prevProps, nextProps) =>
297
- prevProps.tokenized === nextProps.tokenized &&
298
- prevProps.showLineNumbers === nextProps.showLineNumbers &&
299
- prevProps.className === nextProps.className
300
- );
301
-
302
- CodeBlockBody.displayName = "CodeBlockBody";
303
-
304
- export const CodeBlockContainer = ({
305
- className,
306
- language,
307
- style,
308
- ...props
309
- }: HTMLAttributes<HTMLDivElement> & { language: string }) => (
310
- <div
311
- className={cn(
312
- "group relative w-full overflow-hidden rounded-md border bg-background text-foreground",
313
- className
314
- )}
315
- data-language={language}
316
- style={{
317
- containIntrinsicSize: "auto 200px",
318
- contentVisibility: "auto",
319
- ...style,
320
- }}
321
- {...props}
322
- />
323
- );
324
-
325
- export const CodeBlockHeader = ({
326
- children,
327
- className,
328
- ...props
329
- }: HTMLAttributes<HTMLDivElement>) => (
330
- <div
331
- className={cn(
332
- "flex items-center justify-between border-b bg-muted/80 px-3 py-2 text-muted-foreground text-xs",
333
- className
334
- )}
335
- {...props}
336
- >
337
- {children}
338
- </div>
339
- );
340
-
341
- export const CodeBlockTitle = ({
342
- children,
343
- className,
344
- ...props
345
- }: HTMLAttributes<HTMLDivElement>) => (
346
- <div className={cn("flex items-center gap-2", className)} {...props}>
347
- {children}
348
- </div>
349
- );
350
-
351
- export const CodeBlockFilename = ({
352
- children,
353
- className,
354
- ...props
355
- }: HTMLAttributes<HTMLSpanElement>) => (
356
- <span className={cn("font-mono", className)} {...props}>
357
- {children}
358
- </span>
359
- );
360
-
361
- export const CodeBlockActions = ({
362
- children,
363
- className,
364
- ...props
365
- }: HTMLAttributes<HTMLDivElement>) => (
366
- <div
367
- className={cn("-my-1 -mr-1 flex items-center gap-2", className)}
368
- {...props}
369
- >
370
- {children}
371
- </div>
372
- );
373
-
374
- export const CodeBlockContent = ({
375
- code,
376
- language,
377
- showLineNumbers = false,
378
- }: {
379
- code: string;
380
- language: BundledLanguage;
381
- showLineNumbers?: boolean;
382
- }) => {
383
- // Memoized raw tokens for immediate display
384
- const rawTokens = useMemo(() => createRawTokens(code), [code]);
385
-
386
- // Synchronous cache lookup — avoids setState in effect for cached results
387
- const syncTokens = useMemo(
388
- () => highlightCode(code, language) ?? rawTokens,
389
- [code, language, rawTokens]
390
- );
391
-
392
- // Async highlighting result (populated after shiki loads)
393
- const [asyncTokens, setAsyncTokens] = useState<TokenizedCode | null>(null);
394
- const asyncKeyRef = useRef({ code, language });
395
-
396
- // Invalidate stale async tokens synchronously during render
397
- if (
398
- asyncKeyRef.current.code !== code ||
399
- asyncKeyRef.current.language !== language
400
- ) {
401
- asyncKeyRef.current = { code, language };
402
- setAsyncTokens(null);
403
- }
404
-
405
- useEffect(() => {
406
- let cancelled = false;
407
-
408
- highlightCode(code, language, (result) => {
409
- if (!cancelled) {
410
- setAsyncTokens(result);
411
- }
412
- });
413
-
414
- return () => {
415
- cancelled = true;
416
- };
417
- }, [code, language]);
418
-
419
- const tokenized = asyncTokens ?? syncTokens;
420
-
421
- return (
422
- <div className="relative overflow-auto">
423
- <CodeBlockBody showLineNumbers={showLineNumbers} tokenized={tokenized} />
424
- </div>
425
- );
426
- };
427
-
428
- export const CodeBlock = ({
429
- code,
430
- language,
431
- showLineNumbers = false,
432
- className,
433
- children,
434
- ...props
435
- }: CodeBlockProps) => {
436
- const contextValue = useMemo(() => ({ code }), [code]);
437
-
438
- return (
439
- <CodeBlockContext.Provider value={contextValue}>
440
- <CodeBlockContainer className={className} language={language} {...props}>
441
- {children}
442
- <CodeBlockContent
443
- code={code}
444
- language={language}
445
- showLineNumbers={showLineNumbers}
446
- />
447
- </CodeBlockContainer>
448
- </CodeBlockContext.Provider>
449
- );
450
- };
451
-
452
- export type CodeBlockCopyButtonProps = ComponentProps<typeof Button> & {
453
- onCopy?: () => void;
454
- onError?: (error: Error) => void;
455
- timeout?: number;
456
- };
457
-
458
- export const CodeBlockCopyButton = ({
459
- onCopy,
460
- onError,
461
- timeout = 2000,
462
- children,
463
- className,
464
- ...props
465
- }: CodeBlockCopyButtonProps) => {
466
- const [isCopied, setIsCopied] = useState(false);
467
- const timeoutRef = useRef<number>(0);
468
- const { code } = useContext(CodeBlockContext);
469
-
470
- const copyToClipboard = useCallback(async () => {
471
- if (typeof window === "undefined" || !navigator?.clipboard?.writeText) {
472
- onError?.(new Error("Clipboard API not available"));
473
- return;
474
- }
475
-
476
- try {
477
- if (!isCopied) {
478
- await navigator.clipboard.writeText(code);
479
- setIsCopied(true);
480
- onCopy?.();
481
- timeoutRef.current = window.setTimeout(
482
- () => setIsCopied(false),
483
- timeout
484
- );
485
- }
486
- } catch (error) {
487
- onError?.(error as Error);
488
- }
489
- }, [code, onCopy, onError, timeout, isCopied]);
490
-
491
- useEffect(
492
- () => () => {
493
- window.clearTimeout(timeoutRef.current);
494
- },
495
- []
496
- );
497
-
498
- const Icon = isCopied ? CheckIcon : CopyIcon;
499
-
500
- return (
501
- <Button
502
- className={cn("shrink-0", className)}
503
- onClick={copyToClipboard}
504
- size="icon"
505
- variant="ghost"
506
- {...props}
507
- >
508
- {children ?? <Icon size={14} />}
509
- </Button>
510
- );
511
- };
512
-
513
- export type CodeBlockLanguageSelectorProps = ComponentProps<typeof Select>;
514
-
515
- export const CodeBlockLanguageSelector = (
516
- props: CodeBlockLanguageSelectorProps
517
- ) => <Select {...props} />;
518
-
519
- export type CodeBlockLanguageSelectorTriggerProps = ComponentProps<
520
- typeof SelectTrigger
521
- >;
522
-
523
- export const CodeBlockLanguageSelectorTrigger = ({
524
- className,
525
- ...props
526
- }: CodeBlockLanguageSelectorTriggerProps) => (
527
- <SelectTrigger
528
- className={cn(
529
- "h-7 border-none bg-transparent px-2 text-xs shadow-none",
530
- className
531
- )}
532
- size="sm"
533
- {...props}
534
- />
535
- );
536
-
537
- export type CodeBlockLanguageSelectorValueProps = ComponentProps<
538
- typeof SelectValue
539
- >;
540
-
541
- export const CodeBlockLanguageSelectorValue = (
542
- props: CodeBlockLanguageSelectorValueProps
543
- ) => <SelectValue {...props} />;
544
-
545
- export type CodeBlockLanguageSelectorContentProps = ComponentProps<
546
- typeof SelectContent
547
- >;
548
-
549
- export const CodeBlockLanguageSelectorContent = ({
550
- align = "end",
551
- ...props
552
- }: CodeBlockLanguageSelectorContentProps) => (
553
- <SelectContent align={align} {...props} />
554
- );
555
-
556
- export type CodeBlockLanguageSelectorItemProps = ComponentProps<
557
- typeof SelectItem
558
- >;
559
-
560
- export const CodeBlockLanguageSelectorItem = (
561
- props: CodeBlockLanguageSelectorItemProps
562
- ) => <SelectItem {...props} />;