@plugable-io/react 0.0.11 → 0.0.12
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 +39 -36
- package/dist/index.mjs +39 -36
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -575,6 +575,10 @@ function Dropzone({
|
|
|
575
575
|
|
|
576
576
|
// src/hooks/useFiles.ts
|
|
577
577
|
var import_react3 = require("react");
|
|
578
|
+
var DEBUG = true;
|
|
579
|
+
function log(msg, ...args) {
|
|
580
|
+
if (DEBUG) console.log(`[useFiles] ${msg}`, ...args);
|
|
581
|
+
}
|
|
578
582
|
function useFiles({
|
|
579
583
|
metadata,
|
|
580
584
|
startPage = 1,
|
|
@@ -590,8 +594,7 @@ function useFiles({
|
|
|
590
594
|
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
591
595
|
const [page, setPage] = (0, import_react3.useState)(startPage);
|
|
592
596
|
const [hasNext, setHasNext] = (0, import_react3.useState)(false);
|
|
593
|
-
const
|
|
594
|
-
const isInitialMountRef = (0, import_react3.useRef)(true);
|
|
597
|
+
const hasInitializedRef = (0, import_react3.useRef)(false);
|
|
595
598
|
const effectiveStaleTime = staleTime ?? providerStaleTime;
|
|
596
599
|
const metadataKey = JSON.stringify(metadata);
|
|
597
600
|
const stableMetadata = (0, import_react3.useMemo)(() => metadata, [metadataKey]);
|
|
@@ -604,24 +607,29 @@ function useFiles({
|
|
|
604
607
|
page
|
|
605
608
|
}), [stableMetadata, mediaType, perPage, orderBy, orderDirection, page]);
|
|
606
609
|
const fetchFiles = (0, import_react3.useCallback)(async (pageNum, skipCache = false) => {
|
|
610
|
+
const currentParamsKey = JSON.stringify({
|
|
611
|
+
metadata: stableMetadata,
|
|
612
|
+
mediaType,
|
|
613
|
+
perPage,
|
|
614
|
+
orderBy,
|
|
615
|
+
orderDirection,
|
|
616
|
+
page: pageNum
|
|
617
|
+
});
|
|
618
|
+
log("fetchFiles called", { pageNum, skipCache, currentParamsKey });
|
|
607
619
|
if (!skipCache) {
|
|
608
|
-
const
|
|
609
|
-
metadata: stableMetadata,
|
|
610
|
-
mediaType,
|
|
611
|
-
perPage,
|
|
612
|
-
orderBy,
|
|
613
|
-
orderDirection,
|
|
614
|
-
page: pageNum
|
|
615
|
-
});
|
|
616
|
-
const cachedEntry = getCache(cacheKey);
|
|
620
|
+
const cachedEntry = getCache(currentParamsKey);
|
|
617
621
|
if (cachedEntry) {
|
|
618
622
|
const age = Date.now() - cachedEntry.timestamp;
|
|
619
623
|
const isStale = age > effectiveStaleTime;
|
|
624
|
+
log("Cache check", { exists: true, age, isStale });
|
|
620
625
|
if (!isStale) {
|
|
626
|
+
log("Serving from cache");
|
|
621
627
|
setFiles(cachedEntry.files);
|
|
622
628
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
623
629
|
return;
|
|
624
630
|
}
|
|
631
|
+
} else {
|
|
632
|
+
log("Cache miss");
|
|
625
633
|
}
|
|
626
634
|
}
|
|
627
635
|
setIsLoading(true);
|
|
@@ -635,16 +643,10 @@ function useFiles({
|
|
|
635
643
|
...orderBy && { order_by: orderBy },
|
|
636
644
|
...orderDirection && { order_direction: orderDirection }
|
|
637
645
|
};
|
|
646
|
+
log("Sending API Request", options);
|
|
638
647
|
const response = await client.list(options);
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
mediaType,
|
|
642
|
-
perPage,
|
|
643
|
-
orderBy,
|
|
644
|
-
orderDirection,
|
|
645
|
-
page: pageNum
|
|
646
|
-
});
|
|
647
|
-
setCache(cacheKey, {
|
|
648
|
+
log("API Response received", response);
|
|
649
|
+
setCache(currentParamsKey, {
|
|
648
650
|
files: response.files,
|
|
649
651
|
paging: response.paging,
|
|
650
652
|
timestamp: Date.now()
|
|
@@ -652,7 +654,7 @@ function useFiles({
|
|
|
652
654
|
setFiles(response.files);
|
|
653
655
|
setHasNext(response.paging.has_next_page);
|
|
654
656
|
} catch (err) {
|
|
655
|
-
console.error("Failed to load files:", err);
|
|
657
|
+
console.error("[useFiles] Failed to load files:", err);
|
|
656
658
|
setFiles([]);
|
|
657
659
|
setHasNext(false);
|
|
658
660
|
} finally {
|
|
@@ -660,41 +662,42 @@ function useFiles({
|
|
|
660
662
|
}
|
|
661
663
|
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime]);
|
|
662
664
|
(0, import_react3.useEffect)(() => {
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
665
|
+
log("Effect triggered", { paramsKeyWithPage, autoLoad, initialized: hasInitializedRef.current });
|
|
666
|
+
if (!hasInitializedRef.current) {
|
|
667
|
+
hasInitializedRef.current = true;
|
|
668
|
+
if (!autoLoad) {
|
|
669
|
+
log("Skipping initial load because autoLoad is false");
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
668
672
|
}
|
|
669
673
|
const cachedEntry = getCache(paramsKeyWithPage);
|
|
670
|
-
const
|
|
674
|
+
const isStale = cachedEntry ? Date.now() - cachedEntry.timestamp > effectiveStaleTime : true;
|
|
671
675
|
if (cachedEntry) {
|
|
672
|
-
|
|
673
|
-
const isStale = age > effectiveStaleTime;
|
|
676
|
+
log("Immediate cache hit in Effect", { isStale });
|
|
674
677
|
setFiles(cachedEntry.files);
|
|
675
678
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
676
|
-
if (
|
|
677
|
-
|
|
679
|
+
if (!isStale) {
|
|
680
|
+
return;
|
|
678
681
|
}
|
|
679
|
-
} else if (shouldFetch) {
|
|
680
|
-
fetchFiles(page, true);
|
|
681
682
|
}
|
|
683
|
+
log("Proceeding to fetch");
|
|
684
|
+
fetchFiles(page, true);
|
|
682
685
|
}, [paramsKeyWithPage, autoLoad, getCache, effectiveStaleTime, fetchFiles, page]);
|
|
683
686
|
(0, import_react3.useEffect)(() => {
|
|
684
687
|
const unsubscribe = on("file.uploaded", () => {
|
|
688
|
+
log("Event file.uploaded received, refreshing...");
|
|
685
689
|
fetchFiles(page, true);
|
|
686
690
|
});
|
|
687
691
|
return unsubscribe;
|
|
688
692
|
}, [on, fetchFiles, page]);
|
|
689
693
|
const loadNextPage = (0, import_react3.useCallback)(() => {
|
|
690
|
-
if (hasNext)
|
|
691
|
-
setPage((p) => p + 1);
|
|
692
|
-
}
|
|
694
|
+
if (hasNext) setPage((p) => p + 1);
|
|
693
695
|
}, [hasNext]);
|
|
694
696
|
const loadPreviousPage = (0, import_react3.useCallback)(() => {
|
|
695
697
|
setPage((p) => Math.max(1, p - 1));
|
|
696
698
|
}, []);
|
|
697
699
|
const refresh = (0, import_react3.useCallback)(async () => {
|
|
700
|
+
log("Manual refresh triggered");
|
|
698
701
|
await fetchFiles(page, true);
|
|
699
702
|
}, [fetchFiles, page]);
|
|
700
703
|
return {
|
package/dist/index.mjs
CHANGED
|
@@ -529,6 +529,10 @@ function Dropzone({
|
|
|
529
529
|
|
|
530
530
|
// src/hooks/useFiles.ts
|
|
531
531
|
import { useState as useState3, useCallback as useCallback3, useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2 } from "react";
|
|
532
|
+
var DEBUG = true;
|
|
533
|
+
function log(msg, ...args) {
|
|
534
|
+
if (DEBUG) console.log(`[useFiles] ${msg}`, ...args);
|
|
535
|
+
}
|
|
532
536
|
function useFiles({
|
|
533
537
|
metadata,
|
|
534
538
|
startPage = 1,
|
|
@@ -544,8 +548,7 @@ function useFiles({
|
|
|
544
548
|
const [isLoading, setIsLoading] = useState3(false);
|
|
545
549
|
const [page, setPage] = useState3(startPage);
|
|
546
550
|
const [hasNext, setHasNext] = useState3(false);
|
|
547
|
-
const
|
|
548
|
-
const isInitialMountRef = useRef2(true);
|
|
551
|
+
const hasInitializedRef = useRef2(false);
|
|
549
552
|
const effectiveStaleTime = staleTime ?? providerStaleTime;
|
|
550
553
|
const metadataKey = JSON.stringify(metadata);
|
|
551
554
|
const stableMetadata = useMemo2(() => metadata, [metadataKey]);
|
|
@@ -558,24 +561,29 @@ function useFiles({
|
|
|
558
561
|
page
|
|
559
562
|
}), [stableMetadata, mediaType, perPage, orderBy, orderDirection, page]);
|
|
560
563
|
const fetchFiles = useCallback3(async (pageNum, skipCache = false) => {
|
|
564
|
+
const currentParamsKey = JSON.stringify({
|
|
565
|
+
metadata: stableMetadata,
|
|
566
|
+
mediaType,
|
|
567
|
+
perPage,
|
|
568
|
+
orderBy,
|
|
569
|
+
orderDirection,
|
|
570
|
+
page: pageNum
|
|
571
|
+
});
|
|
572
|
+
log("fetchFiles called", { pageNum, skipCache, currentParamsKey });
|
|
561
573
|
if (!skipCache) {
|
|
562
|
-
const
|
|
563
|
-
metadata: stableMetadata,
|
|
564
|
-
mediaType,
|
|
565
|
-
perPage,
|
|
566
|
-
orderBy,
|
|
567
|
-
orderDirection,
|
|
568
|
-
page: pageNum
|
|
569
|
-
});
|
|
570
|
-
const cachedEntry = getCache(cacheKey);
|
|
574
|
+
const cachedEntry = getCache(currentParamsKey);
|
|
571
575
|
if (cachedEntry) {
|
|
572
576
|
const age = Date.now() - cachedEntry.timestamp;
|
|
573
577
|
const isStale = age > effectiveStaleTime;
|
|
578
|
+
log("Cache check", { exists: true, age, isStale });
|
|
574
579
|
if (!isStale) {
|
|
580
|
+
log("Serving from cache");
|
|
575
581
|
setFiles(cachedEntry.files);
|
|
576
582
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
577
583
|
return;
|
|
578
584
|
}
|
|
585
|
+
} else {
|
|
586
|
+
log("Cache miss");
|
|
579
587
|
}
|
|
580
588
|
}
|
|
581
589
|
setIsLoading(true);
|
|
@@ -589,16 +597,10 @@ function useFiles({
|
|
|
589
597
|
...orderBy && { order_by: orderBy },
|
|
590
598
|
...orderDirection && { order_direction: orderDirection }
|
|
591
599
|
};
|
|
600
|
+
log("Sending API Request", options);
|
|
592
601
|
const response = await client.list(options);
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
mediaType,
|
|
596
|
-
perPage,
|
|
597
|
-
orderBy,
|
|
598
|
-
orderDirection,
|
|
599
|
-
page: pageNum
|
|
600
|
-
});
|
|
601
|
-
setCache(cacheKey, {
|
|
602
|
+
log("API Response received", response);
|
|
603
|
+
setCache(currentParamsKey, {
|
|
602
604
|
files: response.files,
|
|
603
605
|
paging: response.paging,
|
|
604
606
|
timestamp: Date.now()
|
|
@@ -606,7 +608,7 @@ function useFiles({
|
|
|
606
608
|
setFiles(response.files);
|
|
607
609
|
setHasNext(response.paging.has_next_page);
|
|
608
610
|
} catch (err) {
|
|
609
|
-
console.error("Failed to load files:", err);
|
|
611
|
+
console.error("[useFiles] Failed to load files:", err);
|
|
610
612
|
setFiles([]);
|
|
611
613
|
setHasNext(false);
|
|
612
614
|
} finally {
|
|
@@ -614,41 +616,42 @@ function useFiles({
|
|
|
614
616
|
}
|
|
615
617
|
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime]);
|
|
616
618
|
useEffect2(() => {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
619
|
+
log("Effect triggered", { paramsKeyWithPage, autoLoad, initialized: hasInitializedRef.current });
|
|
620
|
+
if (!hasInitializedRef.current) {
|
|
621
|
+
hasInitializedRef.current = true;
|
|
622
|
+
if (!autoLoad) {
|
|
623
|
+
log("Skipping initial load because autoLoad is false");
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
622
626
|
}
|
|
623
627
|
const cachedEntry = getCache(paramsKeyWithPage);
|
|
624
|
-
const
|
|
628
|
+
const isStale = cachedEntry ? Date.now() - cachedEntry.timestamp > effectiveStaleTime : true;
|
|
625
629
|
if (cachedEntry) {
|
|
626
|
-
|
|
627
|
-
const isStale = age > effectiveStaleTime;
|
|
630
|
+
log("Immediate cache hit in Effect", { isStale });
|
|
628
631
|
setFiles(cachedEntry.files);
|
|
629
632
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
630
|
-
if (
|
|
631
|
-
|
|
633
|
+
if (!isStale) {
|
|
634
|
+
return;
|
|
632
635
|
}
|
|
633
|
-
} else if (shouldFetch) {
|
|
634
|
-
fetchFiles(page, true);
|
|
635
636
|
}
|
|
637
|
+
log("Proceeding to fetch");
|
|
638
|
+
fetchFiles(page, true);
|
|
636
639
|
}, [paramsKeyWithPage, autoLoad, getCache, effectiveStaleTime, fetchFiles, page]);
|
|
637
640
|
useEffect2(() => {
|
|
638
641
|
const unsubscribe = on("file.uploaded", () => {
|
|
642
|
+
log("Event file.uploaded received, refreshing...");
|
|
639
643
|
fetchFiles(page, true);
|
|
640
644
|
});
|
|
641
645
|
return unsubscribe;
|
|
642
646
|
}, [on, fetchFiles, page]);
|
|
643
647
|
const loadNextPage = useCallback3(() => {
|
|
644
|
-
if (hasNext)
|
|
645
|
-
setPage((p) => p + 1);
|
|
646
|
-
}
|
|
648
|
+
if (hasNext) setPage((p) => p + 1);
|
|
647
649
|
}, [hasNext]);
|
|
648
650
|
const loadPreviousPage = useCallback3(() => {
|
|
649
651
|
setPage((p) => Math.max(1, p - 1));
|
|
650
652
|
}, []);
|
|
651
653
|
const refresh = useCallback3(async () => {
|
|
654
|
+
log("Manual refresh triggered");
|
|
652
655
|
await fetchFiles(page, true);
|
|
653
656
|
}, [fetchFiles, page]);
|
|
654
657
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plugable-io/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "React components and hooks for Plugable File Management API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"react-dom": ">=16.8.0"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@plugable-io/js": "^0.0.
|
|
46
|
+
"@plugable-io/js": "^0.0.11"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "^20.0.0",
|