mirror of
https://github.com/cosscom/coss.git
synced 2026-09-14 20:06:50 +08:00
f5df3a2ae9
* configs improvements * add cache to lint to faster lint checks * Remove check-types from turbo.json * fix: registry buind and ui propagation issues * chore: update TypeScript paths and ESLint ignores, and clean up mobile-nav component * fix: update imports --------- Co-authored-by: pasqualevitiello <pasqualevitiello@gmail.com>
60 lines
1.2 KiB
TypeScript
60 lines
1.2 KiB
TypeScript
import fs from "node:fs/promises"
|
|
import path from "node:path"
|
|
import * as React from "react"
|
|
import { CodeBlock } from "@coss/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>
|
|
)
|
|
}
|