mirror of
https://github.com/cosscom/coss.git
synced 2026-09-14 20:06:50 +08:00
61 lines
1.3 KiB
TypeScript
61 lines
1.3 KiB
TypeScript
import fs from "node:fs/promises"
|
|
import path from "node:path"
|
|
import * as React from "react"
|
|
|
|
import { CodeBlock } from "@workspace/ui/components/code-block"
|
|
|
|
import { getRegistryItem } from "@/lib/registry"
|
|
import { cn } from "@/lib/utils"
|
|
import { CodeCollapsibleWrapper } from "@/components/code-collapsible-wrapper"
|
|
|
|
export async function ComponentSource({
|
|
name,
|
|
src,
|
|
title,
|
|
language,
|
|
collapsible = true,
|
|
className,
|
|
}: React.ComponentProps<"div"> & {
|
|
name?: string
|
|
src?: string
|
|
title?: string
|
|
language?: string
|
|
collapsible?: boolean
|
|
}) {
|
|
if (!name && !src) {
|
|
return null
|
|
}
|
|
|
|
let code: string | undefined
|
|
|
|
if (name) {
|
|
const item = await getRegistryItem(name)
|
|
code = item?.files?.[0]?.content
|
|
}
|
|
|
|
if (src) {
|
|
const file = await fs.readFile(path.join(process.cwd(), src), "utf-8")
|
|
code = file
|
|
}
|
|
|
|
if (!code) {
|
|
return null
|
|
}
|
|
|
|
const lang = language ?? title?.split(".").pop() ?? "tsx"
|
|
|
|
if (!collapsible) {
|
|
return (
|
|
<div className={cn("relative", className)}>
|
|
<CodeBlock code={code} language={lang} title={title} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<CodeCollapsibleWrapper className={className}>
|
|
<CodeBlock code={code} language={lang} title={title} />
|
|
</CodeCollapsibleWrapper>
|
|
)
|
|
}
|