data-primals-engine 1.5.0 → 1.5.2

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 (63) hide show
  1. package/README.md +37 -0
  2. package/client/src/AddWidgetTypeModal.jsx +47 -43
  3. package/client/src/App.jsx +2 -6
  4. package/client/src/App.scss +13 -1
  5. package/client/src/AssistantChat.jsx +363 -323
  6. package/client/src/AssistantChat.scss +27 -10
  7. package/client/src/Dashboard.jsx +480 -396
  8. package/client/src/Dashboard.scss +1 -1
  9. package/client/src/DashboardHtmlViewItem.jsx +147 -0
  10. package/client/src/DashboardView.jsx +654 -569
  11. package/client/src/DataEditor.jsx +10 -3
  12. package/client/src/DataLayout.jsx +807 -755
  13. package/client/src/DataLayout.scss +14 -0
  14. package/client/src/DataTable.jsx +39 -75
  15. package/client/src/Dialog.scss +1 -1
  16. package/client/src/Field.jsx +2057 -1825
  17. package/client/src/FlexViewCard.jsx +44 -0
  18. package/client/src/HistoryDialog.jsx +69 -14
  19. package/client/src/HtmlViewBuilderModal.jsx +91 -0
  20. package/client/src/HtmlViewBuilderModal.scss +18 -0
  21. package/client/src/HtmlViewCard.jsx +44 -0
  22. package/client/src/HtmlViewCard.scss +35 -0
  23. package/client/src/KanbanCard.jsx +1 -2
  24. package/client/src/ModelCreator.jsx +5 -4
  25. package/client/src/ModelCreatorField.jsx +51 -4
  26. package/client/src/ModelList.jsx +280 -236
  27. package/client/src/Notification.jsx +136 -136
  28. package/client/src/Notification.scss +0 -18
  29. package/client/src/Pagination.jsx +5 -3
  30. package/client/src/RelationField.jsx +354 -258
  31. package/client/src/RelationSelectorWidget.jsx +173 -0
  32. package/client/src/contexts/ModelContext.jsx +10 -1
  33. package/client/src/contexts/UIContext.jsx +72 -63
  34. package/client/src/filter.js +263 -212
  35. package/client/src/hooks/useValidation.js +75 -0
  36. package/client/src/translations.js +24 -24
  37. package/package.json +7 -6
  38. package/src/constants.js +1 -1
  39. package/src/core.js +8 -1
  40. package/src/defaultModels.js +1596 -1544
  41. package/src/engine.js +85 -43
  42. package/src/events.js +137 -113
  43. package/src/i18n.js +710 -10
  44. package/src/index.js +3 -0
  45. package/src/modules/assistant/assistant.js +253 -134
  46. package/src/modules/assistant/constants.js +2 -1
  47. package/src/modules/bucket.js +2 -1
  48. package/src/modules/data/data.core.js +118 -92
  49. package/src/modules/data/data.history.js +555 -492
  50. package/src/modules/data/data.js +3 -53
  51. package/src/modules/data/data.operations.js +3381 -3231
  52. package/src/modules/data/data.relations.js +686 -686
  53. package/src/modules/data/data.routes.js +1879 -1821
  54. package/src/modules/data/data.validation.js +81 -2
  55. package/src/modules/file.js +247 -238
  56. package/src/modules/user.js +1 -0
  57. package/src/modules/workflow.js +2 -2
  58. package/src/openai.jobs.js +3 -2
  59. package/src/packs.js +5482 -5478
  60. package/src/sso.js +2 -2
  61. package/src/workers/import-export-worker.js +1 -1
  62. package/test/data.history.integration.test.js +264 -192
  63. package/test/data.integration.test.js +149 -3
@@ -173,7 +173,7 @@
173
173
  display: flex;
174
174
  align-items: center;
175
175
  gap: 0.5rem;
176
- margin-bottom: 1rem;
176
+ margin-bottom: 0;
177
177
  border-bottom: 1px solid #eee;
178
178
  padding-bottom: 0.5rem;
179
179
 
@@ -0,0 +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
+
147
+ export default DashboardHtmlViewItem;