Files
shadcn-ui/apps/v4/lib/blocks.ts
shadcn 84bd724d97 feat: refactor registry (#8598)
* feat: refactor registry

* fix: remove components

* refactor: getActiveStyle

* fix: prettier in build-registry

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* fix

* Update apps/v4/scripts/build-registry.mts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix

* Update apps/v4/scripts/build-registry.mts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update apps/v4/components/block-viewer.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-29 15:07:56 +04:00

58 lines
1.5 KiB
TypeScript

"use server"
import { registryItemSchema } from "shadcn/schema"
import { z } from "zod"
export async function getAllBlockIds(
types: z.infer<typeof registryItemSchema>["type"][] = [
"registry:block",
"registry:internal",
],
categories: string[] = []
): Promise<string[]> {
const blocks = await getAllBlocks(types, categories)
return blocks.map((block) => block.name)
}
export async function getAllBlocks(
types: z.infer<typeof registryItemSchema>["type"][] = [
"registry:block",
"registry:internal",
],
categories: string[] = []
) {
const { Index } = await import("@/registry/__index__")
// Collect all blocks from all styles.
const allBlocks: z.infer<typeof registryItemSchema>[] = []
for (const style in Index) {
const styleIndex = Index[style]
if (typeof styleIndex === "object" && styleIndex !== null) {
for (const itemName in styleIndex) {
const item = styleIndex[itemName]
allBlocks.push(item)
}
}
}
// Validate each block.
const validatedBlocks = allBlocks
.map((block) => {
const result = registryItemSchema.safeParse(block)
return result.success ? result.data : null
})
.filter(
(block): block is z.infer<typeof registryItemSchema> => block !== null
)
return validatedBlocks.filter(
(block) =>
types.includes(block.type) &&
(categories.length === 0 ||
block.categories?.some((category) => categories.includes(category))) &&
!block.name.startsWith("chart-")
)
}