@specverse/engines 4.1.30 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/examples/manifests/frontend-only.yaml +3 -6
- package/assets/examples/manifests/fullstack-app.yaml +5 -7
- package/assets/examples/manifests/fullstack-monorepo.yaml +3 -6
- package/dist/inference/comprehensive-engine.d.ts.map +1 -1
- package/dist/inference/comprehensive-engine.js +3 -19
- package/dist/inference/comprehensive-engine.js.map +1 -1
- package/dist/inference/core/rule-engine.d.ts +31 -0
- package/dist/inference/core/rule-engine.d.ts.map +1 -1
- package/dist/inference/core/rule-engine.js +117 -33
- package/dist/inference/core/rule-engine.js.map +1 -1
- package/dist/inference/core/rule-file-types.d.ts +0 -2
- package/dist/inference/core/rule-file-types.d.ts.map +1 -1
- package/dist/inference/core/rule-file-types.js +3 -6
- package/dist/inference/core/rule-file-types.js.map +1 -1
- package/dist/inference/core/rule-loader.d.ts +5 -15
- package/dist/inference/core/rule-loader.d.ts.map +1 -1
- package/dist/inference/core/rule-loader.js +43 -132
- package/dist/inference/core/rule-loader.js.map +1 -1
- package/dist/inference/core/types.d.ts +0 -6
- package/dist/inference/core/types.d.ts.map +1 -1
- package/dist/inference/core/types.js +0 -4
- package/dist/inference/core/types.js.map +1 -1
- package/dist/inference/logical/generators/component-type-resolver.d.ts +0 -26
- package/dist/inference/logical/generators/component-type-resolver.d.ts.map +1 -1
- package/dist/inference/logical/generators/component-type-resolver.js +0 -19
- package/dist/inference/logical/generators/component-type-resolver.js.map +1 -1
- package/dist/inference/logical/generators/specialist-view-expander.d.ts +1 -17
- package/dist/inference/logical/generators/specialist-view-expander.d.ts.map +1 -1
- package/dist/inference/logical/generators/specialist-view-expander.js +0 -15
- package/dist/inference/logical/generators/specialist-view-expander.js.map +1 -1
- package/dist/inference/logical/generators/view-generator.d.ts +4 -14
- package/dist/inference/logical/generators/view-generator.d.ts.map +1 -1
- package/dist/inference/logical/generators/view-generator.js +6 -26
- package/dist/inference/logical/generators/view-generator.js.map +1 -1
- package/dist/inference/logical/index.d.ts +2 -2
- package/dist/inference/logical/index.d.ts.map +1 -1
- package/dist/inference/logical/logical-engine.d.ts.map +1 -1
- package/dist/inference/logical/logical-engine.js +17 -80
- package/dist/inference/logical/logical-engine.js.map +1 -1
- package/dist/inference/quint-transpiler.d.ts +5 -3
- package/dist/inference/quint-transpiler.d.ts.map +1 -1
- package/dist/inference/quint-transpiler.js +11 -6
- package/dist/inference/quint-transpiler.js.map +1 -1
- package/dist/libs/instance-factories/applications/templates/react-starter/app-tsx-generator.js +110 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/dashboard-body-composer.js +121 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/detail-body-composer.js +78 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/form-body-composer.js +190 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/helpers-emitter.js +45 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/html-to-jsx.js +192 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/list-body-composer.js +46 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/orchestrator.js +30 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/package-json-generator.js +38 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/regen-safety.js +89 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/view-emitter.js +56 -0
- package/dist/libs/instance-factories/applications/templates/react-starter/views-generator.js +66 -0
- package/dist/libs/instance-factories/cli/templates/commander/command-generator.js +14 -11
- package/dist/realize/index.d.ts.map +1 -1
- package/dist/realize/index.js +15 -22
- package/dist/realize/index.js.map +1 -1
- package/dist/registry/utils/manifest-adapter.d.ts +8 -1
- package/dist/registry/utils/manifest-adapter.d.ts.map +1 -1
- package/dist/registry/utils/manifest-adapter.js +8 -1
- package/dist/registry/utils/manifest-adapter.js.map +1 -1
- package/libs/instance-factories/applications/react-app-starter.yaml +150 -0
- package/libs/instance-factories/applications/templates/react-starter/README.md +211 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/dashboard-body-composer.test.ts +153 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/detail-body-composer.test.ts +145 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/form-body-composer.test.ts +175 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/helpers-emitter.test.ts +55 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/html-to-jsx.test.ts +140 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/list-body-composer.test.ts +146 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/orchestrator.test.ts +163 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/parity-p2-factory-imports.test.ts +116 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/parity-p3-rendered-output.test.ts +183 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/regen-safety.test.ts +144 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/starter-generators.test.ts +114 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/view-emitter.test.ts +107 -0
- package/libs/instance-factories/applications/templates/react-starter/__tests__/views-generator.test.ts +139 -0
- package/libs/instance-factories/applications/templates/react-starter/app-tsx-generator.ts +141 -0
- package/libs/instance-factories/applications/templates/react-starter/dashboard-body-composer.ts +174 -0
- package/libs/instance-factories/applications/templates/react-starter/detail-body-composer.ts +135 -0
- package/libs/instance-factories/applications/templates/react-starter/form-body-composer.ts +306 -0
- package/libs/instance-factories/applications/templates/react-starter/helpers-emitter.ts +60 -0
- package/libs/instance-factories/applications/templates/react-starter/html-to-jsx.ts +334 -0
- package/libs/instance-factories/applications/templates/react-starter/list-body-composer.ts +120 -0
- package/libs/instance-factories/applications/templates/react-starter/orchestrator.ts +80 -0
- package/libs/instance-factories/applications/templates/react-starter/package-json-generator.ts +57 -0
- package/libs/instance-factories/applications/templates/react-starter/regen-safety.ts +157 -0
- package/libs/instance-factories/applications/templates/react-starter/skeletons/dashboard.tsx.template +47 -0
- package/libs/instance-factories/applications/templates/react-starter/skeletons/detail.tsx.template +94 -0
- package/libs/instance-factories/applications/templates/react-starter/skeletons/form.tsx.template +114 -0
- package/libs/instance-factories/applications/templates/react-starter/skeletons/list.tsx.template +72 -0
- package/libs/instance-factories/applications/templates/react-starter/view-emitter.ts +151 -0
- package/libs/instance-factories/applications/templates/react-starter/views-generator.ts +137 -0
- package/libs/instance-factories/cli/templates/commander/command-generator.ts +14 -11
- package/package.json +3 -3
- package/dist/libs/instance-factories/applications/templates/react/_view-components-source.js +0 -530
- package/dist/libs/instance-factories/applications/templates/react/app-tsx-generator.js +0 -73
- package/dist/libs/instance-factories/applications/templates/react/field-helpers-generator.js +0 -99
- package/dist/libs/instance-factories/applications/templates/react/package-json-generator.js +0 -49
- package/dist/libs/instance-factories/applications/templates/react/pattern-adapter-generator.js +0 -156
- package/dist/libs/instance-factories/applications/templates/react/react-pattern-adapter.js +0 -935
- package/dist/libs/instance-factories/applications/templates/react/relationship-field-generator.js +0 -143
- package/dist/libs/instance-factories/applications/templates/react/tailwind-adapter-generator.js +0 -646
- package/dist/libs/instance-factories/applications/templates/react/tailwind-adapter-wrapper-generator.js +0 -65
- package/dist/libs/instance-factories/applications/templates/react/view-dashboard-generator.js +0 -143
- package/dist/libs/instance-factories/applications/templates/react/view-detail-generator.js +0 -143
- package/dist/libs/instance-factories/applications/templates/react/view-form-generator.js +0 -355
- package/dist/libs/instance-factories/applications/templates/react/view-list-generator.js +0 -91
- package/dist/libs/instance-factories/applications/templates/react/view-router-generator.js +0 -79
- package/dist/libs/instance-factories/views/index.js +0 -48
- package/dist/libs/instance-factories/views/templates/react/adapters/antd-adapter.js +0 -742
- package/dist/libs/instance-factories/views/templates/react/adapters/mui-adapter.js +0 -824
- package/dist/libs/instance-factories/views/templates/react/adapters/shadcn-adapter.js +0 -719
- package/dist/libs/instance-factories/views/templates/react/app-generator.js +0 -45
- package/dist/libs/instance-factories/views/templates/react/components-generator.js +0 -820
- package/dist/libs/instance-factories/views/templates/react/forms-generator.js +0 -275
- package/dist/libs/instance-factories/views/templates/react/frontend-package-json-generator.js +0 -46
- package/dist/libs/instance-factories/views/templates/react/hooks-generator.js +0 -81
- package/dist/libs/instance-factories/views/templates/react/index-css-generator.js +0 -9
- package/dist/libs/instance-factories/views/templates/react/index-html-generator.js +0 -23
- package/dist/libs/instance-factories/views/templates/react/main-tsx-generator.js +0 -21
- package/dist/libs/instance-factories/views/templates/react/react-component-generator.js +0 -299
- package/dist/libs/instance-factories/views/templates/react/router-generator.js +0 -136
- package/dist/libs/instance-factories/views/templates/react/router-generic-generator.js +0 -107
- package/dist/libs/instance-factories/views/templates/react/shared-utils-generator.js +0 -187
- package/dist/libs/instance-factories/views/templates/react/spec-json-generator.js +0 -7
- package/dist/libs/instance-factories/views/templates/react/types-generator.js +0 -56
- package/dist/libs/instance-factories/views/templates/react/views-metadata-generator.js +0 -27
- package/dist/libs/instance-factories/views/templates/react/vite-config-generator.js +0 -29
- package/dist/libs/instance-factories/views/templates/runtime/runtime-view-renderer.js +0 -261
- package/dist/libs/instance-factories/views/templates/shared/adapter-types.js +0 -34
- package/dist/libs/instance-factories/views/templates/shared/atomic-components-registry.js +0 -800
- package/dist/libs/instance-factories/views/templates/shared/base-generator.js +0 -305
- package/dist/libs/instance-factories/views/templates/shared/component-metadata.js +0 -517
- package/dist/libs/instance-factories/views/templates/shared/composite-pattern-types.js +0 -0
- package/dist/libs/instance-factories/views/templates/shared/composite-patterns.js +0 -445
- package/dist/libs/instance-factories/views/templates/shared/index.js +0 -80
- package/dist/libs/instance-factories/views/templates/shared/pattern-validator.js +0 -210
- package/dist/libs/instance-factories/views/templates/shared/property-mapper.js +0 -492
- package/dist/libs/instance-factories/views/templates/shared/syntax-mapper.js +0 -321
- package/dist/realize/index.js.bak +0 -758
- package/libs/instance-factories/applications/react-app.yaml +0 -186
- package/libs/instance-factories/applications/templates/react/_view-components-source.ts +0 -555
- package/libs/instance-factories/applications/templates/react/app-tsx-generator.ts +0 -94
- package/libs/instance-factories/applications/templates/react/field-helpers-generator.ts +0 -106
- package/libs/instance-factories/applications/templates/react/package-json-generator.ts +0 -57
- package/libs/instance-factories/applications/templates/react/pattern-adapter-generator.ts +0 -179
- package/libs/instance-factories/applications/templates/react/react-pattern-adapter.tsx +0 -1347
- package/libs/instance-factories/applications/templates/react/relationship-field-generator.ts +0 -150
- package/libs/instance-factories/applications/templates/react/tailwind-adapter-generator.ts +0 -704
- package/libs/instance-factories/applications/templates/react/tailwind-adapter-wrapper-generator.ts +0 -84
- package/libs/instance-factories/applications/templates/react/view-dashboard-generator.ts +0 -150
- package/libs/instance-factories/applications/templates/react/view-detail-generator.ts +0 -150
- package/libs/instance-factories/applications/templates/react/view-form-generator.ts +0 -362
- package/libs/instance-factories/applications/templates/react/view-list-generator.ts +0 -98
- package/libs/instance-factories/applications/templates/react/view-router-generator.ts +0 -89
- package/libs/instance-factories/views/README.md +0 -62
- package/libs/instance-factories/views/index.d.ts +0 -13
- package/libs/instance-factories/views/index.d.ts.map +0 -1
- package/libs/instance-factories/views/index.js +0 -18
- package/libs/instance-factories/views/index.js.map +0 -1
- package/libs/instance-factories/views/index.ts +0 -45
- package/libs/instance-factories/views/react-components.yaml +0 -129
- package/libs/instance-factories/views/templates/ARCHITECTURE.md +0 -198
- package/libs/instance-factories/views/templates/react/adapters/antd-adapter.ts +0 -869
- package/libs/instance-factories/views/templates/react/adapters/mui-adapter.ts +0 -953
- package/libs/instance-factories/views/templates/react/adapters/shadcn-adapter.ts +0 -806
- package/libs/instance-factories/views/templates/react/app-generator.ts +0 -55
- package/libs/instance-factories/views/templates/react/components-generator.ts +0 -938
- package/libs/instance-factories/views/templates/react/forms-generator.ts +0 -325
- package/libs/instance-factories/views/templates/react/frontend-package-json-generator.ts +0 -57
- package/libs/instance-factories/views/templates/react/hooks-generator.ts +0 -106
- package/libs/instance-factories/views/templates/react/index-css-generator.ts +0 -14
- package/libs/instance-factories/views/templates/react/index-html-generator.ts +0 -34
- package/libs/instance-factories/views/templates/react/main-tsx-generator.ts +0 -29
- package/libs/instance-factories/views/templates/react/react-component-generator.d.ts +0 -152
- package/libs/instance-factories/views/templates/react/react-component-generator.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/react/react-component-generator.js +0 -398
- package/libs/instance-factories/views/templates/react/react-component-generator.js.map +0 -1
- package/libs/instance-factories/views/templates/react/react-component-generator.ts +0 -533
- package/libs/instance-factories/views/templates/react/router-generator.ts +0 -197
- package/libs/instance-factories/views/templates/react/router-generic-generator.ts +0 -132
- package/libs/instance-factories/views/templates/react/shared-utils-generator.ts +0 -196
- package/libs/instance-factories/views/templates/react/spec-json-generator.ts +0 -17
- package/libs/instance-factories/views/templates/react/types-generator.ts +0 -76
- package/libs/instance-factories/views/templates/react/views-metadata-generator.ts +0 -42
- package/libs/instance-factories/views/templates/react/vite-config-generator.ts +0 -38
- package/libs/instance-factories/views/templates/runtime/runtime-view-renderer.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/runtime/runtime-view-renderer.js.map +0 -1
- package/libs/instance-factories/views/templates/runtime/runtime-view-renderer.ts +0 -474
- package/libs/instance-factories/views/templates/shared/__tests__/composite-patterns.test.ts +0 -242
- package/libs/instance-factories/views/templates/shared/adapter-types.d.ts +0 -77
- package/libs/instance-factories/views/templates/shared/adapter-types.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/shared/adapter-types.js +0 -47
- package/libs/instance-factories/views/templates/shared/adapter-types.js.map +0 -1
- package/libs/instance-factories/views/templates/shared/adapter-types.ts +0 -142
- package/libs/instance-factories/views/templates/shared/atomic-components-registry.d.ts +0 -63
- package/libs/instance-factories/views/templates/shared/atomic-components-registry.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/shared/atomic-components-registry.js +0 -822
- package/libs/instance-factories/views/templates/shared/atomic-components-registry.js.map +0 -1
- package/libs/instance-factories/views/templates/shared/atomic-components-registry.ts +0 -908
- package/libs/instance-factories/views/templates/shared/base-generator.d.ts +0 -247
- package/libs/instance-factories/views/templates/shared/base-generator.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/shared/base-generator.js +0 -363
- package/libs/instance-factories/views/templates/shared/base-generator.js.map +0 -1
- package/libs/instance-factories/views/templates/shared/base-generator.ts +0 -608
- package/libs/instance-factories/views/templates/shared/component-metadata.d.ts +0 -254
- package/libs/instance-factories/views/templates/shared/component-metadata.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/shared/component-metadata.js +0 -602
- package/libs/instance-factories/views/templates/shared/component-metadata.js.map +0 -1
- package/libs/instance-factories/views/templates/shared/component-metadata.ts +0 -803
- package/libs/instance-factories/views/templates/shared/composite-pattern-types.ts +0 -250
- package/libs/instance-factories/views/templates/shared/composite-patterns.ts +0 -535
- package/libs/instance-factories/views/templates/shared/index.ts +0 -68
- package/libs/instance-factories/views/templates/shared/pattern-validator.ts +0 -279
- package/libs/instance-factories/views/templates/shared/property-mapper.d.ts +0 -149
- package/libs/instance-factories/views/templates/shared/property-mapper.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/shared/property-mapper.js +0 -580
- package/libs/instance-factories/views/templates/shared/property-mapper.js.map +0 -1
- package/libs/instance-factories/views/templates/shared/property-mapper.ts +0 -700
- package/libs/instance-factories/views/templates/shared/syntax-mapper.d.ts +0 -143
- package/libs/instance-factories/views/templates/shared/syntax-mapper.d.ts.map +0 -1
- package/libs/instance-factories/views/templates/shared/syntax-mapper.js +0 -420
- package/libs/instance-factories/views/templates/shared/syntax-mapper.js.map +0 -1
- package/libs/instance-factories/views/templates/shared/syntax-mapper.ts +0 -539
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import * as ts from 'typescript';
|
|
3
|
+
import { generate, type ExpandedSpec } from '../views-generator.js';
|
|
4
|
+
|
|
5
|
+
function makeSpec(): ExpandedSpec {
|
|
6
|
+
return {
|
|
7
|
+
models: {
|
|
8
|
+
Post: {
|
|
9
|
+
name: 'Post',
|
|
10
|
+
attributes: {
|
|
11
|
+
id: { type: 'UUID', required: true, auto: 'uuid4' },
|
|
12
|
+
title: { type: 'String', required: true },
|
|
13
|
+
body: { type: 'Text', required: false },
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
Author: {
|
|
17
|
+
name: 'Author',
|
|
18
|
+
attributes: {
|
|
19
|
+
id: { type: 'UUID', required: true, auto: 'uuid4' },
|
|
20
|
+
name: { type: 'String', required: true },
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
views: {},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function assertValidTsx(source: string, label: string): void {
|
|
29
|
+
const result = ts.transpileModule(source, {
|
|
30
|
+
compilerOptions: {
|
|
31
|
+
jsx: ts.JsxEmit.Preserve,
|
|
32
|
+
target: ts.ScriptTarget.ES2022,
|
|
33
|
+
module: ts.ModuleKind.ESNext,
|
|
34
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
35
|
+
strict: false,
|
|
36
|
+
},
|
|
37
|
+
reportDiagnostics: true,
|
|
38
|
+
});
|
|
39
|
+
const errors = result.diagnostics?.filter(d => d.category === ts.DiagnosticCategory.Error) ?? [];
|
|
40
|
+
if (errors.length > 0) {
|
|
41
|
+
const message = errors
|
|
42
|
+
.map(d => ts.flattenDiagnosticMessageText(d.messageText, '\n'))
|
|
43
|
+
.join('\n');
|
|
44
|
+
throw new Error(`${label} failed to parse as TSX:\n${message}\n\n--- source ---\n${source}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe('views-generator.generate', () => {
|
|
49
|
+
it('emits 4 view files per model plus the helpers file', async () => {
|
|
50
|
+
const files = await generate({ spec: makeSpec() });
|
|
51
|
+
const paths = Object.keys(files);
|
|
52
|
+
expect(paths).toContain('src/views/PostListView.tsx');
|
|
53
|
+
expect(paths).toContain('src/views/PostDetailView.tsx');
|
|
54
|
+
expect(paths).toContain('src/views/PostFormView.tsx');
|
|
55
|
+
expect(paths).toContain('src/views/PostDashboardView.tsx');
|
|
56
|
+
expect(paths).toContain('src/views/AuthorListView.tsx');
|
|
57
|
+
expect(paths).toContain('src/views/AuthorDetailView.tsx');
|
|
58
|
+
expect(paths).toContain('src/views/AuthorFormView.tsx');
|
|
59
|
+
expect(paths).toContain('src/views/AuthorDashboardView.tsx');
|
|
60
|
+
expect(paths).toContain('src/lib/entity-display.ts');
|
|
61
|
+
// 2 models × 4 view types + 1 helper = 9 files.
|
|
62
|
+
expect(paths.length).toBe(9);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('every emitted view file parses as valid TSX', async () => {
|
|
66
|
+
const files = await generate({ spec: makeSpec() });
|
|
67
|
+
for (const [path, source] of Object.entries(files)) {
|
|
68
|
+
if (path.endsWith('.tsx')) assertValidTsx(source, path);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('uses a user-declared view spec in preference to a synthesized default', async () => {
|
|
73
|
+
const spec: ExpandedSpec = {
|
|
74
|
+
models: {
|
|
75
|
+
Post: {
|
|
76
|
+
name: 'Post',
|
|
77
|
+
attributes: {
|
|
78
|
+
id: { type: 'UUID', auto: 'uuid4' },
|
|
79
|
+
title: { type: 'String', required: true },
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
views: {
|
|
84
|
+
PostListView: {
|
|
85
|
+
type: 'list',
|
|
86
|
+
model: 'Post',
|
|
87
|
+
uiComponents: {
|
|
88
|
+
customTable: {
|
|
89
|
+
type: 'table',
|
|
90
|
+
properties: {
|
|
91
|
+
columns: ['SUPER CUSTOM COLUMN'],
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
const files = await generate({ spec });
|
|
99
|
+
const out = files['src/views/PostListView.tsx'];
|
|
100
|
+
// The user's custom column header is not directly visible because
|
|
101
|
+
// the current composer ignores user uiComponents (it infers
|
|
102
|
+
// columns from the model). But the view spec flows through — if
|
|
103
|
+
// we later teach the composer to honour uiComponents, this test
|
|
104
|
+
// anchors the contract.
|
|
105
|
+
expect(out).toBeDefined();
|
|
106
|
+
expect(out).toContain('export function PostListView');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('warns on specialist view types and skips them', async () => {
|
|
110
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
111
|
+
try {
|
|
112
|
+
const spec: ExpandedSpec = {
|
|
113
|
+
models: {
|
|
114
|
+
Post: { name: 'Post', attributes: { id: { auto: 'uuid4' }, title: {} } },
|
|
115
|
+
},
|
|
116
|
+
views: {
|
|
117
|
+
PostBoardView: { type: 'board', model: 'Post' },
|
|
118
|
+
PostTimelineView: { type: 'timeline', model: 'Post' },
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
const files = await generate({ spec });
|
|
122
|
+
expect(files['src/views/PostBoardView.tsx']).toBeUndefined();
|
|
123
|
+
expect(files['src/views/PostTimelineView.tsx']).toBeUndefined();
|
|
124
|
+
expect(warn).toHaveBeenCalled();
|
|
125
|
+
const warning = warn.mock.calls[0][0] as string;
|
|
126
|
+
expect(warning).toContain('Skipped');
|
|
127
|
+
expect(warning).toContain('board');
|
|
128
|
+
expect(warning).toContain('timeline');
|
|
129
|
+
} finally {
|
|
130
|
+
warn.mockRestore();
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('handles an empty models map gracefully', async () => {
|
|
135
|
+
const files = await generate({ spec: { models: {}, views: {} } });
|
|
136
|
+
// Only the helpers file is emitted; no view files.
|
|
137
|
+
expect(Object.keys(files)).toEqual(['src/lib/entity-display.ts']);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* App.tsx generator for ReactAppStarter
|
|
3
|
+
*
|
|
4
|
+
* Generates a simple App shell that routes between the emitted view
|
|
5
|
+
* components. Not using react-router — the starter output is
|
|
6
|
+
* deliberately low-magic. Users can add routing when they need
|
|
7
|
+
* URL-level view selection; for now, internal state drives it.
|
|
8
|
+
*
|
|
9
|
+
* Output:
|
|
10
|
+
* - sidebar nav listing every (model × view-type)
|
|
11
|
+
* - a main area rendering the selected view
|
|
12
|
+
* - QueryClientProvider at the root
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { ExpandedSpec } from './views-generator.js';
|
|
16
|
+
|
|
17
|
+
export interface AppGeneratorContext {
|
|
18
|
+
spec: ExpandedSpec;
|
|
19
|
+
manifest?: unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function generate(context: AppGeneratorContext): Promise<string> {
|
|
23
|
+
const models = Object.keys(context.spec.models ?? {});
|
|
24
|
+
const imports = buildImports(models);
|
|
25
|
+
const navEntries = buildNavEntries(models);
|
|
26
|
+
const viewSwitch = buildViewSwitch(models);
|
|
27
|
+
|
|
28
|
+
return `/**
|
|
29
|
+
* App.tsx — generated by @specverse/realize (ReactAppStarter)
|
|
30
|
+
*
|
|
31
|
+
* Safe to edit. Edits are preserved across regeneration via content
|
|
32
|
+
* hashing. This is a minimal shell: a sidebar listing every
|
|
33
|
+
* (model, view-type) plus a main area that renders the selection.
|
|
34
|
+
* Swap in react-router or your preferred routing library when the
|
|
35
|
+
* app needs URL-driven navigation.
|
|
36
|
+
*/
|
|
37
|
+
import { useState } from 'react';
|
|
38
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
39
|
+
${imports}
|
|
40
|
+
|
|
41
|
+
const queryClient = new QueryClient({
|
|
42
|
+
defaultOptions: {
|
|
43
|
+
queries: { staleTime: 5 * 60 * 1000, retry: 1 },
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
type Selection = {
|
|
48
|
+
model: string;
|
|
49
|
+
view: 'list' | 'detail' | 'form' | 'dashboard';
|
|
50
|
+
entityId?: string | number;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function Inner() {
|
|
54
|
+
const [selection, setSelection] = useState<Selection>(${models.length > 0 ? `{ model: '${models[0]}', view: 'list' }` : `{ model: '', view: 'list' }`});
|
|
55
|
+
|
|
56
|
+
const select = (model: string, view: Selection['view']) =>
|
|
57
|
+
setSelection({ model, view });
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<div className="min-h-screen flex bg-gray-50 dark:bg-gray-950">
|
|
61
|
+
<aside className="w-64 shrink-0 border-r border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900">
|
|
62
|
+
<div className="p-4 border-b border-gray-200 dark:border-gray-800">
|
|
63
|
+
<h1 className="text-lg font-semibold text-gray-900 dark:text-gray-100">App</h1>
|
|
64
|
+
</div>
|
|
65
|
+
<nav className="p-2 space-y-4">
|
|
66
|
+
${navEntries}
|
|
67
|
+
</nav>
|
|
68
|
+
</aside>
|
|
69
|
+
|
|
70
|
+
<main className="flex-1 overflow-auto">
|
|
71
|
+
${viewSwitch}
|
|
72
|
+
</main>
|
|
73
|
+
</div>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default function App() {
|
|
78
|
+
return (
|
|
79
|
+
<QueryClientProvider client={queryClient}>
|
|
80
|
+
<Inner />
|
|
81
|
+
</QueryClientProvider>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
88
|
+
// Helpers
|
|
89
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
90
|
+
|
|
91
|
+
function buildImports(models: string[]): string {
|
|
92
|
+
return models.flatMap(m => [
|
|
93
|
+
`import { ${m}ListView } from './views/${m}ListView';`,
|
|
94
|
+
`import { ${m}DetailView } from './views/${m}DetailView';`,
|
|
95
|
+
`import { ${m}FormView } from './views/${m}FormView';`,
|
|
96
|
+
`import { ${m}DashboardView } from './views/${m}DashboardView';`,
|
|
97
|
+
]).join('\n');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function buildNavEntries(models: string[]): string {
|
|
101
|
+
if (models.length === 0) {
|
|
102
|
+
return ' <p className="text-sm text-gray-400 px-2">No models in this spec.</p>';
|
|
103
|
+
}
|
|
104
|
+
return models.map(m => ` <div>
|
|
105
|
+
<div className="px-2 pb-1 text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">
|
|
106
|
+
${m}
|
|
107
|
+
</div>
|
|
108
|
+
<button type="button" onClick={() => select('${m}', 'list')} className="${navButtonCls('list')}">List</button>
|
|
109
|
+
<button type="button" onClick={() => select('${m}', 'dashboard')} className="${navButtonCls('dashboard')}">Dashboard</button>
|
|
110
|
+
<button type="button" onClick={() => select('${m}', 'form')} className="${navButtonCls('form')}">New</button>
|
|
111
|
+
</div>`).join('\n');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function navButtonCls(_view: string): string {
|
|
115
|
+
return (
|
|
116
|
+
'block w-full text-left rounded px-2 py-1 text-sm ' +
|
|
117
|
+
'text-gray-700 hover:bg-gray-100 ' +
|
|
118
|
+
'dark:text-gray-300 dark:hover:bg-gray-800'
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function buildViewSwitch(models: string[]): string {
|
|
123
|
+
if (models.length === 0) {
|
|
124
|
+
return ' <p className="p-6 text-sm text-gray-400">No models — add one to your .specly file and run <code>spv realize</code>.</p>';
|
|
125
|
+
}
|
|
126
|
+
const branches = models.flatMap(m => [
|
|
127
|
+
` {selection.model === '${m}' && selection.view === 'list' && (
|
|
128
|
+
<${m}ListView onSelect={item => setSelection({ model: '${m}', view: 'detail', entityId: (item as any).id })} onCreate={() => setSelection({ model: '${m}', view: 'form' })} />
|
|
129
|
+
)}`,
|
|
130
|
+
` {selection.model === '${m}' && selection.view === 'detail' && selection.entityId !== undefined && (
|
|
131
|
+
<${m}DetailView entityId={selection.entityId} onEdit={item => setSelection({ model: '${m}', view: 'form', entityId: (item as any).id })} onBack={() => setSelection({ model: '${m}', view: 'list' })} onDeleted={() => setSelection({ model: '${m}', view: 'list' })} />
|
|
132
|
+
)}`,
|
|
133
|
+
` {selection.model === '${m}' && selection.view === 'form' && (
|
|
134
|
+
<${m}FormView mode={selection.entityId ? 'update' : 'create'} entityId={selection.entityId} onSuccess={() => setSelection({ model: '${m}', view: 'list' })} onCancel={() => setSelection({ model: '${m}', view: 'list' })} />
|
|
135
|
+
)}`,
|
|
136
|
+
` {selection.model === '${m}' && selection.view === 'dashboard' && (
|
|
137
|
+
<${m}DashboardView onSelect={item => setSelection({ model: '${m}', view: 'detail', entityId: (item as any).id })} />
|
|
138
|
+
)}`,
|
|
139
|
+
]);
|
|
140
|
+
return branches.join('\n');
|
|
141
|
+
}
|
package/libs/instance-factories/applications/templates/react-starter/dashboard-body-composer.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dashboard-view body composer for ReactAppStarter
|
|
3
|
+
*
|
|
4
|
+
* Minimal starter-kit dashboard: metric cards derived from the list
|
|
5
|
+
* query (total count; enum-field breakdowns if the model has a status-
|
|
6
|
+
* like attribute), plus a compact preview of recent records.
|
|
7
|
+
*
|
|
8
|
+
* Aggregation / charts are deferred — they need backend endpoints
|
|
9
|
+
* (`/api/posts/metrics`, `/api/posts/by-month`, …) that the starter
|
|
10
|
+
* kit doesn't assume. TODO comments flag the extension points.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { METADATA_FIELDS } from '@specverse/runtime/views/core';
|
|
14
|
+
import type { EmitContext, ModelSpec } from './view-emitter.js';
|
|
15
|
+
|
|
16
|
+
const METADATA_FIELD_NAMES = new Set(METADATA_FIELDS);
|
|
17
|
+
|
|
18
|
+
export function composeDashboardBody(context: EmitContext): string {
|
|
19
|
+
const modelName = context.model.name;
|
|
20
|
+
const previewColumns = inferPreviewColumns(context.model);
|
|
21
|
+
const enumFields = inferEnumFields(context.model);
|
|
22
|
+
|
|
23
|
+
const lines: string[] = [];
|
|
24
|
+
|
|
25
|
+
// ── Metrics row ────────────────────────────────────────────────────
|
|
26
|
+
lines.push('<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">');
|
|
27
|
+
lines.push(...renderTotalCard(modelName));
|
|
28
|
+
for (const enumField of enumFields) {
|
|
29
|
+
lines.push(...renderEnumBreakdownCard(enumField));
|
|
30
|
+
}
|
|
31
|
+
if (enumFields.length === 0) {
|
|
32
|
+
lines.push(...renderPlaceholderCard());
|
|
33
|
+
}
|
|
34
|
+
lines.push('</div>');
|
|
35
|
+
|
|
36
|
+
// ── TODO: aggregation metrics ──────────────────────────────────────
|
|
37
|
+
lines.push('');
|
|
38
|
+
lines.push('{/* TODO: add aggregation metrics (averages / sums / time series)');
|
|
39
|
+
lines.push(' by adding backend endpoints and per-metric hooks. Wire them in');
|
|
40
|
+
lines.push(' alongside the count cards above. */}');
|
|
41
|
+
|
|
42
|
+
// ── Recent-items preview ───────────────────────────────────────────
|
|
43
|
+
lines.push('');
|
|
44
|
+
lines.push('<div className="rounded-lg border border-gray-200 bg-white dark:border-gray-700 dark:bg-gray-900">');
|
|
45
|
+
lines.push(' <div className="border-b border-gray-200 dark:border-gray-700 px-6 py-3">');
|
|
46
|
+
lines.push(' <h3 className="text-sm font-semibold text-gray-700 dark:text-gray-200 uppercase tracking-wider">');
|
|
47
|
+
lines.push(` Recent ${humanize(modelName)}s`);
|
|
48
|
+
lines.push(' </h3>');
|
|
49
|
+
lines.push(' </div>');
|
|
50
|
+
|
|
51
|
+
if (previewColumns.length === 0) {
|
|
52
|
+
lines.push(' <div className="px-6 py-4 text-sm text-gray-400">No displayable fields.</div>');
|
|
53
|
+
} else {
|
|
54
|
+
lines.push(' <table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">');
|
|
55
|
+
lines.push(' <thead>');
|
|
56
|
+
lines.push(' <tr>');
|
|
57
|
+
for (const col of previewColumns) {
|
|
58
|
+
lines.push(
|
|
59
|
+
` <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">` +
|
|
60
|
+
humanize(col) +
|
|
61
|
+
`</th>`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
lines.push(' </tr>');
|
|
65
|
+
lines.push(' </thead>');
|
|
66
|
+
lines.push(' <tbody className="divide-y divide-gray-200 dark:divide-gray-700">');
|
|
67
|
+
lines.push(' {preview.map((item, idx) => (');
|
|
68
|
+
lines.push(' <tr');
|
|
69
|
+
lines.push(' key={idx}');
|
|
70
|
+
lines.push(' onClick={() => onSelect?.(item)}');
|
|
71
|
+
lines.push(' className="hover:bg-gray-50 dark:hover:bg-gray-800 cursor-pointer"');
|
|
72
|
+
lines.push(' >');
|
|
73
|
+
for (const col of previewColumns) {
|
|
74
|
+
lines.push(
|
|
75
|
+
` <td className="px-6 py-4 text-sm text-gray-700 dark:text-gray-300">` +
|
|
76
|
+
`{String((item as any).${col} ?? '')}</td>`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
lines.push(' </tr>');
|
|
80
|
+
lines.push(' ))}');
|
|
81
|
+
lines.push(' {preview.length === 0 && (');
|
|
82
|
+
lines.push(` <tr><td colSpan={${previewColumns.length}} className="px-6 py-4 text-sm text-gray-400">No records yet.</td></tr>`);
|
|
83
|
+
lines.push(' )}');
|
|
84
|
+
lines.push(' </tbody>');
|
|
85
|
+
lines.push(' </table>');
|
|
86
|
+
}
|
|
87
|
+
lines.push('</div>');
|
|
88
|
+
|
|
89
|
+
return lines.join('\n');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
93
|
+
// Card rendering
|
|
94
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
function renderTotalCard(modelName: string): string[] {
|
|
97
|
+
const pluralLower = humanize(modelName).toLowerCase() + 's';
|
|
98
|
+
return [
|
|
99
|
+
' <div className="rounded-lg border border-gray-200 bg-white p-6 shadow-sm dark:border-gray-700 dark:bg-gray-900">',
|
|
100
|
+
' <p className="text-xs font-medium uppercase tracking-wide text-gray-500 dark:text-gray-400">',
|
|
101
|
+
` Total ${pluralLower}`,
|
|
102
|
+
' </p>',
|
|
103
|
+
' <p className="mt-2 text-3xl font-semibold text-gray-900 dark:text-gray-100">',
|
|
104
|
+
' {items.length}',
|
|
105
|
+
' </p>',
|
|
106
|
+
' </div>',
|
|
107
|
+
];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function renderEnumBreakdownCard(field: EnumField): string[] {
|
|
111
|
+
// One card per enum value — shows count of records where the field
|
|
112
|
+
// equals that value.
|
|
113
|
+
const cards: string[] = [];
|
|
114
|
+
for (const value of field.values) {
|
|
115
|
+
cards.push(
|
|
116
|
+
' <div className="rounded-lg border border-gray-200 bg-white p-6 shadow-sm dark:border-gray-700 dark:bg-gray-900">',
|
|
117
|
+
' <p className="text-xs font-medium uppercase tracking-wide text-gray-500 dark:text-gray-400">',
|
|
118
|
+
` ${humanize(field.name)}: ${humanize(value)}`,
|
|
119
|
+
' </p>',
|
|
120
|
+
' <p className="mt-2 text-3xl font-semibold text-gray-900 dark:text-gray-100">',
|
|
121
|
+
` {items.filter((i: any) => i.${field.name} === ${JSON.stringify(value)}).length}`,
|
|
122
|
+
' </p>',
|
|
123
|
+
' </div>',
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
return cards;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function renderPlaceholderCard(): string[] {
|
|
130
|
+
return [
|
|
131
|
+
' <div className="rounded-lg border border-dashed border-gray-300 p-6 text-sm text-gray-400 dark:border-gray-600">',
|
|
132
|
+
' Add a metric here — e.g. a sum, average, or time-windowed count.',
|
|
133
|
+
' </div>',
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
138
|
+
// Field inference
|
|
139
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
interface EnumField {
|
|
142
|
+
name: string;
|
|
143
|
+
values: string[];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function inferEnumFields(model: ModelSpec): EnumField[] {
|
|
147
|
+
const out: EnumField[] = [];
|
|
148
|
+
const attrs = model.attributes ?? {};
|
|
149
|
+
for (const [name, rawDef] of Object.entries(attrs)) {
|
|
150
|
+
const def = rawDef as { values?: string[] };
|
|
151
|
+
const values = def?.values;
|
|
152
|
+
if (Array.isArray(values) && values.length > 0 && values.length <= 6) {
|
|
153
|
+
out.push({ name, values });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// Cap: one enum breakdown in the card row to avoid card sprawl. The
|
|
157
|
+
// remaining enum fields stay available in the list view; users can
|
|
158
|
+
// promote more breakdowns manually.
|
|
159
|
+
return out.slice(0, 1);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function inferPreviewColumns(model: ModelSpec): string[] {
|
|
163
|
+
const attrs = model.attributes ?? {};
|
|
164
|
+
return Object.keys(attrs)
|
|
165
|
+
.filter(n => !METADATA_FIELD_NAMES.has(n))
|
|
166
|
+
.slice(0, 4); // cap to keep the table readable
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function humanize(name: string): string {
|
|
170
|
+
return name
|
|
171
|
+
.replace(/([A-Z])/g, ' $1')
|
|
172
|
+
.replace(/^./, c => c.toUpperCase())
|
|
173
|
+
.trim();
|
|
174
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detail-view body composer for ReactAppStarter
|
|
3
|
+
*
|
|
4
|
+
* Renders the interior of a detail view as a JSX-safe definition list:
|
|
5
|
+
* business fields first (prominent), metadata fields second (muted).
|
|
6
|
+
* The outer card + actions live in `skeletons/detail.tsx.template`.
|
|
7
|
+
*
|
|
8
|
+
* Unlike list view, the interior here is mostly label→value pairs, not
|
|
9
|
+
* a table. We use `<dl>` / `<dt>` / `<dd>` directly rather than routing
|
|
10
|
+
* through a specific atomic component — the field-list shape isn't one
|
|
11
|
+
* of the adapter's atomic components, and forcing it through a `card`
|
|
12
|
+
* adapter would add structure without clarifying anything in the
|
|
13
|
+
* generated code.
|
|
14
|
+
*
|
|
15
|
+
* BelongsTo relationships: today we emit the raw FK id. A future pass
|
|
16
|
+
* will resolve FK → entity display name (matches runtime's
|
|
17
|
+
* getEntityDisplayName). Marked with a TODO comment in the output so
|
|
18
|
+
* users inspecting the generated file see the hook.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { METADATA_FIELDS } from '@specverse/runtime/views/core';
|
|
22
|
+
import type { EmitContext, ModelSpec } from './view-emitter.js';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Metadata attribute names rendered in a muted style. Sourced from
|
|
26
|
+
* the canonical pattern library so both this composer and the rest
|
|
27
|
+
* of the system stay in sync automatically.
|
|
28
|
+
*/
|
|
29
|
+
const METADATA_FIELD_NAMES = new Set(METADATA_FIELDS);
|
|
30
|
+
|
|
31
|
+
export function composeDetailBody(context: EmitContext): string {
|
|
32
|
+
const { business, metadata } = partitionFields(context.model);
|
|
33
|
+
const belongsTo = extractBelongsTo(context.model);
|
|
34
|
+
|
|
35
|
+
const lines: string[] = [];
|
|
36
|
+
lines.push('<div className="rounded-lg border border-gray-200 bg-white p-6 dark:border-gray-700 dark:bg-gray-900">');
|
|
37
|
+
|
|
38
|
+
if (business.length > 0) {
|
|
39
|
+
lines.push(' <dl className="grid grid-cols-1 gap-4 sm:grid-cols-2">');
|
|
40
|
+
for (const field of business) {
|
|
41
|
+
lines.push(...renderField(field, { muted: false }));
|
|
42
|
+
}
|
|
43
|
+
lines.push(' </dl>');
|
|
44
|
+
} else {
|
|
45
|
+
lines.push(' <p className="text-sm text-gray-400">No business fields defined for this model.</p>');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (belongsTo.length > 0) {
|
|
49
|
+
lines.push('');
|
|
50
|
+
lines.push(' {/* TODO: resolve FK ids → related entity display names.');
|
|
51
|
+
lines.push(' See @specverse/runtime/views/core/entity-display for the');
|
|
52
|
+
lines.push(' canonical resolver, or load the related record in a hook. */}');
|
|
53
|
+
lines.push(' <dl className="mt-6 grid grid-cols-1 gap-4 sm:grid-cols-2 border-t border-gray-200 dark:border-gray-700 pt-4">');
|
|
54
|
+
for (const rel of belongsTo) {
|
|
55
|
+
lines.push(...renderField({ name: `${rel}Id`, label: humanize(rel) }, { muted: false }));
|
|
56
|
+
}
|
|
57
|
+
lines.push(' </dl>');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (metadata.length > 0) {
|
|
61
|
+
lines.push('');
|
|
62
|
+
lines.push(' <dl className="mt-6 grid grid-cols-1 gap-2 sm:grid-cols-2 border-t border-gray-200 dark:border-gray-700 pt-4 text-xs text-gray-500 dark:text-gray-400">');
|
|
63
|
+
for (const field of metadata) {
|
|
64
|
+
lines.push(...renderField(field, { muted: true }));
|
|
65
|
+
}
|
|
66
|
+
lines.push(' </dl>');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
lines.push('</div>');
|
|
70
|
+
return lines.join('\n');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface Field {
|
|
74
|
+
name: string;
|
|
75
|
+
/** Display label; defaults to humanize(name). */
|
|
76
|
+
label?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function partitionFields(model: ModelSpec): { business: Field[]; metadata: Field[] } {
|
|
80
|
+
const attrs = Object.keys(model.attributes ?? {});
|
|
81
|
+
const business: Field[] = [];
|
|
82
|
+
const metadata: Field[] = [];
|
|
83
|
+
for (const name of attrs) {
|
|
84
|
+
const field = { name, label: humanize(name) };
|
|
85
|
+
if (METADATA_FIELD_NAMES.has(name)) {
|
|
86
|
+
metadata.push(field);
|
|
87
|
+
} else {
|
|
88
|
+
business.push(field);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return { business, metadata };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Extract belongsTo relationship names from the model. Returns e.g.
|
|
96
|
+
* `['author']` for a Post that `belongsTo Author`. The skeleton's
|
|
97
|
+
* item already has `authorId` as a scalar field — we surface it
|
|
98
|
+
* explicitly in its own section so the generated UI separates "this
|
|
99
|
+
* entity's data" from "related entities."
|
|
100
|
+
*/
|
|
101
|
+
function extractBelongsTo(model: ModelSpec): string[] {
|
|
102
|
+
const rels = model.relationships ?? {};
|
|
103
|
+
const out: string[] = [];
|
|
104
|
+
for (const [name, def] of Object.entries(rels)) {
|
|
105
|
+
const d = def as { type?: string };
|
|
106
|
+
if (d?.type === 'belongsTo') out.push(name);
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function renderField(field: Field, opts: { muted: boolean }): string[] {
|
|
112
|
+
const label = field.label ?? humanize(field.name);
|
|
113
|
+
const labelCls = opts.muted
|
|
114
|
+
? 'font-medium uppercase tracking-wide text-gray-400 dark:text-gray-500'
|
|
115
|
+
: 'text-sm font-medium text-gray-500 dark:text-gray-400';
|
|
116
|
+
const valueCls = opts.muted
|
|
117
|
+
? 'text-gray-500 dark:text-gray-400'
|
|
118
|
+
: 'mt-1 text-sm text-gray-900 dark:text-gray-100 break-words';
|
|
119
|
+
|
|
120
|
+
return [
|
|
121
|
+
' <div>',
|
|
122
|
+
` <dt className="${labelCls}">${label}</dt>`,
|
|
123
|
+
` <dd className="${valueCls}">` +
|
|
124
|
+
`{String((item as any).${field.name} ?? '')}` +
|
|
125
|
+
'</dd>',
|
|
126
|
+
' </div>',
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function humanize(name: string): string {
|
|
131
|
+
return name
|
|
132
|
+
.replace(/([A-Z])/g, ' $1')
|
|
133
|
+
.replace(/^./, c => c.toUpperCase())
|
|
134
|
+
.trim();
|
|
135
|
+
}
|