@plugable-io/react 0.0.11 → 0.0.13
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.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +30 -40
- package/dist/index.mjs +30 -40
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -122,6 +122,7 @@ interface UseFilesOptions {
|
|
|
122
122
|
orderBy?: 'created_at' | 'name' | 'byte_size';
|
|
123
123
|
orderDirection?: 'asc' | 'desc';
|
|
124
124
|
staleTime?: number;
|
|
125
|
+
date?: string;
|
|
125
126
|
}
|
|
126
127
|
interface UseFilesResult {
|
|
127
128
|
files: FileObject[];
|
|
@@ -136,7 +137,7 @@ interface UseFilesResult {
|
|
|
136
137
|
setPage: (page: number) => void;
|
|
137
138
|
refresh: () => Promise<void>;
|
|
138
139
|
}
|
|
139
|
-
declare function useFiles({ metadata, startPage, perPage, mediaType, autoLoad, orderBy, orderDirection, staleTime, }?: UseFilesOptions): UseFilesResult;
|
|
140
|
+
declare function useFiles({ metadata, startPage, perPage, mediaType, autoLoad, orderBy, orderDirection, staleTime, date, }?: UseFilesOptions): UseFilesResult;
|
|
140
141
|
|
|
141
142
|
interface ThemeConfig {
|
|
142
143
|
accentColor?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -122,6 +122,7 @@ interface UseFilesOptions {
|
|
|
122
122
|
orderBy?: 'created_at' | 'name' | 'byte_size';
|
|
123
123
|
orderDirection?: 'asc' | 'desc';
|
|
124
124
|
staleTime?: number;
|
|
125
|
+
date?: string;
|
|
125
126
|
}
|
|
126
127
|
interface UseFilesResult {
|
|
127
128
|
files: FileObject[];
|
|
@@ -136,7 +137,7 @@ interface UseFilesResult {
|
|
|
136
137
|
setPage: (page: number) => void;
|
|
137
138
|
refresh: () => Promise<void>;
|
|
138
139
|
}
|
|
139
|
-
declare function useFiles({ metadata, startPage, perPage, mediaType, autoLoad, orderBy, orderDirection, staleTime, }?: UseFilesOptions): UseFilesResult;
|
|
140
|
+
declare function useFiles({ metadata, startPage, perPage, mediaType, autoLoad, orderBy, orderDirection, staleTime, date, }?: UseFilesOptions): UseFilesResult;
|
|
140
141
|
|
|
141
142
|
interface ThemeConfig {
|
|
142
143
|
accentColor?: string;
|
package/dist/index.js
CHANGED
|
@@ -583,15 +583,15 @@ function useFiles({
|
|
|
583
583
|
autoLoad = true,
|
|
584
584
|
orderBy,
|
|
585
585
|
orderDirection,
|
|
586
|
-
staleTime
|
|
586
|
+
staleTime,
|
|
587
|
+
date
|
|
587
588
|
} = {}) {
|
|
588
589
|
const { client, on, getCache, setCache, staleTime: providerStaleTime } = usePlugable();
|
|
589
590
|
const [files, setFiles] = (0, import_react3.useState)([]);
|
|
590
591
|
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
591
592
|
const [page, setPage] = (0, import_react3.useState)(startPage);
|
|
592
593
|
const [hasNext, setHasNext] = (0, import_react3.useState)(false);
|
|
593
|
-
const
|
|
594
|
-
const isInitialMountRef = (0, import_react3.useRef)(true);
|
|
594
|
+
const hasInitializedRef = (0, import_react3.useRef)(false);
|
|
595
595
|
const effectiveStaleTime = staleTime ?? providerStaleTime;
|
|
596
596
|
const metadataKey = JSON.stringify(metadata);
|
|
597
597
|
const stableMetadata = (0, import_react3.useMemo)(() => metadata, [metadataKey]);
|
|
@@ -601,19 +601,21 @@ function useFiles({
|
|
|
601
601
|
perPage,
|
|
602
602
|
orderBy,
|
|
603
603
|
orderDirection,
|
|
604
|
-
page
|
|
605
|
-
|
|
604
|
+
page,
|
|
605
|
+
date
|
|
606
|
+
}), [stableMetadata, mediaType, perPage, orderBy, orderDirection, page, date]);
|
|
606
607
|
const fetchFiles = (0, import_react3.useCallback)(async (pageNum, skipCache = false) => {
|
|
608
|
+
const currentParamsKey = JSON.stringify({
|
|
609
|
+
metadata: stableMetadata,
|
|
610
|
+
mediaType,
|
|
611
|
+
perPage,
|
|
612
|
+
orderBy,
|
|
613
|
+
orderDirection,
|
|
614
|
+
page: pageNum,
|
|
615
|
+
date
|
|
616
|
+
});
|
|
607
617
|
if (!skipCache) {
|
|
608
|
-
const
|
|
609
|
-
metadata: stableMetadata,
|
|
610
|
-
mediaType,
|
|
611
|
-
perPage,
|
|
612
|
-
orderBy,
|
|
613
|
-
orderDirection,
|
|
614
|
-
page: pageNum
|
|
615
|
-
});
|
|
616
|
-
const cachedEntry = getCache(cacheKey);
|
|
618
|
+
const cachedEntry = getCache(currentParamsKey);
|
|
617
619
|
if (cachedEntry) {
|
|
618
620
|
const age = Date.now() - cachedEntry.timestamp;
|
|
619
621
|
const isStale = age > effectiveStaleTime;
|
|
@@ -633,18 +635,11 @@ function useFiles({
|
|
|
633
635
|
per_page: perPage,
|
|
634
636
|
with_download_url: true,
|
|
635
637
|
...orderBy && { order_by: orderBy },
|
|
636
|
-
...orderDirection && { order_direction: orderDirection }
|
|
638
|
+
...orderDirection && { order_direction: orderDirection },
|
|
639
|
+
date
|
|
637
640
|
};
|
|
638
641
|
const response = await client.list(options);
|
|
639
|
-
|
|
640
|
-
metadata: stableMetadata,
|
|
641
|
-
mediaType,
|
|
642
|
-
perPage,
|
|
643
|
-
orderBy,
|
|
644
|
-
orderDirection,
|
|
645
|
-
page: pageNum
|
|
646
|
-
});
|
|
647
|
-
setCache(cacheKey, {
|
|
642
|
+
setCache(currentParamsKey, {
|
|
648
643
|
files: response.files,
|
|
649
644
|
paging: response.paging,
|
|
650
645
|
timestamp: Date.now()
|
|
@@ -658,27 +653,24 @@ function useFiles({
|
|
|
658
653
|
} finally {
|
|
659
654
|
setIsLoading(false);
|
|
660
655
|
}
|
|
661
|
-
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime]);
|
|
656
|
+
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime, date]);
|
|
662
657
|
(0, import_react3.useEffect)(() => {
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
658
|
+
if (!hasInitializedRef.current) {
|
|
659
|
+
hasInitializedRef.current = true;
|
|
660
|
+
if (!autoLoad) {
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
668
663
|
}
|
|
669
664
|
const cachedEntry = getCache(paramsKeyWithPage);
|
|
670
|
-
const
|
|
665
|
+
const isStale = cachedEntry ? Date.now() - cachedEntry.timestamp > effectiveStaleTime : true;
|
|
671
666
|
if (cachedEntry) {
|
|
672
|
-
const age = Date.now() - cachedEntry.timestamp;
|
|
673
|
-
const isStale = age > effectiveStaleTime;
|
|
674
667
|
setFiles(cachedEntry.files);
|
|
675
668
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
676
|
-
if (
|
|
677
|
-
|
|
669
|
+
if (!isStale) {
|
|
670
|
+
return;
|
|
678
671
|
}
|
|
679
|
-
} else if (shouldFetch) {
|
|
680
|
-
fetchFiles(page, true);
|
|
681
672
|
}
|
|
673
|
+
fetchFiles(page, true);
|
|
682
674
|
}, [paramsKeyWithPage, autoLoad, getCache, effectiveStaleTime, fetchFiles, page]);
|
|
683
675
|
(0, import_react3.useEffect)(() => {
|
|
684
676
|
const unsubscribe = on("file.uploaded", () => {
|
|
@@ -687,9 +679,7 @@ function useFiles({
|
|
|
687
679
|
return unsubscribe;
|
|
688
680
|
}, [on, fetchFiles, page]);
|
|
689
681
|
const loadNextPage = (0, import_react3.useCallback)(() => {
|
|
690
|
-
if (hasNext)
|
|
691
|
-
setPage((p) => p + 1);
|
|
692
|
-
}
|
|
682
|
+
if (hasNext) setPage((p) => p + 1);
|
|
693
683
|
}, [hasNext]);
|
|
694
684
|
const loadPreviousPage = (0, import_react3.useCallback)(() => {
|
|
695
685
|
setPage((p) => Math.max(1, p - 1));
|
package/dist/index.mjs
CHANGED
|
@@ -537,15 +537,15 @@ function useFiles({
|
|
|
537
537
|
autoLoad = true,
|
|
538
538
|
orderBy,
|
|
539
539
|
orderDirection,
|
|
540
|
-
staleTime
|
|
540
|
+
staleTime,
|
|
541
|
+
date
|
|
541
542
|
} = {}) {
|
|
542
543
|
const { client, on, getCache, setCache, staleTime: providerStaleTime } = usePlugable();
|
|
543
544
|
const [files, setFiles] = useState3([]);
|
|
544
545
|
const [isLoading, setIsLoading] = useState3(false);
|
|
545
546
|
const [page, setPage] = useState3(startPage);
|
|
546
547
|
const [hasNext, setHasNext] = useState3(false);
|
|
547
|
-
const
|
|
548
|
-
const isInitialMountRef = useRef2(true);
|
|
548
|
+
const hasInitializedRef = useRef2(false);
|
|
549
549
|
const effectiveStaleTime = staleTime ?? providerStaleTime;
|
|
550
550
|
const metadataKey = JSON.stringify(metadata);
|
|
551
551
|
const stableMetadata = useMemo2(() => metadata, [metadataKey]);
|
|
@@ -555,19 +555,21 @@ function useFiles({
|
|
|
555
555
|
perPage,
|
|
556
556
|
orderBy,
|
|
557
557
|
orderDirection,
|
|
558
|
-
page
|
|
559
|
-
|
|
558
|
+
page,
|
|
559
|
+
date
|
|
560
|
+
}), [stableMetadata, mediaType, perPage, orderBy, orderDirection, page, date]);
|
|
560
561
|
const fetchFiles = useCallback3(async (pageNum, skipCache = false) => {
|
|
562
|
+
const currentParamsKey = JSON.stringify({
|
|
563
|
+
metadata: stableMetadata,
|
|
564
|
+
mediaType,
|
|
565
|
+
perPage,
|
|
566
|
+
orderBy,
|
|
567
|
+
orderDirection,
|
|
568
|
+
page: pageNum,
|
|
569
|
+
date
|
|
570
|
+
});
|
|
561
571
|
if (!skipCache) {
|
|
562
|
-
const
|
|
563
|
-
metadata: stableMetadata,
|
|
564
|
-
mediaType,
|
|
565
|
-
perPage,
|
|
566
|
-
orderBy,
|
|
567
|
-
orderDirection,
|
|
568
|
-
page: pageNum
|
|
569
|
-
});
|
|
570
|
-
const cachedEntry = getCache(cacheKey);
|
|
572
|
+
const cachedEntry = getCache(currentParamsKey);
|
|
571
573
|
if (cachedEntry) {
|
|
572
574
|
const age = Date.now() - cachedEntry.timestamp;
|
|
573
575
|
const isStale = age > effectiveStaleTime;
|
|
@@ -587,18 +589,11 @@ function useFiles({
|
|
|
587
589
|
per_page: perPage,
|
|
588
590
|
with_download_url: true,
|
|
589
591
|
...orderBy && { order_by: orderBy },
|
|
590
|
-
...orderDirection && { order_direction: orderDirection }
|
|
592
|
+
...orderDirection && { order_direction: orderDirection },
|
|
593
|
+
date
|
|
591
594
|
};
|
|
592
595
|
const response = await client.list(options);
|
|
593
|
-
|
|
594
|
-
metadata: stableMetadata,
|
|
595
|
-
mediaType,
|
|
596
|
-
perPage,
|
|
597
|
-
orderBy,
|
|
598
|
-
orderDirection,
|
|
599
|
-
page: pageNum
|
|
600
|
-
});
|
|
601
|
-
setCache(cacheKey, {
|
|
596
|
+
setCache(currentParamsKey, {
|
|
602
597
|
files: response.files,
|
|
603
598
|
paging: response.paging,
|
|
604
599
|
timestamp: Date.now()
|
|
@@ -612,27 +607,24 @@ function useFiles({
|
|
|
612
607
|
} finally {
|
|
613
608
|
setIsLoading(false);
|
|
614
609
|
}
|
|
615
|
-
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime]);
|
|
610
|
+
}, [client, stableMetadata, mediaType, perPage, orderBy, orderDirection, getCache, setCache, effectiveStaleTime, date]);
|
|
616
611
|
useEffect2(() => {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
612
|
+
if (!hasInitializedRef.current) {
|
|
613
|
+
hasInitializedRef.current = true;
|
|
614
|
+
if (!autoLoad) {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
622
617
|
}
|
|
623
618
|
const cachedEntry = getCache(paramsKeyWithPage);
|
|
624
|
-
const
|
|
619
|
+
const isStale = cachedEntry ? Date.now() - cachedEntry.timestamp > effectiveStaleTime : true;
|
|
625
620
|
if (cachedEntry) {
|
|
626
|
-
const age = Date.now() - cachedEntry.timestamp;
|
|
627
|
-
const isStale = age > effectiveStaleTime;
|
|
628
621
|
setFiles(cachedEntry.files);
|
|
629
622
|
setHasNext(cachedEntry.paging.has_next_page);
|
|
630
|
-
if (
|
|
631
|
-
|
|
623
|
+
if (!isStale) {
|
|
624
|
+
return;
|
|
632
625
|
}
|
|
633
|
-
} else if (shouldFetch) {
|
|
634
|
-
fetchFiles(page, true);
|
|
635
626
|
}
|
|
627
|
+
fetchFiles(page, true);
|
|
636
628
|
}, [paramsKeyWithPage, autoLoad, getCache, effectiveStaleTime, fetchFiles, page]);
|
|
637
629
|
useEffect2(() => {
|
|
638
630
|
const unsubscribe = on("file.uploaded", () => {
|
|
@@ -641,9 +633,7 @@ function useFiles({
|
|
|
641
633
|
return unsubscribe;
|
|
642
634
|
}, [on, fetchFiles, page]);
|
|
643
635
|
const loadNextPage = useCallback3(() => {
|
|
644
|
-
if (hasNext)
|
|
645
|
-
setPage((p) => p + 1);
|
|
646
|
-
}
|
|
636
|
+
if (hasNext) setPage((p) => p + 1);
|
|
647
637
|
}, [hasNext]);
|
|
648
638
|
const loadPreviousPage = useCallback3(() => {
|
|
649
639
|
setPage((p) => Math.max(1, p - 1));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plugable-io/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
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.12"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "^20.0.0",
|