@olenbetong/synergi-react 4.0.0 → 4.1.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 (85) hide show
  1. package/es/components/DateNavigator/index.js +1 -1
  2. package/es/components/FilePond.js +1 -1
  3. package/es/components/ImageEditor/ImageEditor.js +1 -1
  4. package/es/components/ImageEditor/plugins/cropPlugin/CropOverlay.js +1 -1
  5. package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
  6. package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
  7. package/es/components/ImageEditor/plugins/zoomPlugin/ZoomControls.js +1 -1
  8. package/es/components/MergeProgressDialog/index.js +1 -1
  9. package/es/components/PdfViewer/PdfViewer.js +22 -3
  10. package/es/components/Toolbar/index.js +1 -1
  11. package/es/elements/ElementDamage/DamageFormButtons.js +1 -1
  12. package/es/elements/ElementDamage/DescriptionInput.js +1 -1
  13. package/es/elements/ElementDamage/DuplicateDamage/DesktopView.js +1 -1
  14. package/es/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.js +1 -1
  15. package/es/elements/ElementDamage/DuplicateDamage/PhoneView.js +1 -1
  16. package/es/elements/ElementDamage/ElementNumberTextField.js +1 -1
  17. package/es/elements/ElementDamage/RegisterFix.js +1 -1
  18. package/es/elements/ElementDamage/SubmitButtons.js +1 -1
  19. package/es/elements/ElementDamage/TransportElementSelect.js +1 -1
  20. package/es/elements/ElementLookup.js +1 -1
  21. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/native.js +1 -1
  22. package/es/elements/ElementLookupDialog/BarcodeScanner/index.js +1 -1
  23. package/es/elements/ElementLookupDialog/BarcodeScanner/useScanner.js +1 -1
  24. package/es/elements/ElementLookupDialog/HIDScanner/index.js +1 -1
  25. package/es/elements/ElementLookupDialog/Manual/ManualForm.js +1 -1
  26. package/es/elements/ElementLookupDialog/index.js +1 -1
  27. package/es/elements/ElementLookupDialog/utils/Giai.js +1 -1
  28. package/es/elements/ProjectSelect.js +1 -1
  29. package/es/i18n.d.ts +6 -0
  30. package/es/i18n.js +10 -0
  31. package/package.json +7 -7
  32. package/src/chrome/AppShell/index.tsx +29 -29
  33. package/src/chrome/openReport.ts +34 -34
  34. package/src/colorScheme.ts +81 -81
  35. package/src/components/DateNavigator/index.tsx +186 -186
  36. package/src/components/FilePond.tsx +312 -312
  37. package/src/components/ImageEditor/ImageEditor.tsx +343 -343
  38. package/src/components/ImageEditor/index.ts +9 -9
  39. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  40. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  41. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +403 -403
  42. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  43. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  44. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  45. package/src/components/ImageEditor/useEdgeDetection.ts +127 -127
  46. package/src/components/ImageEditor/useImageHistory.ts +197 -197
  47. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  48. package/src/components/LinkedCardList/index.scss +95 -95
  49. package/src/components/MergeProgressDialog/index.tsx +249 -249
  50. package/src/components/PageBanner/index.tsx +80 -80
  51. package/src/components/PdfViewer/PdfViewer.tsx +158 -139
  52. package/src/components/ProgressBar/index.scss +96 -96
  53. package/src/components/SplitContainer/index.tsx +333 -333
  54. package/src/components/Toolbar/index.tsx +212 -212
  55. package/src/designsystemet-web-types.d.ts +24 -24
  56. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  57. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  58. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  59. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  60. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  61. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  62. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  63. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  64. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  65. package/src/elements/ElementLookup.tsx +123 -123
  66. package/src/elements/ElementLookupDialog/BarcodeScanner/decoder.ts +32 -32
  67. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/dbr.ts +118 -118
  68. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/native.ts +103 -103
  69. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +95 -95
  70. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders.test.ts +118 -118
  71. package/src/elements/ElementLookupDialog/BarcodeScanner/duplicateFilter.ts +21 -21
  72. package/src/elements/ElementLookupDialog/BarcodeScanner/index.tsx +90 -90
  73. package/src/elements/ElementLookupDialog/BarcodeScanner/useScanner.ts +280 -280
  74. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  75. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  76. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  77. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  78. package/src/elements/ProjectSelect.tsx +130 -130
  79. package/src/elements/index.ts +15 -15
  80. package/src/filter/index.ts +34 -34
  81. package/src/i18n.ts +13 -0
  82. package/src/index.ts +77 -77
  83. package/src/mui/MuiThemeProvider.tsx +174 -174
  84. package/src/mui/index.ts +1 -1
  85. package/src/test/setup.ts +8 -8
