Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | /** * useLayout — compute visible cards and fields from schema + card config + layout config. */ import type { FlatLayoutConfig, ICardConfig, ICardWidgetEntry, IEnrichedFieldSchema, IEnrichedSchema, ISplitLayoutConfig, ISplitLayoutPanel, ITabLayoutConfig, LayoutConfig, } from '@feasibleone/blong'; import {useMemo} from 'react'; export type {FlatLayoutConfig, ISplitLayoutConfig, LayoutConfig} from '@feasibleone/blong'; /** Minimal shape of a custom editor that useLayout needs to know about */ interface IEditorDef { properties: string[]; } /** Resolved card — enriched card config with derived field list */ export interface IResolvedCard { name: string; /** Card title; undefined means no title header (widget provides its own title) */ label: string | undefined; fields: string[]; config: ICardConfig; /** * Per-entry column overrides for ICardWidgetEntry objects. * Key is 'fieldName#id' (e.g. 'table#table1'); value is the column list. */ columnOverrides?: Record<string, string[]>; } /** A resolved tab/step */ export interface IResolvedTab { id: string; label?: string; icon?: string; /** Each entry is a deck-group: one or more card names stacked in the same grid column. */ cardNames: string[][]; /** Optional React component rendered in place of cards */ component?: React.ComponentType; } /** Result of useLayout */ export interface ILayoutResult { /** Resolved layout rows (array of decks, each deck is array of card names) — flat mode only */ rows: string[][]; /** Resolved cards with their field lists */ cards: Record<string, IResolvedCard>; /** All visible field names */ allFields: string[]; /** Resolved tabs (populated in tab/steps mode, empty in flat mode) */ tabs?: IResolvedTab[]; /** Layout type: 'flat' | 'tabs' | 'steps' | 'split' */ layoutType: 'flat' | 'tabs' | 'steps' | 'split'; /** Tab orientation (only for tabs mode) */ orientation?: string; /** Split layout panels (only for split mode) */ panels?: ISplitLayoutPanel[]; } /** Type guard: is the layout config in tab/steps form? */ export function isTabLayout(config: LayoutConfig): config is ITabLayoutConfig { return !Array.isArray(config) && 'items' in config; } /** Type guard: is the layout config a split layout? */ export function isSplitLayout(config: LayoutConfig): config is ISplitLayoutConfig { return !Array.isArray(config) && (config as ISplitLayoutConfig).type === 'split'; } /** * Check whether a dot-notation field path resolves to a leaf property in the schema. * E.g. 'input.input' checks schema.properties.input.properties.input. * Single-segment paths (e.g. 'table') are checked directly in schemaProps. * Paths with '#id' suffix (ICardWidgetEntry column-override keys) strip the suffix first. */ function schemaHasField( schemaProps: Record<string, IEnrichedFieldSchema>, fieldPath: string, ): boolean { // Strip '#id' suffix generated for ICardWidgetEntry column overrides (e.g. 'table#table1') const hashIdx = fieldPath.indexOf('#'); const basePath = hashIdx >= 0 ? fieldPath.slice(0, hashIdx) : fieldPath; const dot = basePath.indexOf('.'); if (dot === -1) return basePath in schemaProps; const head = basePath.slice(0, dot); const tail = basePath.slice(dot + 1); const nested = schemaProps[head]?.properties; return nested != null && schemaHasField(nested as Record<string, IEnrichedFieldSchema>, tail); } /** * Compute layout structure from schema + cards config + layout key. */ export function useLayout( schema: IEnrichedSchema | undefined, cardsConfig: Record<string, ICardConfig> | undefined, layoutKey: string, layouts: Record<string, LayoutConfig> | undefined, editors?: Record<string, IEditorDef>, ): ILayoutResult { return useMemo(() => { const schemaProps = schema?.properties ?? {}; const cards: Record<string, IResolvedCard> = {}; // Step 1: Build card definitions from config + schema const cardDefs: Record<string, ICardConfig> = {...(cardsConfig ?? {})}; // If no cards config, create a default card with all schema fields if (Object.keys(cardDefs).length === 0) { const allFieldNames = Object.keys(schemaProps); cardDefs['default'] = { label: schema?.title ?? 'Details', fields: allFieldNames, }; } // Resolve each card's field list for (const [name, cardCfg] of Object.entries(cardDefs)) { let fields: string[]; const columnOverrides: Record<string, string[]> = {}; const widgetList = cardCfg.widgets ?? cardCfg.fields; if (Array.isArray(widgetList)) { fields = []; for (const entry of widgetList) { if (typeof entry === 'string') { fields.push(entry); } else { // ICardWidgetEntry: encode as 'fieldName#id' const key = `${entry.name}#${entry.id}`; fields.push(key); columnOverrides[key] = (entry as ICardWidgetEntry).widgets; } } } else if (widgetList && typeof widgetList === 'object') { fields = Object.keys(widgetList); } else { // Derive from schema: all schema fields fields = Object.keys(schemaProps); } // Watch cards contain sub-fields of the watched array — skip top-level schema filter const isWatchCard = !!cardCfg.watch; // Only include fields present in the schema (unless it's a watch/detail card or schema is absent) // Supports dot-notation paths like 'input.input' and 'table#table1' column-override entries. // Custom editor widget names (e.g. 'Period') are allowed even when not in schema. const validFields = isWatchCard || !schema ? fields : fields.filter( f => schemaHasField( schemaProps as Record<string, IEnrichedFieldSchema>, f, ) || !!editors?.[f], ); cards[name] = { name, // undefined label → no card title (e.g. table widget provides its own title) label: 'label' in cardCfg ? cardCfg.label : name, fields: validFields, config: cardCfg, ...(Object.keys(columnOverrides).length > 0 ? {columnOverrides} : {}), }; } // Expand an editor widget name to its sub-properties for allFields tracking. // Other field names pass through unchanged. const expandField = (f: string): string[] => editors?.[f]?.properties ?? [f]; // Step 2: Resolve layout const layoutDef = layouts?.[layoutKey]; if (layoutDef && isTabLayout(layoutDef)) { // Tab / steps layout const tabs: IResolvedTab[] = layoutDef.items.map(item => ({ id: item.id, label: item.label, icon: item.icon, // Reuse flattenLayout so tab widgets follow the same deck-grouping rules // as the flat layout: 'a1' → [['a1']], ['a1','a2'] → [['a1','a2']] cardNames: item.component ? [] : flattenLayout(item.widgets as FlatLayoutConfig), component: item.component, })); const allTabCards = tabs.flatMap(t => t.cardNames.flat()); const allFields = [ ...new Set(allTabCards.flatMap(c => (cards[c]?.fields ?? []).flatMap(expandField))), ]; return { rows: [], cards, allFields, tabs, layoutType: layoutDef.type === 'steps' ? 'steps' : 'tabs', orientation: layoutDef.orientation, }; } if (layoutDef && isSplitLayout(layoutDef)) { // Split layout — resizable panels side by side const panels = layoutDef.panels; const allPanelCards = panels.flatMap(p => p.cards); const allFields = [ ...new Set( allPanelCards.flatMap(c => (cards[c]?.fields ?? []).flatMap(expandField)), ), ]; return { rows: [], cards, allFields, layoutType: 'split', orientation: layoutDef.orientation ?? 'horizontal', panels, }; } // Flat layout let rows: string[][]; if (layoutDef) { rows = flattenLayout(layoutDef as FlatLayoutConfig); } else { rows = Object.keys(cards).map(c => [c]); } const allFields = [ ...new Set(Object.values(cards).flatMap(c => c.fields.flatMap(expandField))), ]; return {rows, cards, allFields, layoutType: 'flat'}; }, [schema, cardsConfig, layoutKey, layouts, editors]); } /** Flatten layout definition to array of column groups. * * Each top-level element in the layout array = **one grid column**. * - A string element → one card alone in that column. * - An array element → multiple cards stacked vertically in that column. * * Examples: * 'habitat' → [['habitat']] (one column, one card) * ['edit', 'denied'] → [['edit', 'denied']] (one column, two stacked cards) * [['edit'], ['denied']] → [['edit', 'denied']] (also one column — inner arrays merge) */ function flattenLayout(layout: FlatLayoutConfig): string[][] { const result: string[][] = []; for (const row of layout) { if (typeof row === 'string') { result.push([row]); } else if (Array.isArray(row)) { // All items in this array share ONE grid column const columnCards: string[] = []; for (const item of row) { if (typeof item === 'string') { columnCards.push(item); } else if (Array.isArray(item)) { columnCards.push(...item); } } if (columnCards.length) result.push(columnCards); } } return result; } |