@plugable-io/react 0.0.4 → 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/dist/index.js +11 -2
- package/dist/index.mjs +12 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -414,6 +414,8 @@ function useFiles({
|
|
|
414
414
|
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
415
415
|
const [page, setPage] = (0, import_react3.useState)(startPage);
|
|
416
416
|
const [hasNext, setHasNext] = (0, import_react3.useState)(false);
|
|
417
|
+
const previousParamsRef = (0, import_react3.useRef)(null);
|
|
418
|
+
const isInitialMountRef = (0, import_react3.useRef)(true);
|
|
417
419
|
const effectiveStaleTime = staleTime ?? providerStaleTime;
|
|
418
420
|
const metadataKey = JSON.stringify(metadata);
|
|
419
421
|
const stableMetadata = (0, import_react3.useMemo)(() => metadata, [metadataKey]);
|
|
@@ -482,16 +484,23 @@ function useFiles({
|
|
|
482
484
|
}
|
|
483
485
|
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime]);
|
|
484
486
|
(0, import_react3.useEffect)(() => {
|
|
487
|
+
const paramsChanged = previousParamsRef.current !== null && previousParamsRef.current !== paramsKeyWithPage;
|
|
488
|
+
const isInitialMount = isInitialMountRef.current;
|
|
489
|
+
previousParamsRef.current = paramsKeyWithPage;
|
|
490
|
+
if (isInitialMount) {
|
|
491
|
+
isInitialMountRef.current = false;
|
|
492
|
+
}
|
|
485
493
|
const cachedEntry = getCache(paramsKeyWithPage);
|
|
494
|
+
const shouldFetch = paramsChanged || isInitialMount && autoLoad;
|
|
486
495
|
if (cachedEntry) {
|
|
487
496
|
const age = Date.now() - cachedEntry.timestamp;
|
|
488
497
|
const isStale = age > effectiveStaleTime;
|
|
489
498
|
setFiles(cachedEntry.files);
|
|
490
499
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
491
|
-
if (isStale &&
|
|
500
|
+
if (paramsChanged || isStale && shouldFetch) {
|
|
492
501
|
fetchFiles(page, true);
|
|
493
502
|
}
|
|
494
|
-
} else if (
|
|
503
|
+
} else if (shouldFetch) {
|
|
495
504
|
fetchFiles(page, true);
|
|
496
505
|
}
|
|
497
506
|
}, [paramsKeyWithPage, autoLoad, getCache, effectiveStaleTime, fetchFiles, page]);
|
package/dist/index.mjs
CHANGED
|
@@ -355,7 +355,7 @@ function Dropzone({
|
|
|
355
355
|
}
|
|
356
356
|
|
|
357
357
|
// src/hooks/useFiles.ts
|
|
358
|
-
import { useState as useState3, useCallback as useCallback3, useEffect, useMemo as useMemo2 } from "react";
|
|
358
|
+
import { useState as useState3, useCallback as useCallback3, useEffect, useMemo as useMemo2, useRef as useRef2 } from "react";
|
|
359
359
|
function useFiles({
|
|
360
360
|
metadata,
|
|
361
361
|
startPage = 1,
|
|
@@ -371,6 +371,8 @@ function useFiles({
|
|
|
371
371
|
const [isLoading, setIsLoading] = useState3(false);
|
|
372
372
|
const [page, setPage] = useState3(startPage);
|
|
373
373
|
const [hasNext, setHasNext] = useState3(false);
|
|
374
|
+
const previousParamsRef = useRef2(null);
|
|
375
|
+
const isInitialMountRef = useRef2(true);
|
|
374
376
|
const effectiveStaleTime = staleTime ?? providerStaleTime;
|
|
375
377
|
const metadataKey = JSON.stringify(metadata);
|
|
376
378
|
const stableMetadata = useMemo2(() => metadata, [metadataKey]);
|
|
@@ -439,16 +441,23 @@ function useFiles({
|
|
|
439
441
|
}
|
|
440
442
|
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime]);
|
|
441
443
|
useEffect(() => {
|
|
444
|
+
const paramsChanged = previousParamsRef.current !== null && previousParamsRef.current !== paramsKeyWithPage;
|
|
445
|
+
const isInitialMount = isInitialMountRef.current;
|
|
446
|
+
previousParamsRef.current = paramsKeyWithPage;
|
|
447
|
+
if (isInitialMount) {
|
|
448
|
+
isInitialMountRef.current = false;
|
|
449
|
+
}
|
|
442
450
|
const cachedEntry = getCache(paramsKeyWithPage);
|
|
451
|
+
const shouldFetch = paramsChanged || isInitialMount && autoLoad;
|
|
443
452
|
if (cachedEntry) {
|
|
444
453
|
const age = Date.now() - cachedEntry.timestamp;
|
|
445
454
|
const isStale = age > effectiveStaleTime;
|
|
446
455
|
setFiles(cachedEntry.files);
|
|
447
456
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
448
|
-
if (isStale &&
|
|
457
|
+
if (paramsChanged || isStale && shouldFetch) {
|
|
449
458
|
fetchFiles(page, true);
|
|
450
459
|
}
|
|
451
|
-
} else if (
|
|
460
|
+
} else if (shouldFetch) {
|
|
452
461
|
fetchFiles(page, true);
|
|
453
462
|
}
|
|
454
463
|
}, [paramsKeyWithPage, autoLoad, getCache, effectiveStaleTime, fetchFiles, page]);
|