forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock-display.tsx
More file actions
56 lines (47 loc) · 1.62 KB
/
block-display.tsx
File metadata and controls
56 lines (47 loc) · 1.62 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
import * as React from 'react';
import { registryItemFileSchema } from 'shadcn/registry';
import { z } from 'zod';
import { highlightCode } from '@/lib/highlight-code';
import { createFileTreeForRegistryItemFiles, getRegistryItem } from '@/lib/registry';
import { cn } from '@/lib/utils';
import { BlockViewer } from '@/components/common/block-viewer';
import { ComponentPreview } from '@/components/common/component-preview';
export async function BlockDisplay({ name }: { name: string }) {
const item = await getCachedRegistryItem(name);
if (!item?.files) {
return null;
}
const [tree, highlightedFiles] = await Promise.all([
getCachedFileTree(item.files),
getCachedHighlightedFiles(item.files),
]);
return (
<BlockViewer item={item} tree={tree} highlightedFiles={highlightedFiles}>
<ComponentPreview
name={item.name}
hideCode
className={cn(
'my-0 **:[.preview]:h-auto **:[.preview]:p-4 **:[.preview>.p-6]:p-0',
item.meta?.containerClassName,
)}
/>
</BlockViewer>
);
}
const getCachedRegistryItem = React.cache(async (name: string) => {
return await getRegistryItem(name);
});
const getCachedFileTree = React.cache(async (files: Array<{ path: string; target?: string }>) => {
if (!files) {
return null;
}
return await createFileTreeForRegistryItemFiles(files);
});
const getCachedHighlightedFiles = React.cache(async (files: z.infer<typeof registryItemFileSchema>[]) => {
return await Promise.all(
files.map(async (file) => ({
...file,
highlightedContent: await highlightCode(file.content ?? ''),
})),
);
});