@semapps/activitypub-components 0.6.6 → 0.6.7
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.cjs.js +329 -68
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +22 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +331 -70
- package/dist/index.es.js.map +1 -1
- package/package.json +5 -5
package/dist/index.es.js
CHANGED
|
@@ -6,12 +6,12 @@ import $85cNH$tiptapextensionplaceholder from "@tiptap/extension-placeholder";
|
|
|
6
6
|
import {Box as $85cNH$Box, Avatar as $85cNH$Avatar, Button as $85cNH$Button, Typography as $85cNH$Typography, CircularProgress as $85cNH$CircularProgress} from "@mui/material";
|
|
7
7
|
import $85cNH$muistylesmakeStyles from "@mui/styles/makeStyles";
|
|
8
8
|
import $85cNH$muiiconsmaterialSend from "@mui/icons-material/Send";
|
|
9
|
-
import {useDataModel as $85cNH$useDataModel, buildBlankNodesQuery as $85cNH$buildBlankNodesQuery} from "@semapps/semantic-data-provider";
|
|
9
|
+
import {useDataModel as $85cNH$useDataModel, buildBlankNodesQuery as $85cNH$buildBlankNodesQuery, getOrCreateWsChannel as $85cNH$getOrCreateWsChannel} from "@semapps/semantic-data-provider";
|
|
10
10
|
import {AuthDialog as $85cNH$AuthDialog} from "@semapps/auth-provider";
|
|
11
11
|
import {mergeAttributes as $85cNH$mergeAttributes} from "@tiptap/core";
|
|
12
12
|
import $85cNH$tiptapextensionmention from "@tiptap/extension-mention";
|
|
13
13
|
import {ReferenceField as $85cNH$ReferenceField, AvatarWithLabelField as $85cNH$AvatarWithLabelField} from "@semapps/field-components";
|
|
14
|
-
import {
|
|
14
|
+
import {useQueries as $85cNH$useQueries, useInfiniteQuery as $85cNH$useInfiniteQuery, useQueryClient as $85cNH$useQueryClient} from "react-query";
|
|
15
15
|
import {ReactRenderer as $85cNH$ReactRenderer} from "@tiptap/react";
|
|
16
16
|
import $85cNH$tippyjs from "tippy.js";
|
|
17
17
|
|
|
@@ -54,7 +54,7 @@ const $338f387df48a40d7$export$1ec8e53e7d982d22 = {
|
|
|
54
54
|
REMOVE: "Remove",
|
|
55
55
|
TENTATIVE_REJECT: "TentativeReject",
|
|
56
56
|
TENTATIVE_ACCEPT: "TentativeAccept",
|
|
57
|
-
|
|
57
|
+
TRAVEL: "Travel",
|
|
58
58
|
UNDO: "Undo",
|
|
59
59
|
UPDATE: "Update",
|
|
60
60
|
VIEW: "View"
|
|
@@ -524,7 +524,8 @@ var $be88b298220210d1$export$2e2bcd8739ae039 = $be88b298220210d1$var$CommentsLis
|
|
|
524
524
|
|
|
525
525
|
|
|
526
526
|
|
|
527
|
-
|
|
527
|
+
|
|
528
|
+
const $577f4953dfa5de4f$export$e57ff0f701c44363 = (value)=>{
|
|
528
529
|
// If the field is null-ish, we suppose there are no values.
|
|
529
530
|
if (value === null || value === undefined) return [];
|
|
530
531
|
// Return as is.
|
|
@@ -534,30 +535,88 @@ const $e4e1b14e0441184d$export$e57ff0f701c44363 = (value)=>{
|
|
|
534
535
|
value
|
|
535
536
|
];
|
|
536
537
|
};
|
|
537
|
-
var $
|
|
538
|
-
arrayOf: $
|
|
538
|
+
var $577f4953dfa5de4f$export$2e2bcd8739ae039 = {
|
|
539
|
+
arrayOf: $577f4953dfa5de4f$export$e57ff0f701c44363
|
|
540
|
+
};
|
|
541
|
+
const $577f4953dfa5de4f$export$34aed805e991a647 = (iterable, predicate)=>{
|
|
542
|
+
const seen = new Set();
|
|
543
|
+
return iterable.filter((item)=>{
|
|
544
|
+
const key = predicate(item);
|
|
545
|
+
if (seen.has(key)) return false;
|
|
546
|
+
seen.add(key);
|
|
547
|
+
return true;
|
|
548
|
+
});
|
|
539
549
|
};
|
|
540
550
|
|
|
541
551
|
|
|
552
|
+
const $c1e897431d8c5742$var$useItemsFromPagesAndNotifications = (pages, notifications, dereferenceItems)=>{
|
|
553
|
+
const dataProvider = (0, $85cNH$useDataProvider)();
|
|
554
|
+
// Add all items from pages and process notifications (possibly new and deleted items).
|
|
555
|
+
const items = (0, $85cNH$useMemo)(()=>{
|
|
556
|
+
const addItems = notifications.map((n)=>n.type === "Add").map((n)=>n.object);
|
|
557
|
+
const removeItems = notifications.map((n)=>n.type === "Remove").map((n)=>n.object.id || n.object);
|
|
558
|
+
const currentItems = !pages ? [] : pages.flatMap((p)=>(0, $577f4953dfa5de4f$export$e57ff0f701c44363)(p.orderedItems || p.items));
|
|
559
|
+
const currentAndNew = currentItems.concat(addItems);
|
|
560
|
+
return currentAndNew// Filter out removed items.
|
|
561
|
+
.filter((item)=>!removeItems.some((r)=>(r.id ?? r) === (item.id ?? item)))// Filter duplicates.
|
|
562
|
+
.filter((item)=>currentAndNew.some((i2)=>(i2.id ?? i2) === (item.id ?? item)));
|
|
563
|
+
}, pages, notifications);
|
|
564
|
+
if (!dereferenceItems) return {
|
|
565
|
+
loadedItems: items,
|
|
566
|
+
isLoading: false,
|
|
567
|
+
isFetching: false
|
|
568
|
+
};
|
|
569
|
+
// Dereference all items, if they are not yet.
|
|
570
|
+
const itemQueries = (0, $85cNH$useQueries)(items.filter((item)=>typeof item === "string").map((itemUri)=>({
|
|
571
|
+
queryKey: [
|
|
572
|
+
"resource",
|
|
573
|
+
itemUri
|
|
574
|
+
],
|
|
575
|
+
queryFn: async ()=>(await dataProvider.fetch(itemUri)).json,
|
|
576
|
+
staleTime: Infinity
|
|
577
|
+
})));
|
|
578
|
+
// Put all loaded items together (might be dereferenced already, so concatenate).
|
|
579
|
+
const loadedItems = items.filter((item)=>typeof item !== "string").concat(itemQueries.flatMap((itemQuery)=>{
|
|
580
|
+
return itemQuery.isSuccess && itemQuery.data || [];
|
|
581
|
+
}));
|
|
582
|
+
console.log("loadedItems", loadedItems.length, "total", items.length);
|
|
583
|
+
const errors = itemQueries.filter((q)=>q.error);
|
|
584
|
+
return {
|
|
585
|
+
loadedItems: loadedItems,
|
|
586
|
+
isLoading: itemQueries.some((q)=>q.isLoading),
|
|
587
|
+
isFetching: itemQueries.some((q)=>q.isFetching),
|
|
588
|
+
errors: errors.length > 0 && errors
|
|
589
|
+
};
|
|
590
|
+
};
|
|
542
591
|
const $c1e897431d8c5742$var$useCollection = (predicateOrUrl, options = {})=>{
|
|
543
|
-
const { dereferenceItems: dereferenceItems = false } = options;
|
|
592
|
+
const { dereferenceItems: dereferenceItems = false, liveUpdates: liveUpdates = true } = options;
|
|
544
593
|
const { data: identity } = (0, $85cNH$useGetIdentity)();
|
|
545
|
-
const [items, setItems] = (0, $85cNH$useState)();
|
|
546
594
|
const [totalItems, setTotalItems] = (0, $85cNH$useState)();
|
|
595
|
+
const [notifications, setNotifications] = (0, $85cNH$useState)([]);
|
|
596
|
+
const [hasLiveUpdates, setHasLiveUpdates] = (0, $85cNH$useState)({
|
|
597
|
+
status: "disconnected",
|
|
598
|
+
error: undefined
|
|
599
|
+
});
|
|
547
600
|
const dataProvider = (0, $85cNH$useDataProvider)();
|
|
548
|
-
|
|
601
|
+
// Get collectionUrl from webId predicate or URL.
|
|
549
602
|
const collectionUrl = (0, $85cNH$useMemo)(()=>{
|
|
550
603
|
if (predicateOrUrl) {
|
|
551
604
|
if (predicateOrUrl.startsWith("http")) return predicateOrUrl;
|
|
552
605
|
if (identity?.webIdData) return identity?.webIdData?.[predicateOrUrl];
|
|
553
606
|
}
|
|
607
|
+
return undefined;
|
|
608
|
+
// throw new Error(`No URL available for useCollection: ${predicateOrUrl}.`);
|
|
554
609
|
}, [
|
|
555
610
|
identity,
|
|
556
611
|
predicateOrUrl
|
|
557
612
|
]);
|
|
613
|
+
// Fetch page of collection item references (if pageParam provided)
|
|
614
|
+
// or default to `collectionUrl` (which should give you the first page).
|
|
558
615
|
const fetchCollection = (0, $85cNH$useCallback)(async ({ pageParam: nextPageUrl })=>{
|
|
616
|
+
// Fetch page or first page (collectionUrl)
|
|
559
617
|
let { json: json } = await dataProvider.fetch(nextPageUrl || collectionUrl);
|
|
560
618
|
if (json.totalItems) setTotalItems(json.totalItems);
|
|
619
|
+
// If first page, handle this here.
|
|
561
620
|
if ((json.type === "OrderedCollection" || json.type === "Collection") && json.first) {
|
|
562
621
|
if (json.first?.items) {
|
|
563
622
|
if (json.first?.items.length === 0 && json.first?.next) // Special case where the first property is an object without items
|
|
@@ -566,17 +625,6 @@ const $c1e897431d8c5742$var$useCollection = (predicateOrUrl, options = {})=>{
|
|
|
566
625
|
} else // Fetch the first page
|
|
567
626
|
({ json: json } = await dataProvider.fetch(json.first));
|
|
568
627
|
}
|
|
569
|
-
// Force dereference of items
|
|
570
|
-
if (dereferenceItems) {
|
|
571
|
-
const itemPredicate = json.items ? "items" : "orderedItems";
|
|
572
|
-
json[itemPredicate] = json[itemPredicate] && await Promise.all((0, $e4e1b14e0441184d$export$e57ff0f701c44363)(json[itemPredicate]).map(async (item)=>{
|
|
573
|
-
if (typeof item === "string") {
|
|
574
|
-
const { json: json } = await dataProvider.fetch(item);
|
|
575
|
-
return json;
|
|
576
|
-
}
|
|
577
|
-
return item;
|
|
578
|
-
}));
|
|
579
|
-
}
|
|
580
628
|
return json;
|
|
581
629
|
}, [
|
|
582
630
|
dataProvider,
|
|
@@ -584,8 +632,9 @@ const $c1e897431d8c5742$var$useCollection = (predicateOrUrl, options = {})=>{
|
|
|
584
632
|
identity,
|
|
585
633
|
setTotalItems
|
|
586
634
|
]);
|
|
587
|
-
|
|
588
|
-
|
|
635
|
+
// Use infiniteQuery to handle pagination, fetching, etc.
|
|
636
|
+
const { data: data, error: collectionError, fetchNextPage: fetchNextPage, refetch: refetch, hasNextPage: hasNextPage, isLoading: isLoadingPage, isFetching: isFetchingPage, isFetchingNextPage: isFetchingNextPage } = (0, $85cNH$useInfiniteQuery)([
|
|
637
|
+
"collection",
|
|
589
638
|
{
|
|
590
639
|
collectionUrl: collectionUrl
|
|
591
640
|
}
|
|
@@ -594,63 +643,57 @@ const $c1e897431d8c5742$var$useCollection = (predicateOrUrl, options = {})=>{
|
|
|
594
643
|
getNextPageParam: (lastPage)=>lastPage.next,
|
|
595
644
|
getPreviousPageParam: (firstPage)=>firstPage.prev
|
|
596
645
|
});
|
|
646
|
+
// Put all items together in a list.
|
|
647
|
+
const { loadedItems: items, isLoading: isLoadingItems, isFetching: isFetchingItems, errors: itemErrors } = $c1e897431d8c5742$var$useItemsFromPagesAndNotifications(data?.pages, notifications, dereferenceItems);
|
|
648
|
+
// Notifications have been processed after hook call, so reset.
|
|
649
|
+
// useEffect(() => {
|
|
650
|
+
// setNotifications([]);
|
|
651
|
+
// }, [notifications])
|
|
652
|
+
// Live Updates
|
|
597
653
|
(0, $85cNH$useEffect)(()=>{
|
|
598
|
-
if (
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
const removeItem = (0, $85cNH$useCallback)((itemId)=>{
|
|
624
|
-
setItems((oldItems)=>oldItems.filter((item)=>typeof item === "string" ? item !== itemId : item.id !== itemId));
|
|
625
|
-
// TODO use queryClient.setQueryData to update items directly in react-query cache
|
|
626
|
-
setTimeout(()=>queryClient.refetchQueries([
|
|
627
|
-
"Collection",
|
|
628
|
-
{
|
|
629
|
-
collectionUrl: collectionUrl
|
|
630
|
-
}
|
|
631
|
-
], {
|
|
632
|
-
active: true,
|
|
633
|
-
exact: true
|
|
634
|
-
}), 2000);
|
|
654
|
+
if (liveUpdates && collectionUrl) // Create ws that listens to collectionUri changes
|
|
655
|
+
(0, $85cNH$getOrCreateWsChannel)(dataProvider.fetch, collectionUrl).then((webSocket)=>{
|
|
656
|
+
webSocket.addEventListener("message", (e)=>{
|
|
657
|
+
if (e.data && e.data.type === "Add" || e.data.type === "Remove") setNotifications([
|
|
658
|
+
...notifications,
|
|
659
|
+
e.data
|
|
660
|
+
]);
|
|
661
|
+
});
|
|
662
|
+
webSocket.addEventListener("error", (e)=>{
|
|
663
|
+
setHasLiveUpdates({
|
|
664
|
+
status: "error",
|
|
665
|
+
error: e
|
|
666
|
+
});
|
|
667
|
+
// TODO: Retry after a while
|
|
668
|
+
});
|
|
669
|
+
webSocket.addEventListener("close", (e)=>{
|
|
670
|
+
setHasLiveUpdates({
|
|
671
|
+
...hasLiveUpdates,
|
|
672
|
+
status: "disconnected"
|
|
673
|
+
});
|
|
674
|
+
});
|
|
675
|
+
setHasLiveUpdates({
|
|
676
|
+
status: "connected"
|
|
677
|
+
});
|
|
678
|
+
}).catch(()=>{}); // If it fails, we won't receive live updates. But that's okay.
|
|
635
679
|
}, [
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
680
|
+
collectionUrl,
|
|
681
|
+
liveUpdates,
|
|
682
|
+
dataProvider.fetch
|
|
639
683
|
]);
|
|
684
|
+
const allErrors = (0, $577f4953dfa5de4f$export$e57ff0f701c44363)(collectionError).concat((0, $577f4953dfa5de4f$export$e57ff0f701c44363)(itemErrors));
|
|
640
685
|
return {
|
|
641
686
|
items: items,
|
|
642
687
|
totalItems: totalItems,
|
|
643
|
-
error:
|
|
688
|
+
error: allErrors.length > 0 && allErrors,
|
|
644
689
|
refetch: refetch,
|
|
645
690
|
fetchNextPage: fetchNextPage,
|
|
646
691
|
hasNextPage: hasNextPage,
|
|
647
|
-
isLoading:
|
|
648
|
-
isFetching:
|
|
692
|
+
isLoading: isLoadingPage || isLoadingItems,
|
|
693
|
+
isFetching: isFetchingPage || isFetchingItems,
|
|
649
694
|
isFetchingNextPage: isFetchingNextPage,
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
removeItem: removeItem,
|
|
653
|
-
url: collectionUrl
|
|
695
|
+
url: collectionUrl,
|
|
696
|
+
hasLiveUpdates: hasLiveUpdates
|
|
654
697
|
};
|
|
655
698
|
};
|
|
656
699
|
var $c1e897431d8c5742$export$2e2bcd8739ae039 = $c1e897431d8c5742$var$useCollection;
|
|
@@ -737,6 +780,224 @@ var $ea214512ab1a2e8f$export$2e2bcd8739ae039 = $ea214512ab1a2e8f$var$ReferenceCo
|
|
|
737
780
|
|
|
738
781
|
|
|
739
782
|
|
|
783
|
+
|
|
784
|
+
const $8281f3ce3b9d6123$var$useItemsFromPages = (pages, dereferenceItems)=>{
|
|
785
|
+
const dataProvider = (0, $85cNH$useDataProvider)();
|
|
786
|
+
const items = (0, $85cNH$useMemo)(()=>pages.flatMap((p)=>(0, $577f4953dfa5de4f$export$e57ff0f701c44363)(p.orderedItems || p.items)), [
|
|
787
|
+
pages
|
|
788
|
+
]);
|
|
789
|
+
// We will force dereference, if some items are not URI string references.
|
|
790
|
+
const shouldDereference = (0, $85cNH$useMemo)(()=>{
|
|
791
|
+
return dereferenceItems || items.some((item)=>typeof item !== "string");
|
|
792
|
+
}, [
|
|
793
|
+
dereferenceItems,
|
|
794
|
+
items
|
|
795
|
+
]);
|
|
796
|
+
// Dereference all items, if necessary (even if shouldDereference is false, the hook needs to be called).
|
|
797
|
+
const itemQueries = (0, $85cNH$useQueries)(!shouldDereference ? [] : items.filter((item)=>typeof item === "string").map((itemUri)=>({
|
|
798
|
+
queryKey: [
|
|
799
|
+
"resource",
|
|
800
|
+
itemUri
|
|
801
|
+
],
|
|
802
|
+
queryFn: async ()=>(await dataProvider.fetch(itemUri)).json,
|
|
803
|
+
staleTime: Infinity
|
|
804
|
+
})));
|
|
805
|
+
if (!shouldDereference) return {
|
|
806
|
+
loadedItems: items,
|
|
807
|
+
isLoading: false,
|
|
808
|
+
isFetching: false
|
|
809
|
+
};
|
|
810
|
+
// Put all loaded items together (might be dereferenced already, so concatenate).
|
|
811
|
+
const loadedItems = items.filter((item)=>typeof item !== "string").concat(itemQueries.flatMap((itemQuery)=>{
|
|
812
|
+
return itemQuery.isSuccess && itemQuery.data || [];
|
|
813
|
+
}));
|
|
814
|
+
const errors = itemQueries.filter((q)=>q.error);
|
|
815
|
+
return {
|
|
816
|
+
loadedItems: loadedItems,
|
|
817
|
+
isLoading: itemQueries.some((q)=>q.isLoading),
|
|
818
|
+
isFetching: itemQueries.some((q)=>q.isFetching),
|
|
819
|
+
errors: errors.length > 0 ? errors : undefined
|
|
820
|
+
};
|
|
821
|
+
};
|
|
822
|
+
/**
|
|
823
|
+
* Subscribe a collection. Supports pagination.
|
|
824
|
+
* @param predicateOrUrl The collection URI or the predicate to get the collection URI from the identity (webId).
|
|
825
|
+
* @param {UseCollectionOptions} options Defaults to `{ dereferenceItems: false, liveUpdates: true }`
|
|
826
|
+
*/ const $8281f3ce3b9d6123$var$useCollection = (predicateOrUrl, options = {})=>{
|
|
827
|
+
const { dereferenceItems: dereferenceItems = false, liveUpdates: liveUpdates = true } = options;
|
|
828
|
+
const { data: identity } = (0, $85cNH$useGetIdentity)();
|
|
829
|
+
const [totalItems, setTotalItems] = (0, $85cNH$useState)();
|
|
830
|
+
const queryClient = (0, $85cNH$useQueryClient)();
|
|
831
|
+
const [hasLiveUpdates, setHasLiveUpdates] = (0, $85cNH$useState)({
|
|
832
|
+
status: "connecting"
|
|
833
|
+
});
|
|
834
|
+
const dataProvider = (0, $85cNH$useDataProvider)();
|
|
835
|
+
// Get collectionUrl from webId predicate or URL.
|
|
836
|
+
const collectionUrl = (0, $85cNH$useMemo)(()=>{
|
|
837
|
+
if (predicateOrUrl) {
|
|
838
|
+
if (predicateOrUrl.startsWith("http")) return predicateOrUrl;
|
|
839
|
+
if (identity?.webIdData) return identity?.webIdData?.[predicateOrUrl];
|
|
840
|
+
}
|
|
841
|
+
return undefined;
|
|
842
|
+
// throw new Error(`No URL available for useCollection: ${predicateOrUrl}.`);
|
|
843
|
+
}, [
|
|
844
|
+
identity,
|
|
845
|
+
predicateOrUrl
|
|
846
|
+
]);
|
|
847
|
+
// Fetch page of collection item references (if pageParam provided)
|
|
848
|
+
// or default to `collectionUrl` (which should give you the first page).
|
|
849
|
+
const fetchCollection = (0, $85cNH$useCallback)(async ({ pageParam: nextPageUrl })=>{
|
|
850
|
+
// Fetch page or first page (collectionUrl)
|
|
851
|
+
let { json: json } = await dataProvider.fetch(nextPageUrl || collectionUrl);
|
|
852
|
+
if (json.totalItems) setTotalItems(json.totalItems);
|
|
853
|
+
// If first page, handle this here.
|
|
854
|
+
if ((json.type === "OrderedCollection" || json.type === "Collection") && json.first) {
|
|
855
|
+
if (json.first?.items) {
|
|
856
|
+
if (json.first?.items.length === 0 && json.first?.next) // Special case where the first property is an object without items
|
|
857
|
+
({ json: json } = await dataProvider.fetch(json.first?.next));
|
|
858
|
+
else json = json.first;
|
|
859
|
+
} else // Fetch the first page
|
|
860
|
+
({ json: json } = await dataProvider.fetch(json.first));
|
|
861
|
+
}
|
|
862
|
+
return json;
|
|
863
|
+
}, [
|
|
864
|
+
dataProvider,
|
|
865
|
+
collectionUrl,
|
|
866
|
+
identity,
|
|
867
|
+
setTotalItems
|
|
868
|
+
]);
|
|
869
|
+
// Use infiniteQuery to handle pagination, fetching, etc.
|
|
870
|
+
const { data: pageData, error: collectionError, fetchNextPage: fetchNextPage, refetch: refetch, hasNextPage: hasNextPage, isLoading: isLoadingPage, isFetching: isFetchingPage, isFetchingNextPage: isFetchingNextPage } = (0, $85cNH$useInfiniteQuery)([
|
|
871
|
+
"collection",
|
|
872
|
+
{
|
|
873
|
+
collectionUrl: collectionUrl
|
|
874
|
+
}
|
|
875
|
+
], fetchCollection, {
|
|
876
|
+
enabled: !!(collectionUrl && identity?.id),
|
|
877
|
+
getNextPageParam: (lastPage)=>lastPage.next,
|
|
878
|
+
getPreviousPageParam: (firstPage)=>firstPage.prev
|
|
879
|
+
});
|
|
880
|
+
// Put all items together in a list (and dereference, if required).
|
|
881
|
+
const { loadedItems: items, isLoading: isLoadingItems, isFetching: isFetchingItems, errors: itemErrors } = $8281f3ce3b9d6123$var$useItemsFromPages(pageData?.pages ?? [], dereferenceItems);
|
|
882
|
+
const allErrors = (0, $577f4953dfa5de4f$export$e57ff0f701c44363)(collectionError).concat((0, $577f4953dfa5de4f$export$e57ff0f701c44363)(itemErrors));
|
|
883
|
+
const addItem = (0, $85cNH$useCallback)((item, shouldRefetch = true)=>{
|
|
884
|
+
queryClient.setQueryData([
|
|
885
|
+
"collection",
|
|
886
|
+
{
|
|
887
|
+
collectionUrl: collectionUrl
|
|
888
|
+
}
|
|
889
|
+
], (oldData)=>{
|
|
890
|
+
if (!oldData) return oldData;
|
|
891
|
+
setTotalItems(totalItems && totalItems + 1);
|
|
892
|
+
// Destructure, so react knows, it needs to re-render the pages.
|
|
893
|
+
const pages = [
|
|
894
|
+
...oldData.pages
|
|
895
|
+
];
|
|
896
|
+
const firstPageItems = pages?.[0]?.orderedItems || pages?.[0]?.items || [];
|
|
897
|
+
firstPageItems.unshift(item);
|
|
898
|
+
oldData.pages = pages;
|
|
899
|
+
return oldData;
|
|
900
|
+
});
|
|
901
|
+
if (shouldRefetch) setTimeout(()=>queryClient.refetchQueries([
|
|
902
|
+
"collection",
|
|
903
|
+
{
|
|
904
|
+
collectionUrl: collectionUrl
|
|
905
|
+
}
|
|
906
|
+
], {
|
|
907
|
+
active: true,
|
|
908
|
+
exact: true
|
|
909
|
+
}), typeof shouldRefetch === "number" ? shouldRefetch : 2000);
|
|
910
|
+
}, [
|
|
911
|
+
queryClient,
|
|
912
|
+
collectionUrl
|
|
913
|
+
]);
|
|
914
|
+
const removeItem = (0, $85cNH$useCallback)((item, shouldRefetch = true)=>{
|
|
915
|
+
queryClient.setQueryData([
|
|
916
|
+
"collection",
|
|
917
|
+
{
|
|
918
|
+
collectionUrl: collectionUrl
|
|
919
|
+
}
|
|
920
|
+
], (oldData)=>{
|
|
921
|
+
if (!oldData) return oldData;
|
|
922
|
+
setTotalItems(totalItems && totalItems - 1);
|
|
923
|
+
// Destructure, so react knows, it needs to re-render the pages array.
|
|
924
|
+
const pages = [
|
|
925
|
+
...oldData.pages
|
|
926
|
+
];
|
|
927
|
+
// Find the item in all pages and remove the item to be removed (either item.id or just item)
|
|
928
|
+
pages.forEach((page)=>{
|
|
929
|
+
if (page.orderedItems) page.orderedItems = page.orderedItems.filter((i)=>(i.id || i) !== (item.id || item));
|
|
930
|
+
else if (page.items) page.items = page.items.filter((i)=>(i.id || i) !== (item?.id || item));
|
|
931
|
+
});
|
|
932
|
+
oldData.pages = pages;
|
|
933
|
+
return oldData;
|
|
934
|
+
});
|
|
935
|
+
if (shouldRefetch) setTimeout(()=>queryClient.refetchQueries([
|
|
936
|
+
"collection",
|
|
937
|
+
{
|
|
938
|
+
collectionUrl: collectionUrl
|
|
939
|
+
}
|
|
940
|
+
], {
|
|
941
|
+
active: true,
|
|
942
|
+
exact: true
|
|
943
|
+
}), typeof shouldRefetch === "number" ? shouldRefetch : 2000);
|
|
944
|
+
}, [
|
|
945
|
+
queryClient,
|
|
946
|
+
collectionUrl
|
|
947
|
+
]);
|
|
948
|
+
// Live Updates
|
|
949
|
+
(0, $85cNH$useEffect)(()=>{
|
|
950
|
+
if (liveUpdates && collectionUrl) // Create ws that listens to collectionUri changes
|
|
951
|
+
(0, $85cNH$getOrCreateWsChannel)(dataProvider.fetch, collectionUrl).then((webSocket)=>{
|
|
952
|
+
webSocket.addEventListener("message", (e)=>{
|
|
953
|
+
const data = JSON.parse(e.data);
|
|
954
|
+
if (data && data.type === "Add") addItem(data.object, true);
|
|
955
|
+
else if (data && data.type === "Remove") removeItem(data.object, true);
|
|
956
|
+
});
|
|
957
|
+
webSocket.addEventListener("error", (e)=>{
|
|
958
|
+
setHasLiveUpdates({
|
|
959
|
+
status: "error",
|
|
960
|
+
error: e
|
|
961
|
+
});
|
|
962
|
+
// TODO: Retry after a while
|
|
963
|
+
});
|
|
964
|
+
webSocket.addEventListener("close", (e)=>{
|
|
965
|
+
if (!hasLiveUpdates.error) setHasLiveUpdates({
|
|
966
|
+
...hasLiveUpdates,
|
|
967
|
+
status: "closed"
|
|
968
|
+
});
|
|
969
|
+
});
|
|
970
|
+
setHasLiveUpdates({
|
|
971
|
+
status: "connected"
|
|
972
|
+
});
|
|
973
|
+
}).catch(()=>{}); // If it fails, we won't receive live updates. But that's okay.
|
|
974
|
+
}, [
|
|
975
|
+
collectionUrl,
|
|
976
|
+
liveUpdates,
|
|
977
|
+
dataProvider
|
|
978
|
+
]);
|
|
979
|
+
return {
|
|
980
|
+
items: items,
|
|
981
|
+
totalItems: totalItems,
|
|
982
|
+
error: allErrors.length > 0 && allErrors,
|
|
983
|
+
refetch: refetch,
|
|
984
|
+
fetchNextPage: fetchNextPage,
|
|
985
|
+
addItem: addItem,
|
|
986
|
+
removeItem: removeItem,
|
|
987
|
+
hasNextPage: hasNextPage,
|
|
988
|
+
isLoading: isLoadingPage || isLoadingItems,
|
|
989
|
+
isFetching: isFetchingPage || isFetchingItems,
|
|
990
|
+
isFetchingNextPage: isFetchingNextPage,
|
|
991
|
+
url: collectionUrl,
|
|
992
|
+
hasLiveUpdates: hasLiveUpdates
|
|
993
|
+
};
|
|
994
|
+
};
|
|
995
|
+
var $8281f3ce3b9d6123$export$2e2bcd8739ae039 = $8281f3ce3b9d6123$var$useCollection;
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
740
1001
|
const $542b37cc25b8ccca$var$useInbox = ()=>{
|
|
741
1002
|
const { data: identity } = (0, $85cNH$useGetIdentity)();
|
|
742
1003
|
const inboxUrl = (0, $85cNH$useMemo)(()=>{
|
|
@@ -1018,5 +1279,5 @@ var $51cccd331ea8b13d$export$2e2bcd8739ae039 = $51cccd331ea8b13d$var$useMentions
|
|
|
1018
1279
|
|
|
1019
1280
|
|
|
1020
1281
|
|
|
1021
|
-
export {$7ce737d4a1c88e63$export$2e2bcd8739ae039 as CommentsField, $d3be168cd1e7aaae$export$2e2bcd8739ae039 as CollectionList, $ea214512ab1a2e8f$export$2e2bcd8739ae039 as ReferenceCollectionField, $
|
|
1282
|
+
export {$7ce737d4a1c88e63$export$2e2bcd8739ae039 as CommentsField, $d3be168cd1e7aaae$export$2e2bcd8739ae039 as CollectionList, $ea214512ab1a2e8f$export$2e2bcd8739ae039 as ReferenceCollectionField, $8281f3ce3b9d6123$export$2e2bcd8739ae039 as useCollection, $542b37cc25b8ccca$export$2e2bcd8739ae039 as useInbox, $641e93142bcf5435$export$2e2bcd8739ae039 as useNodeinfo, $712f7f004b5f345e$export$2e2bcd8739ae039 as useOutbox, $2514c63dc8f4867c$export$2e2bcd8739ae039 as useWebfinger, $51cccd331ea8b13d$export$2e2bcd8739ae039 as useMentions, $338f387df48a40d7$export$1ec8e53e7d982d22 as ACTIVITY_TYPES, $338f387df48a40d7$export$9649665d7ccb0dc2 as ACTOR_TYPES, $338f387df48a40d7$export$c49cfb2681596b20 as OBJECT_TYPES, $338f387df48a40d7$export$4d8d554031975581 as PUBLIC_URI};
|
|
1022
1283
|
//# sourceMappingURL=index.es.js.map
|