@adventurelabs/scout-core 1.4.24 → 1.4.25
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/dist/hooks/useInfiniteQuery.js +19 -15
- package/package.json +1 -1
|
@@ -417,29 +417,30 @@ const feedCursorEq = (a, b) => {
|
|
|
417
417
|
export const useInfiniteFeedByHerd = (herdId, options) => {
|
|
418
418
|
const [pages, setPages] = useState([]);
|
|
419
419
|
const [currentCursor, setCurrentCursor] = useState(null);
|
|
420
|
-
const prevHerdIdRef = useRef();
|
|
420
|
+
const prevHerdIdRef = useRef(undefined);
|
|
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
422
|
const lastResultRef = useRef(null);
|
|
423
|
+
const pagesLengthRef = useRef(0);
|
|
425
424
|
const currentQuery = useGetFeedInfiniteByHerdQuery({
|
|
426
425
|
herdId,
|
|
427
426
|
limit: options.limit || 20,
|
|
428
427
|
cursor: currentCursor,
|
|
429
428
|
supabase: options.supabase,
|
|
430
429
|
}, { skip: !options.enabled || !herdId });
|
|
430
|
+
useEffect(() => {
|
|
431
|
+
pagesLengthRef.current = pages.length;
|
|
432
|
+
}, [pages.length]);
|
|
433
|
+
// Clear all state whenever herd id changes (including to/from undefined)
|
|
431
434
|
useEffect(() => {
|
|
432
435
|
if (prevHerdIdRef.current !== undefined &&
|
|
433
|
-
prevHerdIdRef.current !== herdId
|
|
434
|
-
options.enabled &&
|
|
435
|
-
herdId) {
|
|
436
|
+
prevHerdIdRef.current !== herdId) {
|
|
436
437
|
setPages([]);
|
|
437
438
|
setCurrentCursor(null);
|
|
438
439
|
lastAddedCursorRef.current = undefined;
|
|
439
440
|
lastResultRef.current = null;
|
|
440
441
|
}
|
|
441
442
|
prevHerdIdRef.current = herdId;
|
|
442
|
-
}, [herdId
|
|
443
|
+
}, [herdId]);
|
|
443
444
|
// When we request a new page (cursor changed), clear ref so we merge the new response
|
|
444
445
|
useEffect(() => {
|
|
445
446
|
lastAddedCursorRef.current = undefined;
|
|
@@ -447,8 +448,8 @@ export const useInfiniteFeedByHerd = (herdId, options) => {
|
|
|
447
448
|
useEffect(() => {
|
|
448
449
|
if (!currentQuery.data || currentQuery.isLoading)
|
|
449
450
|
return;
|
|
450
|
-
//
|
|
451
|
-
if (
|
|
451
|
+
// Match dummy: only skip merge when we already have pages and this cursor was already added as a full page
|
|
452
|
+
if (pagesLengthRef.current > 0 &&
|
|
452
453
|
feedCursorEq(lastAddedCursorRef.current ?? null, currentCursor))
|
|
453
454
|
return;
|
|
454
455
|
const items = Array.isArray(currentQuery.data?.items)
|
|
@@ -514,27 +515,30 @@ export const useInfiniteFeedByHerd = (herdId, options) => {
|
|
|
514
515
|
export const useInfiniteFeedByDevice = (deviceId, options) => {
|
|
515
516
|
const [pages, setPages] = useState([]);
|
|
516
517
|
const [currentCursor, setCurrentCursor] = useState(null);
|
|
517
|
-
const prevDeviceIdRef = useRef();
|
|
518
|
+
const prevDeviceIdRef = useRef(undefined);
|
|
518
519
|
const lastAddedCursorRef = useRef(undefined);
|
|
519
520
|
const lastResultRef = useRef(null);
|
|
521
|
+
const pagesLengthRef = useRef(0);
|
|
520
522
|
const currentQuery = useGetFeedInfiniteByDeviceQuery({
|
|
521
523
|
deviceId,
|
|
522
524
|
limit: options.limit || 20,
|
|
523
525
|
cursor: currentCursor,
|
|
524
526
|
supabase: options.supabase,
|
|
525
527
|
}, { skip: !options.enabled || !deviceId });
|
|
528
|
+
useEffect(() => {
|
|
529
|
+
pagesLengthRef.current = pages.length;
|
|
530
|
+
}, [pages.length]);
|
|
531
|
+
// Clear all state whenever device id changes (including to/from undefined)
|
|
526
532
|
useEffect(() => {
|
|
527
533
|
if (prevDeviceIdRef.current !== undefined &&
|
|
528
|
-
prevDeviceIdRef.current !== deviceId
|
|
529
|
-
options.enabled &&
|
|
530
|
-
deviceId) {
|
|
534
|
+
prevDeviceIdRef.current !== deviceId) {
|
|
531
535
|
setPages([]);
|
|
532
536
|
setCurrentCursor(null);
|
|
533
537
|
lastAddedCursorRef.current = undefined;
|
|
534
538
|
lastResultRef.current = null;
|
|
535
539
|
}
|
|
536
540
|
prevDeviceIdRef.current = deviceId;
|
|
537
|
-
}, [deviceId
|
|
541
|
+
}, [deviceId]);
|
|
538
542
|
// When we request a new page (cursor changed), clear ref so we merge the new response
|
|
539
543
|
useEffect(() => {
|
|
540
544
|
lastAddedCursorRef.current = undefined;
|
|
@@ -542,7 +546,7 @@ export const useInfiniteFeedByDevice = (deviceId, options) => {
|
|
|
542
546
|
useEffect(() => {
|
|
543
547
|
if (!currentQuery.data || currentQuery.isLoading)
|
|
544
548
|
return;
|
|
545
|
-
if (
|
|
549
|
+
if (pagesLengthRef.current > 0 &&
|
|
546
550
|
feedCursorEq(lastAddedCursorRef.current ?? null, currentCursor))
|
|
547
551
|
return;
|
|
548
552
|
setPages((prev) => {
|