@raketa-builder/file-input 1.3.8 → 1.3.10

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/dist/index.js CHANGED
@@ -1,1265 +1,3 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
- var __export = (target, all) => {
26
- for (var name in all)
27
- __defProp(target, name, { get: all[name], enumerable: true });
28
- };
29
- var __copyProps = (to, from, except, desc) => {
30
- if (from && typeof from === "object" || typeof from === "function") {
31
- for (let key of __getOwnPropNames(from))
32
- if (!__hasOwnProp.call(to, key) && key !== except)
33
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
34
- }
35
- return to;
36
- };
37
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
- // If the importer is in node compatibility mode or this is not an ESM
39
- // file that has been converted to a CommonJS file using a Babel-
40
- // compatible transform (i.e. "__esModule" has not been set), then set
41
- // "default" to the CommonJS "module.exports" for node compatibility.
42
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
- mod
44
- ));
45
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
- var __async = (__this, __arguments, generator) => {
47
- return new Promise((resolve, reject) => {
48
- var fulfilled = (value) => {
49
- try {
50
- step(generator.next(value));
51
- } catch (e) {
52
- reject(e);
53
- }
54
- };
55
- var rejected = (value) => {
56
- try {
57
- step(generator.throw(value));
58
- } catch (e) {
59
- reject(e);
60
- }
61
- };
62
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
63
- step((generator = generator.apply(__this, __arguments)).next());
64
- });
65
- };
66
-
67
- // src/index.js
68
- var index_exports = {};
69
- __export(index_exports, {
70
- FileManager: () => FileManager,
71
- FilePicker: () => FilePicker_default
72
- });
73
- module.exports = __toCommonJS(index_exports);
74
-
75
- // src/FilePicker/index.js
76
- var import_react7 = __toESM(require("react"));
77
- var import_ui7 = require("@raketa-builder/ui");
78
-
79
- // src/FilePicker/FileCategory.js
80
- var VIDEO = ["video/3gpp", "video/3gpp2", "video/mp4", "video/webm"];
81
- var AUDIO = [
82
- "audio/webm",
83
- "audio/wav",
84
- "audio/mpeg",
85
- "audio/aac",
86
- "audio/midi",
87
- "audio/x-midi",
88
- "audio/ogg",
89
- "audio/3gpp",
90
- "audio/3gpp2",
91
- "audio/m4a"
92
- ];
93
- var DOCUMENT = [
94
- "application/pdf",
95
- "application/msword",
96
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
97
- "application/vnd.ms-excel",
98
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
99
- "application/vnd.ms-powerpoint",
100
- "application/vnd.openxmlformats-officedocument.presentationml.presentation",
101
- "text/plain",
102
- "application/epub+zip",
103
- "application/x-mobipocket-ebook",
104
- "application/rtf",
105
- "text/csv",
106
- "application/vnd.apple.pages",
107
- "application/vnd.apple.numbers",
108
- "application/vnd.apple.keynote"
109
- ];
110
- var IMAGE = [
111
- "image/png",
112
- "image/x-png",
113
- "image/vnd.mozilla.apng",
114
- "image/jpeg",
115
- "image/svg+xml",
116
- "image/gif",
117
- "image/avif",
118
- "image/webp",
119
- "application/postscript",
120
- "mage/heic",
121
- "mage/heic-sequence"
122
- ];
123
- var ARCHIVE = [
124
- "application/zip",
125
- "application/gzip",
126
- "application/x-gtar",
127
- "application/x-tar",
128
- "application/x-rar-compressed",
129
- "application/x-7z-compressed"
130
- ];
131
- var MIME_TYPES = IMAGE.concat(VIDEO).concat(DOCUMENT).concat(AUDIO).concat(ARCHIVE);
132
- var MAPPING = {
133
- video: VIDEO,
134
- document: DOCUMENT,
135
- image: IMAGE,
136
- audio: AUDIO,
137
- archive: ARCHIVE,
138
- other: []
139
- };
140
- var FILE_CATEGORIES = {
141
- document: "Document",
142
- archive: "Archive",
143
- image: "Image",
144
- video: "Video",
145
- audio: "Audio",
146
- other: "Other"
147
- };
148
- function toMimeTypes(fileCategory) {
149
- return MAPPING[fileCategory] || [];
150
- }
151
- function toExcludedMimeTypes(fileCategory) {
152
- if (fileCategory === "other") {
153
- return MIME_TYPES;
154
- }
155
- return [];
156
- }
157
- function toFileCategory(mimeType, format = "robot") {
158
- let fileCategory = null;
159
- Object.keys(FILE_CATEGORIES).forEach((key) => {
160
- if (fileCategory) {
161
- return;
162
- }
163
- if (MAPPING[key].find((mt) => mt === mimeType)) {
164
- fileCategory = key;
165
- }
166
- });
167
- if (!fileCategory) {
168
- fileCategory = "other";
169
- }
170
- if (format === "human") {
171
- return FILE_CATEGORIES[fileCategory] || fileCategory;
172
- }
173
- return fileCategory;
174
- }
175
- function fileCategoriesOptions() {
176
- return Object.keys(FILE_CATEGORIES).map((key) => [key, FILE_CATEGORIES[key]]);
177
- }
178
- function sanitizeFileCategory(fileCategory) {
179
- if (fileCategory === "other") {
180
- return null;
181
- }
182
- if (Object.keys(FILE_CATEGORIES).includes(fileCategory)) {
183
- return fileCategory;
184
- }
185
- return null;
186
- }
187
-
188
- // src/FilePicker/stateManagement.js
189
- function mimeTypes(category) {
190
- return toMimeTypes(category);
191
- }
192
- function excludeMimeTypes(category) {
193
- return toExcludedMimeTypes(category);
194
- }
195
- function initialState(initialSelected, initialCategory) {
196
- return {
197
- open: false,
198
- fetching: false,
199
- selected: initialSelected,
200
- uploads: [],
201
- files: {
202
- initial: true,
203
- list: [],
204
- nextCursor: null,
205
- filters: {
206
- keyword: "",
207
- category: initialCategory || ""
208
- }
209
- }
210
- };
211
- }
212
- function initialUploadState(file, allowedMimeTypes) {
213
- if (allowedMimeTypes.length === 0 || allowedMimeTypes.includes(file.type)) {
214
- return {
215
- type: "pending",
216
- payload: {}
217
- };
218
- }
219
- return {
220
- type: "rejected",
221
- payload: {
222
- errors: [`Files of type "${file.type}" are not supported.`]
223
- }
224
- };
225
- }
226
- function uploadFiles(fileManager, files, allowedFileCategory, setState) {
227
- const allowedMimeTypes = toMimeTypes(allowedFileCategory);
228
- const fileUploads = files.map((file) => ({
229
- file,
230
- id: (Math.random() * 1e6).toFixed(0),
231
- state: initialUploadState(file, allowedMimeTypes)
232
- }));
233
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
234
- uploads: [...currentState.uploads, ...fileUploads]
235
- }));
236
- fileUploads.filter((fu) => fu.state.type === "pending").forEach(
237
- (fileUpload) => fileManager.createAndUpload(
238
- fileUpload,
239
- (tmpId, newState) => setState((currentState) => {
240
- if (newState.type === "completed") {
241
- if (currentState.files.list.find(
242
- (file) => file.id === newState.payload.document.id
243
- )) {
244
- return currentState;
245
- }
246
- return __spreadProps(__spreadValues({}, currentState), {
247
- uploads: currentState.uploads.filter(
248
- (upload) => upload.id !== tmpId
249
- ),
250
- files: __spreadProps(__spreadValues({}, currentState.files), {
251
- list: [
252
- __spreadValues({}, newState.payload.document),
253
- ...currentState.files.list
254
- ]
255
- })
256
- });
257
- }
258
- const newUploads = currentState.uploads.map((upload) => {
259
- if (upload.id === tmpId) {
260
- return __spreadProps(__spreadValues({}, upload), { state: newState });
261
- }
262
- return upload;
263
- });
264
- return __spreadProps(__spreadValues({}, currentState), { uploads: newUploads });
265
- })
266
- )
267
- );
268
- }
269
- function loadFiles(fileManager, state, setState) {
270
- return __async(this, null, function* () {
271
- if (state.files.initial) {
272
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
273
- open: true,
274
- fetching: true
275
- }));
276
- const resp = yield fileManager.all({
277
- mimeTypes: mimeTypes(state.files.filters.category),
278
- excludeMimeTypes: excludeMimeTypes(state.files.filters.category)
279
- });
280
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
281
- fetching: false,
282
- files: {
283
- initial: false,
284
- list: resp.list || [],
285
- nextCursor: resp.pagination.cursor,
286
- filters: {
287
- keyword: "",
288
- category: ""
289
- }
290
- }
291
- }));
292
- } else {
293
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
294
- open: true
295
- }));
296
- }
297
- });
298
- }
299
- function searchFiles(fileManager, filters, state, setState) {
300
- return __async(this, null, function* () {
301
- if ((filters.keyword !== state.files.filters.keyword || filters.category !== state.files.filters.category) && !state.fetching) {
302
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
303
- fetching: true
304
- }));
305
- const resp = yield fileManager.all({
306
- nextCursor: null,
307
- keyword: filters.keyword,
308
- mimeTypes: mimeTypes(filters.category),
309
- excludeMimeTypes: excludeMimeTypes(filters.category)
310
- });
311
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
312
- fetching: false,
313
- files: {
314
- list: [...resp.list],
315
- nextCursor: resp.pagination.cursor,
316
- filters: {
317
- keyword: filters.keyword,
318
- category: filters.category
319
- }
320
- }
321
- }));
322
- } else {
323
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
324
- fetching: false,
325
- files: __spreadProps(__spreadValues({}, currentState.files), {
326
- filters: {
327
- keyword: filters.keyword,
328
- category: filters.category
329
- }
330
- })
331
- }));
332
- }
333
- });
334
- }
335
- function deleteFile(fileManager, fileId, state, setState, onChange) {
336
- return __async(this, null, function* () {
337
- var _a, _b;
338
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
339
- fetching: true
340
- }));
341
- const response = yield fileManager.destroy(fileId);
342
- let removeFromList = false;
343
- let removeSelected = false;
344
- if (response.errors) {
345
- alert(`Something went wrong while deleting document#${fileId}`);
346
- }
347
- if (!response.errors && response.document) {
348
- removeFromList = true;
349
- if (fileId === ((_b = (_a = state.selected) == null ? void 0 : _a.document) == null ? void 0 : _b.id)) {
350
- removeSelected = true;
351
- }
352
- }
353
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
354
- fetching: false,
355
- selected: removeSelected ? null : currentState.selected,
356
- files: __spreadProps(__spreadValues({}, currentState.files), {
357
- list: currentState.files.list.filter((f) => f.id !== fileId)
358
- })
359
- }));
360
- if (removeSelected) {
361
- onChange(null);
362
- }
363
- });
364
- }
365
- function selectFile(fileId, setState) {
366
- setState((currentState) => {
367
- const file = currentState.files.list.find((f) => f.id === fileId);
368
- let newSelected = null;
369
- if (file) {
370
- newSelected = {
371
- document: file,
372
- uri: `raketacloud://documents/${file.id}`
373
- };
374
- }
375
- return __spreadProps(__spreadValues({}, currentState), {
376
- selected: newSelected
377
- });
378
- });
379
- }
380
- function loadMoreFiles(fileManager, state, setState) {
381
- if (!state.files.nextCursor) {
382
- return null;
383
- }
384
- return function loadMore() {
385
- return __async(this, null, function* () {
386
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
387
- fetching: true
388
- }));
389
- const resp = yield fileManager.all({
390
- nextCursor: state.files.nextCursor,
391
- keyword: state.files.filters.keyword,
392
- mimeTypes: mimeTypes(state.files.filters.category),
393
- excludeMimeTypes: excludeMimeTypes(state.files.filters.category)
394
- });
395
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
396
- fetching: false,
397
- files: {
398
- list: [...currentState.files.list, ...resp.list],
399
- nextCursor: resp.pagination.cursor,
400
- filters: __spreadValues({}, currentState.files.filters)
401
- }
402
- }));
403
- });
404
- };
405
- }
406
-
407
- // src/FilePicker/Dialog.js
408
- var import_react = __toESM(require("react"));
409
- var import_react_dropzone = require("react-dropzone");
410
- var import_ui = require("@raketa-builder/ui");
411
- var import_jsx_runtime = require("react/jsx-runtime");
412
- var DropZone = ({ children, onDrop }) => {
413
- const onFilesDrop = (0, import_react.useCallback)(
414
- (acceptedFiles) => {
415
- onDrop(acceptedFiles);
416
- },
417
- [onDrop]
418
- );
419
- const { getRootProps, getInputProps, isDragActive } = (0, import_react_dropzone.useDropzone)({
420
- onDropAccepted: onFilesDrop,
421
- noClick: true
422
- });
423
- return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
424
- "div",
425
- __spreadProps(__spreadValues({
426
- className: (0, import_ui.cn)(
427
- "r-reset",
428
- "r-dropzone-modal",
429
- isDragActive ? "r-dropzone-modal-active" : ""
430
- )
431
- }, getRootProps()), {
432
- children: [
433
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", __spreadValues({}, getInputProps())),
434
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: isDragActive && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { className: "r-fs-large", children: "Drop here" }) }),
435
- !isDragActive && children
436
- ]
437
- })
438
- );
439
- };
440
- var Dialog = ({
441
- disablePrimary = false,
442
- onClose,
443
- onPrimary,
444
- onDrop,
445
- children
446
- }) => {
447
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
448
- import_ui.Modal,
449
- {
450
- title: "Files",
451
- onClose,
452
- onDrop,
453
- customWrapper: DropZone,
454
- FooterContent: () => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
455
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
456
- import_ui.Button,
457
- {
458
- type: "button",
459
- variant: "primary",
460
- disabled: disablePrimary,
461
- onClick: onPrimary,
462
- children: "Select"
463
- }
464
- ),
465
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.Button, { type: "button", variant: "secondary", onClick: onClose, children: "Cancel" })
466
- ] }),
467
- children
468
- }
469
- );
470
- };
471
- var Dialog_default = Dialog;
472
-
473
- // src/FilePicker/FileSearch.js
474
- var import_react2 = __toESM(require("react"));
475
- var import_ui2 = require("@raketa-builder/ui");
476
- var import_jsx_runtime2 = require("react/jsx-runtime");
477
- var useDebunce = (value, onDebouncedChange) => {
478
- const timerRef = import_react2.default.useRef(null);
479
- import_react2.default.useEffect(() => {
480
- timerRef.current = setTimeout(() => {
481
- onDebouncedChange(value);
482
- }, 500);
483
- return () => {
484
- clearTimeout(timerRef.current);
485
- };
486
- }, [value]);
487
- };
488
- var SearchInput = ({ onChange }) => {
489
- const [q, setQ] = import_react2.default.useState("");
490
- useDebunce(q, onChange);
491
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
492
- import_ui2.Input,
493
- {
494
- placeholder: "Start typing file name...",
495
- value: q,
496
- onChange: (e) => {
497
- setQ(e.target.value);
498
- }
499
- }
500
- );
501
- };
502
- var FileSearch = ({ fetching, value, allowedFileCategory, onChange }) => {
503
- const onCategoryPick = (val) => {
504
- onChange(__spreadProps(__spreadValues({}, value), {
505
- category: val
506
- }));
507
- };
508
- const onSearchTermChange = (q) => {
509
- onChange(__spreadProps(__spreadValues({}, value), {
510
- keyword: q
511
- }));
512
- };
513
- if (allowedFileCategory) {
514
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "r-spacing-bottom-sm", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SearchInput, { onChange: onSearchTermChange }) });
515
- }
516
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
517
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SearchInput, { onChange: onSearchTermChange }),
518
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ui2.Stack, { className: "r-spacing-both-sm", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: (0, import_ui2.cn)("r-reset", "r-tab-wrapper"), children: [
519
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
520
- "button",
521
- {
522
- type: "button",
523
- disabled: fetching,
524
- className: `r-reset-button r-tab-button ${(value == null ? void 0 : value.category) === "" ? "r-tab-button-active" : ""}`,
525
- onClick: () => onCategoryPick(""),
526
- children: "All"
527
- }
528
- ),
529
- fileCategoriesOptions().map((category) => {
530
- const [key, label] = category;
531
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
532
- "button",
533
- {
534
- type: "button",
535
- disabled: fetching,
536
- className: `r-reset-button r-tab-button ${(value == null ? void 0 : value.category) === key ? "r-tab-button-active" : ""}`,
537
- onClick: () => onCategoryPick(key),
538
- children: label
539
- },
540
- key
541
- );
542
- })
543
- ] }) })
544
- ] });
545
- };
546
- var FileSearch_default = FileSearch;
547
-
548
- // src/FilePicker/FileList.js
549
- var import_react3 = __toESM(require("react"));
550
- var import_ui3 = require("@raketa-builder/ui");
551
-
552
- // src/FilePicker/utils.js
553
- function fileSizeFormatter(size) {
554
- if (size < 1024) {
555
- return `${size} B`;
556
- }
557
- if (size < 1024 * 1024) {
558
- return `${(size / 1024).toFixed(2)} kB`;
559
- }
560
- if (size < 1024 * 1024 * 1024) {
561
- return `${(size / 1024 / 1024).toFixed(2)} MB`;
562
- }
563
- return `${(size / 1024 / 1024 / 1024).toFixed(2)} GB`;
564
- }
565
-
566
- // src/FilePicker/FileList.js
567
- var import_jsx_runtime3 = require("react/jsx-runtime");
568
- var DeleteIcon = (props) => {
569
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
570
- "svg",
571
- __spreadProps(__spreadValues({
572
- width: "24",
573
- height: "25",
574
- viewBox: "0 0 24 25",
575
- fill: "none",
576
- xmlns: "http://www.w3.org/2000/svg",
577
- className: "r-file-input-delete-icon"
578
- }, props), {
579
- children: [
580
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("title", { children: "Delete" }),
581
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
582
- "path",
583
- {
584
- d: "M3 6.5H5H21",
585
- stroke: "currentColor",
586
- strokeWidth: "2",
587
- strokeLinecap: "round",
588
- strokeLinejoin: "round"
589
- }
590
- ),
591
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
592
- "path",
593
- {
594
- d: "M19 6.5V20.5C19 21.0304 18.7893 21.5391 18.4142 21.9142C18.0391 22.2893 17.5304 22.5 17 22.5H7C6.46957 22.5 5.96086 22.2893 5.58579 21.9142C5.21071 21.5391 5 21.0304 5 20.5V6.5M8 6.5V4.5C8 3.96957 8.21071 3.46086 8.58579 3.08579C8.96086 2.71071 9.46957 2.5 10 2.5H14C14.5304 2.5 15.0391 2.71071 15.4142 3.08579C15.7893 3.46086 16 3.96957 16 4.5V6.5",
595
- stroke: "currentColor",
596
- strokeWidth: "2",
597
- strokeLinecap: "round",
598
- strokeLinejoin: "round"
599
- }
600
- ),
601
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
602
- "path",
603
- {
604
- d: "M10 11.5V17.5",
605
- stroke: "currentColor",
606
- strokeWidth: "2",
607
- strokeLinecap: "round",
608
- strokeLinejoin: "round"
609
- }
610
- ),
611
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
612
- "path",
613
- {
614
- d: "M14 11.5V17.5",
615
- stroke: "currentColor",
616
- strokeWidth: "2",
617
- strokeLinecap: "round",
618
- strokeLinejoin: "round"
619
- }
620
- )
621
- ]
622
- })
623
- );
624
- };
625
- var CopyIcon = (props) => /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
626
- "svg",
627
- __spreadProps(__spreadValues({
628
- xmlns: "http://www.w3.org/2000/svg",
629
- width: "24",
630
- height: "25",
631
- viewBox: "0 0 24 25",
632
- fill: "none",
633
- className: "r-file-input-copy-icon"
634
- }, props), {
635
- children: [
636
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("title", { children: "Copy file URL" }),
637
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
638
- "path",
639
- {
640
- d: "M10 13.5C10.4295 14.0741 10.9774 14.5491 11.6066 14.8929C12.2357 15.2367 12.9315 15.4411 13.6467 15.4923C14.3618 15.5435 15.0796 15.4403 15.7513 15.1897C16.4231 14.9392 17.0331 14.547 17.54 14.04L20.54 11.04C21.4508 10.097 21.9548 8.83394 21.9434 7.52296C21.932 6.21198 21.4061 4.95791 20.4791 4.03087C19.5521 3.10383 18.298 2.57799 16.987 2.5666C15.676 2.5552 14.413 3.05918 13.47 3.96997L11.75 5.67997",
641
- stroke: "black",
642
- strokeWidth: "2",
643
- strokeLinecap: "round",
644
- strokeLinejoin: "round"
645
- }
646
- ),
647
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
648
- "path",
649
- {
650
- d: "M14 11.5C13.5705 10.9259 13.0226 10.4508 12.3934 10.107C11.7642 9.76328 11.0684 9.55886 10.3533 9.50765C9.63816 9.45643 8.92037 9.55961 8.24861 9.8102C7.57685 10.0608 6.96684 10.4529 6.45996 10.96L3.45996 13.96C2.54917 14.903 2.04519 16.166 2.05659 17.477C2.06798 18.788 2.59382 20.042 3.52086 20.9691C4.4479 21.8961 5.70197 22.422 7.01295 22.4334C8.32393 22.4447 9.58694 21.9408 10.53 21.03L12.24 19.32",
651
- stroke: "black",
652
- strokeWidth: "2",
653
- strokeLinecap: "round",
654
- strokeLinejoin: "round"
655
- }
656
- )
657
- ]
658
- })
659
- );
660
- var formatDate = (dateStr) => new Date(dateStr).toLocaleDateString(void 0, {
661
- year: "numeric",
662
- month: "long",
663
- day: "numeric",
664
- hour: "numeric",
665
- minute: "numeric",
666
- second: "numeric"
667
- });
668
- var FileList = ({
669
- files,
670
- fetching,
671
- selected,
672
- allowDelete,
673
- onMore,
674
- onSelect,
675
- onDelete
676
- }) => {
677
- const showLoadMore = !fetching && onMore;
678
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "r-reset", children: [
679
- /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("table", { className: "r-table", children: [
680
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("thead", { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("tr", { children: [
681
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("th", { children: "Name" }),
682
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("th", { children: "Type" }),
683
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("th", { children: "Size" }),
684
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("th", { children: "Uploaded" })
685
- ] }) }),
686
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("tbody", { children: files.map((file) => /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
687
- "tr",
688
- {
689
- className: (0, import_ui3.cn)(
690
- "r-file-input-file-row",
691
- (selected == null ? void 0 : selected.id) === file.id ? "r-file-input-file-row-active" : ""
692
- ),
693
- onClick: () => onSelect(file.id),
694
- children: [
695
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
696
- "td",
697
- {
698
- style: {
699
- width: "50%"
700
- },
701
- children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
702
- import_ui3.Stack,
703
- {
704
- className: "r-stack-align-center",
705
- style: { gap: "0.5rem" },
706
- children: [
707
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
708
- import_ui3.Input,
709
- {
710
- type: "radio",
711
- checked: (selected == null ? void 0 : selected.id) === file.id,
712
- readOnly: true
713
- }
714
- ),
715
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: decodeURI(file.filename) }),
716
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
717
- CopyIcon,
718
- {
719
- onClick: (e) => {
720
- e.stopPropagation();
721
- navigator.clipboard.writeText(file.url);
722
- }
723
- }
724
- ),
725
- allowDelete && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
726
- DeleteIcon,
727
- {
728
- onClick: (event) => {
729
- event.stopPropagation();
730
- if (confirm("Are you sure?")) {
731
- onDelete(file.id);
732
- }
733
- }
734
- }
735
- )
736
- ]
737
- }
738
- )
739
- }
740
- ),
741
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("td", { children: toFileCategory(file.mimeType, "human") }),
742
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("td", { children: fileSizeFormatter(file.size) }),
743
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("td", { children: formatDate(file.createdAt) })
744
- ]
745
- },
746
- file.id
747
- )) })
748
- ] }),
749
- showLoadMore && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_ui3.Button, { variant: "secondary", disabled: fetching, onClick: onMore, children: "Load more" })
750
- ] });
751
- };
752
- var FileList_default = FileList;
753
-
754
- // src/FilePicker/FileInput.js
755
- var import_react4 = __toESM(require("react"));
756
- var import_ui4 = require("@raketa-builder/ui");
757
- var import_jsx_runtime4 = require("react/jsx-runtime");
758
- var FileInput = ({ value, label, hint, onBrowse, onClear }) => {
759
- var _a;
760
- const filename = (_a = value == null ? void 0 : value.document) == null ? void 0 : _a.filename;
761
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_ui4.FormGroup, { children: [
762
- label && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_ui4.Label, { children: label }),
763
- /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_ui4.Stack, { className: "r-stack-justify-center", style: { gap: "0.5rem" }, children: [
764
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
765
- import_ui4.Input,
766
- {
767
- disabled: true,
768
- placeholder: filename ? filename : "No file selected"
769
- }
770
- ),
771
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_ui4.Button, { type: "button", variant: "primary", onClick: onBrowse, children: "Browse..." }),
772
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_ui4.Button, { type: "button", variant: "secondary", onClick: () => onClear(), children: "Clear" })
773
- ] }),
774
- hint && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_ui4.Hint, { children: hint })
775
- ] });
776
- };
777
- var FileInput_default = FileInput;
778
-
779
- // src/FilePicker/UploadProgress.js
780
- var import_react5 = __toESM(require("react"));
781
- var import_ui5 = require("@raketa-builder/ui");
782
- var import_jsx_runtime5 = require("react/jsx-runtime");
783
- var UploadState = ({ upload }) => {
784
- var _a, _b;
785
- switch (upload.state.type) {
786
- case "pending":
787
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: "r-reset", children: [
788
- fileSizeFormatter(0),
789
- " / ",
790
- fileSizeFormatter(upload.file.size || 0)
791
- ] });
792
- case "progress":
793
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
794
- "div",
795
- {
796
- className: (0, import_ui5.cn)(
797
- "r-reset",
798
- "r-stack",
799
- "r-stack-justify-beteen",
800
- "r-stack-align-center"
801
- ),
802
- children: [
803
- fileSizeFormatter(((_b = (_a = upload.state) == null ? void 0 : _a.payload) == null ? void 0 : _b.loaded) || 0),
804
- " /",
805
- " ",
806
- fileSizeFormatter(upload.file.size || 0)
807
- ]
808
- }
809
- );
810
- case "uploaded":
811
- case "completed":
812
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: "r-reset", children: [
813
- fileSizeFormatter(upload.file.size || 0),
814
- " /",
815
- " ",
816
- fileSizeFormatter(upload.file.size || 0)
817
- ] });
818
- case "errored":
819
- console.error({ upload });
820
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: (0, import_ui5.cn)("r-reset", "r-hint-error"), children: "Failed to upload" });
821
- case "rejected":
822
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: (0, import_ui5.cn)("r-reset", "r-hint-error"), children: upload.state.payload.errors.join("; ") });
823
- default:
824
- console.error({ upload });
825
- throw `Unknown state: ${upload.state}`;
826
- }
827
- };
828
- var UploadName = ({ upload }) => {
829
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "r-reset", children: upload.file.name });
830
- };
831
- var UploadProgress = ({ uploads }) => {
832
- if (uploads.length === 0) {
833
- return null;
834
- }
835
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: (0, import_ui5.cn)("r-reset", "r-spacing-top-sm"), children: uploads.map((upload) => /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
836
- import_ui5.Stack,
837
- {
838
- className: (0, import_ui5.cn)(
839
- "r-reset",
840
- "r-stack-justify-beteen",
841
- "r-stack-align-center",
842
- "r-file-progress"
843
- ),
844
- children: [
845
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(UploadName, { upload }),
846
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(UploadState, { upload })
847
- ]
848
- },
849
- upload.id
850
- )) });
851
- };
852
- var UploadProgress_default = UploadProgress;
853
-
854
- // src/FilePicker/UploadFilesButton.js
855
- var import_react6 = __toESM(require("react"));
856
- var import_ui6 = require("@raketa-builder/ui");
857
- var import_jsx_runtime6 = require("react/jsx-runtime");
858
- var UploadFilesButton = ({ allowedFileCategory, onSelect }) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
859
- "div",
860
- {
861
- className: (0, import_ui6.cn)("r-reset", "r-spacing-both-sm", "r-file-input-upload-button"),
862
- children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("label", { className: (0, import_ui6.cn)("r-button", "r-button-secondary"), children: [
863
- "Upload files...",
864
- /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
865
- "input",
866
- {
867
- id: "r-files-input",
868
- type: "file",
869
- multiple: true,
870
- accept: allowedFileCategory ? toMimeTypes(allowedFileCategory) : "",
871
- onChange: (event) => {
872
- const fileList = event.target.files;
873
- const files = [];
874
- for (let i = 0; i < fileList.length; i++) {
875
- files[i] = fileList.item(i);
876
- }
877
- onSelect(files);
878
- }
879
- }
880
- )
881
- ] })
882
- }
883
- );
884
- var UploadFilesButton_default = UploadFilesButton;
885
-
886
- // src/FilePicker/index.js
887
- var import_jsx_runtime7 = require("react/jsx-runtime");
888
- var FilePicker = ({
889
- fileManager,
890
- value: initialValue,
891
- label,
892
- hint,
893
- fileCategory = null,
894
- allowFilesDelete = true,
895
- onChange
896
- }) => {
897
- var _a;
898
- const allowedFileCategory = import_react7.default.useMemo(
899
- () => sanitizeFileCategory(fileCategory),
900
- [fileCategory]
901
- );
902
- const [state, setState] = import_react7.default.useState(
903
- initialState(initialValue, allowedFileCategory)
904
- );
905
- const onFilesUpload = (files) => {
906
- uploadFiles(fileManager, files, allowedFileCategory, setState);
907
- };
908
- const onBrowse = () => __async(null, null, function* () {
909
- loadFiles(fileManager, state, setState);
910
- });
911
- const onClear = () => {
912
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
913
- selected: null
914
- }));
915
- onChange(null);
916
- };
917
- const onSelectFile = () => {
918
- if (state.selected) {
919
- onChange(state.selected);
920
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), { open: false }));
921
- } else {
922
- alert("No file selected!");
923
- }
924
- };
925
- const onClose = () => {
926
- setState((currentState) => __spreadProps(__spreadValues({}, currentState), { open: false }));
927
- };
928
- const onSearchFiltersUpdate = (value) => __async(null, null, function* () {
929
- searchFiles(fileManager, value, state, setState);
930
- });
931
- const onDeleteFile = (fileId) => __async(null, null, function* () {
932
- deleteFile(fileManager, fileId, state, setState, onChange);
933
- });
934
- return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_jsx_runtime7.Fragment, { children: [
935
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
936
- FileInput_default,
937
- {
938
- label,
939
- hint,
940
- value: initialValue,
941
- onBrowse,
942
- onClear
943
- }
944
- ),
945
- state.open && /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(
946
- Dialog_default,
947
- {
948
- disablePrimary: !state.selected,
949
- onPrimary: onSelectFile,
950
- onClose,
951
- onDrop: onFilesUpload,
952
- children: [
953
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
954
- UploadFilesButton_default,
955
- {
956
- allowedFileCategory,
957
- onSelect: onFilesUpload
958
- }
959
- ),
960
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(UploadProgress_default, { uploads: state.uploads }),
961
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_ui7.H, { className: "r-spacing-top-sm", children: "Search" }),
962
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
963
- FileSearch_default,
964
- {
965
- allowedFileCategory,
966
- fetching: state.fetching,
967
- value: state.files.filters,
968
- onChange: onSearchFiltersUpdate
969
- }
970
- ),
971
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
972
- FileList_default,
973
- {
974
- fetching: state.fetching,
975
- files: state.files.list,
976
- onMore: loadMoreFiles(fileManager, state, setState),
977
- allowDelete: allowFilesDelete,
978
- onDelete: onDeleteFile,
979
- selected: (_a = state.selected) == null ? void 0 : _a.document,
980
- onSelect: (fileId) => selectFile(fileId, setState)
981
- }
982
- )
983
- ]
984
- }
985
- )
986
- ] });
987
- };
988
- var FilePicker_default = FilePicker;
989
-
990
- // src/FileManager.js
991
- var DOCUMENTS_QUERY = `
992
- query($input: DocumentsInput!) {
993
- documents(input: $input) {
994
- list {
995
- id
996
- filename
997
- mimeType
998
- size
999
- uploadStatus
1000
- url
1001
- createdAt
1002
- }
1003
- pagination {
1004
- cursor
1005
- }
1006
- }
1007
- }
1008
- `;
1009
- var CREATE_DOCUMENT_MUTATION = `
1010
- mutation($input: CreateDocumentInput!) {
1011
- createDocument(input: $input) {
1012
- document {
1013
- id
1014
- filename
1015
- mimeType
1016
- size
1017
- uploadStatus
1018
- url
1019
- createdAt
1020
- uploadUrl
1021
- }
1022
- errors {
1023
- key
1024
- message
1025
- codes {
1026
- key
1027
- placeholders
1028
- }
1029
- }
1030
- }
1031
- }
1032
- `;
1033
- var NOTIFY_UPLOADED_DOCUMENT_MUTATION = `
1034
- mutation($input: NotifyUploadedDocumentInput!) {
1035
- notifyUploadedDocument(input: $input) {
1036
- document {
1037
- id
1038
- filename
1039
- mimeType
1040
- size
1041
- uploadStatus
1042
- url
1043
- createdAt
1044
- }
1045
- errors {
1046
- key
1047
- message
1048
- codes {
1049
- key
1050
- placeholders
1051
- }
1052
- }
1053
- }
1054
- }
1055
- `;
1056
- var DELETE_DOCUMENT_MUTATION = `
1057
- mutation($input: DeleteDocumentInput!) {
1058
- deleteDocument(input: $input) {
1059
- document {
1060
- id
1061
- filename
1062
- mimeType
1063
- size
1064
- uploadStatus
1065
- url
1066
- createdAt
1067
- }
1068
- errors {
1069
- key
1070
- message
1071
- codes {
1072
- key
1073
- placeholders
1074
- }
1075
- }
1076
- }
1077
- }
1078
- `;
1079
- function uploadFile(file, document, progressCallback) {
1080
- return __async(this, null, function* () {
1081
- const xhr = new XMLHttpRequest();
1082
- return new Promise((resolve) => {
1083
- xhr.upload.addEventListener("progress", (event) => {
1084
- if (event.lengthComputable) {
1085
- progressCallback({
1086
- type: "progress",
1087
- payload: {
1088
- loaded: event.loaded,
1089
- total: event.total
1090
- }
1091
- });
1092
- }
1093
- });
1094
- xhr.addEventListener("loadend", (event) => {
1095
- if (xhr.readyState === 4 && xhr.status === 200) {
1096
- progressCallback({
1097
- type: "uploaded",
1098
- payload: {}
1099
- });
1100
- } else {
1101
- progressCallback({
1102
- type: "errored",
1103
- payload: {
1104
- errors: `ReadyState: ${xhr.readyState} - ${xhr.status} - ${event.type}: ${event.loaded} bytes transferred.`
1105
- }
1106
- });
1107
- }
1108
- resolve(xhr.readyState === 4 && xhr.status === 200);
1109
- });
1110
- xhr.withCredentials = false;
1111
- xhr.open("PUT", document.uploadUrl, true);
1112
- xhr.setRequestHeader("content-type", file.type);
1113
- const reader = new FileReader();
1114
- reader.onload = (event) => {
1115
- xhr.send(event.target.result);
1116
- };
1117
- reader.readAsArrayBuffer(file);
1118
- });
1119
- });
1120
- }
1121
- var FileManager = class {
1122
- constructor(url, headers) {
1123
- this.url = url;
1124
- this.headers = headers;
1125
- }
1126
- graphQLRequest(query, variables) {
1127
- return __async(this, null, function* () {
1128
- return fetch(this.url, {
1129
- method: "POST",
1130
- headers: Object.assign(
1131
- { "Content-Type": "application/json" },
1132
- this.headers
1133
- ),
1134
- body: JSON.stringify({
1135
- query,
1136
- variables
1137
- })
1138
- }).then((res) => res.json());
1139
- });
1140
- }
1141
- buildFilters(params) {
1142
- var _a, _b;
1143
- const filters = {
1144
- uploadStatus: "COMPLETED"
1145
- };
1146
- if (((_a = params == null ? void 0 : params.mimeTypes) == null ? void 0 : _a.length) > 0) {
1147
- filters.mimeTypes = params.mimeTypes;
1148
- }
1149
- if (((_b = params == null ? void 0 : params.excludeMimeTypes) == null ? void 0 : _b.length) > 0) {
1150
- filters.excludeMimeTypes = params.excludeMimeTypes;
1151
- }
1152
- if (params == null ? void 0 : params.keyword) {
1153
- filters.keyword = params.keyword;
1154
- }
1155
- return filters;
1156
- }
1157
- buildVariables(params) {
1158
- const variables = {
1159
- input: {
1160
- filters: this.buildFilters(params)
1161
- }
1162
- };
1163
- if (params.nextCursor) {
1164
- variables.input.pagination = { cursor: params.nextCursor };
1165
- }
1166
- return variables;
1167
- }
1168
- all() {
1169
- return __async(this, arguments, function* (params = {}) {
1170
- return this.graphQLRequest(
1171
- DOCUMENTS_QUERY,
1172
- this.buildVariables(params)
1173
- ).then((d) => {
1174
- return (d == null ? void 0 : d.data.documents) || {};
1175
- });
1176
- });
1177
- }
1178
- createAndUpload(fileInfo, progressCallback) {
1179
- return __async(this, null, function* () {
1180
- const { file, id } = fileInfo;
1181
- const { document, errors } = yield this.create({
1182
- filename: file.name,
1183
- mime_type: file.type,
1184
- size: file.size
1185
- });
1186
- if (errors) {
1187
- progressCallback(id, {
1188
- type: "errored",
1189
- payload: {
1190
- errors
1191
- }
1192
- });
1193
- return;
1194
- }
1195
- const result = yield uploadFile(
1196
- file,
1197
- document,
1198
- (state) => progressCallback(id, state)
1199
- );
1200
- if (result) {
1201
- const notify = yield this.notifyUploaded(document.id);
1202
- if (notify.errors) {
1203
- progressCallback(id, {
1204
- type: "errored",
1205
- payload: {
1206
- errors
1207
- }
1208
- });
1209
- return;
1210
- }
1211
- progressCallback(id, {
1212
- type: "completed",
1213
- payload: {
1214
- document: notify.document
1215
- }
1216
- });
1217
- }
1218
- });
1219
- }
1220
- create(params) {
1221
- return __async(this, null, function* () {
1222
- return this.graphQLRequest(CREATE_DOCUMENT_MUTATION, {
1223
- input: {
1224
- document: {
1225
- filename: params.filename,
1226
- mime_type: params.mime_type,
1227
- size: params.size
1228
- }
1229
- }
1230
- }).then((d) => {
1231
- var _a;
1232
- return ((_a = d == null ? void 0 : d.data) == null ? void 0 : _a.createDocument) || {};
1233
- });
1234
- });
1235
- }
1236
- notifyUploaded(id) {
1237
- return __async(this, null, function* () {
1238
- return this.graphQLRequest(NOTIFY_UPLOADED_DOCUMENT_MUTATION, {
1239
- input: {
1240
- id
1241
- }
1242
- }).then((d) => {
1243
- var _a;
1244
- return ((_a = d == null ? void 0 : d.data) == null ? void 0 : _a.notifyUploadedDocument) || {};
1245
- });
1246
- });
1247
- }
1248
- destroy(id) {
1249
- return __async(this, null, function* () {
1250
- return this.graphQLRequest(DELETE_DOCUMENT_MUTATION, {
1251
- input: {
1252
- id
1253
- }
1254
- }).then((d) => {
1255
- var _a;
1256
- return ((_a = d == null ? void 0 : d.data) == null ? void 0 : _a.deleteDocument) || {};
1257
- });
1258
- });
1259
- }
1260
- };
1261
- // Annotate the CommonJS export names for ESM import in node:
1262
- 0 && (module.exports = {
1263
- FileManager,
1264
- FilePicker
1265
- });
1
+ import './chunk-6FNC3XMI.js';
2
+ export { default as FilePicker } from './FilePicker';
3
+ export { default as FileManager } from './FileManager';