mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-11 09:51:40 +00:00
* feat: implement shadcn/registry * feat: add schema field * fix: import * chore: add changeset * chore: remove console * fix: tests * fix: diff command * feat: move to schema/registy-item.json * fix * ci: switch to node 20 * ci: build packages
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
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 { BlockViewer } from "@/components/block-viewer"
|
|
|
|
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} />
|
|
)
|
|
}
|
|
|
|
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 ?? ""),
|
|
}))
|
|
)
|
|
}
|
|
)
|