@bento-core/table 1.0.0-c3dc.30 → 1.0.0-c3dc.31

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.
@@ -46,12 +46,12 @@ const CPIModal = _ref => {
46
46
  } = _ref;
47
47
  const [sortBy, setSortBy] = (0, _react.useState)('associated_id');
48
48
  const [sortOrder, setSortOrder] = (0, _react.useState)('asc');
49
- const [data, setData] = (0, _react.useState)(row.cpi_data);
50
- (0, _react.useEffect)(() => {
51
- setData(row.cpi_data);
52
- }, [row]);
49
+ const [data, setData] = (0, _react.useState)(() => {
50
+ const result = row.cpi_data;
51
+ return result;
52
+ });
53
53
  const sortingData = (column, newOrder) => {
54
- const sortedData = [...data].sort((a, b) => a[column].localeCompare(b[column]));
54
+ const sortedData = data.sort((a, b) => a[column].localeCompare(b[column]));
55
55
  if (newOrder === 'desc') {
56
56
  return sortedData.reverse();
57
57
  }
@@ -27,9 +27,11 @@ const DownloadButton = _ref => {
27
27
  let {
28
28
  count,
29
29
  queryVariables,
30
- table
30
+ table,
31
+ buttonConfig
31
32
  } = _ref;
32
33
  const [listDisplay, setListDisplay] = (0, _react.useState)('none');
34
+ const [isDownloading, setIsDownloading] = (0, _react.useState)(false);
33
35
  const dropdownSelection = (0, _react.useRef)(null);
34
36
  const useOutsideAlerter = ref => {
35
37
  (0, _react.useEffect)(() => {
@@ -49,6 +51,7 @@ const DownloadButton = _ref => {
49
51
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null);
50
52
  }
51
53
  const client = (0, _client.useApolloClient)();
54
+ const downloadLimit = buttonConfig === null || buttonConfig === void 0 ? void 0 : buttonConfig.downloadLimit;
52
55
  function cleanData(result) {
53
56
  function hasHTMLTags(str) {
54
57
  const htmlTagPattern = /<\/?[a-z][\s\S]*>/i; // Regex to match HTML tags
@@ -69,57 +72,146 @@ const DownloadButton = _ref => {
69
72
  });
70
73
  return cleanedResult;
71
74
  }
72
- async function downloadSCSVFile() {
75
+
76
+ // --- retry helper (recursive ESLINT no await-in-loop — we want this to be sequential) ---
77
+ async function retry(fn) {
78
+ let retries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
79
+ let attempt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
80
+ try {
81
+ return await fn();
82
+ } catch (err) {
83
+ if (attempt >= retries) throw err;
84
+ return retry(fn, retries, attempt + 1);
85
+ }
86
+ }
87
+ async function fetchCountWithRetry() {
73
88
  const {
74
- query,
75
- paginationAPIField
89
+ statsQuery,
90
+ statsQueryName,
91
+ statsField
76
92
  } = table;
77
- const result = await client.query({
78
- query,
79
- variables: _objectSpread(_objectSpread({}, queryVariables), {}, {
80
- page: 0,
81
- first: 10000,
82
- order_by: table.sortBy,
83
- sort_direction: table.sortOrder
84
- })
85
- }).then(response => {
86
- if (paginationAPIField && response && response.data) {
87
- return response.data[paginationAPIField];
88
- }
89
- return response.data;
90
- });
91
- (0, _downloadTable.downloadData)(cleanData(result), table, table.downloadFileName, 'csv');
93
+ return retry(() => client.query({
94
+ query: statsQuery,
95
+ variables: queryVariables
96
+ }).then(_ref2 => {
97
+ let {
98
+ data
99
+ } = _ref2;
100
+ const queryResponse = data[statsQueryName];
101
+ return {
102
+ totalCount: queryResponse[statsField],
103
+ pageSize: downloadLimit || queryResponse.pageSize || 5000
104
+ };
105
+ }));
92
106
  }
93
- async function downloadJsonFile() {
107
+ async function fetchDownloadWithRetry(variables) {
94
108
  const {
95
109
  query,
96
110
  paginationAPIField
97
111
  } = table;
98
- const result = await client.query({
112
+ return retry(() => client.query({
99
113
  query,
100
- variables: _objectSpread(_objectSpread({}, queryVariables), {}, {
101
- page: 0,
102
- first: 10000,
103
- order_by: table.sortBy,
104
- sort_direction: table.sortOrder
105
- })
114
+ variables
106
115
  }).then(response => {
107
116
  if (paginationAPIField && response && response.data) {
108
117
  return response.data[paginationAPIField];
109
118
  }
110
119
  return response.data;
111
- });
112
- (0, _downloadTable.downloadData)(cleanData(result), table, table.downloadFileName, 'json');
120
+ }));
121
+ }
122
+ async function downloadFile(type) {
123
+ setIsDownloading(true);
124
+ try {
125
+ const {
126
+ totalCount,
127
+ pageSize
128
+ } = await fetchCountWithRetry();
129
+ let completedEntries = 0;
130
+ let allData = [];
131
+ while (completedEntries < totalCount) {
132
+ const variables = _objectSpread(_objectSpread({}, queryVariables), {}, {
133
+ offset: Math.floor(completedEntries / pageSize) * pageSize,
134
+ first: Math.min(pageSize, totalCount - completedEntries),
135
+ order_by: table.sortBy,
136
+ sort_direction: table.sortOrder
137
+ });
138
+ // eslint-disable-next-line no-await-in-loop
139
+ const data = await fetchDownloadWithRetry(variables);
140
+ allData = allData.concat(data);
141
+ completedEntries += data.length;
142
+ }
143
+ (0, _downloadTable.downloadData)(cleanData(allData), table, table.downloadFileName, type);
144
+ } catch (error) {
145
+ console.error('Error fetching count:', error);
146
+ } finally {
147
+ setIsDownloading(false);
148
+ }
149
+ }
150
+ async function downloadFileParallel(type) {
151
+ let concurrency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
152
+ setIsDownloading(true);
153
+ try {
154
+ const {
155
+ totalCount,
156
+ pageSize
157
+ } = await fetchCountWithRetry();
158
+ const totalChunks = Math.ceil(totalCount / pageSize);
159
+ const results = new Array(totalChunks);
160
+ for (let i = 0; i < totalChunks; i += concurrency) {
161
+ const batch = [];
162
+ for (let j = i; j < Math.min(i + concurrency, totalChunks); j += 1) {
163
+ const offset = j * pageSize;
164
+ const first = Math.min(pageSize, totalCount - offset);
165
+ const variables = _objectSpread(_objectSpread({}, queryVariables), {}, {
166
+ offset,
167
+ first,
168
+ order_by: table.sortBy,
169
+ sort_direction: table.sortOrder
170
+ });
171
+ batch.push(fetchDownloadWithRetry(variables).then(data => ({
172
+ index: j,
173
+ data
174
+ })));
175
+ }
176
+
177
+ // eslint-disable-next-line no-await-in-loop
178
+ const batchResults = await Promise.all(batch);
179
+ batchResults.forEach(_ref3 => {
180
+ let {
181
+ index,
182
+ data
183
+ } = _ref3;
184
+ results[index] = data;
185
+ });
186
+ }
187
+ const allData = results.flat();
188
+ (0, _downloadTable.downloadData)(cleanData(allData), table, table.downloadFileName, type);
189
+ } catch (error) {
190
+ console.error('Error fetching count:', error);
191
+ } finally {
192
+ setIsDownloading(false);
193
+ }
113
194
  }
114
195
  const downloadTableCSV = (0, _react.useCallback)(() => {
115
- downloadSCSVFile();
196
+ if (isDownloading) return;
197
+ if (table.asyncDownload) {
198
+ downloadFileParallel('csv');
199
+ } else {
200
+ downloadFile('csv');
201
+ }
116
202
  setListDisplay('none');
117
- }, [queryVariables, table]);
203
+ }, [queryVariables, table, isDownloading]);
118
204
  const downloadTableJson = (0, _react.useCallback)(() => {
119
- downloadJsonFile();
205
+ if (isDownloading) return;
206
+ if (table.asyncDownload) {
207
+ downloadFileParallel('json');
208
+ } else {
209
+ downloadFile('json');
210
+ }
120
211
  setListDisplay('none');
121
- }, [queryVariables, table]);
212
+ }, [queryVariables, table, isDownloading]);
122
213
  const handleClickButton = () => {
214
+ if (isDownloading) return;
123
215
  if (listDisplay === 'none') {
124
216
  setListDisplay('block');
125
217
  } else {
@@ -172,14 +264,18 @@ const DownloadButton = _ref => {
172
264
  });
173
265
  const classes = useStyles();
174
266
  return /*#__PURE__*/_react.default.createElement("div", {
175
- className: classes.dropdown
267
+ className: classes.dropdown,
268
+ style: isDownloading ? {
269
+ cursor: 'wait'
270
+ } : {}
176
271
  }, /*#__PURE__*/_react.default.createElement(_core.Tooltip, {
177
- title: table.downloadButtonTooltipText || 'Download filtered results'
272
+ title: isDownloading ? 'Download in progress...' : table.downloadButtonTooltipText || 'Download filtered results'
178
273
  }, count !== 0 ? /*#__PURE__*/_react.default.createElement(_core.IconButton, {
179
274
  onClick: handleClickButton,
180
275
  style: {
181
276
  padding: '0'
182
- }
277
+ },
278
+ disabled: isDownloading
183
279
  }, /*#__PURE__*/_react.default.createElement(_icons.CloudDownload, null), /*#__PURE__*/_react.default.createElement(_KeyboardArrowDownOutlined.default, {
184
280
  className: classes.arrowdownIcon
185
281
  })) : /*#__PURE__*/_react.default.createElement(_core.IconButton, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bento-core/table",
3
- "version": "1.0.0-c3dc.30",
3
+ "version": "1.0.0-c3dc.31",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "build": "cross-env-shell rm -rf dist && NODE_ENV=production BABEL_ENV=es babel src --out-dir dist --copy-files",
@@ -1,4 +1,4 @@
1
- import React, { useState, useEffect } from 'react';
1
+ import React, { useState } from 'react';
2
2
  import {
3
3
  Box,
4
4
  Modal,
@@ -44,15 +44,13 @@ const CPIModal = ({
44
44
  }) => {
45
45
  const [sortBy, setSortBy] = useState('associated_id');
46
46
  const [sortOrder, setSortOrder] = useState('asc');
47
- const [data, setData] = useState(row.cpi_data);
48
-
49
- useEffect(() => {
50
- setData(row.cpi_data);
51
- }, [row]);
52
-
47
+ const [data, setData] = useState(() => {
48
+ const result = row.cpi_data;
49
+ return result;
50
+ });
53
51
 
54
52
  const sortingData = (column, newOrder) => {
55
- const sortedData = [...data].sort((a, b) => a[column].localeCompare(b[column]));
53
+ const sortedData = data.sort((a, b) => a[column].localeCompare(b[column]));
56
54
 
57
55
  if (newOrder === 'desc') {
58
56
  return sortedData.reverse();
@@ -23,8 +23,10 @@ const DownloadButton = ({
23
23
  count,
24
24
  queryVariables,
25
25
  table,
26
+ buttonConfig,
26
27
  }) => {
27
28
  const [listDisplay, setListDisplay] = useState('none');
29
+ const [isDownloading, setIsDownloading] = useState(false);
28
30
  const dropdownSelection = useRef(null);
29
31
  const useOutsideAlerter = (ref) => {
30
32
  useEffect(() => {
@@ -46,6 +48,7 @@ const DownloadButton = ({
46
48
  }
47
49
 
48
50
  const client = useApolloClient();
51
+ const downloadLimit = buttonConfig?.downloadLimit;
49
52
 
50
53
  function cleanData(result) {
51
54
  function hasHTMLTags(str) {
@@ -68,68 +71,136 @@ const DownloadButton = ({
68
71
  return cleanedResult;
69
72
  }
70
73
 
71
- async function downloadSCSVFile() {
72
- const {
73
- query,
74
- paginationAPIField,
75
- } = table;
74
+ // --- retry helper (recursive ESLINT no await-in-loop — we want this to be sequential) ---
75
+ async function retry(fn, retries = 3, attempt = 1) {
76
+ try {
77
+ return await fn();
78
+ } catch (err) {
79
+ if (attempt >= retries) throw err;
80
+ return retry(fn, retries, attempt + 1);
81
+ }
82
+ }
83
+
84
+ async function fetchCountWithRetry() {
85
+ const { statsQuery, statsQueryName, statsField } = table;
86
+ return retry(() => client.query({
87
+ query: statsQuery,
88
+ variables: queryVariables,
89
+ }).then(({ data }) => {
90
+ const queryResponse = data[statsQueryName];
91
+ return {
92
+ totalCount: queryResponse[statsField],
93
+ pageSize: downloadLimit || queryResponse.pageSize || 5000,
94
+ };
95
+ }));
96
+ }
76
97
 
77
- const result = await client.query({
98
+ async function fetchDownloadWithRetry(variables) {
99
+ const { query, paginationAPIField } = table;
100
+ return retry(() => client.query({
78
101
  query,
79
- variables: {
80
- ...queryVariables,
81
- page: 0,
82
- first: 10000,
83
- order_by: table.sortBy,
84
- sort_direction: table.sortOrder,
85
- },
86
- })
87
- .then((response) => {
88
- if (paginationAPIField && response && response.data) {
89
- return response.data[paginationAPIField];
90
- }
91
- return response.data;
92
- });
102
+ variables,
103
+ }).then((response) => {
104
+ if (paginationAPIField && response && response.data) {
105
+ return response.data[paginationAPIField];
106
+ }
107
+ return response.data;
108
+ }));
109
+ }
110
+
111
+ async function downloadFile(type) {
112
+ setIsDownloading(true);
113
+ try {
114
+ const { totalCount, pageSize } = await fetchCountWithRetry();
93
115
 
94
- downloadData(cleanData(result), table, table.downloadFileName, 'csv');
116
+ let completedEntries = 0;
117
+ let allData = [];
118
+ while (completedEntries < totalCount) {
119
+ const variables = {
120
+ ...queryVariables,
121
+ offset: Math.floor(completedEntries / pageSize) * pageSize,
122
+ first: Math.min(pageSize, totalCount - completedEntries),
123
+ order_by: table.sortBy,
124
+ sort_direction: table.sortOrder,
125
+ };
126
+ // eslint-disable-next-line no-await-in-loop
127
+ const data = await fetchDownloadWithRetry(variables);
128
+ allData = allData.concat(data);
129
+ completedEntries += data.length;
130
+ }
131
+ downloadData(cleanData(allData), table, table.downloadFileName, type);
132
+ } catch (error) {
133
+ console.error('Error fetching count:', error);
134
+ } finally {
135
+ setIsDownloading(false);
136
+ }
95
137
  }
96
138
 
97
- async function downloadJsonFile() {
98
- const {
99
- query,
100
- paginationAPIField,
101
- } = table;
139
+ async function downloadFileParallel(type, concurrency = 5) {
140
+ setIsDownloading(true);
141
+ try {
142
+ const { totalCount, pageSize } = await fetchCountWithRetry();
143
+ const totalChunks = Math.ceil(totalCount / pageSize);
144
+ const results = new Array(totalChunks);
102
145
 
103
- const result = await client.query({
104
- query,
105
- variables: {
106
- ...queryVariables,
107
- page: 0,
108
- first: 10000,
109
- order_by: table.sortBy,
110
- sort_direction: table.sortOrder,
111
- },
112
- })
113
- .then((response) => {
114
- if (paginationAPIField && response && response.data) {
115
- return response.data[paginationAPIField];
146
+ for (let i = 0; i < totalChunks; i += concurrency) {
147
+ const batch = [];
148
+
149
+ for (let j = i; j < Math.min(i + concurrency, totalChunks); j += 1) {
150
+ const offset = j * pageSize;
151
+ const first = Math.min(pageSize, totalCount - offset);
152
+
153
+ const variables = {
154
+ ...queryVariables,
155
+ offset,
156
+ first,
157
+ order_by: table.sortBy,
158
+ sort_direction: table.sortOrder,
159
+ };
160
+
161
+ batch.push(
162
+ fetchDownloadWithRetry(variables).then((data) => ({ index: j, data })),
163
+ );
116
164
  }
117
- return response.data;
118
- });
119
- downloadData(cleanData(result), table, table.downloadFileName, 'json');
165
+
166
+ // eslint-disable-next-line no-await-in-loop
167
+ const batchResults = await Promise.all(batch);
168
+ batchResults.forEach(({ index, data }) => {
169
+ results[index] = data;
170
+ });
171
+ }
172
+
173
+ const allData = results.flat();
174
+ downloadData(cleanData(allData), table, table.downloadFileName, type);
175
+ } catch (error) {
176
+ console.error('Error fetching count:', error);
177
+ } finally {
178
+ setIsDownloading(false);
179
+ }
120
180
  }
121
181
 
122
182
  const downloadTableCSV = useCallback(() => {
123
- downloadSCSVFile();
183
+ if (isDownloading) return;
184
+ if (table.asyncDownload) {
185
+ downloadFileParallel('csv');
186
+ } else {
187
+ downloadFile('csv');
188
+ }
124
189
  setListDisplay('none');
125
- }, [queryVariables, table]);
190
+ }, [queryVariables, table, isDownloading]);
126
191
 
127
192
  const downloadTableJson = useCallback(() => {
128
- downloadJsonFile();
193
+ if (isDownloading) return;
194
+ if (table.asyncDownload) {
195
+ downloadFileParallel('json');
196
+ } else {
197
+ downloadFile('json');
198
+ }
129
199
  setListDisplay('none');
130
- }, [queryVariables, table]);
200
+ }, [queryVariables, table, isDownloading]);
131
201
 
132
202
  const handleClickButton = () => {
203
+ if (isDownloading) return;
133
204
  if (listDisplay === 'none') {
134
205
  setListDisplay('block');
135
206
  } else {
@@ -185,13 +256,13 @@ const DownloadButton = ({
185
256
  const classes = useStyles();
186
257
 
187
258
  return (
188
- <div className={classes.dropdown}>
259
+ <div className={classes.dropdown} style={isDownloading ? { cursor: 'wait' } : {}}>
189
260
 
190
- <Tooltip title={table.downloadButtonTooltipText || 'Download filtered results'}>
261
+ <Tooltip title={isDownloading ? 'Download in progress...' : (table.downloadButtonTooltipText || 'Download filtered results')}>
191
262
  {
192
263
  count !== 0
193
264
  ? (
194
- <IconButton onClick={handleClickButton} style={{ padding: '0' }}>
265
+ <IconButton onClick={handleClickButton} style={{ padding: '0' }} disabled={isDownloading}>
195
266
  <CloudDownload />
196
267
  <KeyboardArrowDownOutlinedIcon className={classes.arrowdownIcon} />
197
268
  </IconButton>