@@ -1,312 +1,312 @@
1
- import "filepond-plugin-file-poster/dist/filepond-plugin-file-poster.css";
2
- import "filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css";
3
- import "filepond/dist/filepond.css";
4
-
5
- import { getLocalizedString } from "@olenbetong/appframe-core";
6
- import type { DataObject } from "@olenbetong/appframe-data";
7
- import { useData, useFetchData } from "@olenbetong/appframe-react";
8
-
9
- import {
10
- create,
11
- type FilePondErrorDescription,
12
- type FilePondFile,
13
- type FilePondInitialFile,
14
- type FilePond as FilePondInstance,
15
- type FilePondOptions,
16
- FileStatus,
17
- registerPlugin,
18
- } from "filepond";
19
- import FilePondPluginImageExifOrientation from "filepond-plugin-image-exif-orientation";
20
- import FilePondPluginImagePreview from "filepond-plugin-image-preview";
21
- import FilePondPluginImageResize from "filepond-plugin-image-resize";
22
- import FilePondPluginImageTransform from "filepond-plugin-image-transform";
23
- import { type RefObject, useEffect, useMemo, useRef } from "react";
24
-
25
- export { FileStatus, registerPlugin };
26
-
27
- registerPlugin(
28
- FilePondPluginImageExifOrientation,
29
- FilePondPluginImageResize,
30
- FilePondPluginImagePreview,
31
- FilePondPluginImageTransform,
32
- );
33
-
34
- let isMostLikelyMobile =
35
- navigator.userAgent.match(/Android/i) ||
36
- navigator.userAgent.match(/iPhone/i) ||
37
- navigator.userAgent.match(/iPad/i) ||
38
- navigator.userAgent.match(/iPod/i);
39
-
40
- const labels = {
41
- labelIdle: isMostLikelyMobile
42
- ? getLocalizedString("Take a photo, or upload an existing image/file")
43
- : getLocalizedString('Drag & Drop your files or <span class="filepond--label-action"> Browse </span>'),
44
- labelInvalidField: getLocalizedString("Field contains invalid files"),
45
- labelFileWaitingForSize: getLocalizedString("Waiting for size"),
46
- labelFileSizeNotAvailable: getLocalizedString("Size not available"),
47
- labelFileLoading: getLocalizedString("Loading"),
48
- labelFileLoadError: getLocalizedString("Error during load"),
49
- labelFileProcessing: getLocalizedString("Uploading"),
50
- labelFileProcessingComplete: getLocalizedString("Upload complete"),
51
- labelFileProcessingAborted: getLocalizedString("Upload cancelled"),
52
- labelFileProcessingError: getLocalizedString("Error during upload"),
53
- labelFileProcessingRevertError: getLocalizedString("Error during revert"),
54
- labelFileRemoveError: getLocalizedString("Error during remove"),
55
- labelTapToCancel: getLocalizedString("tap to cancel"),
56
- labelTapToRetry: getLocalizedString("tap to retry"),
57
- labelTapToUndo: getLocalizedString("tap to undo"),
58
- labelButtonRemoveItem: getLocalizedString("Remove"),
59
- labelButtonAbortItemLoad: getLocalizedString("Abort"),
60
- labelButtonRetryItemLoad: getLocalizedString("Retry"),
61
- labelButtonAbortItemProcessing: getLocalizedString("Cancel"),
62
- labelButtonUndoItemProcessing: getLocalizedString("Undo"),
63
- labelButtonRetryItemProcessing: getLocalizedString("Retry"),
64
- labelButtonProcessItem: getLocalizedString("Upload"),
65
- labelMaxFileSizeExceeded: getLocalizedString("File is too large"),
66
- labelMaxFileSize: getLocalizedString("Maximum file size is {filesize}"),
67
- labelMaxTotalFileSizeExceeded: getLocalizedString("Maximum total size exceeded"),
68
- labelMaxTotalFileSize: getLocalizedString("Maximum total file size is {filesize}"),
69
- labelFileTypeNotAllowed: getLocalizedString("File of invalid type"),
70
- fileValidateTypeLabelExpectedTypes: getLocalizedString("Expects {allButLastType} or {lastType}"),
71
- imageValidateSizeLabelFormatError: getLocalizedString("Image type not supported"),
72
- imageValidateSizeLabelImageSizeTooSmall: getLocalizedString("Image is too small"),
73
- imageValidateSizeLabelImageSizeTooBig: getLocalizedString("Image is too big"),
74
- imageValidateSizeLabelExpectedMinSize: getLocalizedString("Minimum size is {minWidth} × {minHeight}"),
75
- imageValidateSizeLabelExpectedMaxSize: getLocalizedString("Maximum size is {maxWidth} × {maxHeight}"),
76
- imageValidateSizeLabelImageResolutionTooLow: getLocalizedString("Resolution is too low"),
77
- imageValidateSizeLabelImageResolutionTooHigh: getLocalizedString("Resolution is too high"),
78
- imageValidateSizeLabelExpectedMinResolution: getLocalizedString("Minimum resolution is {minResolution}"),
79
- imageValidateSizeLabelExpectedMaxResolution: getLocalizedString("Maximum resolution is {maxResolution}"),
80
- };
81
-
82
- type FilePondProps<T extends Record<string, unknown>> = React.HTMLProps<HTMLInputElement> & {
83
- dataObject: DataObject<T>;
84
- fields: Partial<T>;
85
- onUploadStart: () => void;
86
- onFileUploaded: (error: FilePondErrorDescription | null, file: FilePondFile) => void;
87
- onFinishedUploads: () => void;
88
- onError: (error: string) => void;
89
- onFileRemoved?: (primKey: string) => void;
90
- /**
91
- * If the file should be uploaded to a blob field, set the field name here
92
- */
93
- fieldName?: string;
94
- /**
95
- * If the file should be added to an existing record, set the prim key here
96
- */
97
- primKey?: string;
98
- /**
99
- * If a reference to the file input element is needed, pass a ref here.
100
- */
101
- inputRef?: RefObject<HTMLInputElement>;
102
- /**
103
- * Options to pass to FilePond
104
- */
105
- filePondOptions?: Partial<FilePondOptions>;
106
- attachmentPrimKeys?: string[];
107
- whereClause?: string;
108
- };
109
-
110
- function getFileUrl<T extends Record<string, unknown>>(
111
- dataObject: DataObject<T>,
112
- fieldName?: string,
113
- primKey?: string,
114
- action = "upload",
115
- ) {
116
- let url =
117
- action === "upload"
118
- ? dataObject.dataHandler.getUploadUrl()
119
- : dataObject.dataHandler.getFileStoreURL(primKey ?? "", fieldName ?? "");
120
-
121
- if (action === "upload") {
122
- url += "/";
123
- }
124
-
125
- return url?.replaceAll("//", "/") ?? "";
126
- }
127
-
128
- type FileStoreDataObjectRecord = {
129
- PrimKey: string;
130
- FileName: string | null;
131
- FileSize: number | null;
132
- FileRef: string | null;
133
- ElementRef: string | null;
134
- Damage: boolean;
135
- DamageID: number | null;
136
- };
137
-
138
- export function FilePond<T extends FileStoreDataObjectRecord>({
139
- dataObject,
140
- fields,
141
- onUploadStart,
142
- onFileUploaded,
143
- onFinishedUploads,
144
- onFileRemoved,
145
- onError,
146
- fieldName,
147
- filePondOptions,
148
- primKey,
149
- attachmentPrimKeys,
150
- whereClause,
151
- inputRef: inputRefProp,
152
- ...props
153
- }: FilePondProps<T>) {
154
- let inputRef = useRef<HTMLInputElement>(null);
155
- let pondRef = useRef<FilePondInstance | null>(null);
156
- let filesData = useData(dataObject);
157
- let { data: filesFetched } = useFetchData(dataObject, whereClause ?? "1=2");
158
- let files = useMemo(() => {
159
- if (whereClause) {
160
- return filesFetched;
161
- }
162
- return filesData;
163
- }, [whereClause, filesFetched, filesData]);
164
- let clearFilesRef = useRef(false);
165
-
166
- useEffect(() => {
167
- return () => {
168
- pondRef.current?.destroy();
169
- pondRef.current = null;
170
- };
171
- }, []);
172
-
173
- useEffect(() => {
174
- async function removeUploadedFile(source: string, load: () => void, error: (error: string) => void) {
175
- // this method is also called when we clear the files on data object
176
- // refresh. Haven't found any elegant way to work around that, so instead
177
- // we set this ref to true when we remove the files to have a way
178
- // to discover why the files are being removed
179
- if (clearFilesRef.current) return;
180
-
181
- let { dataHandler } = dataObject;
182
- try {
183
- let result = await dataHandler.destroy({
184
- PrimKey: source,
185
- } as Partial<T>);
186
-
187
- if (result) {
188
- onFileRemoved?.(source);
189
- load();
190
- } else {
191
- error("Failed to delete file on server. Delete request returned false");
192
- }
193
- } catch (err: any) {
194
- error(
195
- err.responseText ??
196
- err.statusText ??
197
- err.message ??
198
- "Failed to delete file on server but no error was returned.",
199
- );
200
- }
201
- }
202
-
203
- let options: FilePondOptions = {
204
- allowMultiple: Boolean(props.multiple),
205
- allowImageResize: true,
206
- imageResizeMode: "contain",
207
- imageResizeTargetWidth: 2400,
208
- imageResizeTargetHeight: 2400,
209
- imageResizeUpscale: false,
210
- ...labels,
211
- onaddfilestart: (file) => {
212
- if (file.status !== 5 && file.status !== 6 && file.status !== 10) {
213
- onUploadStart();
214
- }
215
- },
216
- onprocessfile: (error, file) => {
217
- onFileUploaded(error, file);
218
-
219
- let refFiles = pondRef.current?.getFiles();
220
- let found = refFiles?.find(
221
- (file) => file.status !== 2 && file.status !== 5 && file.status !== 6 && file.status !== 10,
222
- );
223
-
224
- if (found === undefined) {
225
- onFinishedUploads();
226
- }
227
- },
228
- onprocessfiles: () => onFinishedUploads(),
229
- onerror: (error) => onError(error.body),
230
- beforeRemoveFile: () => {
231
- if (confirm(getLocalizedString("Are you sure you want to remove this file?"))) {
232
- return true;
233
- }
234
-
235
- return false;
236
- },
237
- server: {
238
- load: getFileUrl(dataObject, fieldName, primKey, "view"),
239
- restore: getFileUrl(dataObject, fieldName, primKey, "view"),
240
-
241
- revert: removeUploadedFile,
242
- remove: removeUploadedFile,
243
-
244
- process: {
245
- url: getFileUrl(dataObject, fieldName),
246
- ondata: (formData) => {
247
- let masterChildCriteria = dataObject.getParameter("masterChildCriteria");
248
- let allFields = { ...masterChildCriteria, ...fields };
249
-
250
- for (let [field, value] of Object.entries(allFields)) {
251
- if (value !== undefined) {
252
- formData.append(field, String(value));
253
- }
254
- }
255
-
256
- return formData;
257
- },
258
- onload: (response) => JSON.parse(response),
259
- },
260
- },
261
- ...filePondOptions,
262
- };
263
-
264
- if (pondRef.current) {
265
- pondRef.current.setOptions(options);
266
- } else {
267
- pondRef.current = create(inputRefProp?.current ?? inputRef.current ?? undefined, options);
268
- }
269
- }, [
270
- dataObject,
271
- props.multiple,
272
- fields,
273
- fieldName,
274
- primKey,
275
- inputRefProp,
276
- filePondOptions,
277
- onError,
278
- onFileUploaded,
279
- onUploadStart,
280
- onFinishedUploads,
281
- onFileRemoved,
282
- ]);
283
-
284
- useEffect(() => {
285
- if (pondRef.current) {
286
- files.forEach((file, index) => {
287
- let options: { index: number } & Partial<FilePondInitialFile["options"]> = {
288
- index,
289
- type: "local",
290
- };
291
-
292
- pondRef.current?.addFile(file.PrimKey, options);
293
- });
294
- }
295
-
296
- return () => {
297
- // Indicate that we are clearing the files because data has changed,
298
- // to prevent the remove callback from deleting the files
299
- clearFilesRef.current = true;
300
- pondRef.current?.removeFiles();
301
- clearFilesRef.current = false;
302
- };
303
- }, [files]);
304
-
305
- return (
306
- <div className="filepond--wrapper">
307
- {attachmentPrimKeys && <input type="hidden" name="AttachmentPrimkeys" value={attachmentPrimKeys} />}
308
-
309
- <input type="file" {...props} name="File" ref={inputRefProp ?? inputRef} />
310
- </div>
311
- );
312
- }
1
+ import "filepond-plugin-file-poster/dist/filepond-plugin-file-poster.css";
2
+ import "filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css";
3
+ import "filepond/dist/filepond.css";
4
+
5
+ import type { DataObject } from "@olenbetong/appframe-data";
6
+ import { useData, useFetchData } from "@olenbetong/appframe-react";
7
+
8
+ import {
9
+ create,
10
+ type FilePondErrorDescription,
11
+ type FilePondFile,
12
+ type FilePondInitialFile,
13
+ type FilePond as FilePondInstance,
14
+ type FilePondOptions,
15
+ FileStatus,
16
+ registerPlugin,
17
+ } from "filepond";
18
+ import FilePondPluginImageExifOrientation from "filepond-plugin-image-exif-orientation";
19
+ import FilePondPluginImagePreview from "filepond-plugin-image-preview";
20
+ import FilePondPluginImageResize from "filepond-plugin-image-resize";
21
+ import FilePondPluginImageTransform from "filepond-plugin-image-transform";
22
+ import { type RefObject, useEffect, useMemo, useRef } from "react";
23
+ import { getLocalizedString } from "../i18n.js";
24
+
25
+ export { FileStatus, registerPlugin };
26
+
27
+ registerPlugin(
28
+ FilePondPluginImageExifOrientation,
29
+ FilePondPluginImageResize,
30
+ FilePondPluginImagePreview,
31
+ FilePondPluginImageTransform,
32
+ );
33
+
34
+ let isMostLikelyMobile =
35
+ navigator.userAgent.match(/Android/i) ||
36
+ navigator.userAgent.match(/iPhone/i) ||
37
+ navigator.userAgent.match(/iPad/i) ||
38
+ navigator.userAgent.match(/iPod/i);
39
+
40
+ const labels = {
41
+ labelIdle: isMostLikelyMobile
42
+ ? getLocalizedString("Take a photo, or upload an existing image/file")
43
+ : getLocalizedString('Drag & Drop your files or <span class="filepond--label-action"> Browse </span>'),
44
+ labelInvalidField: getLocalizedString("Field contains invalid files"),
45
+ labelFileWaitingForSize: getLocalizedString("Waiting for size"),
46
+ labelFileSizeNotAvailable: getLocalizedString("Size not available"),
47
+ labelFileLoading: getLocalizedString("Loading"),
48
+ labelFileLoadError: getLocalizedString("Error during load"),
49
+ labelFileProcessing: getLocalizedString("Uploading"),
50
+ labelFileProcessingComplete: getLocalizedString("Upload complete"),
51
+ labelFileProcessingAborted: getLocalizedString("Upload cancelled"),
52
+ labelFileProcessingError: getLocalizedString("Error during upload"),
53
+ labelFileProcessingRevertError: getLocalizedString("Error during revert"),
54
+ labelFileRemoveError: getLocalizedString("Error during remove"),
55
+ labelTapToCancel: getLocalizedString("tap to cancel"),
56
+ labelTapToRetry: getLocalizedString("tap to retry"),
57
+ labelTapToUndo: getLocalizedString("tap to undo"),
58
+ labelButtonRemoveItem: getLocalizedString("Remove"),
59
+ labelButtonAbortItemLoad: getLocalizedString("Abort"),
60
+ labelButtonRetryItemLoad: getLocalizedString("Retry"),
61
+ labelButtonAbortItemProcessing: getLocalizedString("Cancel"),
62
+ labelButtonUndoItemProcessing: getLocalizedString("Undo"),
63
+ labelButtonRetryItemProcessing: getLocalizedString("Retry"),
64
+ labelButtonProcessItem: getLocalizedString("Upload"),
65
+ labelMaxFileSizeExceeded: getLocalizedString("File is too large"),
66
+ labelMaxFileSize: getLocalizedString("Maximum file size is {filesize}"),
67
+ labelMaxTotalFileSizeExceeded: getLocalizedString("Maximum total size exceeded"),
68
+ labelMaxTotalFileSize: getLocalizedString("Maximum total file size is {filesize}"),
69
+ labelFileTypeNotAllowed: getLocalizedString("File of invalid type"),
70
+ fileValidateTypeLabelExpectedTypes: getLocalizedString("Expects {allButLastType} or {lastType}"),
71
+ imageValidateSizeLabelFormatError: getLocalizedString("Image type not supported"),
72
+ imageValidateSizeLabelImageSizeTooSmall: getLocalizedString("Image is too small"),
73
+ imageValidateSizeLabelImageSizeTooBig: getLocalizedString("Image is too big"),
74
+ imageValidateSizeLabelExpectedMinSize: getLocalizedString("Minimum size is {minWidth} × {minHeight}"),
75
+ imageValidateSizeLabelExpectedMaxSize: getLocalizedString("Maximum size is {maxWidth} × {maxHeight}"),
76
+ imageValidateSizeLabelImageResolutionTooLow: getLocalizedString("Resolution is too low"),
77
+ imageValidateSizeLabelImageResolutionTooHigh: getLocalizedString("Resolution is too high"),
78
+ imageValidateSizeLabelExpectedMinResolution: getLocalizedString("Minimum resolution is {minResolution}"),
79
+ imageValidateSizeLabelExpectedMaxResolution: getLocalizedString("Maximum resolution is {maxResolution}"),
80
+ };
81
+
82
+ type FilePondProps<T extends Record<string, unknown>> = React.HTMLProps<HTMLInputElement> & {
83
+ dataObject: DataObject<T>;
84
+ fields: Partial<T>;
85
+ onUploadStart: () => void;
86
+ onFileUploaded: (error: FilePondErrorDescription | null, file: FilePondFile) => void;
87
+ onFinishedUploads: () => void;
88
+ onError: (error: string) => void;
89
+ onFileRemoved?: (primKey: string) => void;
90
+ /**
91
+ * If the file should be uploaded to a blob field, set the field name here
92
+ */
93
+ fieldName?: string;
94
+ /**
95
+ * If the file should be added to an existing record, set the prim key here
96
+ */
97
+ primKey?: string;
98
+ /**
99
+ * If a reference to the file input element is needed, pass a ref here.
100
+ */
101
+ inputRef?: RefObject<HTMLInputElement>;
102
+ /**
103
+ * Options to pass to FilePond
104
+ */
105
+ filePondOptions?: Partial<FilePondOptions>;
106
+ attachmentPrimKeys?: string[];
107
+ whereClause?: string;
108
+ };
109
+
110
+ function getFileUrl<T extends Record<string, unknown>>(
111
+ dataObject: DataObject<T>,
112
+ fieldName?: string,
113
+ primKey?: string,
114
+ action = "upload",
115
+ ) {
116
+ let url =
117
+ action === "upload"
118
+ ? dataObject.dataHandler.getUploadUrl()
119
+ : dataObject.dataHandler.getFileStoreURL(primKey ?? "", fieldName ?? "");
120
+
121
+ if (action === "upload") {
122
+ url += "/";
123
+ }
124
+
125
+ return url?.replaceAll("//", "/") ?? "";
126
+ }
127
+
128
+ type FileStoreDataObjectRecord = {
129
+ PrimKey: string;
130
+ FileName: string | null;
131
+ FileSize: number | null;
132
+ FileRef: string | null;
133
+ ElementRef: string | null;
134
+ Damage: boolean;
135
+ DamageID: number | null;
136
+ };
137
+
138
+ export function FilePond<T extends FileStoreDataObjectRecord>({
139
+ dataObject,
140
+ fields,
141
+ onUploadStart,
142
+ onFileUploaded,
143
+ onFinishedUploads,
144
+ onFileRemoved,
145
+ onError,
146
+ fieldName,
147
+ filePondOptions,
148
+ primKey,
149
+ attachmentPrimKeys,
150
+ whereClause,
151
+ inputRef: inputRefProp,
152
+ ...props
153
+ }: FilePondProps<T>) {
154
+ let inputRef = useRef<HTMLInputElement>(null);
155
+ let pondRef = useRef<FilePondInstance | null>(null);
156
+ let filesData = useData(dataObject);
157
+ let { data: filesFetched } = useFetchData(dataObject, whereClause ?? "1=2");
158
+ let files = useMemo(() => {
159
+ if (whereClause) {
160
+ return filesFetched;
161
+ }
162
+ return filesData;
163
+ }, [whereClause, filesFetched, filesData]);
164
+ let clearFilesRef = useRef(false);
165
+
166
+ useEffect(() => {
167
+ return () => {
168
+ pondRef.current?.destroy();
169
+ pondRef.current = null;
170
+ };
171
+ }, []);
172
+
173
+ useEffect(() => {
174
+ async function removeUploadedFile(source: string, load: () => void, error: (error: string) => void) {
175
+ // this method is also called when we clear the files on data object
176
+ // refresh. Haven't found any elegant way to work around that, so instead
177
+ // we set this ref to true when we remove the files to have a way
178
+ // to discover why the files are being removed
179
+ if (clearFilesRef.current) return;
180
+
181
+ let { dataHandler } = dataObject;
182
+ try {
183
+ let result = await dataHandler.destroy({
184
+ PrimKey: source,
185
+ } as Partial<T>);
186
+
187
+ if (result) {
188
+ onFileRemoved?.(source);
189
+ load();
190
+ } else {
191
+ error("Failed to delete file on server. Delete request returned false");
192
+ }
193
+ } catch (err: any) {
194
+ error(
195
+ err.responseText ??
196
+ err.statusText ??
197
+ err.message ??
198
+ "Failed to delete file on server but no error was returned.",
199
+ );
200
+ }
201
+ }
202
+
203
+ let options: FilePondOptions = {
204
+ allowMultiple: Boolean(props.multiple),
205
+ allowImageResize: true,
206
+ imageResizeMode: "contain",
207
+ imageResizeTargetWidth: 2400,
208
+ imageResizeTargetHeight: 2400,
209
+ imageResizeUpscale: false,
210
+ ...labels,
211
+ onaddfilestart: (file) => {
212
+ if (file.status !== 5 && file.status !== 6 && file.status !== 10) {
213
+ onUploadStart();
214
+ }
215
+ },
216
+ onprocessfile: (error, file) => {
217
+ onFileUploaded(error, file);
218
+
219
+ let refFiles = pondRef.current?.getFiles();
220
+ let found = refFiles?.find(
221
+ (file) => file.status !== 2 && file.status !== 5 && file.status !== 6 && file.status !== 10,
222
+ );
223
+
224
+ if (found === undefined) {
225
+ onFinishedUploads();
226
+ }
227
+ },
228
+ onprocessfiles: () => onFinishedUploads(),
229
+ onerror: (error) => onError(error.body),
230
+ beforeRemoveFile: () => {
231
+ if (confirm(getLocalizedString("Are you sure you want to remove this file?"))) {
232
+ return true;
233
+ }
234
+
235
+ return false;
236
+ },
237
+ server: {
238
+ load: getFileUrl(dataObject, fieldName, primKey, "view"),
239
+ restore: getFileUrl(dataObject, fieldName, primKey, "view"),
240
+
241
+ revert: removeUploadedFile,
242
+ remove: removeUploadedFile,
243
+
244
+ process: {
245
+ url: getFileUrl(dataObject, fieldName),
246
+ ondata: (formData) => {
247
+ let masterChildCriteria = dataObject.getParameter("masterChildCriteria");
248
+ let allFields = { ...masterChildCriteria, ...fields };
249
+
250
+ for (let [field, value] of Object.entries(allFields)) {
251
+ if (value !== undefined) {
252
+ formData.append(field, String(value));
253
+ }
254
+ }
255
+
256
+ return formData;
257
+ },
258
+ onload: (response) => JSON.parse(response),
259
+ },
260
+ },
261
+ ...filePondOptions,
262
+ };
263
+
264
+ if (pondRef.current) {
265
+ pondRef.current.setOptions(options);
266
+ } else {
267
+ pondRef.current = create(inputRefProp?.current ?? inputRef.current ?? undefined, options);
268
+ }
269
+ }, [
270
+ dataObject,
271
+ props.multiple,
272
+ fields,
273
+ fieldName,
274
+ primKey,
275
+ inputRefProp,
276
+ filePondOptions,
277
+ onError,
278
+ onFileUploaded,
279
+ onUploadStart,
280
+ onFinishedUploads,
281
+ onFileRemoved,
282
+ ]);
283
+
284
+ useEffect(() => {
285
+ if (pondRef.current) {
286
+ files.forEach((file, index) => {
287
+ let options: { index: number } & Partial<FilePondInitialFile["options"]> = {
288
+ index,
289
+ type: "local",
290
+ };
291
+
292
+ pondRef.current?.addFile(file.PrimKey, options);
293
+ });
294
+ }
295
+
296
+ return () => {
297
+ // Indicate that we are clearing the files because data has changed,
298
+ // to prevent the remove callback from deleting the files
299
+ clearFilesRef.current = true;
300
+ pondRef.current?.removeFiles();
301
+ clearFilesRef.current = false;
302
+ };
303
+ }, [files]);
304
+
305
+ return (
306
+ <div className="filepond--wrapper">
307
+ {attachmentPrimKeys && <input type="hidden" name="AttachmentPrimkeys" value={attachmentPrimKeys} />}
308
+
309
+ <input type="file" {...props} name="File" ref={inputRefProp ?? inputRef} />
310
+ </div>
311
+ );
312
+ }