Skip to content

Commit 8f0c26f

Browse files
authored
feat(www): code for blocks (#5756)
* feat: update blocks * fix: scrollbars * fix: code viewer * test(shadcn): fix
1 parent 149b321 commit 8f0c26f

File tree

101 files changed

+7139
-1731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+7139
-1731
lines changed

apps/www/__registry__/index.tsx

+4,076-628
Large diffs are not rendered by default.

apps/www/app/(app)/blocks/page.tsx

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
import * as React from "react"
2-
import { unstable_cache } from "next/cache"
32

43
import { getAllBlockIds } from "@/lib/blocks"
54
import { BlockDisplay } from "@/components/block-display"
65

7-
const BLOCKS_WHITELIST_PREFIXES = ["sidebar", "login"]
8-
9-
const getBlocks = unstable_cache(async () => {
10-
return (await getAllBlockIds()).filter((name) =>
11-
BLOCKS_WHITELIST_PREFIXES.some((prefix) => name.startsWith(prefix))
12-
)
13-
}, ["blocks"])
6+
import "@/styles/mdx.css"
147

158
export default async function BlocksPage() {
16-
const blocks = await getBlocks()
9+
const blocks = await getAllBlockIds()
1710

1811
return (
1912
<div className="gap-3 md:flex md:flex-row-reverse md:items-start">
2013
<div className="grid flex-1 gap-12 md:gap-24 lg:gap-48">
21-
{blocks.map((name, index) => (
22-
<React.Suspense key={`${name}-${index}`}>
23-
<BlockDisplay name={name} />
24-
</React.Suspense>
14+
{blocks.map((name) => (
15+
<BlockDisplay key={name} name={name} />
2516
))}
2617
</div>
2718
</div>

apps/www/app/(blocks)/blocks/[style]/[name]/page.tsx

+20-31
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import * as React from "react"
12
import { Metadata } from "next"
23
import { notFound } from "next/navigation"
34

45
import { siteConfig } from "@/config/site"
5-
import { getAllBlockIds, getBlock } from "@/lib/blocks"
6+
import { getAllBlockIds } from "@/lib/blocks"
67
import { absoluteUrl, cn } from "@/lib/utils"
7-
import { BlockChunk } from "@/components/block-chunk"
8-
import { BlockWrapper } from "@/components/block-wrapper"
98
import { Style, styles } from "@/registry/registry-styles"
109

1110
import "@/styles/mdx.css"
11+
import { getRegistryComponent, getRegistryItem } from "@/lib/registry"
12+
13+
const getCachedRegistryItem = React.cache(
14+
async (name: string, style: Style["name"]) => {
15+
return await getRegistryItem(name, style)
16+
}
17+
)
1218

1319
export async function generateMetadata({
1420
params,
@@ -19,23 +25,23 @@ export async function generateMetadata({
1925
}
2026
}): Promise<Metadata> {
2127
const { name, style } = params
22-
const block = await getBlock(name, style)
28+
const item = await getCachedRegistryItem(name, style)
2329

24-
if (!block) {
30+
if (!item) {
2531
return {}
2632
}
2733

28-
const title = block.name
29-
const description = block.description
34+
const title = item.name
35+
const description = item.description
3036

3137
return {
32-
title: `${block.description} - ${block.name}`,
38+
title: `${item.name}${item.description ? ` - ${item.description}` : ""}`,
3339
description,
3440
openGraph: {
3541
title,
3642
description,
3743
type: "article",
38-
url: absoluteUrl(`/blocks/${block.name}`),
44+
url: absoluteUrl(`/blocks/${style}/${item.name}`),
3945
images: [
4046
{
4147
url: siteConfig.ogImage,
@@ -76,39 +82,22 @@ export default async function BlockPage({
7682
}
7783
}) {
7884
const { name, style } = params
79-
const block = await getBlock(name, style)
85+
const item = await getCachedRegistryItem(name, style)
86+
const Component = getRegistryComponent(name, style)
8087

81-
if (!block) {
88+
if (!item || !Component) {
8289
return notFound()
8390
}
8491

85-
const Component = block.component
86-
87-
const chunks = block.chunks?.map((chunk) => ({ ...chunk }))
88-
delete block.component
89-
block.chunks?.map((chunk) => delete chunk.component)
90-
9192
return (
9293
<>
93-
{/* <ThemesStyle /> */}
9494
<div
9595
className={cn(
9696
"themes-wrapper bg-background",
97-
block.container?.className
97+
item.meta?.containerClassName
9898
)}
9999
>
100-
<BlockWrapper block={block}>
101-
<Component />
102-
{chunks?.map((chunk, index) => (
103-
<BlockChunk
104-
key={chunk.name}
105-
block={block}
106-
chunk={block.chunks?.[index]}
107-
>
108-
<chunk.component />
109-
</BlockChunk>
110-
))}
111-
</BlockWrapper>
100+
<Component />
112101
</div>
113102
</>
114103
)

apps/www/app/layout.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "@/styles/globals.css"
22
import { Metadata, Viewport } from "next"
33

44
import { META_THEME_COLORS, siteConfig } from "@/config/site"
5-
import { fontSans } from "@/lib/fonts"
5+
import { fontMono, fontSans } from "@/lib/fonts"
66
import { cn } from "@/lib/utils"
77
import { Analytics } from "@/components/analytics"
88
import { ThemeProvider } from "@/components/providers"
@@ -92,7 +92,8 @@ export default function RootLayout({ children }: RootLayoutProps) {
9292
<body
9393
className={cn(
9494
"min-h-screen bg-background font-sans antialiased",
95-
fontSans.variable
95+
fontSans.variable,
96+
fontMono.variable
9697
)}
9798
>
9899
<ThemeProvider

apps/www/components/block-chunk.tsx

-57
This file was deleted.

apps/www/components/block-display.tsx

+43-23
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
1-
import { unstable_cache } from "next/cache"
1+
import * as React from "react"
2+
import { z } from "zod"
23

3-
import { getBlock } from "@/lib/blocks"
4-
import { BlockPreview } from "@/components/block-preview"
4+
import { highlightCode } from "@/lib/highlight-code"
5+
import {
6+
createFileTreeForRegistryItemFiles,
7+
getRegistryItem,
8+
} from "@/lib/registry"
9+
import { BlockViewer } from "@/components/block-viewer"
10+
import { registryItemFileSchema } from "@/registry/schema"
511

6-
const getBlockByName = unstable_cache(
7-
async (name: string) => {
8-
const block = await getBlock(name)
12+
export async function BlockDisplay({ name }: { name: string }) {
13+
const item = await getCachedRegistryItem(name)
14+
15+
if (!item?.files) {
16+
return null
17+
}
18+
19+
const [tree, highlightedFiles] = await Promise.all([
20+
getCachedFileTree(item.files),
21+
getCachedHighlightedFiles(item.files),
22+
])
23+
24+
return (
25+
<BlockViewer item={item} tree={tree} highlightedFiles={highlightedFiles} />
26+
)
27+
}
28+
29+
const getCachedRegistryItem = React.cache(async (name: string) => {
30+
return await getRegistryItem(name)
31+
})
932

10-
if (!block) {
33+
const getCachedFileTree = React.cache(
34+
async (files: Array<{ path: string; target?: string }>) => {
35+
if (!files) {
1136
return null
1237
}
1338

14-
return {
15-
name: block.name,
16-
style: block.style,
17-
description: block.description,
18-
container: block.container,
19-
}
20-
},
21-
["block"]
39+
return await createFileTreeForRegistryItemFiles(files)
40+
}
2241
)
2342

24-
export async function BlockDisplay({ name }: { name: string }) {
25-
const block = await getBlockByName(name)
26-
27-
if (!block) {
28-
return null
43+
const getCachedHighlightedFiles = React.cache(
44+
async (files: z.infer<typeof registryItemFileSchema>[]) => {
45+
return await Promise.all(
46+
files.map(async (file) => ({
47+
...file,
48+
highlightedContent: await highlightCode(file.content ?? ""),
49+
}))
50+
)
2951
}
30-
31-
return <BlockPreview key={block.name} block={block} />
32-
}
52+
)

apps/www/components/block-preview.tsx

-67
This file was deleted.

0 commit comments

Comments
 (0)