data-primals-engine 1.5.2 → 1.6.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/README.md +924 -915
- package/client/README.md +136 -136
- package/client/index.js +0 -1
- package/client/public/doc/API.postman_collection.json +213 -213
- package/client/public/react.svg +9 -9
- package/client/src/AddWidgetTypeModal.jsx +47 -47
- package/client/src/App.css +42 -42
- package/client/src/App.jsx +92 -150
- package/client/src/App.scss +6 -0
- package/client/src/AssistantChat.jsx +362 -363
- package/client/src/Button.jsx +12 -12
- package/client/src/Dashboard.jsx +480 -480
- package/client/src/DashboardHtmlViewItem.jsx +146 -146
- package/client/src/DashboardView.jsx +654 -654
- package/client/src/DataEditor.jsx +383 -383
- package/client/src/DataLayout.jsx +779 -808
- package/client/src/DataTable.jsx +782 -822
- package/client/src/DataTable.scss +186 -186
- package/client/src/GeolocationField.jsx +93 -93
- package/client/src/HistoryDialog.jsx +307 -307
- package/client/src/HistoryDialog.scss +319 -319
- package/client/src/HtmlViewBuilderModal.jsx +90 -90
- package/client/src/HtmlViewBuilderModal.scss +17 -17
- package/client/src/HtmlViewCard.jsx +43 -43
- package/client/src/ModelCreatorField.jsx +950 -950
- package/client/src/ModelList.jsx +280 -280
- package/client/src/Notification.jsx +136 -136
- package/client/src/PackGallery.jsx +391 -391
- package/client/src/PackGallery.scss +231 -231
- package/client/src/Pagination.jsx +143 -143
- package/client/src/RelationField.jsx +354 -354
- package/client/src/RelationSelectorWidget.jsx +172 -172
- package/client/src/RelationValue.jsx +2 -1
- package/client/src/contexts/CommandContext.jsx +260 -0
- package/client/src/contexts/ModelContext.jsx +4 -1
- package/client/src/contexts/UIContext.jsx +72 -72
- package/client/src/filter.js +263 -263
- package/client/src/index.css +90 -90
- package/package.json +6 -5
- package/perf/artillery-hooks.js +37 -37
- package/perf/setup.yml +25 -25
- package/src/constants.js +4 -0
- package/src/engine.js +335 -335
- package/src/events.js +232 -137
- package/src/filter.js +274 -274
- package/src/modules/assistant/assistant.js +225 -83
- package/src/modules/auth-google/index.js +50 -50
- package/src/modules/auth-microsoft/index.js +81 -81
- package/src/modules/data/data.core.js +118 -118
- package/src/modules/data/data.history.js +555 -555
- package/src/modules/data/data.operations.js +3401 -3381
- package/src/modules/data/data.relations.js +686 -686
- package/src/modules/data/data.routes.js +1879 -1879
- package/src/modules/data/data.validation.js +2 -2
- package/src/modules/file.js +247 -247
- package/src/packs.js +2 -2
- package/src/providers.js +2 -2
- package/src/services/stripe.js +196 -196
- package/src/sso.js +193 -193
- package/test/data.history.integration.test.js +264 -264
- package/test/data.integration.test.js +1206 -1206
- package/test/events.test.js +108 -10
- package/test/model.integration.test.js +377 -377
|
@@ -1,308 +1,308 @@
|
|
|
1
|
-
import {useCallback, useEffect, useState} from "react";
|
|
2
|
-
import { Dialog } from "./Dialog.jsx";
|
|
3
|
-
import Button from "./Button.jsx";
|
|
4
|
-
import { FaHistory } from "react-icons/fa";
|
|
5
|
-
import {Trans, useTranslation} from "react-i18next";
|
|
6
|
-
import "./HistoryDialog.scss";
|
|
7
|
-
import {Tooltip} from "react-tooltip";
|
|
8
|
-
import {CodeField} from "./Field.jsx";
|
|
9
|
-
import {Pagination} from "./Pagination.jsx";
|
|
10
|
-
import {useModelContext} from "./contexts/ModelContext.jsx";
|
|
11
|
-
|
|
12
|
-
// Helper to render values in a friendly way for the summary list
|
|
13
|
-
const renderSummaryValue = (value, field, t) => {
|
|
14
|
-
if (value === undefined || value === null) {
|
|
15
|
-
return <i className="value-empty">empty</i>;
|
|
16
|
-
}
|
|
17
|
-
if (field?.type === 'string_t' && t) {
|
|
18
|
-
// For string_t, the stored value is the key. We display the translated value.
|
|
19
|
-
const key = (typeof value === 'object' && value?.key) ? value.key : String(value);
|
|
20
|
-
const displayValue = t(key, key);
|
|
21
|
-
return <span className="value-text" title={key}>{displayValue}</span>;
|
|
22
|
-
}
|
|
23
|
-
if (typeof value === 'boolean') {
|
|
24
|
-
return <code className="value-boolean">{value.toString()}</code>;
|
|
25
|
-
}
|
|
26
|
-
if (Array.isArray(value)) {
|
|
27
|
-
return <code className="value-object" title={JSON.stringify(value, null, 2)}>[...] ({value.length} items)</code>
|
|
28
|
-
}
|
|
29
|
-
if (typeof value === 'object' && value !== null) {
|
|
30
|
-
return <code className="value-object" title={JSON.stringify(value, null, 2)}>{`{...}`}</code>
|
|
31
|
-
}
|
|
32
|
-
const strValue = String(value);
|
|
33
|
-
if (strValue.length > 50) {
|
|
34
|
-
return <span className="value-text" title={strValue}>{strValue.substring(0, 47) + '...'}</span>;
|
|
35
|
-
}
|
|
36
|
-
return <span className="value-text">{strValue}</span>;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// New sub-component for rendering a single field change
|
|
40
|
-
const ChangeDetail = ({ field, from, to }) => {
|
|
41
|
-
const { t } = useTranslation();
|
|
42
|
-
const { selectedModel } = useModelContext();
|
|
43
|
-
const fieldObj = selectedModel?.fields?.find(f => f.name === field);
|
|
44
|
-
|
|
45
|
-
const getTooltipText = (value, field) => {
|
|
46
|
-
if (field?.type === 'string_t') {
|
|
47
|
-
const key = (typeof value === 'object' && value?.key) ? value.key : String(value);
|
|
48
|
-
return t(key, key); // Affiche la valeur traduite dans la tooltip
|
|
49
|
-
}
|
|
50
|
-
return (typeof value === 'object' && value !== null) ? JSON.stringify(value, null, 2) : String(value);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const tt = getTooltipText(to, fieldObj);
|
|
54
|
-
const f = getTooltipText(from, fieldObj);
|
|
55
|
-
return (
|
|
56
|
-
<div className="change-detail">
|
|
57
|
-
<strong className="change-field-name" title={field}>{field}</strong>
|
|
58
|
-
<div className="change-values">
|
|
59
|
-
<div className="value-from" data-tooltip-id={"changeTooltip"} data-tooltip-content={f}>{renderSummaryValue(from, fieldObj, t)}</div>
|
|
60
|
-
<div className="change-arrow">→</div>
|
|
61
|
-
<div className="value-to" data-tooltip-id={"changeTooltip"} data-tooltip-content={tt}>{renderSummaryValue(to, fieldObj, t)}</div>
|
|
62
|
-
</div>
|
|
63
|
-
</div>
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// Sub-component for displaying a single history entry
|
|
68
|
-
const HistoryEntry = ({ entry, onPreview, isSelected }) => {
|
|
69
|
-
const { t } = useTranslation();
|
|
70
|
-
const { selectedModel } = useModelContext();
|
|
71
|
-
// The backend has transformed the data for us.
|
|
72
|
-
// _id: history doc id, _v: version, _op: operation type (i,u,d), _rid: original record id
|
|
73
|
-
const { _id, _v, _op, _updatedAt, _user, ...data } = entry; // ...data collects the remaining fields
|
|
74
|
-
|
|
75
|
-
const operationDetails = {
|
|
76
|
-
// The keys 'i', 'u', 'd' correspond to what the backend sends now.
|
|
77
|
-
'i': { label: t('history.op.create', 'Creation'), className: 'op-insert' },
|
|
78
|
-
'u': { label: t('history.op.update', 'Update'), className: 'op-update' },
|
|
79
|
-
'd': { label: t('history.op.delete', 'Deletion'), className: 'op-delete' },
|
|
80
|
-
};
|
|
81
|
-
const opInfo = operationDetails[_op] || { label: _op, className: '' };
|
|
82
|
-
|
|
83
|
-
const renderData = () => {
|
|
84
|
-
if (_op === 'u') {
|
|
85
|
-
const changes = Object.entries(data).filter(([, value]) => value && typeof value === 'object' && 'from' in value && 'to' in value);
|
|
86
|
-
if (changes.length === 0) {
|
|
87
|
-
return <div className="no-changes-text">{t('history.noChanges', 'No relevant changes recorded.')}</div>;
|
|
88
|
-
}
|
|
89
|
-
return (
|
|
90
|
-
<div className="changes-list">
|
|
91
|
-
{changes.map(([field, change]) => (
|
|
92
|
-
<ChangeDetail key={field} field={field} from={change.from} to={change.to} />
|
|
93
|
-
))}
|
|
94
|
-
</div>
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// For 'i' (create) and 'd' (delete), show a human-friendly summary of the snapshot.
|
|
99
|
-
const snapshotEntries = Object.entries(data);
|
|
100
|
-
if (snapshotEntries.length === 0) {
|
|
101
|
-
return <div className="no-changes-text">{t('history.noData', 'No data in snapshot.')}</div>;
|
|
102
|
-
}
|
|
103
|
-
return (
|
|
104
|
-
<div className="snapshot-list">
|
|
105
|
-
{snapshotEntries.map(([key, value]) => {
|
|
106
|
-
const fieldObj = selectedModel?.fields?.find(f => f.name === key);
|
|
107
|
-
return (<div className="snapshot-item" key={key}>
|
|
108
|
-
<strong className="snapshot-key" title={key}>{key}:</strong>
|
|
109
|
-
<span className="snapshot-value">{renderSummaryValue(value, fieldObj, t)}</span>
|
|
110
|
-
</div>);
|
|
111
|
-
})}
|
|
112
|
-
</div>
|
|
113
|
-
);
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
return (
|
|
117
|
-
<div className={`history-entry ${isSelected ? 'selected' : ''}`}>
|
|
118
|
-
<div className="history-entry-header">
|
|
119
|
-
<span className={`op-badge ${opInfo.className}`}>{opInfo.label}</span>
|
|
120
|
-
<span className="history-date">{t('history.datePrefix', 'On')} {new Date(_updatedAt).toLocaleString()}</span>
|
|
121
|
-
{_user && <span className="history-user">{t('history.byUser', 'by')} <strong>{_user}</strong></span>}
|
|
122
|
-
<span className="history-version">{t('history.version', 'Version:')} {_v}</span>
|
|
123
|
-
<Button className="btn-preview" onClick={() => onPreview(_v)}>
|
|
124
|
-
<Trans i18nKey={"history.previewAction"}>Preview</Trans>
|
|
125
|
-
</Button>
|
|
126
|
-
</div>
|
|
127
|
-
<div className="history-entry-data">
|
|
128
|
-
{renderData()}
|
|
129
|
-
</div>
|
|
130
|
-
</div>
|
|
131
|
-
);
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
export const HistoryDialog = ({ modelName, recordId, onClose }) => {
|
|
135
|
-
const [history, setHistory] = useState([]);
|
|
136
|
-
const [loading, setLoading] = useState(true);
|
|
137
|
-
const [error, setError] = useState(null);
|
|
138
|
-
const [previewData, setPreviewData] = useState(null);
|
|
139
|
-
const [previewLoading, setPreviewLoading] = useState(false);
|
|
140
|
-
const [selectedVersion, setSelectedVersion] = useState(null);
|
|
141
|
-
const [page, setPage] = useState(1);
|
|
142
|
-
const [totalCount, setTotalCount] = useState(0);
|
|
143
|
-
const [startDate, setStartDate] = useState('');
|
|
144
|
-
const [endDate, setEndDate] = useState('');
|
|
145
|
-
const elementsPerPage = 10;
|
|
146
|
-
const { t, i18n } = useTranslation();
|
|
147
|
-
|
|
148
|
-
const fetchHistory = useCallback(async () => {
|
|
149
|
-
if (!modelName || !recordId) return;
|
|
150
|
-
setLoading(true);
|
|
151
|
-
setError(null);
|
|
152
|
-
try {
|
|
153
|
-
const params = new URLSearchParams({ page, limit: elementsPerPage, lang: i18n.language });
|
|
154
|
-
if (startDate) params.append('startDate', startDate);
|
|
155
|
-
if (endDate) params.append('endDate', endDate);
|
|
156
|
-
const query = await fetch(`/api/data/history/${modelName}/${recordId}?${params.toString()}`);
|
|
157
|
-
const response = await query.json();
|
|
158
|
-
if (response.success) {
|
|
159
|
-
setHistory(response.data);
|
|
160
|
-
setTotalCount(response.count || 0);
|
|
161
|
-
} else {
|
|
162
|
-
setError(response.error || i18n.t("history.error", "Could not load history."));
|
|
163
|
-
}
|
|
164
|
-
} catch (err) {
|
|
165
|
-
setError(i18n.t("history.error", "Could not load history."));
|
|
166
|
-
console.error(err);
|
|
167
|
-
} finally {
|
|
168
|
-
setLoading(false);
|
|
169
|
-
}
|
|
170
|
-
}, [modelName, recordId, i18n.language, page, elementsPerPage, startDate, endDate]);
|
|
171
|
-
|
|
172
|
-
// Réinitialise la page à 1 lorsque les filtres de date changent
|
|
173
|
-
useEffect(() => {
|
|
174
|
-
setPage(1);
|
|
175
|
-
}, [startDate, endDate]);
|
|
176
|
-
|
|
177
|
-
useEffect(() => {
|
|
178
|
-
fetchHistory();
|
|
179
|
-
}, [fetchHistory]);
|
|
180
|
-
|
|
181
|
-
const handlePreview = async (version) => {
|
|
182
|
-
if (selectedVersion === version) return; // Already selected
|
|
183
|
-
|
|
184
|
-
setSelectedVersion(version);
|
|
185
|
-
setPreviewLoading(true);
|
|
186
|
-
setPreviewData(null);
|
|
187
|
-
try {
|
|
188
|
-
const res = await fetch(`/api/data/history/${modelName}/${recordId}/${version}`);
|
|
189
|
-
const response = await res.json();
|
|
190
|
-
if (response.success) {
|
|
191
|
-
setPreviewData(response.data);
|
|
192
|
-
} else {
|
|
193
|
-
setPreviewData({ error: response.error || i18n.t("history.previewError", "Could not load revision preview.") });
|
|
194
|
-
}
|
|
195
|
-
} catch (err) {
|
|
196
|
-
setPreviewData({ error: i18n.t("history.previewError", "Could not load revision preview.") });
|
|
197
|
-
console.error(err);
|
|
198
|
-
} finally {
|
|
199
|
-
setPreviewLoading(false);
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
const handleRevert = async (version) => {
|
|
204
|
-
if (!window.confirm(t('history.confirmRevert', `Are you sure you want to revert to version ${version}? This will create a new version with the content of this revision.`))) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
setPreviewLoading(true); // Reuse preview loading state to indicate activity
|
|
209
|
-
try {
|
|
210
|
-
const res = await fetch(`/api/data/history/${modelName}/${recordId}/revert/${version}`, {
|
|
211
|
-
method: 'POST',
|
|
212
|
-
headers: { 'Content-Type': 'application/json' }
|
|
213
|
-
});
|
|
214
|
-
const response = await res.json();
|
|
215
|
-
|
|
216
|
-
if (response.success) {
|
|
217
|
-
alert(t('history.revertSuccess', 'Document successfully reverted. The history will now be updated.'));
|
|
218
|
-
// Refresh history list and clear preview
|
|
219
|
-
await fetchHistory();
|
|
220
|
-
setPreviewData(null);
|
|
221
|
-
setSelectedVersion(null);
|
|
222
|
-
} else {
|
|
223
|
-
alert(t('history.revertError', `Error reverting: ${response.error || 'Unknown error'}`));
|
|
224
|
-
}
|
|
225
|
-
} catch (err) {
|
|
226
|
-
alert(t('history.revertError', 'An error occurred while reverting.'));
|
|
227
|
-
console.error(err);
|
|
228
|
-
} finally {
|
|
229
|
-
setPreviewLoading(false);
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
return (
|
|
234
|
-
<Dialog
|
|
235
|
-
title={<><FaHistory style={{ marginRight: '8px' }} /> <Trans i18nKey={"history.title"}>Historique de l'enregistrement</Trans></>}
|
|
236
|
-
onClose={onClose}
|
|
237
|
-
isClosable={true}
|
|
238
|
-
className="history-dialog"
|
|
239
|
-
>
|
|
240
|
-
<Tooltip id={"changeTooltip"} clickable={true} />
|
|
241
|
-
<div className="history-dialog-content-split">
|
|
242
|
-
<div className="history-list-container">
|
|
243
|
-
{loading && <div><Trans i18nKey={"history.loading"}>Chargement de l'historique..</Trans></div>}
|
|
244
|
-
<div className="history-filters">
|
|
245
|
-
<div className="date-filter">
|
|
246
|
-
<label htmlFor="startDate"><Trans i18nKey="history.startDate">From</Trans></label>
|
|
247
|
-
<input type="date" id="startDate" value={startDate} onChange={(e) => setStartDate(e.target.value)} />
|
|
248
|
-
</div>
|
|
249
|
-
<div className="date-filter">
|
|
250
|
-
<label htmlFor="endDate"><Trans i18nKey="history.endDate">To</Trans></label>
|
|
251
|
-
<input type="date" id="endDate" value={endDate} onChange={(e) => setEndDate(e.target.value)} />
|
|
252
|
-
</div>
|
|
253
|
-
<Button className="btn-clear-filters" onClick={() => { setStartDate(''); setEndDate(''); }} title={t('history.clearDates', 'Clear date filters')}>
|
|
254
|
-
<Trans i18nKey="history.clear">Clear</Trans>
|
|
255
|
-
</Button>
|
|
256
|
-
</div>
|
|
257
|
-
{error && <div className="error-message">{error}</div>}
|
|
258
|
-
{!loading && !error && (<>
|
|
259
|
-
<div className="history-list">
|
|
260
|
-
{history.length > 0 ? (
|
|
261
|
-
history.map(entry =>
|
|
262
|
-
<HistoryEntry
|
|
263
|
-
key={entry._id}
|
|
264
|
-
entry={entry}
|
|
265
|
-
onPreview={handlePreview}
|
|
266
|
-
isSelected={selectedVersion === entry._v}
|
|
267
|
-
/>)
|
|
268
|
-
) : (
|
|
269
|
-
<div><Trans i18nKey={"history.noHistory"}>Aucun historique trouvé pour cet enregistrement.</Trans></div>
|
|
270
|
-
)}
|
|
271
|
-
</div>
|
|
272
|
-
{totalCount > elementsPerPage && <Pagination
|
|
273
|
-
totalCount={totalCount}
|
|
274
|
-
page={page}
|
|
275
|
-
setPage={setPage}
|
|
276
|
-
elementsPerPage={elementsPerPage}
|
|
277
|
-
visibleItemsCount={5}
|
|
278
|
-
hasPreviousNext={true}
|
|
279
|
-
/>}
|
|
280
|
-
</>)}
|
|
281
|
-
</div>
|
|
282
|
-
<div className="history-preview-container">
|
|
283
|
-
<div className="preview-header">
|
|
284
|
-
<h4><Trans i18nKey={"history.previewTitle"}>Revision Preview</Trans></h4>
|
|
285
|
-
{previewData && !previewData.error && (
|
|
286
|
-
<Button
|
|
287
|
-
className="btn-revert"
|
|
288
|
-
onClick={() => handleRevert(selectedVersion)}
|
|
289
|
-
disabled={previewLoading}
|
|
290
|
-
title={t('history.revertTooltip', 'Revert the document to this state. This creates a new version.')}
|
|
291
|
-
>
|
|
292
|
-
<Trans i18nKey={"history.revertAction"}>Revert to this version</Trans>
|
|
293
|
-
</Button>
|
|
294
|
-
)}
|
|
295
|
-
</div>
|
|
296
|
-
|
|
297
|
-
<div className="history-preview-area">
|
|
298
|
-
{previewLoading && <div><Trans i18nKey={"history.loadingPreview"}>Loading preview...</Trans></div>}
|
|
299
|
-
{!previewLoading && previewData && <CodeField value={JSON.stringify(previewData,null, 2)} language={"json"} disabled={true} />}
|
|
300
|
-
{!previewLoading && !previewData && (
|
|
301
|
-
<div className="placeholder"><Trans i18nKey={"history.selectRevision"}>Select a revision to preview its full state.</Trans></div>
|
|
302
|
-
)}
|
|
303
|
-
</div>
|
|
304
|
-
</div>
|
|
305
|
-
</div>
|
|
306
|
-
</Dialog>
|
|
307
|
-
);
|
|
1
|
+
import {useCallback, useEffect, useState} from "react";
|
|
2
|
+
import { Dialog } from "./Dialog.jsx";
|
|
3
|
+
import Button from "./Button.jsx";
|
|
4
|
+
import { FaHistory } from "react-icons/fa";
|
|
5
|
+
import {Trans, useTranslation} from "react-i18next";
|
|
6
|
+
import "./HistoryDialog.scss";
|
|
7
|
+
import {Tooltip} from "react-tooltip";
|
|
8
|
+
import {CodeField} from "./Field.jsx";
|
|
9
|
+
import {Pagination} from "./Pagination.jsx";
|
|
10
|
+
import {useModelContext} from "./contexts/ModelContext.jsx";
|
|
11
|
+
|
|
12
|
+
// Helper to render values in a friendly way for the summary list
|
|
13
|
+
const renderSummaryValue = (value, field, t) => {
|
|
14
|
+
if (value === undefined || value === null) {
|
|
15
|
+
return <i className="value-empty">empty</i>;
|
|
16
|
+
}
|
|
17
|
+
if (field?.type === 'string_t' && t) {
|
|
18
|
+
// For string_t, the stored value is the key. We display the translated value.
|
|
19
|
+
const key = (typeof value === 'object' && value?.key) ? value.key : String(value);
|
|
20
|
+
const displayValue = t(key, key);
|
|
21
|
+
return <span className="value-text" title={key}>{displayValue}</span>;
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === 'boolean') {
|
|
24
|
+
return <code className="value-boolean">{value.toString()}</code>;
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
return <code className="value-object" title={JSON.stringify(value, null, 2)}>[...] ({value.length} items)</code>
|
|
28
|
+
}
|
|
29
|
+
if (typeof value === 'object' && value !== null) {
|
|
30
|
+
return <code className="value-object" title={JSON.stringify(value, null, 2)}>{`{...}`}</code>
|
|
31
|
+
}
|
|
32
|
+
const strValue = String(value);
|
|
33
|
+
if (strValue.length > 50) {
|
|
34
|
+
return <span className="value-text" title={strValue}>{strValue.substring(0, 47) + '...'}</span>;
|
|
35
|
+
}
|
|
36
|
+
return <span className="value-text">{strValue}</span>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// New sub-component for rendering a single field change
|
|
40
|
+
const ChangeDetail = ({ field, from, to }) => {
|
|
41
|
+
const { t } = useTranslation();
|
|
42
|
+
const { selectedModel } = useModelContext();
|
|
43
|
+
const fieldObj = selectedModel?.fields?.find(f => f.name === field);
|
|
44
|
+
|
|
45
|
+
const getTooltipText = (value, field) => {
|
|
46
|
+
if (field?.type === 'string_t') {
|
|
47
|
+
const key = (typeof value === 'object' && value?.key) ? value.key : String(value);
|
|
48
|
+
return t(key, key); // Affiche la valeur traduite dans la tooltip
|
|
49
|
+
}
|
|
50
|
+
return (typeof value === 'object' && value !== null) ? JSON.stringify(value, null, 2) : String(value);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const tt = getTooltipText(to, fieldObj);
|
|
54
|
+
const f = getTooltipText(from, fieldObj);
|
|
55
|
+
return (
|
|
56
|
+
<div className="change-detail">
|
|
57
|
+
<strong className="change-field-name" title={field}>{field}</strong>
|
|
58
|
+
<div className="change-values">
|
|
59
|
+
<div className="value-from" data-tooltip-id={"changeTooltip"} data-tooltip-content={f}>{renderSummaryValue(from, fieldObj, t)}</div>
|
|
60
|
+
<div className="change-arrow">→</div>
|
|
61
|
+
<div className="value-to" data-tooltip-id={"changeTooltip"} data-tooltip-content={tt}>{renderSummaryValue(to, fieldObj, t)}</div>
|
|
62
|
+
</div>
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Sub-component for displaying a single history entry
|
|
68
|
+
const HistoryEntry = ({ entry, onPreview, isSelected }) => {
|
|
69
|
+
const { t } = useTranslation();
|
|
70
|
+
const { selectedModel } = useModelContext();
|
|
71
|
+
// The backend has transformed the data for us.
|
|
72
|
+
// _id: history doc id, _v: version, _op: operation type (i,u,d), _rid: original record id
|
|
73
|
+
const { _id, _v, _op, _updatedAt, _user, ...data } = entry; // ...data collects the remaining fields
|
|
74
|
+
|
|
75
|
+
const operationDetails = {
|
|
76
|
+
// The keys 'i', 'u', 'd' correspond to what the backend sends now.
|
|
77
|
+
'i': { label: t('history.op.create', 'Creation'), className: 'op-insert' },
|
|
78
|
+
'u': { label: t('history.op.update', 'Update'), className: 'op-update' },
|
|
79
|
+
'd': { label: t('history.op.delete', 'Deletion'), className: 'op-delete' },
|
|
80
|
+
};
|
|
81
|
+
const opInfo = operationDetails[_op] || { label: _op, className: '' };
|
|
82
|
+
|
|
83
|
+
const renderData = () => {
|
|
84
|
+
if (_op === 'u') {
|
|
85
|
+
const changes = Object.entries(data).filter(([, value]) => value && typeof value === 'object' && 'from' in value && 'to' in value);
|
|
86
|
+
if (changes.length === 0) {
|
|
87
|
+
return <div className="no-changes-text">{t('history.noChanges', 'No relevant changes recorded.')}</div>;
|
|
88
|
+
}
|
|
89
|
+
return (
|
|
90
|
+
<div className="changes-list">
|
|
91
|
+
{changes.map(([field, change]) => (
|
|
92
|
+
<ChangeDetail key={field} field={field} from={change.from} to={change.to} />
|
|
93
|
+
))}
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// For 'i' (create) and 'd' (delete), show a human-friendly summary of the snapshot.
|
|
99
|
+
const snapshotEntries = Object.entries(data);
|
|
100
|
+
if (snapshotEntries.length === 0) {
|
|
101
|
+
return <div className="no-changes-text">{t('history.noData', 'No data in snapshot.')}</div>;
|
|
102
|
+
}
|
|
103
|
+
return (
|
|
104
|
+
<div className="snapshot-list">
|
|
105
|
+
{snapshotEntries.map(([key, value]) => {
|
|
106
|
+
const fieldObj = selectedModel?.fields?.find(f => f.name === key);
|
|
107
|
+
return (<div className="snapshot-item" key={key}>
|
|
108
|
+
<strong className="snapshot-key" title={key}>{key}:</strong>
|
|
109
|
+
<span className="snapshot-value">{renderSummaryValue(value, fieldObj, t)}</span>
|
|
110
|
+
</div>);
|
|
111
|
+
})}
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<div className={`history-entry ${isSelected ? 'selected' : ''}`}>
|
|
118
|
+
<div className="history-entry-header">
|
|
119
|
+
<span className={`op-badge ${opInfo.className}`}>{opInfo.label}</span>
|
|
120
|
+
<span className="history-date">{t('history.datePrefix', 'On')} {new Date(_updatedAt).toLocaleString()}</span>
|
|
121
|
+
{_user && <span className="history-user">{t('history.byUser', 'by')} <strong>{_user}</strong></span>}
|
|
122
|
+
<span className="history-version">{t('history.version', 'Version:')} {_v}</span>
|
|
123
|
+
<Button className="btn-preview" onClick={() => onPreview(_v)}>
|
|
124
|
+
<Trans i18nKey={"history.previewAction"}>Preview</Trans>
|
|
125
|
+
</Button>
|
|
126
|
+
</div>
|
|
127
|
+
<div className="history-entry-data">
|
|
128
|
+
{renderData()}
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
131
|
+
);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const HistoryDialog = ({ modelName, recordId, onClose }) => {
|
|
135
|
+
const [history, setHistory] = useState([]);
|
|
136
|
+
const [loading, setLoading] = useState(true);
|
|
137
|
+
const [error, setError] = useState(null);
|
|
138
|
+
const [previewData, setPreviewData] = useState(null);
|
|
139
|
+
const [previewLoading, setPreviewLoading] = useState(false);
|
|
140
|
+
const [selectedVersion, setSelectedVersion] = useState(null);
|
|
141
|
+
const [page, setPage] = useState(1);
|
|
142
|
+
const [totalCount, setTotalCount] = useState(0);
|
|
143
|
+
const [startDate, setStartDate] = useState('');
|
|
144
|
+
const [endDate, setEndDate] = useState('');
|
|
145
|
+
const elementsPerPage = 10;
|
|
146
|
+
const { t, i18n } = useTranslation();
|
|
147
|
+
|
|
148
|
+
const fetchHistory = useCallback(async () => {
|
|
149
|
+
if (!modelName || !recordId) return;
|
|
150
|
+
setLoading(true);
|
|
151
|
+
setError(null);
|
|
152
|
+
try {
|
|
153
|
+
const params = new URLSearchParams({ page, limit: elementsPerPage, lang: i18n.language });
|
|
154
|
+
if (startDate) params.append('startDate', startDate);
|
|
155
|
+
if (endDate) params.append('endDate', endDate);
|
|
156
|
+
const query = await fetch(`/api/data/history/${modelName}/${recordId}?${params.toString()}`);
|
|
157
|
+
const response = await query.json();
|
|
158
|
+
if (response.success) {
|
|
159
|
+
setHistory(response.data);
|
|
160
|
+
setTotalCount(response.count || 0);
|
|
161
|
+
} else {
|
|
162
|
+
setError(response.error || i18n.t("history.error", "Could not load history."));
|
|
163
|
+
}
|
|
164
|
+
} catch (err) {
|
|
165
|
+
setError(i18n.t("history.error", "Could not load history."));
|
|
166
|
+
console.error(err);
|
|
167
|
+
} finally {
|
|
168
|
+
setLoading(false);
|
|
169
|
+
}
|
|
170
|
+
}, [modelName, recordId, i18n.language, page, elementsPerPage, startDate, endDate]);
|
|
171
|
+
|
|
172
|
+
// Réinitialise la page à 1 lorsque les filtres de date changent
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
setPage(1);
|
|
175
|
+
}, [startDate, endDate]);
|
|
176
|
+
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
fetchHistory();
|
|
179
|
+
}, [fetchHistory]);
|
|
180
|
+
|
|
181
|
+
const handlePreview = async (version) => {
|
|
182
|
+
if (selectedVersion === version) return; // Already selected
|
|
183
|
+
|
|
184
|
+
setSelectedVersion(version);
|
|
185
|
+
setPreviewLoading(true);
|
|
186
|
+
setPreviewData(null);
|
|
187
|
+
try {
|
|
188
|
+
const res = await fetch(`/api/data/history/${modelName}/${recordId}/${version}`);
|
|
189
|
+
const response = await res.json();
|
|
190
|
+
if (response.success) {
|
|
191
|
+
setPreviewData(response.data);
|
|
192
|
+
} else {
|
|
193
|
+
setPreviewData({ error: response.error || i18n.t("history.previewError", "Could not load revision preview.") });
|
|
194
|
+
}
|
|
195
|
+
} catch (err) {
|
|
196
|
+
setPreviewData({ error: i18n.t("history.previewError", "Could not load revision preview.") });
|
|
197
|
+
console.error(err);
|
|
198
|
+
} finally {
|
|
199
|
+
setPreviewLoading(false);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const handleRevert = async (version) => {
|
|
204
|
+
if (!window.confirm(t('history.confirmRevert', `Are you sure you want to revert to version ${version}? This will create a new version with the content of this revision.`))) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
setPreviewLoading(true); // Reuse preview loading state to indicate activity
|
|
209
|
+
try {
|
|
210
|
+
const res = await fetch(`/api/data/history/${modelName}/${recordId}/revert/${version}`, {
|
|
211
|
+
method: 'POST',
|
|
212
|
+
headers: { 'Content-Type': 'application/json' }
|
|
213
|
+
});
|
|
214
|
+
const response = await res.json();
|
|
215
|
+
|
|
216
|
+
if (response.success) {
|
|
217
|
+
alert(t('history.revertSuccess', 'Document successfully reverted. The history will now be updated.'));
|
|
218
|
+
// Refresh history list and clear preview
|
|
219
|
+
await fetchHistory();
|
|
220
|
+
setPreviewData(null);
|
|
221
|
+
setSelectedVersion(null);
|
|
222
|
+
} else {
|
|
223
|
+
alert(t('history.revertError', `Error reverting: ${response.error || 'Unknown error'}`));
|
|
224
|
+
}
|
|
225
|
+
} catch (err) {
|
|
226
|
+
alert(t('history.revertError', 'An error occurred while reverting.'));
|
|
227
|
+
console.error(err);
|
|
228
|
+
} finally {
|
|
229
|
+
setPreviewLoading(false);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
return (
|
|
234
|
+
<Dialog
|
|
235
|
+
title={<><FaHistory style={{ marginRight: '8px' }} /> <Trans i18nKey={"history.title"}>Historique de l'enregistrement</Trans></>}
|
|
236
|
+
onClose={onClose}
|
|
237
|
+
isClosable={true}
|
|
238
|
+
className="history-dialog"
|
|
239
|
+
>
|
|
240
|
+
<Tooltip id={"changeTooltip"} clickable={true} />
|
|
241
|
+
<div className="history-dialog-content-split">
|
|
242
|
+
<div className="history-list-container">
|
|
243
|
+
{loading && <div><Trans i18nKey={"history.loading"}>Chargement de l'historique..</Trans></div>}
|
|
244
|
+
<div className="history-filters">
|
|
245
|
+
<div className="date-filter">
|
|
246
|
+
<label htmlFor="startDate"><Trans i18nKey="history.startDate">From</Trans></label>
|
|
247
|
+
<input type="date" id="startDate" value={startDate} onChange={(e) => setStartDate(e.target.value)} />
|
|
248
|
+
</div>
|
|
249
|
+
<div className="date-filter">
|
|
250
|
+
<label htmlFor="endDate"><Trans i18nKey="history.endDate">To</Trans></label>
|
|
251
|
+
<input type="date" id="endDate" value={endDate} onChange={(e) => setEndDate(e.target.value)} />
|
|
252
|
+
</div>
|
|
253
|
+
<Button className="btn-clear-filters" onClick={() => { setStartDate(''); setEndDate(''); }} title={t('history.clearDates', 'Clear date filters')}>
|
|
254
|
+
<Trans i18nKey="history.clear">Clear</Trans>
|
|
255
|
+
</Button>
|
|
256
|
+
</div>
|
|
257
|
+
{error && <div className="error-message">{error}</div>}
|
|
258
|
+
{!loading && !error && (<>
|
|
259
|
+
<div className="history-list">
|
|
260
|
+
{history.length > 0 ? (
|
|
261
|
+
history.map(entry =>
|
|
262
|
+
<HistoryEntry
|
|
263
|
+
key={entry._id}
|
|
264
|
+
entry={entry}
|
|
265
|
+
onPreview={handlePreview}
|
|
266
|
+
isSelected={selectedVersion === entry._v}
|
|
267
|
+
/>)
|
|
268
|
+
) : (
|
|
269
|
+
<div><Trans i18nKey={"history.noHistory"}>Aucun historique trouvé pour cet enregistrement.</Trans></div>
|
|
270
|
+
)}
|
|
271
|
+
</div>
|
|
272
|
+
{totalCount > elementsPerPage && <Pagination
|
|
273
|
+
totalCount={totalCount}
|
|
274
|
+
page={page}
|
|
275
|
+
setPage={setPage}
|
|
276
|
+
elementsPerPage={elementsPerPage}
|
|
277
|
+
visibleItemsCount={5}
|
|
278
|
+
hasPreviousNext={true}
|
|
279
|
+
/>}
|
|
280
|
+
</>)}
|
|
281
|
+
</div>
|
|
282
|
+
<div className="history-preview-container">
|
|
283
|
+
<div className="preview-header">
|
|
284
|
+
<h4><Trans i18nKey={"history.previewTitle"}>Revision Preview</Trans></h4>
|
|
285
|
+
{previewData && !previewData.error && (
|
|
286
|
+
<Button
|
|
287
|
+
className="btn-revert"
|
|
288
|
+
onClick={() => handleRevert(selectedVersion)}
|
|
289
|
+
disabled={previewLoading}
|
|
290
|
+
title={t('history.revertTooltip', 'Revert the document to this state. This creates a new version.')}
|
|
291
|
+
>
|
|
292
|
+
<Trans i18nKey={"history.revertAction"}>Revert to this version</Trans>
|
|
293
|
+
</Button>
|
|
294
|
+
)}
|
|
295
|
+
</div>
|
|
296
|
+
|
|
297
|
+
<div className="history-preview-area">
|
|
298
|
+
{previewLoading && <div><Trans i18nKey={"history.loadingPreview"}>Loading preview...</Trans></div>}
|
|
299
|
+
{!previewLoading && previewData && <CodeField value={JSON.stringify(previewData,null, 2)} language={"json"} disabled={true} />}
|
|
300
|
+
{!previewLoading && !previewData && (
|
|
301
|
+
<div className="placeholder"><Trans i18nKey={"history.selectRevision"}>Select a revision to preview its full state.</Trans></div>
|
|
302
|
+
)}
|
|
303
|
+
</div>
|
|
304
|
+
</div>
|
|
305
|
+
</div>
|
|
306
|
+
</Dialog>
|
|
307
|
+
);
|
|
308
308
|
};
|