/** * Component sections in the page editor (RFC-0015 §8, DESIGN.md §2aa): the deployment's block * types, their icons, a new section from a type's example, and the props form generated from the * type's schema (`BlockPropsForm`). A section whose type this deployment does not know is kept * as is and shown read-only. */ import type { ComponentSection } from "@cloudhal/core-contracts"; import { type BlockType, localized, sectionSchema, validateProps } from "@cloudhal/core-ts/blocks"; import { Banner, BlockPropsForm, blockIcon, InputArea } from "@cloudhal/ui"; import { WarningCircle } from "@phosphor-icons/react"; import { useQuery } from "@tanstack/react-query"; import { useCallback, useMemo } from "react"; import { keep, useI18n } from "../i18n/index.tsx"; import { api } from "../lib/api.ts"; export { blockIcon }; /** The deployment's block types (rarely change: cached for the session). */ export function useBlockTypes(siteId: string): BlockType[] { const query = useQuery({ queryKey: ["block-types", siteId], queryFn: () => api.blockTypes(siteId), staleTime: Number.POSITIVE_INFINITY, retry: false, }); return query.data ?? []; } /** A new component section from a type's example (a container's example text as `content`). */ export function newBlockSection(id: string, type: BlockType): ComponentSection { return { id, kind: "component", type: type.name, props: { ...type.example.props, ...(type.content === "markdown" && type.example.markdown ? { content: type.example.markdown } : {}), }, }; } export function ComponentSectionForm({ section, onChange, types, readOnly, }: { section: ComponentSection; onChange: (next: ComponentSection) => void; types: readonly BlockType[]; readOnly: boolean; }) { const { t, locale } = useI18n(); const type = types.find((x) => x.name === section.type); const schema = useMemo(() => (type ? sectionSchema(type) : undefined), [type]); const issues = useMemo( () => (schema ? validateProps(schema, section.props) : []), [schema, section.props], ); if (!type || !schema) return (