data-primals-engine 1.5.2 → 1.6.1

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.
Files changed (70) hide show
  1. package/README.md +938 -915
  2. package/client/README.md +136 -136
  3. package/client/index.js +0 -1
  4. package/client/public/doc/API.postman_collection.json +213 -213
  5. package/client/public/react.svg +9 -9
  6. package/client/src/AddWidgetTypeModal.jsx +47 -47
  7. package/client/src/App.css +42 -42
  8. package/client/src/App.jsx +92 -150
  9. package/client/src/App.scss +6 -0
  10. package/client/src/AssistantChat.jsx +362 -363
  11. package/client/src/Button.jsx +12 -12
  12. package/client/src/Dashboard.jsx +480 -480
  13. package/client/src/DashboardHtmlViewItem.jsx +146 -146
  14. package/client/src/DashboardView.jsx +654 -654
  15. package/client/src/DataEditor.jsx +383 -383
  16. package/client/src/DataLayout.jsx +779 -808
  17. package/client/src/DataTable.jsx +782 -822
  18. package/client/src/DataTable.scss +186 -186
  19. package/client/src/GeolocationField.jsx +93 -93
  20. package/client/src/HistoryDialog.jsx +307 -307
  21. package/client/src/HistoryDialog.scss +319 -319
  22. package/client/src/HtmlViewBuilderModal.jsx +90 -90
  23. package/client/src/HtmlViewBuilderModal.scss +17 -17
  24. package/client/src/HtmlViewCard.jsx +43 -43
  25. package/client/src/ModelCreator.jsx +683 -686
  26. package/client/src/ModelCreator.scss +1 -1
  27. package/client/src/ModelCreatorField.jsx +950 -950
  28. package/client/src/ModelImporter.jsx +3 -0
  29. package/client/src/ModelList.jsx +280 -280
  30. package/client/src/Notification.jsx +136 -136
  31. package/client/src/PackGallery.jsx +391 -391
  32. package/client/src/PackGallery.scss +231 -231
  33. package/client/src/Pagination.jsx +143 -143
  34. package/client/src/RelationField.jsx +354 -354
  35. package/client/src/RelationSelectorWidget.jsx +172 -172
  36. package/client/src/RelationValue.jsx +2 -1
  37. package/client/src/contexts/CommandContext.jsx +260 -0
  38. package/client/src/contexts/ModelContext.jsx +4 -1
  39. package/client/src/contexts/UIContext.jsx +72 -72
  40. package/client/src/filter.js +263 -263
  41. package/client/src/index.css +90 -90
  42. package/package.json +6 -5
  43. package/perf/artillery-hooks.js +37 -37
  44. package/perf/setup.yml +25 -25
  45. package/src/constants.js +4 -0
  46. package/src/defaultModels.js +7 -0
  47. package/src/engine.js +335 -335
  48. package/src/events.js +232 -137
  49. package/src/filter.js +274 -274
  50. package/src/index.js +1 -0
  51. package/src/modules/assistant/assistant.js +225 -83
  52. package/src/modules/auth-google/index.js +50 -50
  53. package/src/modules/auth-microsoft/index.js +81 -81
  54. package/src/modules/data/data.core.js +118 -118
  55. package/src/modules/data/data.history.js +555 -555
  56. package/src/modules/data/data.operations.js +112 -9
  57. package/src/modules/data/data.relations.js +686 -686
  58. package/src/modules/data/data.routes.js +1879 -1879
  59. package/src/modules/data/data.validation.js +2 -2
  60. package/src/modules/file.js +247 -247
  61. package/src/modules/user.js +14 -9
  62. package/src/packs.js +2 -2
  63. package/src/providers.js +2 -2
  64. package/src/services/stripe.js +196 -196
  65. package/src/sso.js +193 -193
  66. package/test/data.history.integration.test.js +264 -264
  67. package/test/data.integration.test.js +1281 -1206
  68. package/test/events.test.js +108 -10
  69. package/test/model.integration.test.js +377 -377
  70. package/test/user.test.js +106 -1
