@raketa-builder/file-input 1.3.8 → 1.3.9

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.
@@ -0,0 +1,78 @@
1
+ import { __spreadProps, __spreadValues } from '../chunk-6FNC3XMI.js';
2
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
+ import React from 'react';
4
+ import { Stack, cn, Input } from '@raketa-builder/ui';
5
+ import { fileCategoriesOptions } from './FileCategory';
6
+
7
+ const useDebunce = (value, onDebouncedChange) => {
8
+ const timerRef = React.useRef(null);
9
+ React.useEffect(() => {
10
+ timerRef.current = setTimeout(() => {
11
+ onDebouncedChange(value);
12
+ }, 500);
13
+ return () => {
14
+ clearTimeout(timerRef.current);
15
+ };
16
+ }, [value]);
17
+ };
18
+ const SearchInput = ({ onChange }) => {
19
+ const [q, setQ] = React.useState("");
20
+ useDebunce(q, onChange);
21
+ return /* @__PURE__ */ jsx(
22
+ Input,
23
+ {
24
+ placeholder: "Start typing file name...",
25
+ value: q,
26
+ onChange: (e) => {
27
+ setQ(e.target.value);
28
+ }
29
+ }
30
+ );
31
+ };
32
+ const FileSearch = ({ fetching, value, allowedFileCategory, onChange }) => {
33
+ const onCategoryPick = (val) => {
34
+ onChange(__spreadProps(__spreadValues({}, value), {
35
+ category: val
36
+ }));
37
+ };
38
+ const onSearchTermChange = (q) => {
39
+ onChange(__spreadProps(__spreadValues({}, value), {
40
+ keyword: q
41
+ }));
42
+ };
43
+ if (allowedFileCategory) {
44
+ return /* @__PURE__ */ jsx("div", { className: "r-spacing-bottom-sm", children: /* @__PURE__ */ jsx(SearchInput, { onChange: onSearchTermChange }) });
45
+ }
46
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
47
+ /* @__PURE__ */ jsx(SearchInput, { onChange: onSearchTermChange }),
48
+ /* @__PURE__ */ jsx(Stack, { className: "r-spacing-both-sm", children: /* @__PURE__ */ jsxs("div", { className: cn("r-reset", "r-tab-wrapper"), children: [
49
+ /* @__PURE__ */ jsx(
50
+ "button",
51
+ {
52
+ type: "button",
53
+ disabled: fetching,
54
+ className: `r-reset-button r-tab-button ${(value == null ? void 0 : value.category) === "" ? "r-tab-button-active" : ""}`,
55
+ onClick: () => onCategoryPick(""),
56
+ children: "All"
57
+ }
58
+ ),
59
+ fileCategoriesOptions().map((category) => {
60
+ const [key, label] = category;
61
+ return /* @__PURE__ */ jsx(
62
+ "button",
63
+ {
64
+ type: "button",
65
+ disabled: fetching,
66
+ className: `r-reset-button r-tab-button ${(value == null ? void 0 : value.category) === key ? "r-tab-button-active" : ""}`,
67
+ onClick: () => onCategoryPick(key),
68
+ children: label
69
+ },
70
+ key
71
+ );
72
+ })
73
+ ] }) })
74
+ ] });
75
+ };
76
+ var FileSearch_default = FileSearch;
77
+
78
+ export { FileSearch_default as default };
@@ -0,0 +1,35 @@
1
+ import '../chunk-6FNC3XMI.js';
2
+ import { jsx, jsxs } from 'react/jsx-runtime';
3
+ import 'react';
4
+ import { cn } from '@raketa-builder/ui';
5
+ import { toMimeTypes } from './FileCategory';
6
+
7
+ const UploadFilesButton = ({ allowedFileCategory, onSelect }) => /* @__PURE__ */ jsx(
8
+ "div",
9
+ {
10
+ className: cn("r-reset", "r-spacing-both-sm", "r-file-input-upload-button"),
11
+ children: /* @__PURE__ */ jsxs("label", { className: cn("r-button", "r-button-secondary"), children: [
12
+ "Upload files...",
13
+ /* @__PURE__ */ jsx(
14
+ "input",
15
+ {
16
+ id: "r-files-input",
17
+ type: "file",
18
+ multiple: true,
19
+ accept: allowedFileCategory ? toMimeTypes(allowedFileCategory) : "",
20
+ onChange: (event) => {
21
+ const fileList = event.target.files;
22
+ const files = [];
23
+ for (let i = 0; i < fileList.length; i++) {
24
+ files[i] = fileList.item(i);
25
+ }
26
+ onSelect(files);
27
+ }
28
+ }
29
+ )
30
+ ] })
31
+ }
32
+ );
33
+ var UploadFilesButton_default = UploadFilesButton;
34
+
35
+ export { UploadFilesButton_default as default };
@@ -0,0 +1,78 @@
1
+ import '../chunk-6FNC3XMI.js';
2
+ import { jsx, jsxs } from 'react/jsx-runtime';
3
+ import 'react';
4
+ import { cn, Stack } from '@raketa-builder/ui';
5
+ import { fileSizeFormatter } from './utils';
6
+
7
+ const UploadState = ({ upload }) => {
8
+ var _a, _b;
9
+ switch (upload.state.type) {
10
+ case "pending":
11
+ return /* @__PURE__ */ jsxs("div", { className: "r-reset", children: [
12
+ fileSizeFormatter(0),
13
+ " / ",
14
+ fileSizeFormatter(upload.file.size || 0)
15
+ ] });
16
+ case "progress":
17
+ return /* @__PURE__ */ jsxs(
18
+ "div",
19
+ {
20
+ className: cn(
21
+ "r-reset",
22
+ "r-stack",
23
+ "r-stack-justify-beteen",
24
+ "r-stack-align-center"
25
+ ),
26
+ children: [
27
+ fileSizeFormatter(((_b = (_a = upload.state) == null ? void 0 : _a.payload) == null ? void 0 : _b.loaded) || 0),
28
+ " /",
29
+ " ",
30
+ fileSizeFormatter(upload.file.size || 0)
31
+ ]
32
+ }
33
+ );
34
+ case "uploaded":
35
+ case "completed":
36
+ return /* @__PURE__ */ jsxs("div", { className: "r-reset", children: [
37
+ fileSizeFormatter(upload.file.size || 0),
38
+ " /",
39
+ " ",
40
+ fileSizeFormatter(upload.file.size || 0)
41
+ ] });
42
+ case "errored":
43
+ console.error({ upload });
44
+ return /* @__PURE__ */ jsx("div", { className: cn("r-reset", "r-hint-error"), children: "Failed to upload" });
45
+ case "rejected":
46
+ return /* @__PURE__ */ jsx("div", { className: cn("r-reset", "r-hint-error"), children: upload.state.payload.errors.join("; ") });
47
+ default:
48
+ console.error({ upload });
49
+ throw `Unknown state: ${upload.state}`;
50
+ }
51
+ };
52
+ const UploadName = ({ upload }) => {
53
+ return /* @__PURE__ */ jsx("div", { className: "r-reset", children: upload.file.name });
54
+ };
55
+ const UploadProgress = ({ uploads }) => {
56
+ if (uploads.length === 0) {
57
+ return null;
58
+ }
59
+ return /* @__PURE__ */ jsx("div", { className: cn("r-reset", "r-spacing-top-sm"), children: uploads.map((upload) => /* @__PURE__ */ jsxs(
60
+ Stack,
61
+ {
62
+ className: cn(
63
+ "r-reset",
64
+ "r-stack-justify-beteen",
65
+ "r-stack-align-center",
66
+ "r-file-progress"
67
+ ),
68
+ children: [
69
+ /* @__PURE__ */ jsx(UploadName, { upload }),
70
+ /* @__PURE__ */ jsx(UploadState, { upload })
71
+ ]
72
+ },
73
+ upload.id
74
+ )) });
75
+ };
76
+ var UploadProgress_default = UploadProgress;
77
+
78
+ export { UploadProgress_default as default };
@@ -0,0 +1,116 @@
1
+ import { __async, __spreadProps, __spreadValues } from '../chunk-6FNC3XMI.js';
2
+ import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
3
+ import React from 'react';
4
+ import { H } from '@raketa-builder/ui';
5
+ import { initialState, selectFile, loadMoreFiles, uploadFiles, loadFiles, searchFiles, deleteFile } from './stateManagement';
6
+ import Dialog from './Dialog';
7
+ import FileSearch from './FileSearch';
8
+ import FileList from './FileList';
9
+ import FileInput from './FileInput';
10
+ import UploadProgress from './UploadProgress';
11
+ import UploadFilesButton from './UploadFilesButton';
12
+ import { sanitizeFileCategory } from './FileCategory';
13
+
14
+ const FilePicker = ({
15
+ fileManager,
16
+ value: initialValue,
17
+ label,
18
+ hint,
19
+ fileCategory = null,
20
+ allowFilesDelete = true,
21
+ onChange
22
+ }) => {
23
+ var _a;
24
+ const allowedFileCategory = React.useMemo(
25
+ () => sanitizeFileCategory(fileCategory),
26
+ [fileCategory]
27
+ );
28
+ const [state, setState] = React.useState(
29
+ initialState(initialValue, allowedFileCategory)
30
+ );
31
+ const onFilesUpload = (files) => {
32
+ uploadFiles(fileManager, files, allowedFileCategory, setState);
33
+ };
34
+ const onBrowse = () => __async(null, null, function* () {
35
+ loadFiles(fileManager, state, setState);
36
+ });
37
+ const onClear = () => {
38
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
39
+ selected: null
40
+ }));
41
+ onChange(null);
42
+ };
43
+ const onSelectFile = () => {
44
+ if (state.selected) {
45
+ onChange(state.selected);
46
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), { open: false }));
47
+ } else {
48
+ alert("No file selected!");
49
+ }
50
+ };
51
+ const onClose = () => {
52
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), { open: false }));
53
+ };
54
+ const onSearchFiltersUpdate = (value) => __async(null, null, function* () {
55
+ searchFiles(fileManager, value, state, setState);
56
+ });
57
+ const onDeleteFile = (fileId) => __async(null, null, function* () {
58
+ deleteFile(fileManager, fileId, state, setState, onChange);
59
+ });
60
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
61
+ /* @__PURE__ */ jsx(
62
+ FileInput,
63
+ {
64
+ label,
65
+ hint,
66
+ value: initialValue,
67
+ onBrowse,
68
+ onClear
69
+ }
70
+ ),
71
+ state.open && /* @__PURE__ */ jsxs(
72
+ Dialog,
73
+ {
74
+ disablePrimary: !state.selected,
75
+ onPrimary: onSelectFile,
76
+ onClose,
77
+ onDrop: onFilesUpload,
78
+ children: [
79
+ /* @__PURE__ */ jsx(
80
+ UploadFilesButton,
81
+ {
82
+ allowedFileCategory,
83
+ onSelect: onFilesUpload
84
+ }
85
+ ),
86
+ /* @__PURE__ */ jsx(UploadProgress, { uploads: state.uploads }),
87
+ /* @__PURE__ */ jsx(H, { className: "r-spacing-top-sm", children: "Search" }),
88
+ /* @__PURE__ */ jsx(
89
+ FileSearch,
90
+ {
91
+ allowedFileCategory,
92
+ fetching: state.fetching,
93
+ value: state.files.filters,
94
+ onChange: onSearchFiltersUpdate
95
+ }
96
+ ),
97
+ /* @__PURE__ */ jsx(
98
+ FileList,
99
+ {
100
+ fetching: state.fetching,
101
+ files: state.files.list,
102
+ onMore: loadMoreFiles(fileManager, state, setState),
103
+ allowDelete: allowFilesDelete,
104
+ onDelete: onDeleteFile,
105
+ selected: (_a = state.selected) == null ? void 0 : _a.document,
106
+ onSelect: (fileId) => selectFile(fileId, setState)
107
+ }
108
+ )
109
+ ]
110
+ }
111
+ )
112
+ ] });
113
+ };
114
+ var FilePicker_default = FilePicker;
115
+
116
+ export { FilePicker_default as default };
@@ -0,0 +1,220 @@
1
+ import { __spreadProps, __spreadValues, __async } from '../chunk-6FNC3XMI.js';
2
+ import { toMimeTypes, toExcludedMimeTypes } from './FileCategory';
3
+
4
+ function mimeTypes(category) {
5
+ return toMimeTypes(category);
6
+ }
7
+ function excludeMimeTypes(category) {
8
+ return toExcludedMimeTypes(category);
9
+ }
10
+ function initialState(initialSelected, initialCategory) {
11
+ return {
12
+ open: false,
13
+ fetching: false,
14
+ selected: initialSelected,
15
+ uploads: [],
16
+ files: {
17
+ initial: true,
18
+ list: [],
19
+ nextCursor: null,
20
+ filters: {
21
+ keyword: "",
22
+ category: initialCategory || ""
23
+ }
24
+ }
25
+ };
26
+ }
27
+ function initialUploadState(file, allowedMimeTypes) {
28
+ if (allowedMimeTypes.length === 0 || allowedMimeTypes.includes(file.type)) {
29
+ return {
30
+ type: "pending",
31
+ payload: {}
32
+ };
33
+ }
34
+ return {
35
+ type: "rejected",
36
+ payload: {
37
+ errors: [`Files of type "${file.type}" are not supported.`]
38
+ }
39
+ };
40
+ }
41
+ function uploadFiles(fileManager, files, allowedFileCategory, setState) {
42
+ const allowedMimeTypes = toMimeTypes(allowedFileCategory);
43
+ const fileUploads = files.map((file) => ({
44
+ file,
45
+ id: (Math.random() * 1e6).toFixed(0),
46
+ state: initialUploadState(file, allowedMimeTypes)
47
+ }));
48
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
49
+ uploads: [...currentState.uploads, ...fileUploads]
50
+ }));
51
+ fileUploads.filter((fu) => fu.state.type === "pending").forEach(
52
+ (fileUpload) => fileManager.createAndUpload(
53
+ fileUpload,
54
+ (tmpId, newState) => setState((currentState) => {
55
+ if (newState.type === "completed") {
56
+ if (currentState.files.list.find(
57
+ (file) => file.id === newState.payload.document.id
58
+ )) {
59
+ return currentState;
60
+ }
61
+ return __spreadProps(__spreadValues({}, currentState), {
62
+ uploads: currentState.uploads.filter(
63
+ (upload) => upload.id !== tmpId
64
+ ),
65
+ files: __spreadProps(__spreadValues({}, currentState.files), {
66
+ list: [
67
+ __spreadValues({}, newState.payload.document),
68
+ ...currentState.files.list
69
+ ]
70
+ })
71
+ });
72
+ }
73
+ const newUploads = currentState.uploads.map((upload) => {
74
+ if (upload.id === tmpId) {
75
+ return __spreadProps(__spreadValues({}, upload), { state: newState });
76
+ }
77
+ return upload;
78
+ });
79
+ return __spreadProps(__spreadValues({}, currentState), { uploads: newUploads });
80
+ })
81
+ )
82
+ );
83
+ }
84
+ function loadFiles(fileManager, state, setState) {
85
+ return __async(this, null, function* () {
86
+ if (state.files.initial) {
87
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
88
+ open: true,
89
+ fetching: true
90
+ }));
91
+ const resp = yield fileManager.all({
92
+ mimeTypes: mimeTypes(state.files.filters.category),
93
+ excludeMimeTypes: excludeMimeTypes(state.files.filters.category)
94
+ });
95
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
96
+ fetching: false,
97
+ files: {
98
+ initial: false,
99
+ list: resp.list || [],
100
+ nextCursor: resp.pagination.cursor,
101
+ filters: {
102
+ keyword: "",
103
+ category: ""
104
+ }
105
+ }
106
+ }));
107
+ } else {
108
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
109
+ open: true
110
+ }));
111
+ }
112
+ });
113
+ }
114
+ function searchFiles(fileManager, filters, state, setState) {
115
+ return __async(this, null, function* () {
116
+ if ((filters.keyword !== state.files.filters.keyword || filters.category !== state.files.filters.category) && !state.fetching) {
117
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
118
+ fetching: true
119
+ }));
120
+ const resp = yield fileManager.all({
121
+ nextCursor: null,
122
+ keyword: filters.keyword,
123
+ mimeTypes: mimeTypes(filters.category),
124
+ excludeMimeTypes: excludeMimeTypes(filters.category)
125
+ });
126
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
127
+ fetching: false,
128
+ files: {
129
+ list: [...resp.list],
130
+ nextCursor: resp.pagination.cursor,
131
+ filters: {
132
+ keyword: filters.keyword,
133
+ category: filters.category
134
+ }
135
+ }
136
+ }));
137
+ } else {
138
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
139
+ fetching: false,
140
+ files: __spreadProps(__spreadValues({}, currentState.files), {
141
+ filters: {
142
+ keyword: filters.keyword,
143
+ category: filters.category
144
+ }
145
+ })
146
+ }));
147
+ }
148
+ });
149
+ }
150
+ function deleteFile(fileManager, fileId, state, setState, onChange) {
151
+ return __async(this, null, function* () {
152
+ var _a, _b;
153
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
154
+ fetching: true
155
+ }));
156
+ const response = yield fileManager.destroy(fileId);
157
+ let removeSelected = false;
158
+ if (response.errors) {
159
+ alert(`Something went wrong while deleting document#${fileId}`);
160
+ }
161
+ if (!response.errors && response.document) {
162
+ if (fileId === ((_b = (_a = state.selected) == null ? void 0 : _a.document) == null ? void 0 : _b.id)) {
163
+ removeSelected = true;
164
+ }
165
+ }
166
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
167
+ fetching: false,
168
+ selected: removeSelected ? null : currentState.selected,
169
+ files: __spreadProps(__spreadValues({}, currentState.files), {
170
+ list: currentState.files.list.filter((f) => f.id !== fileId)
171
+ })
172
+ }));
173
+ if (removeSelected) {
174
+ onChange(null);
175
+ }
176
+ });
177
+ }
178
+ function selectFile(fileId, setState) {
179
+ setState((currentState) => {
180
+ const file = currentState.files.list.find((f) => f.id === fileId);
181
+ let newSelected = null;
182
+ if (file) {
183
+ newSelected = {
184
+ document: file,
185
+ uri: `raketacloud://documents/${file.id}`
186
+ };
187
+ }
188
+ return __spreadProps(__spreadValues({}, currentState), {
189
+ selected: newSelected
190
+ });
191
+ });
192
+ }
193
+ function loadMoreFiles(fileManager, state, setState) {
194
+ if (!state.files.nextCursor) {
195
+ return null;
196
+ }
197
+ return function loadMore() {
198
+ return __async(this, null, function* () {
199
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
200
+ fetching: true
201
+ }));
202
+ const resp = yield fileManager.all({
203
+ nextCursor: state.files.nextCursor,
204
+ keyword: state.files.filters.keyword,
205
+ mimeTypes: mimeTypes(state.files.filters.category),
206
+ excludeMimeTypes: excludeMimeTypes(state.files.filters.category)
207
+ });
208
+ setState((currentState) => __spreadProps(__spreadValues({}, currentState), {
209
+ fetching: false,
210
+ files: {
211
+ list: [...currentState.files.list, ...resp.list],
212
+ nextCursor: resp.pagination.cursor,
213
+ filters: __spreadValues({}, currentState.files.filters)
214
+ }
215
+ }));
216
+ });
217
+ };
218
+ }
219
+
220
+ export { deleteFile, initialState, loadFiles, loadMoreFiles, searchFiles, selectFile, uploadFiles };
@@ -1,15 +1,16 @@
1
- export function fileSizeFormatter(size) {
1
+ import '../chunk-6FNC3XMI.js';
2
+
3
+ function fileSizeFormatter(size) {
2
4
  if (size < 1024) {
3
5
  return `${size} B`;
4
6
  }
5
-
6
7
  if (size < 1024 * 1024) {
7
8
  return `${(size / 1024).toFixed(2)} kB`;
8
9
  }
9
-
10
10
  if (size < 1024 * 1024 * 1024) {
11
11
  return `${(size / 1024 / 1024).toFixed(2)} MB`;
12
12
  }
13
-
14
13
  return `${(size / 1024 / 1024 / 1024).toFixed(2)} GB`;
15
14
  }
15
+
16
+ export { fileSizeFormatter };
@@ -0,0 +1,41 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+
41
+ export { __async, __spreadProps, __spreadValues };