@economic/taco 2.47.0-server-1 → 2.47.0-server-2

Sign up to get free protection for your applications and to get access to all the features.
@@ -22098,6 +22098,249 @@ Navigation2.Link = Link$3;
22098
22098
  Navigation2.Section = Section;
22099
22099
  Navigation2.Content = Content$a;
22100
22100
 
22101
+ const DATASET_SIZE_MULTIPLIER = 15;
22102
+ function useTableDataLoader2(fetchPage, fetchAll, options = {
22103
+ pageSize: DEFAULT_PAGE_SIZE
22104
+ }) {
22105
+ const loadPage = function (pageIndex, sorting, filters, search, hiddenColumns, reset = false) {
22106
+ try {
22107
+ if (_hasFetchedAll.current) {
22108
+ return Promise.resolve();
22109
+ }
22110
+ // if page is already loading, abort, otherwise mark it as loading
22111
+ if (_pendingPageRequests.current[pageIndex]) {
22112
+ return Promise.resolve();
22113
+ } else {
22114
+ _pendingPageRequests.current[pageIndex] = true;
22115
+ }
22116
+ // if the page is already loaded and has actual rows, abort
22117
+ if (data.pages.includes(pageIndex)) {
22118
+ const position = data.pages.indexOf(pageIndex);
22119
+ if (data.rows[position * pageSize] !== undefined) {
22120
+ return Promise.resolve();
22121
+ }
22122
+ }
22123
+ // set the sorting so we can track if it changed between loads
22124
+ _lastUsedSorting.current = sorting;
22125
+ // set the filters so we can track if it changed between loads
22126
+ _lastUsedFilters.current = filters;
22127
+ const direction = getDirection2(pageIndex, _lastFetchedPage.current);
22128
+ const _temp2 = _finallyRethrows(function () {
22129
+ return _catch(function () {
22130
+ return Promise.resolve(fetchPage(pageIndex, pageSize, sorting, filters, search, hiddenColumns)).then(function (response) {
22131
+ length.current = response.length;
22132
+ // update state, here we do some "magic" to support "load in place"
22133
+ setData(currentData => {
22134
+ const nextPages = getPages(pageIndex, _lastFetchedPage.current, reset ? [] : currentData.pages, direction);
22135
+ let nextRows = currentData.rows;
22136
+ if (reset || !direction) {
22137
+ const index = nextPages.indexOf(pageIndex);
22138
+ const startIndex = (index > -1 ? index : 0) * pageSize;
22139
+ if (reset) {
22140
+ nextRows = [].concat(response.data);
22141
+ } else if (startIndex > 0 || pageIndex === 0) {
22142
+ nextRows = Array(startIndex).fill(undefined).concat(response.data);
22143
+ } else if (startIndex === 0) {
22144
+ nextRows = response.data.concat(currentData.rows.slice(pageSize));
22145
+ }
22146
+ } else if (direction === 'forward') {
22147
+ // if the new data will exceed the dataset size, then chop off the start or end
22148
+ // this keeps the stored dataset to our preferred size
22149
+ if (currentData.rows.length >= DATASET_SIZE) {
22150
+ nextRows = currentData.rows.slice(DEFAULT_PAGE_SIZE).concat(response.data);
22151
+ } else {
22152
+ nextRows = currentData.rows.concat(response.data);
22153
+ }
22154
+ } else if (direction === 'backward') {
22155
+ if (currentData.rows.length >= DATASET_SIZE) {
22156
+ nextRows = response.data.concat(currentData.rows.slice(0, -1 * pageSize));
22157
+ } else {
22158
+ nextRows = currentData.rows.concat(response.data);
22159
+ }
22160
+ }
22161
+ return {
22162
+ rows: nextRows,
22163
+ pages: nextPages
22164
+ };
22165
+ });
22166
+ });
22167
+ }, function () {});
22168
+ }, function (_wasThrown2, _result2) {
22169
+ _hasFetchedAll.current = false;
22170
+ // reset pending requests
22171
+ delete _pendingPageRequests.current[pageIndex];
22172
+ // update the last loaded page
22173
+ _lastFetchedPage.current = {
22174
+ index: pageIndex,
22175
+ direction
22176
+ };
22177
+ if (_wasThrown2) throw _result2;
22178
+ return _result2;
22179
+ });
22180
+ return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function () {}) : void 0);
22181
+ } catch (e) {
22182
+ return Promise.reject(e);
22183
+ }
22184
+ };
22185
+ const {
22186
+ pageSize
22187
+ } = options;
22188
+ const DATASET_SIZE = DATASET_SIZE_MULTIPLIER * pageSize;
22189
+ // track the data length, we don't know it until the first request
22190
+ const length = React__default.useRef(0);
22191
+ // data will be filled after the first request
22192
+ const [data, setData] = React__default.useState({
22193
+ rows: [],
22194
+ pages: []
22195
+ });
22196
+ // track which pages have been loaded to dedupe requests
22197
+ const _pendingPageRequests = React__default.useRef({});
22198
+ const _lastUsedSorting = React__default.useRef([]);
22199
+ const _lastUsedFilters = React__default.useRef([]);
22200
+ const _lastUsedSearch = React__default.useRef();
22201
+ const _lastUsedHiddenColumns = React__default.useRef([]);
22202
+ const _lastFetchedPage = React__default.useRef({
22203
+ index: undefined,
22204
+ direction: undefined
22205
+ });
22206
+ const _hasFetchedAll = React__default.useRef(false);
22207
+ const loadAll = function (sorting, filters, search, hiddenColumns) {
22208
+ try {
22209
+ _hasFetchedAll.current = true;
22210
+ // set the sorting so we can track if it changed between loads
22211
+ _lastUsedSorting.current = sorting;
22212
+ // set the filters so we can track if it changed between loads
22213
+ _lastUsedFilters.current = filters;
22214
+ const _temp = _finallyRethrows(function () {
22215
+ return _catch(function () {
22216
+ return Promise.resolve(fetchAll(sorting, filters, search, hiddenColumns)).then(function (response) {
22217
+ length.current = response.length;
22218
+ setData({
22219
+ rows: response.data,
22220
+ pages: Array.from(Array(response.length / pageSize).keys())
22221
+ });
22222
+ });
22223
+ }, function () {});
22224
+ }, function (_wasThrown, _result) {
22225
+ // reset pending requests
22226
+ _pendingPageRequests.current = {};
22227
+ if (_wasThrown) throw _result;
22228
+ return _result;
22229
+ });
22230
+ return Promise.resolve(_temp && _temp.then ? _temp.then(function () {}) : void 0);
22231
+ } catch (e) {
22232
+ return Promise.reject(e);
22233
+ }
22234
+ };
22235
+ const invalidate = function () {
22236
+ try {
22237
+ // reset stuff
22238
+ _pendingPageRequests.current = {};
22239
+ _hasFetchedAll.current = false;
22240
+ // load the last page that we scrolled to
22241
+ return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), _lastUsedSorting.current, _lastUsedFilters.current, _lastUsedSearch.current, _lastUsedHiddenColumns.current, true);
22242
+ } catch (e) {
22243
+ return Promise.reject(e);
22244
+ }
22245
+ };
22246
+ // search works client side - it fetches all then works client side - so these handlers are a little "weird"
22247
+ // if a search is currently "active", we need to re load all because
22248
+ const handleSort = function (sorting) {
22249
+ try {
22250
+ _lastUsedSorting.current = sorting;
22251
+ // reset stuff
22252
+ _pendingPageRequests.current = {};
22253
+ _hasFetchedAll.current = false;
22254
+ return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), sorting, _lastUsedFilters.current, _lastUsedSearch.current, _lastUsedHiddenColumns.current, true);
22255
+ } catch (e) {
22256
+ return Promise.reject(e);
22257
+ }
22258
+ };
22259
+ const handleFilter = function (filters, hiddenColumns) {
22260
+ try {
22261
+ _lastUsedFilters.current = filters;
22262
+ _lastUsedHiddenColumns.current = hiddenColumns;
22263
+ // reset stuff
22264
+ _pendingPageRequests.current = {};
22265
+ _hasFetchedAll.current = false;
22266
+ return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), _lastUsedSorting.current, filters, _lastUsedSearch.current, hiddenColumns, true);
22267
+ } catch (e) {
22268
+ return Promise.reject(e);
22269
+ }
22270
+ };
22271
+ const handleSearch = function (search, hiddenColumns) {
22272
+ try {
22273
+ _lastUsedSearch.current = search;
22274
+ _lastUsedHiddenColumns.current = hiddenColumns;
22275
+ // reset stuff
22276
+ _pendingPageRequests.current = {};
22277
+ _hasFetchedAll.current = false;
22278
+ return loadPage(getCurrentPage(data.pages, _lastFetchedPage.current), _lastUsedSorting.current, _lastUsedFilters.current, search, hiddenColumns, true);
22279
+ } catch (e) {
22280
+ return Promise.reject(e);
22281
+ }
22282
+ };
22283
+ return [{
22284
+ data: data.rows,
22285
+ pages: data.pages,
22286
+ length: length.current,
22287
+ loadAll,
22288
+ loadPage,
22289
+ onChangeFilter: handleFilter,
22290
+ onChangeSearch: handleSearch,
22291
+ onChangeSort: handleSort,
22292
+ pageSize,
22293
+ _experimentalDataLoader2: true
22294
+ }, invalidate];
22295
+ }
22296
+ function getCurrentPage(currentPages, lastFetchedPage) {
22297
+ if (currentPages.length <= 2) {
22298
+ var _currentPages$;
22299
+ return (_currentPages$ = currentPages[0]) !== null && _currentPages$ !== void 0 ? _currentPages$ : 0;
22300
+ }
22301
+ const middle = Math.floor(DATASET_SIZE_MULTIPLIER / 2);
22302
+ if (lastFetchedPage.index) {
22303
+ if (!lastFetchedPage.direction) {
22304
+ return lastFetchedPage.index;
22305
+ }
22306
+ return lastFetchedPage.direction === 'forward' ? lastFetchedPage.index + middle : lastFetchedPage.index - middle;
22307
+ }
22308
+ return 0;
22309
+ }
22310
+ function getDirection2(pageIndex, lastUsedPage) {
22311
+ if (lastUsedPage.index === undefined) {
22312
+ return undefined;
22313
+ }
22314
+ if (pageIndex > lastUsedPage.index) {
22315
+ if (lastUsedPage.index + 1 === pageIndex || lastUsedPage.index + DATASET_SIZE_MULTIPLIER === pageIndex) {
22316
+ return 'forward';
22317
+ }
22318
+ } else if (pageIndex < lastUsedPage.index) {
22319
+ if (lastUsedPage.index - 1 === pageIndex || lastUsedPage.index - DATASET_SIZE_MULTIPLIER === pageIndex) {
22320
+ return 'backward';
22321
+ }
22322
+ }
22323
+ return undefined;
22324
+ }
22325
+ function getPages(pageIndex, lastUsedPage, currentPages, direction) {
22326
+ if (currentPages.length && (pageIndex === lastUsedPage.index || currentPages.includes(pageIndex))) {
22327
+ return currentPages;
22328
+ }
22329
+ if (direction === 'forward') {
22330
+ const nextPages = currentPages.length === DATASET_SIZE_MULTIPLIER ? currentPages.slice(1) : currentPages;
22331
+ return nextPages.concat(pageIndex);
22332
+ }
22333
+ if (direction === 'backward') {
22334
+ const nextPages = currentPages.length === DATASET_SIZE_MULTIPLIER ? currentPages.slice(0, -1) : currentPages;
22335
+ return [pageIndex].concat(nextPages);
22336
+ }
22337
+ if (pageIndex === 0) {
22338
+ return [0];
22339
+ }
22340
+ // don't go forward, the current page will trigger load of the next page
22341
+ return [pageIndex - 1, pageIndex];
22342
+ }
22343
+
22101
22344
  const useBoundaryOverflowDetection = (ref, dependencies = []) => {
22102
22345
  const [boundaryIndex, setBoundaryIndex] = React__default.useState();
22103
22346
  const dimensions = useBoundingClientRectListener(ref, dependencies);
@@ -22237,6 +22480,7 @@ exports.useOnClickOutside = useOnClickOutside;
22237
22480
  exports.usePagination = usePagination;
22238
22481
  exports.useRadioGroup = useRadioGroup;
22239
22482
  exports.useTableDataLoader = useTableDataLoader;
22483
+ exports.useTableDataLoader2 = useTableDataLoader2;
22240
22484
  exports.useTableRowCreation = useTableRowCreation;
22241
22485
  exports.useToast = useToast;
22242
22486
  //# sourceMappingURL=taco.cjs.development.js.map