react-csv-autopilot 0.0.3 → 0.0.5

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 CHANGED
@@ -56,7 +56,7 @@ function ExportButton() {
56
56
  });
57
57
 
58
58
  const handleExport = async () => {
59
- await handler.start({
59
+ await handler.execute({
60
60
  fileName: 'users-export',
61
61
  columns: [
62
62
  { key: 'id', label: 'ID' },
@@ -99,7 +99,7 @@ Hook that provides access to the CSV export controller.
99
99
  ```typescript
100
100
  const { handler } = useExportCSV();
101
101
 
102
- await handler.start({
102
+ await handler.execute({
103
103
  fileName: 'data-export',
104
104
  columns: [...],
105
105
  getNextPage: async (offset) => {...}
package/dist/index.cjs CHANGED
@@ -61,8 +61,8 @@ resolveStrategy_fn = function() {
61
61
 
62
62
  // src/core/strategy/BolbExportStrategy.ts
63
63
  var BolbExportStrategy = class {
64
- export(params) {
65
- return Promise.resolve({});
64
+ export(_params) {
65
+ return Promise.resolve({ finished: true, totalRowsLoaded: 10 });
66
66
  }
67
67
  };
68
68
  var BolbExportStrategy_default = BolbExportStrategy;
@@ -86,8 +86,8 @@ var _WorkerManager = class _WorkerManager {
86
86
  workerUrl = "/worker.js";
87
87
  }
88
88
  __privateSet(this, _worker, new Worker(workerUrl, {
89
- type: "module",
90
- name: WEB_WORKER_NAME
89
+ name: WEB_WORKER_NAME,
90
+ type: "module"
91
91
  }));
92
92
  __privateMethod(this, _WorkerManager_instances, listenerRegistry_fn).call(this);
93
93
  }
@@ -97,9 +97,9 @@ var _WorkerManager = class _WorkerManager {
97
97
  async triggerWorker(payload) {
98
98
  const id = payload.id ?? Math.random().toString(36).substr(2);
99
99
  const p = new Promise((resolve, reject) => {
100
- pending.set(id, { resolve, reject });
100
+ pending.set(id, { reject, resolve });
101
101
  });
102
- __privateGet(this, _worker).postMessage(payload);
102
+ __privateGet(this, _worker)?.postMessage(payload);
103
103
  return p;
104
104
  }
105
105
  terminate() {
@@ -112,7 +112,7 @@ var _WorkerManager = class _WorkerManager {
112
112
  _worker = new WeakMap();
113
113
  _WorkerManager_instances = new WeakSet();
114
114
  listenerRegistry_fn = function() {
115
- __privateGet(this, _worker).addEventListener("message", (event) => {
115
+ __privateGet(this, _worker)?.addEventListener("message", (event) => {
116
116
  const { id, result, error } = event.data;
117
117
  const entity = pending.get(id);
118
118
  if (!entity) {
@@ -125,7 +125,7 @@ listenerRegistry_fn = function() {
125
125
  entity.resolve(result);
126
126
  }
127
127
  });
128
- __privateGet(this, _worker).addEventListener("error", (event) => {
128
+ __privateGet(this, _worker)?.addEventListener("error", (event) => {
129
129
  for (const [, { reject }] of pending) {
130
130
  reject(event);
131
131
  }
@@ -144,7 +144,7 @@ var FsAccessExportStrategy = class {
144
144
  const _suggestedName = params?.fileName || "export";
145
145
  const fileHandle = await window.showSaveFilePicker({
146
146
  suggestedName: _suggestedName,
147
- types: [{ description: "CSV file", accept: { "text/csv": [".csv"] } }]
147
+ types: [{ accept: { "text/csv": [".csv"] }, description: "CSV file" }]
148
148
  });
149
149
  const writableFileStream = await fileHandle.createWritable();
150
150
  let iterator = 0;
@@ -158,44 +158,51 @@ var FsAccessExportStrategy = class {
158
158
  const safeRows = Array.isArray(response.rows) ? response?.rows : [];
159
159
  const safeTotal = response.total ?? 0;
160
160
  const isRowsEmpty = !safeRows || !safeRows.length;
161
- totalRowsLoaded = isRowsEmpty ? safeTotal : totalRowsLoaded += safeRows.length;
161
+ const nextRowsLoaded = totalRowsLoaded + safeRows.length;
162
+ totalRowsLoaded = isRowsEmpty ? safeTotal : nextRowsLoaded;
162
163
  const isFinished = totalRowsLoaded >= safeTotal;
163
164
  if (isRowsEmpty) {
164
165
  messaging.postMessage(
165
166
  JSON.stringify({
166
- type: "done",
167
+ loadedItemsCount: totalRowsLoaded,
167
168
  total: safeTotal,
168
- loadedItemsCount: totalRowsLoaded
169
+ type: "done"
169
170
  })
170
171
  );
171
- await this.workerManager.triggerWorker({ id: iterator, type: "completed" });
172
+ await this.workerManager.triggerWorker({
173
+ id: iterator,
174
+ type: "completed"
175
+ });
172
176
  messaging.close();
173
177
  controller.close();
174
178
  return;
175
179
  }
176
180
  const csvChunks = await this.workerManager.triggerWorker({
177
- id: iterator,
178
- type: "to_csv_chunk",
181
+ columns: params.columns,
179
182
  data: safeRows,
180
- columns: params.columns
183
+ id: iterator,
184
+ type: "to_csv_chunk"
181
185
  });
182
186
  messaging.postMessage(
183
187
  JSON.stringify({
184
- type: "progress",
188
+ loadedItemsCount: totalRowsLoaded,
185
189
  total: safeTotal,
186
- loadedItemsCount: totalRowsLoaded
190
+ type: "progress"
187
191
  })
188
192
  );
189
193
  controller.enqueue(encoder.encode(csvChunks));
190
194
  if (isFinished) {
191
195
  messaging.postMessage(
192
196
  JSON.stringify({
193
- type: "done",
197
+ loadedItemsCount: totalRowsLoaded,
194
198
  total: safeTotal,
195
- loadedItemsCount: totalRowsLoaded
199
+ type: "done"
196
200
  })
197
201
  );
198
- await this.workerManager.triggerWorker({ id: iterator, type: "completed" });
202
+ await this.workerManager.triggerWorker({
203
+ id: iterator,
204
+ type: "completed"
205
+ });
199
206
  messaging.close();
200
207
  controller.close();
201
208
  return;
@@ -204,9 +211,9 @@ var FsAccessExportStrategy = class {
204
211
  controller.error(error);
205
212
  messaging.postMessage(
206
213
  JSON.stringify({
207
- type: "failed",
214
+ loadedItemsCount: totalRowsLoaded,
208
215
  total: 0,
209
- loadedItemsCount: totalRowsLoaded
216
+ type: "failed"
210
217
  })
211
218
  );
212
219
  }
@@ -222,25 +229,20 @@ var FsAccessExportStrategy = class {
222
229
  }
223
230
  return {
224
231
  finished: true,
225
- totalRowsLoaded,
226
- logs: {
227
- warnings: []
228
- }
232
+ totalRowsLoaded
229
233
  };
230
234
  }
231
235
  };
232
236
  var FsAccessExportStrategy_default = FsAccessExportStrategy;
233
237
 
234
- // src/core/fabric/ExportControlFabric.ts
235
- var ExportControlFabric = class {
236
- static create() {
237
- const controller = new ExportController({
238
- fsAccessStrategy: new FsAccessExportStrategy_default(),
239
- blobExportStrategy: new BolbExportStrategy_default()
240
- });
241
- return controller;
242
- }
243
- };
238
+ // src/core/createExportController.ts
239
+ function createExportController() {
240
+ return new ExportController({
241
+ blobExportStrategy: new BolbExportStrategy_default(),
242
+ fsAccessStrategy: new FsAccessExportStrategy_default()
243
+ });
244
+ }
245
+ var createExportController_default = createExportController;
244
246
 
245
247
  // src/core/ExportControllerSingleton.ts
246
248
  var _ExportControllerSingleton = class _ExportControllerSingleton {
@@ -248,7 +250,7 @@ var _ExportControllerSingleton = class _ExportControllerSingleton {
248
250
  if (_ExportControllerSingleton.instance) {
249
251
  return _ExportControllerSingleton.instance;
250
252
  }
251
- _ExportControllerSingleton.instance = ExportControlFabric.create();
253
+ _ExportControllerSingleton.instance = createExportController_default();
252
254
  _ExportControllerSingleton.initialized = true;
253
255
  return _ExportControllerSingleton.instance;
254
256
  }
@@ -268,7 +270,7 @@ var ExportControllerSingleton_default = ExportControllerSingleton;
268
270
  function useExportCSV() {
269
271
  const exportCallbackRef = (0, import_react.useRef)(ExportControllerSingleton_default.init());
270
272
  return {
271
- handler: exportCallbackRef.current
273
+ handler: exportCallbackRef?.current
272
274
  };
273
275
  }
274
276
  var useExportCSV_default = useExportCSV;
@@ -289,6 +291,6 @@ function useMessageExportCSV(cb) {
289
291
  return () => {
290
292
  channel.close();
291
293
  };
292
- }, []);
294
+ }, [cb]);
293
295
  }
294
296
  var useMessageExportCSV_default = useMessageExportCSV;
package/dist/index.d.cts CHANGED
@@ -5,26 +5,30 @@ type Column = {
5
5
  timezone?: "UTC" | string;
6
6
  formatType?: formatterTypes;
7
7
  };
8
- type ExportParams = {
8
+ type ExportParams<T> = {
9
9
  fileName: string;
10
10
  columns: Column[];
11
11
  getNextPage: (offset: number) => Promise<{
12
- rows: any[];
12
+ rows: T[];
13
13
  total: number;
14
14
  }>;
15
15
  };
16
+ type ExportResponse = {
17
+ finished: boolean;
18
+ totalRowsLoaded: number;
19
+ };
16
20
  interface ExportStrategy {
17
- export(params: ExportParams): Promise<any>;
21
+ export<T>(params: ExportParams<T>): Promise<ExportResponse>;
18
22
  }
19
23
 
20
24
  declare class BolbExportStrategy implements ExportStrategy {
21
- export(params: ExportParams): Promise<any>;
25
+ export<T>(_params: ExportParams<T>): Promise<ExportResponse>;
22
26
  }
23
27
 
24
28
  declare class FsAccessExportStrategy implements ExportStrategy {
25
29
  private workerManager;
26
30
  constructor();
27
- export(params: ExportParams): Promise<any>;
31
+ export<T>(params: ExportParams<T>): Promise<ExportResponse>;
28
32
  }
29
33
 
30
34
  type ExportControllerDeps = {
@@ -35,7 +39,7 @@ declare class ExportController {
35
39
  #private;
36
40
  private readonly deps;
37
41
  constructor(deps: ExportControllerDeps);
38
- start(params: ExportParams): Promise<any>;
42
+ start<T>(params: ExportParams<T>): Promise<ExportResponse>;
39
43
  }
40
44
 
41
45
  declare function useExportCSV(): {
package/dist/index.d.ts CHANGED
@@ -5,26 +5,30 @@ type Column = {
5
5
  timezone?: "UTC" | string;
6
6
  formatType?: formatterTypes;
7
7
  };
8
- type ExportParams = {
8
+ type ExportParams<T> = {
9
9
  fileName: string;
10
10
  columns: Column[];
11
11
  getNextPage: (offset: number) => Promise<{
12
- rows: any[];
12
+ rows: T[];
13
13
  total: number;
14
14
  }>;
15
15
  };
16
+ type ExportResponse = {
17
+ finished: boolean;
18
+ totalRowsLoaded: number;
19
+ };
16
20
  interface ExportStrategy {
17
- export(params: ExportParams): Promise<any>;
21
+ export<T>(params: ExportParams<T>): Promise<ExportResponse>;
18
22
  }
19
23
 
20
24
  declare class BolbExportStrategy implements ExportStrategy {
21
- export(params: ExportParams): Promise<any>;
25
+ export<T>(_params: ExportParams<T>): Promise<ExportResponse>;
22
26
  }
23
27
 
24
28
  declare class FsAccessExportStrategy implements ExportStrategy {
25
29
  private workerManager;
26
30
  constructor();
27
- export(params: ExportParams): Promise<any>;
31
+ export<T>(params: ExportParams<T>): Promise<ExportResponse>;
28
32
  }
29
33
 
30
34
  type ExportControllerDeps = {
@@ -35,7 +39,7 @@ declare class ExportController {
35
39
  #private;
36
40
  private readonly deps;
37
41
  constructor(deps: ExportControllerDeps);
38
- start(params: ExportParams): Promise<any>;
42
+ start<T>(params: ExportParams<T>): Promise<ExportResponse>;
39
43
  }
40
44
 
41
45
  declare function useExportCSV(): {
package/dist/index.js CHANGED
@@ -35,8 +35,8 @@ resolveStrategy_fn = function() {
35
35
 
36
36
  // src/core/strategy/BolbExportStrategy.ts
37
37
  var BolbExportStrategy = class {
38
- export(params) {
39
- return Promise.resolve({});
38
+ export(_params) {
39
+ return Promise.resolve({ finished: true, totalRowsLoaded: 10 });
40
40
  }
41
41
  };
42
42
  var BolbExportStrategy_default = BolbExportStrategy;
@@ -59,8 +59,8 @@ var _WorkerManager = class _WorkerManager {
59
59
  workerUrl = "/worker.js";
60
60
  }
61
61
  __privateSet(this, _worker, new Worker(workerUrl, {
62
- type: "module",
63
- name: WEB_WORKER_NAME
62
+ name: WEB_WORKER_NAME,
63
+ type: "module"
64
64
  }));
65
65
  __privateMethod(this, _WorkerManager_instances, listenerRegistry_fn).call(this);
66
66
  }
@@ -70,9 +70,9 @@ var _WorkerManager = class _WorkerManager {
70
70
  async triggerWorker(payload) {
71
71
  const id = payload.id ?? Math.random().toString(36).substr(2);
72
72
  const p = new Promise((resolve, reject) => {
73
- pending.set(id, { resolve, reject });
73
+ pending.set(id, { reject, resolve });
74
74
  });
75
- __privateGet(this, _worker).postMessage(payload);
75
+ __privateGet(this, _worker)?.postMessage(payload);
76
76
  return p;
77
77
  }
78
78
  terminate() {
@@ -85,7 +85,7 @@ var _WorkerManager = class _WorkerManager {
85
85
  _worker = new WeakMap();
86
86
  _WorkerManager_instances = new WeakSet();
87
87
  listenerRegistry_fn = function() {
88
- __privateGet(this, _worker).addEventListener("message", (event) => {
88
+ __privateGet(this, _worker)?.addEventListener("message", (event) => {
89
89
  const { id, result, error } = event.data;
90
90
  const entity = pending.get(id);
91
91
  if (!entity) {
@@ -98,7 +98,7 @@ listenerRegistry_fn = function() {
98
98
  entity.resolve(result);
99
99
  }
100
100
  });
101
- __privateGet(this, _worker).addEventListener("error", (event) => {
101
+ __privateGet(this, _worker)?.addEventListener("error", (event) => {
102
102
  for (const [, { reject }] of pending) {
103
103
  reject(event);
104
104
  }
@@ -117,7 +117,7 @@ var FsAccessExportStrategy = class {
117
117
  const _suggestedName = params?.fileName || "export";
118
118
  const fileHandle = await window.showSaveFilePicker({
119
119
  suggestedName: _suggestedName,
120
- types: [{ description: "CSV file", accept: { "text/csv": [".csv"] } }]
120
+ types: [{ accept: { "text/csv": [".csv"] }, description: "CSV file" }]
121
121
  });
122
122
  const writableFileStream = await fileHandle.createWritable();
123
123
  let iterator = 0;
@@ -131,44 +131,51 @@ var FsAccessExportStrategy = class {
131
131
  const safeRows = Array.isArray(response.rows) ? response?.rows : [];
132
132
  const safeTotal = response.total ?? 0;
133
133
  const isRowsEmpty = !safeRows || !safeRows.length;
134
- totalRowsLoaded = isRowsEmpty ? safeTotal : totalRowsLoaded += safeRows.length;
134
+ const nextRowsLoaded = totalRowsLoaded + safeRows.length;
135
+ totalRowsLoaded = isRowsEmpty ? safeTotal : nextRowsLoaded;
135
136
  const isFinished = totalRowsLoaded >= safeTotal;
136
137
  if (isRowsEmpty) {
137
138
  messaging.postMessage(
138
139
  JSON.stringify({
139
- type: "done",
140
+ loadedItemsCount: totalRowsLoaded,
140
141
  total: safeTotal,
141
- loadedItemsCount: totalRowsLoaded
142
+ type: "done"
142
143
  })
143
144
  );
144
- await this.workerManager.triggerWorker({ id: iterator, type: "completed" });
145
+ await this.workerManager.triggerWorker({
146
+ id: iterator,
147
+ type: "completed"
148
+ });
145
149
  messaging.close();
146
150
  controller.close();
147
151
  return;
148
152
  }
149
153
  const csvChunks = await this.workerManager.triggerWorker({
150
- id: iterator,
151
- type: "to_csv_chunk",
154
+ columns: params.columns,
152
155
  data: safeRows,
153
- columns: params.columns
156
+ id: iterator,
157
+ type: "to_csv_chunk"
154
158
  });
155
159
  messaging.postMessage(
156
160
  JSON.stringify({
157
- type: "progress",
161
+ loadedItemsCount: totalRowsLoaded,
158
162
  total: safeTotal,
159
- loadedItemsCount: totalRowsLoaded
163
+ type: "progress"
160
164
  })
161
165
  );
162
166
  controller.enqueue(encoder.encode(csvChunks));
163
167
  if (isFinished) {
164
168
  messaging.postMessage(
165
169
  JSON.stringify({
166
- type: "done",
170
+ loadedItemsCount: totalRowsLoaded,
167
171
  total: safeTotal,
168
- loadedItemsCount: totalRowsLoaded
172
+ type: "done"
169
173
  })
170
174
  );
171
- await this.workerManager.triggerWorker({ id: iterator, type: "completed" });
175
+ await this.workerManager.triggerWorker({
176
+ id: iterator,
177
+ type: "completed"
178
+ });
172
179
  messaging.close();
173
180
  controller.close();
174
181
  return;
@@ -177,9 +184,9 @@ var FsAccessExportStrategy = class {
177
184
  controller.error(error);
178
185
  messaging.postMessage(
179
186
  JSON.stringify({
180
- type: "failed",
187
+ loadedItemsCount: totalRowsLoaded,
181
188
  total: 0,
182
- loadedItemsCount: totalRowsLoaded
189
+ type: "failed"
183
190
  })
184
191
  );
185
192
  }
@@ -195,25 +202,20 @@ var FsAccessExportStrategy = class {
195
202
  }
196
203
  return {
197
204
  finished: true,
198
- totalRowsLoaded,
199
- logs: {
200
- warnings: []
201
- }
205
+ totalRowsLoaded
202
206
  };
203
207
  }
204
208
  };
205
209
  var FsAccessExportStrategy_default = FsAccessExportStrategy;
206
210
 
207
- // src/core/fabric/ExportControlFabric.ts
208
- var ExportControlFabric = class {
209
- static create() {
210
- const controller = new ExportController({
211
- fsAccessStrategy: new FsAccessExportStrategy_default(),
212
- blobExportStrategy: new BolbExportStrategy_default()
213
- });
214
- return controller;
215
- }
216
- };
211
+ // src/core/createExportController.ts
212
+ function createExportController() {
213
+ return new ExportController({
214
+ blobExportStrategy: new BolbExportStrategy_default(),
215
+ fsAccessStrategy: new FsAccessExportStrategy_default()
216
+ });
217
+ }
218
+ var createExportController_default = createExportController;
217
219
 
218
220
  // src/core/ExportControllerSingleton.ts
219
221
  var _ExportControllerSingleton = class _ExportControllerSingleton {
@@ -221,7 +223,7 @@ var _ExportControllerSingleton = class _ExportControllerSingleton {
221
223
  if (_ExportControllerSingleton.instance) {
222
224
  return _ExportControllerSingleton.instance;
223
225
  }
224
- _ExportControllerSingleton.instance = ExportControlFabric.create();
226
+ _ExportControllerSingleton.instance = createExportController_default();
225
227
  _ExportControllerSingleton.initialized = true;
226
228
  return _ExportControllerSingleton.instance;
227
229
  }
@@ -241,7 +243,7 @@ var ExportControllerSingleton_default = ExportControllerSingleton;
241
243
  function useExportCSV() {
242
244
  const exportCallbackRef = useRef(ExportControllerSingleton_default.init());
243
245
  return {
244
- handler: exportCallbackRef.current
246
+ handler: exportCallbackRef?.current
245
247
  };
246
248
  }
247
249
  var useExportCSV_default = useExportCSV;
@@ -262,7 +264,7 @@ function useMessageExportCSV(cb) {
262
264
  return () => {
263
265
  channel.close();
264
266
  };
265
- }, []);
267
+ }, [cb]);
266
268
  }
267
269
  var useMessageExportCSV_default = useMessageExportCSV;
268
270
  export {
package/package.json CHANGED
@@ -1,19 +1,21 @@
1
1
  {
2
- "name": "react-csv-autopilot",
3
- "version": "0.0.3",
4
- "description": "React hooks for CSV export with automatic pagination - drop the function, we handle the rest",
5
2
  "author": "Pavlo Kuzina",
6
- "license": "MIT",
7
- "type": "module",
8
- "sideEffects": false,
9
- "main": "dist/index.cjs",
10
- "module": "dist/index.js",
11
- "types": "dist/index.d.ts",
3
+ "bugs": {
4
+ "url": "https://github.com/PashaSchool/utils-kit/issues"
5
+ },
6
+ "description": "React hooks for CSV export with automatic pagination - drop the function, we handle the rest",
7
+ "devDependencies": {
8
+ "@types/react": "^18",
9
+ "@types/wicg-file-system-access": "^2023.10.7",
10
+ "react": "^19.2.3",
11
+ "tsup": "^8.0.0",
12
+ "typescript": "^5.0.0"
13
+ },
12
14
  "exports": {
13
15
  ".": {
14
- "types": "./dist/index.d.ts",
15
16
  "import": "./dist/index.js",
16
- "require": "./dist/index.cjs"
17
+ "require": "./dist/index.cjs",
18
+ "types": "./dist/index.d.ts"
17
19
  }
18
20
  },
19
21
  "files": [
@@ -21,28 +23,6 @@
21
23
  "README.md",
22
24
  "LICENSE"
23
25
  ],
24
- "scripts": {
25
- "build": "tsup",
26
- "dev": "tsup --watch"
27
- },
28
- "peerDependencies": {
29
- "react": "^18 || ^19"
30
- },
31
- "devDependencies": {
32
- "@types/react": "^18",
33
- "@types/wicg-file-system-access": "^2023.10.7",
34
- "react": "^19.2.3",
35
- "tsup": "^8.0.0",
36
- "typescript": "^5.0.0"
37
- },
38
- "repository": {
39
- "type": "git",
40
- "url": "git+https://github.com/PashaSchool/utils-kit.git",
41
- "directory": "packages/react-csv-autopilot"
42
- },
43
- "bugs": {
44
- "url": "https://github.com/PashaSchool/utils-kit/issues"
45
- },
46
26
  "homepage": "https://github.com/PashaSchool/utils-kit/blob/main/packages/react-csv-autopilot/README.md",
47
27
  "keywords": [
48
28
  "react",
@@ -60,5 +40,25 @@
60
40
  "file-system-access",
61
41
  "large-datasets",
62
42
  "data-export"
63
- ]
43
+ ],
44
+ "license": "MIT",
45
+ "main": "dist/index.cjs",
46
+ "module": "dist/index.js",
47
+ "name": "react-csv-autopilot",
48
+ "peerDependencies": {
49
+ "react": "^18 || ^19"
50
+ },
51
+ "repository": {
52
+ "directory": "packages/react-csv-autopilot",
53
+ "type": "git",
54
+ "url": "git+https://github.com/PashaSchool/utils-kit.git"
55
+ },
56
+ "scripts": {
57
+ "build": "tsup",
58
+ "dev": "tsup --watch"
59
+ },
60
+ "sideEffects": false,
61
+ "type": "module",
62
+ "types": "dist/index.d.ts",
63
+ "version": "0.0.5"
64
64
  }