@@ -1,147 +1,147 @@
1
- import React, { useEffect, useMemo } from 'react';
2
- import { useQuery } from 'react-query';
3
- import { useTranslation } from 'react-i18next';
4
- import { FaSpinner } from 'react-icons/fa';
5
- import Handlebars from 'handlebars';
6
- import './HtmlViewCard.scss';
7
- import { useModelContext } from "./contexts/ModelContext.jsx";
8
-
9
- /**
10
- * Pre-processes data to make it compatible with templates generated by the AI.
11
- * - Adds a `.value` property to translated fields (string_t, richtext_t).
12
- * - Recursively processes relations.
13
- * @param {object|Array} data - The data to process.
14
- * @param {object} model - The model definition for the data.
15
- * @param {Array} allModels - All available models.
16
- * @param {object} tr - The translation object { t, i18n }.
17
- * @returns {object|Array} The processed data.
18
- */
19
- const preprocessDataForHandlebars = (data, model, allModels, tr) => {
20
- if (!data || !model) return data;
21
-
22
- if (Array.isArray(data)) {
23
- return data.map(item => preprocessDataForHandlebars(item, model, allModels, tr));
24
- }
25
-
26
- // Create a shallow copy to avoid mutating the original data from react-query cache
27
- const processedData = { ...data };
28
-
29
- for (const key in processedData) {
30
- if (!Object.prototype.hasOwnProperty.call(processedData, key)) continue;
31
-
32
- const fieldDef = model.fields.find(f => f.name === key);
33
- if (!fieldDef) continue;
34
-
35
- const value = processedData[key];
36
-
37
- if ((fieldDef.type === 'string_t' || fieldDef.type === 'richtext_t') && typeof value === 'object' && value !== null) {
38
- const lang = (tr.i18n.resolvedLanguage || tr.i18n.language).split(/[-_]/)?.[0];
39
- // Add the `.value` property the AI expects, making it non-enumerable to avoid issues
40
- Object.defineProperty(value, 'value', {
41
- value: value[lang] || value.en || value.fr || Object.values(value)[0] || '',
42
- writable: true,
43
- configurable: true,
44
- enumerable: false // Important!
45
- });
46
- } else if (fieldDef.type === 'relation' && value) {
47
- const relatedModel = allModels.find(m => m.name === fieldDef.relation);
48
- if (relatedModel) {
49
- // Recursively process the related data
50
- processedData[key] = preprocessDataForHandlebars(value, relatedModel, allModels, tr);
51
- }
52
- }
53
- }
54
- return processedData;
55
- };
56
-
57
- /**
58
- * Renders a simple HTML template by substituting placeholders.
59
- * Uses Handlebars.js for robust templating.
60
- *
61
- * @param {string} templateString The template with placeholders.
62
- * @param {Array<object>} data The array of data objects to inject.
63
- * @param {object} options - Additional options including model, allModels, and tr.
64
- * @returns {string} The rendered HTML string.
65
- */
66
- export const renderHtmlTemplate = (templateString, data, options = {}) => {
67
- const { model, allModels, tr } = options;
68
- if (!templateString) return '';
69
-
70
- try {
71
- const processedData = preprocessDataForHandlebars(data, model, allModels, tr);
72
- const template = Handlebars.compile(templateString);
73
- return template({ data: processedData });
74
- } catch (e) {
75
- console.error("Handlebars rendering error:", e);
76
- return `<div class="error-state">Template Error: ${e.message}</div>`;
77
- }
78
- };
79
-
80
- /**
81
- * Récupère les données et affiche une vue HTML personnalisée sur le tableau de bord.
82
- * @param {object} props
83
- * @param {object} props.config - La configuration de la vue depuis la disposition du tableau de bord.
84
- */
85
- const DashboardHtmlViewItem = ({ config }) => {
86
- const { t, i18n } = useTranslation();
87
- const { models } = useModelContext();
88
- const { title, model: modelName, filter, sort, limit, template, css } = config;
89
-
90
- // Génère un ID unique et stable pour ce composant
91
- const containerId = useMemo(() => `html-view-dashboard-${Math.random().toString(36).substring(2, 9)}`, []);
92
- const model = useMemo(() => models.find(m => m.name === modelName), [models, modelName]);
93
-
94
- const { data: queryResult, isLoading, error } = useQuery(
95
- ['dashboardHtmlView', modelName, filter, sort, limit],
96
- async () => {
97
- const response = await fetch(`/api/data/search?model=${modelName}&depth=2&limit=${limit || 10}&sort=${JSON.stringify(sort || {})}`, {
98
- method: 'POST',
99
- headers: { 'Content-Type': 'application/json' },
100
- body: JSON.stringify({ filter: filter || {}}),
101
- });
102
- if (!response.ok) throw new Error('Échec de la récupération des données pour la vue HTML');
103
- return response.json();
104
- },
105
- {
106
- enabled: !!modelName && !!template,
107
- refetchOnWindowFocus: false,
108
- }
109
- );
110
-
111
- // Effet pour injecter et nettoyer le CSS
112
- useEffect(() => {
113
- if (!css) return;
114
-
115
- const styleElement = document.createElement('style');
116
- // Remplace le placeholder par notre ID unique pour scoper le CSS
117
- styleElement.innerHTML = css.replace(/\{\{containerId\}\}/g, containerId);
118
- document.head.appendChild(styleElement);
119
-
120
- // Fonction de nettoyage : retire le style quand le composant est démonté
121
- return () => {
122
- document.head.removeChild(styleElement);
123
- };
124
- }, [css, containerId]);
125
-
126
- const renderContent = () => {
127
- if (isLoading) return <div className="loading-state"><FaSpinner className="spin-icon" /> {t('loading', 'Chargement...')}</div>;
128
- if (error) return <div className="error-state">{t('error.generic', 'Erreur: {{message}}', { message: error.message })}</div>;
129
- if (!queryResult || !queryResult.data) return <div className="empty-state">{t('dashboards.noDataForView', 'Aucune donnée à afficher.')}</div>;
130
-
131
- const renderedHtml = renderHtmlTemplate(template, queryResult.data, {
132
- model: model,
133
- allModels: models,
134
- tr: { t, i18n }
135
- });
136
- return <div className="html-view-content" dangerouslySetInnerHTML={{ __html: renderedHtml }} />;
137
- };
138
-
139
- return (
140
- <div id={containerId} className="html-view-card dashboard-widget">
141
- {title && <h4>{title}</h4>}
142
- {renderContent()}
143
- </div>
144
- );
145
- };
146
-
1
+ import React, { useEffect, useMemo } from 'react';
2
+ import { useQuery } from 'react-query';
3
+ import { useTranslation } from 'react-i18next';
4
+ import { FaSpinner } from 'react-icons/fa';
5
+ import Handlebars from 'handlebars';
6
+ import './HtmlViewCard.scss';
7
+ import { useModelContext } from "./contexts/ModelContext.jsx";
8
+
9
+ /**
10
+ * Pre-processes data to make it compatible with templates generated by the AI.
11
+ * - Adds a `.value` property to translated fields (string_t, richtext_t).
12
+ * - Recursively processes relations.
13
+ * @param {object|Array} data - The data to process.
14
+ * @param {object} model - The model definition for the data.
15
+ * @param {Array} allModels - All available models.
16
+ * @param {object} tr - The translation object { t, i18n }.
17
+ * @returns {object|Array} The processed data.
18
+ */
19
+ const preprocessDataForHandlebars = (data, model, allModels, tr) => {
20
+ if (!data || !model) return data;
21
+
22
+ if (Array.isArray(data)) {
23
+ return data.map(item => preprocessDataForHandlebars(item, model, allModels, tr));
24
+ }
25
+
26
+ // Create a shallow copy to avoid mutating the original data from react-query cache
27
+ const processedData = { ...data };
28
+
29
+ for (const key in processedData) {
30
+ if (!Object.prototype.hasOwnProperty.call(processedData, key)) continue;
31
+
32
+ const fieldDef = model.fields.find(f => f.name === key);
33
+ if (!fieldDef) continue;
34
+
35
+ const value = processedData[key];
36
+
37
+ if ((fieldDef.type === 'string_t' || fieldDef.type === 'richtext_t') && typeof value === 'object' && value !== null) {
38
+ const lang = (tr.i18n.resolvedLanguage || tr.i18n.language).split(/[-_]/)?.[0];
39
+ // Add the `.value` property the AI expects, making it non-enumerable to avoid issues
40
+ Object.defineProperty(value, 'value', {
41
+ value: value[lang] || value.en || value.fr || Object.values(value)[0] || '',
42
+ writable: true,
43
+ configurable: true,
44
+ enumerable: false // Important!
45
+ });
46
+ } else if (fieldDef.type === 'relation' && value) {
47
+ const relatedModel = allModels.find(m => m.name === fieldDef.relation);
48
+ if (relatedModel) {
49
+ // Recursively process the related data
50
+ processedData[key] = preprocessDataForHandlebars(value, relatedModel, allModels, tr);
51
+ }
52
+ }
53
+ }
54
+ return processedData;
55
+ };
56
+
57
+ /**
58
+ * Renders a simple HTML template by substituting placeholders.
59
+ * Uses Handlebars.js for robust templating.
60
+ *
61
+ * @param {string} templateString The template with placeholders.
62
+ * @param {Array<object>} data The array of data objects to inject.
63
+ * @param {object} options - Additional options including model, allModels, and tr.
64
+ * @returns {string} The rendered HTML string.
65
+ */
66
+ export const renderHtmlTemplate = (templateString, data, options = {}) => {
67
+ const { model, allModels, tr } = options;
68
+ if (!templateString) return '';
69
+
70
+ try {
71
+ const processedData = preprocessDataForHandlebars(data, model, allModels, tr);
72
+ const template = Handlebars.compile(templateString);
73
+ return template({ data: processedData });
74
+ } catch (e) {
75
+ console.error("Handlebars rendering error:", e);
76
+ return `<div class="error-state">Template Error: ${e.message}</div>`;
77
+ }
78
+ };
79
+
80
+ /**
81
+ * Récupère les données et affiche une vue HTML personnalisée sur le tableau de bord.
82
+ * @param {object} props
83
+ * @param {object} props.config - La configuration de la vue depuis la disposition du tableau de bord.
84
+ */
85
+ const DashboardHtmlViewItem = ({ config }) => {
86
+ const { t, i18n } = useTranslation();
87
+ const { models } = useModelContext();
88
+ const { title, model: modelName, filter, sort, limit, template, css } = config;
89
+
90
+ // Génère un ID unique et stable pour ce composant
91
+ const containerId = useMemo(() => `html-view-dashboard-${Math.random().toString(36).substring(2, 9)}`, []);
92
+ const model = useMemo(() => models.find(m => m.name === modelName), [models, modelName]);
93
+
94
+ const { data: queryResult, isLoading, error } = useQuery(
95
+ ['dashboardHtmlView', modelName, filter, sort, limit],
96
+ async () => {
97
+ const response = await fetch(`/api/data/search?model=${modelName}&depth=2&limit=${limit || 10}&sort=${JSON.stringify(sort || {})}`, {
98
+ method: 'POST',
99
+ headers: { 'Content-Type': 'application/json' },
100
+ body: JSON.stringify({ filter: filter || {}}),
101
+ });
102
+ if (!response.ok) throw new Error('Échec de la récupération des données pour la vue HTML');
103
+ return response.json();
104
+ },
105
+ {
106
+ enabled: !!modelName && !!template,
107
+ refetchOnWindowFocus: false,
108
+ }
109
+ );
110
+
111
+ // Effet pour injecter et nettoyer le CSS
112
+ useEffect(() => {
113
+ if (!css) return;
114
+
115
+ const styleElement = document.createElement('style');
116
+ // Remplace le placeholder par notre ID unique pour scoper le CSS
117
+ styleElement.innerHTML = css.replace(/\{\{containerId\}\}/g, containerId);
118
+ document.head.appendChild(styleElement);
119
+
120
+ // Fonction de nettoyage : retire le style quand le composant est démonté
121
+ return () => {
122
+ document.head.removeChild(styleElement);
123
+ };
124
+ }, [css, containerId]);
125
+
126
+ const renderContent = () => {
127
+ if (isLoading) return <div className="loading-state"><FaSpinner className="spin-icon" /> {t('loading', 'Chargement...')}</div>;
128
+ if (error) return <div className="error-state">{t('error.generic', 'Erreur: {{message}}', { message: error.message })}</div>;
129
+ if (!queryResult || !queryResult.data) return <div className="empty-state">{t('dashboards.noDataForView', 'Aucune donnée à afficher.')}</div>;
130
+
131
+ const renderedHtml = renderHtmlTemplate(template, queryResult.data, {
132
+ model: model,
133
+ allModels: models,
134
+ tr: { t, i18n }
135
+ });
136
+ return <div className="html-view-content" dangerouslySetInnerHTML={{ __html: renderedHtml }} />;
137
+ };
138
+
139
+ return (
140
+ <div id={containerId} className="html-view-card dashboard-widget">
141
+ {title && <h4>{title}</h4>}
142
+ {renderContent()}
143
+ </div>
144
+ );
145
+ };
146
+
147
147
  export default DashboardHtmlViewItem;