@adventurelabs/scout-core 1.4.23 → 1.4.24

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.
@@ -419,6 +419,9 @@ export const useInfiniteFeedByHerd = (herdId, options) => {
419
419
  const [currentCursor, setCurrentCursor] = useState(null);
420
420
  const prevHerdIdRef = useRef();
421
421
  const lastAddedCursorRef = useRef(undefined);
422
+ // Store hasMore/nextCursor from the last merged response (like dummy's currentResult) so loadMore
423
+ // always has a usable nextCursor even when currentQuery has switched to the next request.
424
+ const lastResultRef = useRef(null);
422
425
  const currentQuery = useGetFeedInfiniteByHerdQuery({
423
426
  herdId,
424
427
  limit: options.limit || 20,
@@ -433,6 +436,7 @@ export const useInfiniteFeedByHerd = (herdId, options) => {
433
436
  setPages([]);
434
437
  setCurrentCursor(null);
435
438
  lastAddedCursorRef.current = undefined;
439
+ lastResultRef.current = null;
436
440
  }
437
441
  prevHerdIdRef.current = herdId;
438
442
  }, [herdId, options.enabled]);
@@ -447,14 +451,18 @@ export const useInfiniteFeedByHerd = (herdId, options) => {
447
451
  if (pages.length > 0 &&
448
452
  feedCursorEq(lastAddedCursorRef.current ?? null, currentCursor))
449
453
  return;
454
+ const items = Array.isArray(currentQuery.data?.items)
455
+ ? currentQuery.data.items
456
+ : [];
457
+ const limit = options.limit || 20;
458
+ // Store hasMore/nextCursor from this response so loadMore can use it (like dummy's currentResult)
459
+ lastResultRef.current = {
460
+ hasMore: currentQuery.data.hasMore ?? false,
461
+ nextCursor: currentQuery.data.nextCursor ?? null,
462
+ };
450
463
  setPages((prev) => {
451
464
  const existingPage = prev.find((p) => feedCursorEq(p.cursor, currentCursor));
452
465
  if (!existingPage) {
453
- const items = Array.isArray(currentQuery.data?.items)
454
- ? currentQuery.data.items
455
- : [];
456
- const limit = options.limit || 20;
457
- // Only mark "last added" for full pages so partial/empty responses don't block a later merge
458
466
  if (items.length >= limit) {
459
467
  lastAddedCursorRef.current = currentCursor;
460
468
  }
@@ -467,16 +475,18 @@ export const useInfiniteFeedByHerd = (herdId, options) => {
467
475
  });
468
476
  }, [currentQuery.data, currentQuery.isLoading, currentCursor, pages.length, options.limit]);
469
477
  const loadMore = useCallback(() => {
470
- if (currentQuery.data?.hasMore &&
471
- currentQuery.data.nextCursor &&
478
+ const result = lastResultRef.current;
479
+ if (result?.hasMore &&
480
+ result.nextCursor != null &&
472
481
  !currentQuery.isLoading) {
473
- setCurrentCursor(currentQuery.data.nextCursor);
482
+ setCurrentCursor(result.nextCursor);
474
483
  }
475
- }, [currentQuery.data, currentQuery.isLoading]);
484
+ }, [currentQuery.isLoading]);
476
485
  const refetch = useCallback(() => {
477
486
  setPages([]);
478
487
  setCurrentCursor(null);
479
488
  lastAddedCursorRef.current = undefined;
489
+ lastResultRef.current = null;
480
490
  currentQuery.refetch();
481
491
  }, [currentQuery]);
482
492
  const allItems = useMemo(() => {
@@ -493,7 +503,7 @@ export const useInfiniteFeedByHerd = (herdId, options) => {
493
503
  items: allItems,
494
504
  isLoading: currentQuery.isLoading && pages.length === 0,
495
505
  isLoadingMore: currentQuery.isLoading && pages.length > 0,
496
- hasMore: (currentQuery.data?.hasMore ??
506
+ hasMore: (lastResultRef.current?.hasMore ??
497
507
  (currentCursor !== null && pages.length > 0)) ??
498
508
  false,
499
509
  loadMore,
@@ -506,6 +516,7 @@ export const useInfiniteFeedByDevice = (deviceId, options) => {
506
516
  const [currentCursor, setCurrentCursor] = useState(null);
507
517
  const prevDeviceIdRef = useRef();
508
518
  const lastAddedCursorRef = useRef(undefined);
519
+ const lastResultRef = useRef(null);
509
520
  const currentQuery = useGetFeedInfiniteByDeviceQuery({
510
521
  deviceId,
511
522
  limit: options.limit || 20,
@@ -520,6 +531,7 @@ export const useInfiniteFeedByDevice = (deviceId, options) => {
520
531
  setPages([]);
521
532
  setCurrentCursor(null);
522
533
  lastAddedCursorRef.current = undefined;
534
+ lastResultRef.current = null;
523
535
  }
524
536
  prevDeviceIdRef.current = deviceId;
525
537
  }, [deviceId, options.enabled]);
@@ -543,6 +555,10 @@ export const useInfiniteFeedByDevice = (deviceId, options) => {
543
555
  if (items.length >= limit) {
544
556
  lastAddedCursorRef.current = currentCursor;
545
557
  }
558
+ lastResultRef.current = {
559
+ hasMore: currentQuery.data?.hasMore ?? false,
560
+ nextCursor: currentQuery.data?.nextCursor ?? null,
561
+ };
546
562
  return [
547
563
  ...prev,
548
564
  { cursor: currentCursor, data: items },
@@ -552,16 +568,18 @@ export const useInfiniteFeedByDevice = (deviceId, options) => {
552
568
  });
553
569
  }, [currentQuery.data, currentQuery.isLoading, currentCursor, pages.length, options.limit]);
554
570
  const loadMore = useCallback(() => {
555
- if (currentQuery.data?.hasMore &&
556
- currentQuery.data.nextCursor &&
571
+ const result = lastResultRef.current;
572
+ if (result?.hasMore &&
573
+ result.nextCursor != null &&
557
574
  !currentQuery.isLoading) {
558
- setCurrentCursor(currentQuery.data.nextCursor);
575
+ setCurrentCursor(result.nextCursor);
559
576
  }
560
- }, [currentQuery.data, currentQuery.isLoading]);
577
+ }, [currentQuery.isLoading]);
561
578
  const refetch = useCallback(() => {
562
579
  setPages([]);
563
580
  setCurrentCursor(null);
564
581
  lastAddedCursorRef.current = undefined;
582
+ lastResultRef.current = null;
565
583
  currentQuery.refetch();
566
584
  }, [currentQuery]);
567
585
  const allItems = useMemo(() => {
@@ -578,7 +596,7 @@ export const useInfiniteFeedByDevice = (deviceId, options) => {
578
596
  items: allItems,
579
597
  isLoading: currentQuery.isLoading && pages.length === 0,
580
598
  isLoadingMore: currentQuery.isLoading && pages.length > 0,
581
- hasMore: (currentQuery.data?.hasMore ??
599
+ hasMore: (lastResultRef.current?.hasMore ??
582
600
  (currentCursor !== null && pages.length > 0)) ??
583
601
  false,
584
602
  loadMore,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.4.23",
3
+ "version": "1.4.24",
4
4
  "description": "Core utilities and helpers for Adventure Labs Scout applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",