diff --git a/packages/ui/src/modules/types/Readme.md b/packages/ui/src/modules/types/Readme.md new file mode 100644 index 00000000..85038c65 --- /dev/null +++ b/packages/ui/src/modules/types/Readme.md @@ -0,0 +1,212 @@ +# Types Module + +The `types` module is the foundation of PoolyPress's high-level schema definition system. It provides an intuitive, drag-and-drop interface for users to construct complex data forms while storing the underlying data in a fully relational database and mapping to standard React JSON Schema Form (RJSF) payloads for the frontend. + +## Architecture & Data Flow + +At its core, this module is the interface between our dynamic backend types system (with primitives, structures, enums, and flags) and front-end forms. + +### Key Files and Directories +* `./TypesEditor.tsx` - The central orchestrator switching between Visual DND mode and JSON/UI Schema edit mode. Contains the complex logic bridging Flat Builder Output <-> Relational DB fields. +* `./TypeRenderer.tsx` - Raw schema editor rendering the Live Preview in the sidebar using RJSF. +* `./builder/` - Code strictly related to the Drag-and-Drop Form Builder + * `./builder/TypeBuilder.tsx` - Configures `@dnd-kit` contexts and ties drag operations into the Zustand store. + * `./builder/TypeBuilderContent.tsx` - Three-pane visual layout (Palette, Canvas, Config) powered by UI components and the drag context. + * `./builder/useStore.ts` - Local Zustand store that eliminates component prop-drilling for managing builder layout state. + +### Relational Data Structures & Caching + +The schemas defined in `src/modules/types/client-types.ts` (Frontend) and `server/src/products/serving/db/db-types.ts` (Backend) define the boundaries of the Type System. + +A single `TypeDefinition` is actually a composite of multiple relational tables: +1. `types` (The root definition: `id`, `name`, `kind`, `json_schema`, `meta`, `settings`) +2. `type_structure_fields` (When kind is 'structure': The 1-to-many joined edges connecting this structure's `id` to native child `field_type_id` rows) +3. `type_enum_values` & `type_flag_values` (For specific strict sets) + +**Backend Caching Model (`db-types.ts`)**: +Because types map the entire application schema, they must be exceptionally fast. The system heavily guards PostgreSQL by using `appCache` (a 5-minute memory cache). Inside the backend loader (`getTypeState`), all tables (`types`, `type_structure_fields`, `type_enum_values`, etc.) are pulled fully into memory arrays, manually mapped into heavily joined `typesMap` Dictionary Nodes, and preserved across requests. The client receives these pre-enriched `TypeDefinition` payloads directly via the unified GET route. +## Built-In Types vs App Level Types + +### Built-in Primitives +The base types managed in this system mirror JSON schema scalars: +* **String** (Text lines, Textareas) +* **Number/Integer** +* **Boolean** + +### Complex Formats +* **Structures / Objects**: Nested collections of fields. +* **Enums**: Hardcoded dropdowns and specific arrays of allowed values. +* **Flags**: Bitmask-style boolean representations. + +### App Level Context Pickers +PoolyPress doesn't just manage raw JSON scalar data; it handles relational metadata for the entire app. For these references, we use specific UI *Widgets*. +Instead of storing complex objects, forms store UUIDs (Strings) and display a specialized picker for them: +* **Users** +* **Groups** +* **Categories** +* **Apps/Products** +* **Images / Media** + +## How We Deal With Custom Widgets (RJSF) + +By default, RJSF generates standard HTML inputs based on the variable's type. +To override this (e.g. rendering our custom "Category Picker" rather than a raw UUID text input field), we inject a custom widget via `uiSchema`. + +```json +{ + "ui:widget": "category_picker" +} +``` + +### Widget and Template Registration +To prevent fragmented widget and template files, everything is highly consolidated: + +* **`./AppTypeWidgets.tsx`** - Contains all specialized form components. Rather than having a `userPickerWidget.tsx`, `groupPickerWidget.tsx`, etc., they are all exported from here tightly grouped. +* **`./RJSFTemplates.tsx`** - Combines custom RJSF structural overwrites (like customized submit buttons, field templates, grid layouts, ObjectField layouts) alongside compiling the `customWidgets` object registry used by the builder. +* **`./RJSFFormWrapper.tsx`** - The highest-level component that wraps a standard RJSF `