@bitsocial/bitsocial-react-hooks 0.1.0
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/LICENSE +674 -0
- package/README.md +1365 -0
- package/dist/hooks/accounts/accounts.d.ts +64 -0
- package/dist/hooks/accounts/accounts.d.ts.map +1 -0
- package/dist/hooks/accounts/accounts.js +706 -0
- package/dist/hooks/accounts/accounts.js.map +1 -0
- package/dist/hooks/accounts/index.d.ts +2 -0
- package/dist/hooks/accounts/index.d.ts.map +1 -0
- package/dist/hooks/accounts/index.js +2 -0
- package/dist/hooks/accounts/index.js.map +1 -0
- package/dist/hooks/accounts/utils.d.ts +6 -0
- package/dist/hooks/accounts/utils.d.ts.map +1 -0
- package/dist/hooks/accounts/utils.js +226 -0
- package/dist/hooks/accounts/utils.js.map +1 -0
- package/dist/hooks/actions/actions.d.ts +19 -0
- package/dist/hooks/actions/actions.d.ts.map +1 -0
- package/dist/hooks/actions/actions.js +552 -0
- package/dist/hooks/actions/actions.js.map +1 -0
- package/dist/hooks/actions/index.d.ts +2 -0
- package/dist/hooks/actions/index.d.ts.map +1 -0
- package/dist/hooks/actions/index.js +2 -0
- package/dist/hooks/actions/index.js.map +1 -0
- package/dist/hooks/authors/author-avatars.d.ts +28 -0
- package/dist/hooks/authors/author-avatars.d.ts.map +1 -0
- package/dist/hooks/authors/author-avatars.js +191 -0
- package/dist/hooks/authors/author-avatars.js.map +1 -0
- package/dist/hooks/authors/authors.d.ts +37 -0
- package/dist/hooks/authors/authors.d.ts.map +1 -0
- package/dist/hooks/authors/authors.js +509 -0
- package/dist/hooks/authors/authors.js.map +1 -0
- package/dist/hooks/authors/index.d.ts +2 -0
- package/dist/hooks/authors/index.d.ts.map +1 -0
- package/dist/hooks/authors/index.js +2 -0
- package/dist/hooks/authors/index.js.map +1 -0
- package/dist/hooks/authors/utils.d.ts +4 -0
- package/dist/hooks/authors/utils.d.ts.map +1 -0
- package/dist/hooks/authors/utils.js +21 -0
- package/dist/hooks/authors/utils.js.map +1 -0
- package/dist/hooks/comments.d.ts +17 -0
- package/dist/hooks/comments.d.ts.map +1 -0
- package/dist/hooks/comments.js +351 -0
- package/dist/hooks/comments.js.map +1 -0
- package/dist/hooks/communities.d.ts +31 -0
- package/dist/hooks/communities.d.ts.map +1 -0
- package/dist/hooks/communities.js +389 -0
- package/dist/hooks/communities.js.map +1 -0
- package/dist/hooks/feeds/feeds.d.ts +18 -0
- package/dist/hooks/feeds/feeds.d.ts.map +1 -0
- package/dist/hooks/feeds/feeds.js +315 -0
- package/dist/hooks/feeds/feeds.js.map +1 -0
- package/dist/hooks/feeds/index.d.ts +2 -0
- package/dist/hooks/feeds/index.d.ts.map +1 -0
- package/dist/hooks/feeds/index.js +2 -0
- package/dist/hooks/feeds/index.js.map +1 -0
- package/dist/hooks/pkc-rpc.d.ts +7 -0
- package/dist/hooks/pkc-rpc.d.ts.map +1 -0
- package/dist/hooks/pkc-rpc.js +88 -0
- package/dist/hooks/pkc-rpc.js.map +1 -0
- package/dist/hooks/replies.d.ts +5 -0
- package/dist/hooks/replies.d.ts.map +1 -0
- package/dist/hooks/replies.js +155 -0
- package/dist/hooks/replies.js.map +1 -0
- package/dist/hooks/states.d.ts +15 -0
- package/dist/hooks/states.d.ts.map +1 -0
- package/dist/hooks/states.js +213 -0
- package/dist/hooks/states.js.map +1 -0
- package/dist/hooks/utils/use-interval.d.ts +3 -0
- package/dist/hooks/utils/use-interval.d.ts.map +1 -0
- package/dist/hooks/utils/use-interval.js +36 -0
- package/dist/hooks/utils/use-interval.js.map +1 -0
- package/dist/hooks/utils/use-previous.d.ts +1 -0
- package/dist/hooks/utils/use-previous.js +10 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +128 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/chain/chain.d.ts +36 -0
- package/dist/lib/chain/chain.d.ts.map +1 -0
- package/dist/lib/chain/chain.js +195 -0
- package/dist/lib/chain/chain.js.map +1 -0
- package/dist/lib/chain/index.d.ts +4 -0
- package/dist/lib/chain/index.d.ts.map +1 -0
- package/dist/lib/chain/index.js +4 -0
- package/dist/lib/chain/index.js.map +1 -0
- package/dist/lib/community-address.d.ts +6 -0
- package/dist/lib/community-address.d.ts.map +1 -0
- package/dist/lib/community-address.js +26 -0
- package/dist/lib/community-address.js.map +1 -0
- package/dist/lib/community-ref.d.ts +23 -0
- package/dist/lib/community-ref.d.ts.map +1 -0
- package/dist/lib/community-ref.js +113 -0
- package/dist/lib/community-ref.js.map +1 -0
- package/dist/lib/debug-utils.d.ts +9 -0
- package/dist/lib/debug-utils.d.ts.map +1 -0
- package/dist/lib/debug-utils.js +21 -0
- package/dist/lib/debug-utils.js.map +1 -0
- package/dist/lib/feed-sort-type.d.ts +2 -0
- package/dist/lib/feed-sort-type.d.ts.map +1 -0
- package/dist/lib/feed-sort-type.js +22 -0
- package/dist/lib/feed-sort-type.js.map +1 -0
- package/dist/lib/localforage-lru/index.d.ts +3 -0
- package/dist/lib/localforage-lru/index.d.ts.map +1 -0
- package/dist/lib/localforage-lru/index.js +46 -0
- package/dist/lib/localforage-lru/index.js.map +1 -0
- package/dist/lib/localforage-lru/localforage-lru.d.ts +6 -0
- package/dist/lib/localforage-lru/localforage-lru.d.ts.map +1 -0
- package/dist/lib/localforage-lru/localforage-lru.js +182 -0
- package/dist/lib/localforage-lru/localforage-lru.js.map +1 -0
- package/dist/lib/pkc-compat.d.ts +25 -0
- package/dist/lib/pkc-compat.d.ts.map +1 -0
- package/dist/lib/pkc-compat.js +131 -0
- package/dist/lib/pkc-compat.js.map +1 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.d.ts +3 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.d.ts.map +1 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.js +280 -0
- package/dist/lib/pkc-js/fixtures/markdown-example.js.map +1 -0
- package/dist/lib/pkc-js/index.d.ts +11 -0
- package/dist/lib/pkc-js/index.d.ts.map +1 -0
- package/dist/lib/pkc-js/index.js +85 -0
- package/dist/lib/pkc-js/index.js.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.d.ts +3 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.d.ts.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.js +1235 -0
- package/dist/lib/pkc-js/pkc-js-mock-content.js.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock.d.ts +137 -0
- package/dist/lib/pkc-js/pkc-js-mock.d.ts.map +1 -0
- package/dist/lib/pkc-js/pkc-js-mock.js +644 -0
- package/dist/lib/pkc-js/pkc-js-mock.js.map +1 -0
- package/dist/lib/polyfill.d.ts +3 -0
- package/dist/lib/polyfill.d.ts.map +1 -0
- package/dist/lib/polyfill.js +14 -0
- package/dist/lib/polyfill.js.map +1 -0
- package/dist/lib/protocol-compat.d.ts +14 -0
- package/dist/lib/protocol-compat.d.ts.map +1 -0
- package/dist/lib/protocol-compat.js +67 -0
- package/dist/lib/protocol-compat.js.map +1 -0
- package/dist/lib/test-utils.d.ts +29 -0
- package/dist/lib/test-utils.d.ts.map +1 -0
- package/dist/lib/test-utils.js +184 -0
- package/dist/lib/test-utils.js.map +1 -0
- package/dist/lib/utils/comment-moderation.d.ts +4 -0
- package/dist/lib/utils/comment-moderation.d.ts.map +1 -0
- package/dist/lib/utils/comment-moderation.js +56 -0
- package/dist/lib/utils/comment-moderation.js.map +1 -0
- package/dist/lib/utils/index.d.ts +4 -0
- package/dist/lib/utils/index.d.ts.map +1 -0
- package/dist/lib/utils/index.js +4 -0
- package/dist/lib/utils/index.js.map +1 -0
- package/dist/lib/utils/utils.d.ts +23 -0
- package/dist/lib/utils/utils.d.ts.map +1 -0
- package/dist/lib/utils/utils.js +375 -0
- package/dist/lib/utils/utils.js.map +1 -0
- package/dist/lib/validator.d.ts +30 -0
- package/dist/lib/validator.d.ts.map +1 -0
- package/dist/lib/validator.js +307 -0
- package/dist/lib/validator.js.map +1 -0
- package/dist/stores/accounts/account-generator.d.ts +51 -0
- package/dist/stores/accounts/account-generator.d.ts.map +1 -0
- package/dist/stores/accounts/account-generator.js +160 -0
- package/dist/stores/accounts/account-generator.js.map +1 -0
- package/dist/stores/accounts/accounts-actions-internal.d.ts +8 -0
- package/dist/stores/accounts/accounts-actions-internal.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-actions-internal.js +403 -0
- package/dist/stores/accounts/accounts-actions-internal.js.map +1 -0
- package/dist/stores/accounts/accounts-actions.d.ts +46 -0
- package/dist/stores/accounts/accounts-actions.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-actions.js +1341 -0
- package/dist/stores/accounts/accounts-actions.js.map +1 -0
- package/dist/stores/accounts/accounts-database.d.ts +34 -0
- package/dist/stores/accounts/accounts-database.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-database.js +685 -0
- package/dist/stores/accounts/accounts-database.js.map +1 -0
- package/dist/stores/accounts/accounts-store.d.ts +32 -0
- package/dist/stores/accounts/accounts-store.d.ts.map +1 -0
- package/dist/stores/accounts/accounts-store.js +169 -0
- package/dist/stores/accounts/accounts-store.js.map +1 -0
- package/dist/stores/accounts/index.d.ts +4 -0
- package/dist/stores/accounts/index.d.ts.map +1 -0
- package/dist/stores/accounts/index.js +4 -0
- package/dist/stores/accounts/index.js.map +1 -0
- package/dist/stores/accounts/utils.d.ts +49 -0
- package/dist/stores/accounts/utils.d.ts.map +1 -0
- package/dist/stores/accounts/utils.js +419 -0
- package/dist/stores/accounts/utils.js.map +1 -0
- package/dist/stores/authors-comments/authors-comments-store.d.ts +37 -0
- package/dist/stores/authors-comments/authors-comments-store.d.ts.map +1 -0
- package/dist/stores/authors-comments/authors-comments-store.js +338 -0
- package/dist/stores/authors-comments/authors-comments-store.js.map +1 -0
- package/dist/stores/authors-comments/index.d.ts +4 -0
- package/dist/stores/authors-comments/index.d.ts.map +1 -0
- package/dist/stores/authors-comments/index.js +4 -0
- package/dist/stores/authors-comments/index.js.map +1 -0
- package/dist/stores/authors-comments/utils.d.ts +14 -0
- package/dist/stores/authors-comments/utils.d.ts.map +1 -0
- package/dist/stores/authors-comments/utils.js +81 -0
- package/dist/stores/authors-comments/utils.js.map +1 -0
- package/dist/stores/comments/comments-store.d.ts +19 -0
- package/dist/stores/comments/comments-store.d.ts.map +1 -0
- package/dist/stores/comments/comments-store.js +385 -0
- package/dist/stores/comments/comments-store.js.map +1 -0
- package/dist/stores/comments/index.d.ts +4 -0
- package/dist/stores/comments/index.d.ts.map +1 -0
- package/dist/stores/comments/index.js +4 -0
- package/dist/stores/comments/index.js.map +1 -0
- package/dist/stores/communities/communities-store.d.ts +17 -0
- package/dist/stores/communities/communities-store.d.ts.map +1 -0
- package/dist/stores/communities/communities-store.js +304 -0
- package/dist/stores/communities/communities-store.js.map +1 -0
- package/dist/stores/communities/index.d.ts +4 -0
- package/dist/stores/communities/index.d.ts.map +1 -0
- package/dist/stores/communities/index.js +4 -0
- package/dist/stores/communities/index.js.map +1 -0
- package/dist/stores/communities-pages/communities-pages-store.d.ts +23 -0
- package/dist/stores/communities-pages/communities-pages-store.d.ts.map +1 -0
- package/dist/stores/communities-pages/communities-pages-store.js +316 -0
- package/dist/stores/communities-pages/communities-pages-store.js.map +1 -0
- package/dist/stores/communities-pages/index.d.ts +4 -0
- package/dist/stores/communities-pages/index.d.ts.map +1 -0
- package/dist/stores/communities-pages/index.js +4 -0
- package/dist/stores/communities-pages/index.js.map +1 -0
- package/dist/stores/feeds/feed-sorter.d.ts +5 -0
- package/dist/stores/feeds/feed-sorter.d.ts.map +1 -0
- package/dist/stores/feeds/feed-sorter.js +135 -0
- package/dist/stores/feeds/feed-sorter.js.map +1 -0
- package/dist/stores/feeds/feeds-store.d.ts +25 -0
- package/dist/stores/feeds/feeds-store.d.ts.map +1 -0
- package/dist/stores/feeds/feeds-store.js +459 -0
- package/dist/stores/feeds/feeds-store.js.map +1 -0
- package/dist/stores/feeds/index.d.ts +4 -0
- package/dist/stores/feeds/index.d.ts.map +1 -0
- package/dist/stores/feeds/index.js +4 -0
- package/dist/stores/feeds/index.js.map +1 -0
- package/dist/stores/feeds/utils.d.ts +43 -0
- package/dist/stores/feeds/utils.d.ts.map +1 -0
- package/dist/stores/feeds/utils.js +736 -0
- package/dist/stores/feeds/utils.js.map +1 -0
- package/dist/stores/replies/index.d.ts +4 -0
- package/dist/stores/replies/index.d.ts.map +1 -0
- package/dist/stores/replies/index.js +4 -0
- package/dist/stores/replies/index.js.map +1 -0
- package/dist/stores/replies/replies-comments-store.d.ts +8 -0
- package/dist/stores/replies/replies-comments-store.d.ts.map +1 -0
- package/dist/stores/replies/replies-comments-store.js +23 -0
- package/dist/stores/replies/replies-comments-store.js.map +1 -0
- package/dist/stores/replies/replies-store.d.ts +29 -0
- package/dist/stores/replies/replies-store.d.ts.map +1 -0
- package/dist/stores/replies/replies-store.js +413 -0
- package/dist/stores/replies/replies-store.js.map +1 -0
- package/dist/stores/replies/utils.d.ts +25 -0
- package/dist/stores/replies/utils.d.ts.map +1 -0
- package/dist/stores/replies/utils.js +549 -0
- package/dist/stores/replies/utils.js.map +1 -0
- package/dist/stores/replies-pages/index.d.ts +4 -0
- package/dist/stores/replies-pages/index.d.ts.map +1 -0
- package/dist/stores/replies-pages/index.js +4 -0
- package/dist/stores/replies-pages/index.js.map +1 -0
- package/dist/stores/replies-pages/replies-pages-store.d.ts +20 -0
- package/dist/stores/replies-pages/replies-pages-store.d.ts.map +1 -0
- package/dist/stores/replies-pages/replies-pages-store.js +270 -0
- package/dist/stores/replies-pages/replies-pages-store.js.map +1 -0
- package/dist/stores/replies-pages/utils.d.ts +3 -0
- package/dist/stores/replies-pages/utils.d.ts.map +1 -0
- package/dist/stores/replies-pages/utils.js +43 -0
- package/dist/stores/replies-pages/utils.js.map +1 -0
- package/dist/types.d.ts +638 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +160 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/stores/replies-pages/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,uBAAuB,CAAC;AACtD,cAAc,uBAAuB,CAAC;AACtC,eAAe,iBAAiB,CAAC","sourcesContent":["import repliesPagesStore from \"./replies-pages-store\";\nexport * from \"./replies-pages-store\";\nexport default repliesPagesStore;\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import Logger from "@pkcprotocol/pkc-logger";
|
|
2
|
+
export declare const log: Logger;
|
|
3
|
+
import { RepliesPages, Comment, Comments } from "../../types.js";
|
|
4
|
+
type RepliesPagesState = {
|
|
5
|
+
repliesPages: RepliesPages;
|
|
6
|
+
comments: Comments;
|
|
7
|
+
addNextRepliesPageToStore: Function;
|
|
8
|
+
addRepliesPageCommentsToStore: Function;
|
|
9
|
+
};
|
|
10
|
+
declare const repliesPagesStore: import("zustand").UseBoundStore<import("zustand").StoreApi<RepliesPagesState>>;
|
|
11
|
+
/**
|
|
12
|
+
* Util function to get all pages in the store for a
|
|
13
|
+
* specific comment+sortType using `RepliesPage.nextCid`
|
|
14
|
+
*/
|
|
15
|
+
export declare const getRepliesPages: (comment: Comment, sortType: string, repliesPages: RepliesPages) => import("../../types.js").CommunityPage[];
|
|
16
|
+
export declare const getRepliesFirstPageCid: (comment: Comment, sortType: string) => any;
|
|
17
|
+
export declare const resetRepliesPagesStore: () => Promise<void>;
|
|
18
|
+
export declare const resetRepliesPagesDatabaseAndStore: () => Promise<void>;
|
|
19
|
+
export default repliesPagesStore;
|
|
20
|
+
//# sourceMappingURL=replies-pages-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replies-pages-store.d.ts","sourceRoot":"","sources":["../../../src/stores/replies-pages/replies-pages-store.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAE7C,eAAO,MAAM,GAAG,QAAiD,CAAC;AAClE,OAAO,EAAe,YAAY,EAAW,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAgBpF,KAAK,iBAAiB,GAAG;IACvB,YAAY,EAAE,YAAY,CAAC;IAC3B,QAAQ,EAAE,QAAQ,CAAC;IACnB,yBAAyB,EAAE,QAAQ,CAAC;IACpC,6BAA6B,EAAE,QAAQ,CAAC;CACzC,CAAC;AAEF,QAAA,MAAM,iBAAiB,gFAsKtB,CAAC;AA6DF;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,SAAS,OAAO,EAAE,UAAU,MAAM,EAAE,cAAc,YAAY,0CA2B7F,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAAI,SAAS,OAAO,EAAE,UAAU,MAAM,QAcxE,CAAC;AAKF,eAAO,MAAM,sBAAsB,qBAQlC,CAAC;AAGF,eAAO,MAAM,iCAAiC,qBAG7C,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import utils from "../../lib/utils/index.js";
|
|
11
|
+
import Logger from "@pkcprotocol/pkc-logger";
|
|
12
|
+
// include replies pages store with feeds for debugging
|
|
13
|
+
export const log = Logger("bitsocial-react-hooks:replies:stores");
|
|
14
|
+
import accountsStore from "../accounts/index.js";
|
|
15
|
+
import commentsStore from "../comments/index.js";
|
|
16
|
+
import { addChildrenRepliesFeedsToAddToStore } from "./utils.js";
|
|
17
|
+
import localForageLru from "../../lib/localforage-lru/index.js";
|
|
18
|
+
import createStore from "zustand";
|
|
19
|
+
import assert from "assert";
|
|
20
|
+
const repliesPagesDatabase = localForageLru.createInstance({
|
|
21
|
+
name: "bitsocialReactHooks-repliesPages",
|
|
22
|
+
size: 500,
|
|
23
|
+
});
|
|
24
|
+
// reset all event listeners in between tests
|
|
25
|
+
const listeners = [];
|
|
26
|
+
const repliesPagesStore = createStore((setState, getState) => ({
|
|
27
|
+
// TODO: eventually clear old pages and comments from memory
|
|
28
|
+
repliesPages: {},
|
|
29
|
+
comments: {},
|
|
30
|
+
addNextRepliesPageToStore: (comment, sortType, account) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
assert((comment === null || comment === void 0 ? void 0 : comment.cid) && typeof (comment === null || comment === void 0 ? void 0 : comment.cid) === "string", `repliesPagesStore.addNextRepliesPageToStore comment '${comment}' invalid`);
|
|
33
|
+
assert(sortType && typeof sortType === "string", `repliesPagesStore.addNextRepliesPageToStore sortType '${sortType}' invalid`);
|
|
34
|
+
assert(typeof ((_a = account === null || account === void 0 ? void 0 : account.pkc) === null || _a === void 0 ? void 0 : _a.createComment) === "function", `repliesPagesStore.addNextRepliesPageToStore account '${account}' invalid`);
|
|
35
|
+
// check the preloaded replies on comment.replies.pages first, then the comment.replies.pageCids
|
|
36
|
+
const repliesFirstPageCid = getRepliesFirstPageCid(comment, sortType);
|
|
37
|
+
if (!repliesFirstPageCid) {
|
|
38
|
+
log(`repliesPagesStore.addNextRepliesPageToStore comment '${comment === null || comment === void 0 ? void 0 : comment.cid}' sortType '${sortType}' no repliesFirstPageCid`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// all replies pages in store
|
|
42
|
+
const repliesPagesStore = getState();
|
|
43
|
+
// only specific pages of the comment+sortType
|
|
44
|
+
const repliesPages = getRepliesPages(comment, sortType, repliesPagesStore.repliesPages);
|
|
45
|
+
// if no pages exist yet, add the first page
|
|
46
|
+
let pageCidToAdd;
|
|
47
|
+
if (!repliesPages.length) {
|
|
48
|
+
pageCidToAdd = repliesFirstPageCid;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const nextCid = (_b = repliesPages[repliesPages.length - 1]) === null || _b === void 0 ? void 0 : _b.nextCid;
|
|
52
|
+
// if last nextCid is undefined, reached end of pages
|
|
53
|
+
if (!nextCid) {
|
|
54
|
+
log.trace("repliesPagesStore.addNextRepliesPageToStore no more pages", {
|
|
55
|
+
commentCid: comment.cid,
|
|
56
|
+
sortType,
|
|
57
|
+
account,
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
pageCidToAdd = nextCid;
|
|
62
|
+
}
|
|
63
|
+
// page is already added or pending
|
|
64
|
+
if (repliesPagesStore.repliesPages[pageCidToAdd] ||
|
|
65
|
+
fetchPagePending[account.id + pageCidToAdd]) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
fetchPagePending[account.id + pageCidToAdd] = true;
|
|
69
|
+
let page;
|
|
70
|
+
try {
|
|
71
|
+
page = yield fetchPage(pageCidToAdd, comment, account);
|
|
72
|
+
log.trace("repliesPagesStore.addNextRepliesPageToStore comment.replies.getPage", {
|
|
73
|
+
pageCid: pageCidToAdd,
|
|
74
|
+
page,
|
|
75
|
+
commentCid: comment.cid,
|
|
76
|
+
communityAddress: comment.communityAddress,
|
|
77
|
+
account,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
fetchPagePending[account.id + pageCidToAdd] = false;
|
|
85
|
+
}
|
|
86
|
+
// find new comments in the page (missing-or-fresher: insert when absent or incoming is fresher)
|
|
87
|
+
const flattenedComments = utils.flattenCommentsPages(page);
|
|
88
|
+
const { comments } = getState();
|
|
89
|
+
let hasNewComments = false;
|
|
90
|
+
const newComments = {};
|
|
91
|
+
for (const comment of flattenedComments) {
|
|
92
|
+
if (!comment.cid)
|
|
93
|
+
continue;
|
|
94
|
+
const existing = comments[comment.cid];
|
|
95
|
+
const incomingFresh = Math.max(comment.updatedAt || 0, comment.timestamp || 0, 0);
|
|
96
|
+
const existingFresh = existing
|
|
97
|
+
? Math.max(existing.updatedAt || 0, existing.timestamp || 0, 0)
|
|
98
|
+
: -1;
|
|
99
|
+
if (!existing || incomingFresh > existingFresh) {
|
|
100
|
+
// don't clone the comment to save memory, comments remain a pointer to the page object
|
|
101
|
+
newComments[comment.cid] = comment;
|
|
102
|
+
hasNewComments = true;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// add missing children replies feeds
|
|
106
|
+
addChildrenRepliesFeedsToAddToStore(page, comment);
|
|
107
|
+
setState(({ repliesPages, comments }) => {
|
|
108
|
+
const newState = { repliesPages: Object.assign(Object.assign({}, repliesPages), { [pageCidToAdd]: page }) };
|
|
109
|
+
if (hasNewComments) {
|
|
110
|
+
newState.comments = Object.assign(Object.assign({}, comments), newComments);
|
|
111
|
+
}
|
|
112
|
+
return newState;
|
|
113
|
+
});
|
|
114
|
+
log("repliesPagesStore.addNextRepliesPageToStore", {
|
|
115
|
+
pageCid: pageCidToAdd,
|
|
116
|
+
commentCid: comment.cid,
|
|
117
|
+
sortType,
|
|
118
|
+
page,
|
|
119
|
+
account,
|
|
120
|
+
});
|
|
121
|
+
// when publishing a comment, you don't yet know its CID
|
|
122
|
+
// so when a new comment is fetched, check to see if it's your own
|
|
123
|
+
// comment, and if yes, add the CID to your account comments database
|
|
124
|
+
for (const comment of flattenedComments) {
|
|
125
|
+
accountsStore
|
|
126
|
+
.getState()
|
|
127
|
+
.accountsActionsInternal.addCidToAccountComment(comment)
|
|
128
|
+
.catch((error) => log.error("repliesPagesStore.addNextRepliesPageToStore addCidToAccountComment error", {
|
|
129
|
+
comment,
|
|
130
|
+
error,
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
}),
|
|
134
|
+
// comments contain preloaded pages, those page comments must be added separately
|
|
135
|
+
addRepliesPageCommentsToStore: (comment) => {
|
|
136
|
+
var _a;
|
|
137
|
+
if (!((_a = comment.replies) === null || _a === void 0 ? void 0 : _a.pages)) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// find new comments in the page (missing-or-fresher: insert when absent or incoming is fresher)
|
|
141
|
+
const flattenedComments = utils.flattenCommentsPages(comment.replies.pages);
|
|
142
|
+
const { comments } = getState();
|
|
143
|
+
let hasNewComments = false;
|
|
144
|
+
const newComments = {};
|
|
145
|
+
for (const c of flattenedComments) {
|
|
146
|
+
if (!c.cid)
|
|
147
|
+
continue;
|
|
148
|
+
const existing = comments[c.cid];
|
|
149
|
+
const incomingFresh = Math.max(c.updatedAt || 0, c.timestamp || 0, 0);
|
|
150
|
+
const existingFresh = existing
|
|
151
|
+
? Math.max(existing.updatedAt || 0, existing.timestamp || 0, 0)
|
|
152
|
+
: -1;
|
|
153
|
+
if (!existing || incomingFresh > existingFresh) {
|
|
154
|
+
// don't clone the comment to save memory, comments remain a pointer to the page object
|
|
155
|
+
newComments[c.cid] = c;
|
|
156
|
+
hasNewComments = true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (!hasNewComments) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
setState(({ comments }) => {
|
|
163
|
+
return { comments: Object.assign(Object.assign({}, comments), newComments) };
|
|
164
|
+
});
|
|
165
|
+
log("repliesPagesStore.addRepliesPageCommentsToStore", { comment, newComments });
|
|
166
|
+
},
|
|
167
|
+
}));
|
|
168
|
+
// set clients states on comments store so the frontend can display it, dont persist in db because a reload cancels updating
|
|
169
|
+
const onCommentRepliesClientsStateChange = (commentCid) => (clientState, clientType, sortType, clientUrl) => {
|
|
170
|
+
commentsStore.setState((state) => {
|
|
171
|
+
// make sure not undefined, sometimes happens in e2e tests
|
|
172
|
+
if (!state.comments[commentCid]) {
|
|
173
|
+
return {};
|
|
174
|
+
}
|
|
175
|
+
const client = { state: clientState };
|
|
176
|
+
const comment = Object.assign({}, state.comments[commentCid]);
|
|
177
|
+
comment.replies = Object.assign({}, comment.replies);
|
|
178
|
+
comment.replies.clients = Object.assign({}, comment.replies.clients);
|
|
179
|
+
comment.replies.clients[clientType] = Object.assign({}, comment.replies.clients[clientType]);
|
|
180
|
+
comment.replies.clients[clientType][sortType] = Object.assign({}, comment.replies.clients[clientType][sortType]);
|
|
181
|
+
comment.replies.clients[clientType][sortType][clientUrl] = client;
|
|
182
|
+
return { comments: Object.assign(Object.assign({}, state.comments), { [commentCid]: comment }) };
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
const fetchPageComments = {}; // cache pkc.createComment because sometimes it's slow
|
|
186
|
+
let fetchPagePending = {};
|
|
187
|
+
const fetchPage = (pageCid, comment, account) => __awaiter(void 0, void 0, void 0, function* () {
|
|
188
|
+
var _a;
|
|
189
|
+
// replies page is cached
|
|
190
|
+
const cachedRepliesPage = yield repliesPagesDatabase.getItem(pageCid);
|
|
191
|
+
if (cachedRepliesPage) {
|
|
192
|
+
return cachedRepliesPage;
|
|
193
|
+
}
|
|
194
|
+
if (!fetchPageComments[comment.cid]) {
|
|
195
|
+
fetchPageComments[comment.cid] = yield account.pkc.createComment({
|
|
196
|
+
cid: comment.cid,
|
|
197
|
+
postCid: comment.postCid,
|
|
198
|
+
communityAddress: comment.communityAddress,
|
|
199
|
+
depth: comment.depth,
|
|
200
|
+
});
|
|
201
|
+
listeners.push(fetchPageComments[comment.cid]);
|
|
202
|
+
// set clients states on communities store so the frontend can display it
|
|
203
|
+
utils.pageClientsOnStateChange((_a = fetchPageComments[comment.cid].replies) === null || _a === void 0 ? void 0 : _a.clients, onCommentRepliesClientsStateChange(comment.cid));
|
|
204
|
+
}
|
|
205
|
+
const onError = (error) => log.error(`repliesPagesStore comment '${comment.cid}' failed comment.replies.getPage page cid '${pageCid}':`, error);
|
|
206
|
+
const fetchedRepliesPage = yield utils.retryInfinity(() => fetchPageComments[comment.cid].replies.getPage({ cid: pageCid }), { onError });
|
|
207
|
+
yield repliesPagesDatabase.setItem(pageCid, utils.clone(fetchedRepliesPage));
|
|
208
|
+
return fetchedRepliesPage;
|
|
209
|
+
});
|
|
210
|
+
/**
|
|
211
|
+
* Util function to get all pages in the store for a
|
|
212
|
+
* specific comment+sortType using `RepliesPage.nextCid`
|
|
213
|
+
*/
|
|
214
|
+
export const getRepliesPages = (comment, sortType, repliesPages) => {
|
|
215
|
+
var _a;
|
|
216
|
+
assert(repliesPages && typeof repliesPages === "object", `getRepliesPages repliesPages '${repliesPages}' invalid`);
|
|
217
|
+
const pages = [];
|
|
218
|
+
const firstPageCid = getRepliesFirstPageCid(comment, sortType);
|
|
219
|
+
// comment has no pages
|
|
220
|
+
// TODO: if a loaded comment doesn't have a first page, it's unclear what we should do
|
|
221
|
+
// should we try to use another sort type by default, like 'best', or should we just ignore it?
|
|
222
|
+
// 'return pages' to ignore it for now
|
|
223
|
+
if (!firstPageCid) {
|
|
224
|
+
return pages;
|
|
225
|
+
}
|
|
226
|
+
const firstPage = repliesPages[firstPageCid];
|
|
227
|
+
if (!firstPage) {
|
|
228
|
+
return pages;
|
|
229
|
+
}
|
|
230
|
+
pages.push(firstPage);
|
|
231
|
+
while (true) {
|
|
232
|
+
const nextCid = (_a = pages[pages.length - 1]) === null || _a === void 0 ? void 0 : _a.nextCid;
|
|
233
|
+
const repliesPage = nextCid && repliesPages[nextCid];
|
|
234
|
+
if (!repliesPage) {
|
|
235
|
+
return pages;
|
|
236
|
+
}
|
|
237
|
+
pages.push(repliesPage);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
export const getRepliesFirstPageCid = (comment, sortType) => {
|
|
241
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
242
|
+
assert(comment === null || comment === void 0 ? void 0 : comment.cid, `getRepliesFirstPageCid comment '${comment}' invalid`);
|
|
243
|
+
assert(sortType && typeof sortType === "string", `getRepliesFirstPageCid sortType '${sortType}' invalid`);
|
|
244
|
+
// comment has preloaded replies for sort type
|
|
245
|
+
if ((_c = (_b = (_a = comment.replies) === null || _a === void 0 ? void 0 : _a.pages) === null || _b === void 0 ? void 0 : _b[sortType]) === null || _c === void 0 ? void 0 : _c.comments) {
|
|
246
|
+
return (_f = (_e = (_d = comment.replies) === null || _d === void 0 ? void 0 : _d.pages) === null || _e === void 0 ? void 0 : _e[sortType]) === null || _f === void 0 ? void 0 : _f.nextCid;
|
|
247
|
+
}
|
|
248
|
+
return (_h = (_g = comment.replies) === null || _g === void 0 ? void 0 : _g.pageCids) === null || _h === void 0 ? void 0 : _h[sortType];
|
|
249
|
+
// TODO: if a loaded comment doesn't have a first page, it's unclear what we should do
|
|
250
|
+
// should we try to use another sort type by default, like 'best', or should we just ignore it?
|
|
251
|
+
};
|
|
252
|
+
// reset store in between tests
|
|
253
|
+
const originalState = repliesPagesStore.getState();
|
|
254
|
+
// async function because some stores have async init
|
|
255
|
+
export const resetRepliesPagesStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
256
|
+
fetchPagePending = {};
|
|
257
|
+
// remove all event listeners
|
|
258
|
+
listeners.forEach((listener) => listener.removeAllListeners());
|
|
259
|
+
// destroy all component subscriptions to the store
|
|
260
|
+
repliesPagesStore.destroy();
|
|
261
|
+
// restore original state
|
|
262
|
+
repliesPagesStore.setState(originalState);
|
|
263
|
+
});
|
|
264
|
+
// reset database and store in between tests
|
|
265
|
+
export const resetRepliesPagesDatabaseAndStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
266
|
+
yield localForageLru.createInstance({ name: "bitsocialReactHooks-repliesPages" }).clear();
|
|
267
|
+
yield resetRepliesPagesStore();
|
|
268
|
+
});
|
|
269
|
+
export default repliesPagesStore;
|
|
270
|
+
//# sourceMappingURL=replies-pages-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replies-pages-store.js","sourceRoot":"","sources":["../../../src/stores/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,OAAO,QAAQ,KAAK,QAAQ,EACxC,oCAAoC,QAAQ,WAAW,CACxD,CAAC;IACF,8CAA8C;IAC9C,IAAI,MAAA,MAAA,MAAA,OAAO,CAAC,OAAO,0CAAE,KAAK,0CAAG,QAAQ,CAAC,0CAAE,QAAQ,EAAE,CAAC;QACjD,OAAO,MAAA,MAAA,MAAA,OAAO,CAAC,OAAO,0CAAE,KAAK,0CAAG,QAAQ,CAAC,0CAAE,OAAO,CAAC;IACrD,CAAC;IACD,OAAO,MAAA,MAAA,OAAO,CAAC,OAAO,0CAAE,QAAQ,0CAAG,QAAQ,CAAC,CAAC;IAE7C,sFAAsF;IACtF,+FAA+F;AACjG,CAAC,CAAC;AAEF,+BAA+B;AAC/B,MAAM,aAAa,GAAG,iBAAiB,CAAC,QAAQ,EAAE,CAAC;AACnD,qDAAqD;AACrD,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAS,EAAE;IAC/C,gBAAgB,GAAG,EAAE,CAAC;IACtB,6BAA6B;IAC7B,SAAS,CAAC,OAAO,CAAC,CAAC,QAAa,EAAE,EAAE,CAAC,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAC;IACpE,mDAAmD;IACnD,iBAAiB,CAAC,OAAO,EAAE,CAAC;IAC5B,yBAAyB;IACzB,iBAAiB,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;AAC5C,CAAC,CAAA,CAAC;AAEF,4CAA4C;AAC5C,MAAM,CAAC,MAAM,iCAAiC,GAAG,GAAS,EAAE;IAC1D,MAAM,cAAc,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,kCAAkC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;IAC1F,MAAM,sBAAsB,EAAE,CAAC;AACjC,CAAC,CAAA,CAAC;AAEF,eAAe,iBAAiB,CAAC","sourcesContent":["import utils from \"../../lib/utils\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\n// include replies pages store with feeds for debugging\nexport const log = Logger(\"bitsocial-react-hooks:replies:stores\");\nimport { RepliesPage, RepliesPages, Account, Comment, Comments } from \"../../types\";\nimport accountsStore from \"../accounts\";\nimport commentsStore, { CommentsState } from \"../comments\";\nimport { addChildrenRepliesFeedsToAddToStore } from \"./utils\";\nimport localForageLru from \"../../lib/localforage-lru\";\nimport createStore from \"zustand\";\nimport assert from \"assert\";\n\nconst repliesPagesDatabase = localForageLru.createInstance({\n name: \"bitsocialReactHooks-repliesPages\",\n size: 500,\n});\n\n// reset all event listeners in between tests\nconst listeners: any = [];\n\ntype RepliesPagesState = {\n repliesPages: RepliesPages;\n comments: Comments;\n addNextRepliesPageToStore: Function;\n addRepliesPageCommentsToStore: Function;\n};\n\nconst repliesPagesStore = createStore<RepliesPagesState>(\n (setState: Function, getState: Function) => ({\n // TODO: eventually clear old pages and comments from memory\n repliesPages: {},\n comments: {},\n\n addNextRepliesPageToStore: async (comment: Comment, sortType: string, account: Account) => {\n assert(\n comment?.cid && typeof comment?.cid === \"string\",\n `repliesPagesStore.addNextRepliesPageToStore comment '${comment}' invalid`,\n );\n assert(\n sortType && typeof sortType === \"string\",\n `repliesPagesStore.addNextRepliesPageToStore sortType '${sortType}' invalid`,\n );\n assert(\n typeof account?.pkc?.createComment === \"function\",\n `repliesPagesStore.addNextRepliesPageToStore account '${account}' invalid`,\n );\n\n // check the preloaded replies on comment.replies.pages first, then the comment.replies.pageCids\n const repliesFirstPageCid = getRepliesFirstPageCid(comment, sortType);\n if (!repliesFirstPageCid) {\n log(\n `repliesPagesStore.addNextRepliesPageToStore comment '${comment?.cid}' sortType '${sortType}' no repliesFirstPageCid`,\n );\n return;\n }\n\n // all replies pages in store\n const repliesPagesStore = getState();\n // only specific pages of the comment+sortType\n const repliesPages = getRepliesPages(comment, sortType, repliesPagesStore.repliesPages);\n\n // if no pages exist yet, add the first page\n let pageCidToAdd: string;\n if (!repliesPages.length) {\n pageCidToAdd = repliesFirstPageCid;\n } else {\n const nextCid = repliesPages[repliesPages.length - 1]?.nextCid;\n // if last nextCid is undefined, reached end of pages\n if (!nextCid) {\n log.trace(\"repliesPagesStore.addNextRepliesPageToStore no more pages\", {\n commentCid: comment.cid,\n sortType,\n account,\n });\n return;\n }\n\n pageCidToAdd = nextCid;\n }\n\n // page is already added or pending\n if (\n repliesPagesStore.repliesPages[pageCidToAdd] ||\n fetchPagePending[account.id + pageCidToAdd]\n ) {\n return;\n }\n\n fetchPagePending[account.id + pageCidToAdd] = true;\n let page: RepliesPage;\n try {\n page = await fetchPage(pageCidToAdd, comment, account);\n log.trace(\"repliesPagesStore.addNextRepliesPageToStore comment.replies.getPage\", {\n pageCid: pageCidToAdd,\n page,\n commentCid: comment.cid,\n communityAddress: comment.communityAddress,\n account,\n });\n } catch (e) {\n throw e;\n } finally {\n fetchPagePending[account.id + pageCidToAdd] = false;\n }\n\n // find new comments in the page (missing-or-fresher: insert when absent or incoming is fresher)\n const flattenedComments = utils.flattenCommentsPages(page);\n const { comments } = getState();\n let hasNewComments = false;\n const newComments: Comments = {};\n for (const comment of flattenedComments) {\n if (!comment.cid) continue;\n const existing = comments[comment.cid];\n const incomingFresh = Math.max(comment.updatedAt || 0, comment.timestamp || 0, 0);\n const existingFresh = existing\n ? Math.max(existing.updatedAt || 0, existing.timestamp || 0, 0)\n : -1;\n if (!existing || incomingFresh > existingFresh) {\n // don't clone the comment to save memory, comments remain a pointer to the page object\n newComments[comment.cid] = comment;\n hasNewComments = true;\n }\n }\n\n // add missing children replies feeds\n addChildrenRepliesFeedsToAddToStore(page, comment);\n\n setState(({ repliesPages, comments }: any) => {\n const newState: any = { repliesPages: { ...repliesPages, [pageCidToAdd]: page } };\n if (hasNewComments) {\n newState.comments = { ...comments, ...newComments };\n }\n return newState;\n });\n log(\"repliesPagesStore.addNextRepliesPageToStore\", {\n pageCid: pageCidToAdd,\n commentCid: comment.cid,\n sortType,\n page,\n account,\n });\n\n // when publishing a comment, you don't yet know its CID\n // so when a new comment is fetched, check to see if it's your own\n // comment, and if yes, add the CID to your account comments database\n for (const comment of flattenedComments) {\n accountsStore\n .getState()\n .accountsActionsInternal.addCidToAccountComment(comment)\n .catch((error: unknown) =>\n log.error(\"repliesPagesStore.addNextRepliesPageToStore addCidToAccountComment error\", {\n comment,\n error,\n }),\n );\n }\n },\n\n // comments contain preloaded pages, those page comments must be added separately\n addRepliesPageCommentsToStore: (comment: Comment) => {\n if (!comment.replies?.pages) {\n return;\n }\n\n // find new comments in the page (missing-or-fresher: insert when absent or incoming is fresher)\n const flattenedComments = utils.flattenCommentsPages(comment.replies.pages);\n const { comments } = getState();\n let hasNewComments = false;\n const newComments: Comments = {};\n for (const c of flattenedComments) {\n if (!c.cid) continue;\n const existing = comments[c.cid];\n const incomingFresh = Math.max(c.updatedAt || 0, c.timestamp || 0, 0);\n const existingFresh = existing\n ? Math.max(existing.updatedAt || 0, existing.timestamp || 0, 0)\n : -1;\n if (!existing || incomingFresh > existingFresh) {\n // don't clone the comment to save memory, comments remain a pointer to the page object\n newComments[c.cid] = c;\n hasNewComments = true;\n }\n }\n\n if (!hasNewComments) {\n return;\n }\n\n setState(({ comments }: any) => {\n return { comments: { ...comments, ...newComments } };\n });\n log(\"repliesPagesStore.addRepliesPageCommentsToStore\", { comment, newComments });\n },\n }),\n);\n\n// set clients states on comments store so the frontend can display it, dont persist in db because a reload cancels updating\nconst onCommentRepliesClientsStateChange =\n (commentCid: string) =>\n (clientState: string, clientType: string, sortType: string, clientUrl: string) => {\n commentsStore.setState((state: CommentsState) => {\n // make sure not undefined, sometimes happens in e2e tests\n if (!state.comments[commentCid]) {\n return {};\n }\n const client = { state: clientState };\n const comment = { ...state.comments[commentCid] };\n comment.replies = { ...comment.replies };\n comment.replies.clients = { ...comment.replies.clients };\n comment.replies.clients[clientType] = { ...comment.replies.clients[clientType] };\n comment.replies.clients[clientType][sortType] = {\n ...comment.replies.clients[clientType][sortType],\n };\n comment.replies.clients[clientType][sortType][clientUrl] = client;\n return { comments: { ...state.comments, [commentCid]: comment } };\n });\n };\n\nconst fetchPageComments: { [commentCid: string]: any } = {}; // cache pkc.createComment because sometimes it's slow\nlet fetchPagePending: { [key: string]: boolean } = {};\nconst fetchPage = async (pageCid: string, comment: Comment, account: Account) => {\n // replies page is cached\n const cachedRepliesPage = await repliesPagesDatabase.getItem(pageCid);\n if (cachedRepliesPage) {\n return cachedRepliesPage;\n }\n if (!fetchPageComments[comment.cid]) {\n fetchPageComments[comment.cid] = await account.pkc.createComment({\n cid: comment.cid,\n postCid: comment.postCid,\n communityAddress: comment.communityAddress,\n depth: comment.depth,\n });\n listeners.push(fetchPageComments[comment.cid]);\n\n // set clients states on communities store so the frontend can display it\n utils.pageClientsOnStateChange(\n fetchPageComments[comment.cid].replies?.clients,\n onCommentRepliesClientsStateChange(comment.cid),\n );\n }\n\n const onError = (error: any) =>\n log.error(\n `repliesPagesStore comment '${comment.cid}' failed comment.replies.getPage page cid '${pageCid}':`,\n error,\n );\n const fetchedRepliesPage = await utils.retryInfinity(\n () => fetchPageComments[comment.cid].replies.getPage({ cid: pageCid }),\n { onError },\n );\n await repliesPagesDatabase.setItem(pageCid, utils.clone(fetchedRepliesPage));\n return fetchedRepliesPage;\n};\n\n/**\n * Util function to get all pages in the store for a\n * specific comment+sortType using `RepliesPage.nextCid`\n */\nexport const getRepliesPages = (comment: Comment, sortType: string, repliesPages: RepliesPages) => {\n assert(\n repliesPages && typeof repliesPages === \"object\",\n `getRepliesPages repliesPages '${repliesPages}' invalid`,\n );\n const pages: RepliesPage[] = [];\n const firstPageCid = getRepliesFirstPageCid(comment, sortType);\n // comment has no pages\n // TODO: if a loaded comment doesn't have a first page, it's unclear what we should do\n // should we try to use another sort type by default, like 'best', or should we just ignore it?\n // 'return pages' to ignore it for now\n if (!firstPageCid) {\n return pages;\n }\n const firstPage = repliesPages[firstPageCid];\n if (!firstPage) {\n return pages;\n }\n pages.push(firstPage);\n while (true) {\n const nextCid = pages[pages.length - 1]?.nextCid;\n const repliesPage = nextCid && repliesPages[nextCid];\n if (!repliesPage) {\n return pages;\n }\n pages.push(repliesPage);\n }\n};\n\nexport const getRepliesFirstPageCid = (comment: Comment, sortType: string) => {\n assert(comment?.cid, `getRepliesFirstPageCid comment '${comment}' invalid`);\n assert(\n sortType && typeof sortType === \"string\",\n `getRepliesFirstPageCid sortType '${sortType}' invalid`,\n );\n // comment has preloaded replies for sort type\n if (comment.replies?.pages?.[sortType]?.comments) {\n return comment.replies?.pages?.[sortType]?.nextCid;\n }\n return comment.replies?.pageCids?.[sortType];\n\n // TODO: if a loaded comment doesn't have a first page, it's unclear what we should do\n // should we try to use another sort type by default, like 'best', or should we just ignore it?\n};\n\n// reset store in between tests\nconst originalState = repliesPagesStore.getState();\n// async function because some stores have async init\nexport const resetRepliesPagesStore = async () => {\n fetchPagePending = {};\n // remove all event listeners\n listeners.forEach((listener: any) => listener.removeAllListeners());\n // destroy all component subscriptions to the store\n repliesPagesStore.destroy();\n // restore original state\n repliesPagesStore.setState(originalState);\n};\n\n// reset database and store in between tests\nexport const resetRepliesPagesDatabaseAndStore = async () => {\n await localForageLru.createInstance({ name: \"bitsocialReactHooks-repliesPages\" }).clear();\n await resetRepliesPagesStore();\n};\n\nexport default repliesPagesStore;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/stores/replies-pages/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAA2C,OAAO,EAAE,MAAM,aAAa,CAAC;AAkB5F,eAAO,MAAM,mCAAmC,GAAI,MAAM,WAAW,EAAE,SAAS,OAAO,SAoCtF,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import repliesStore from "../replies/index.js";
|
|
2
|
+
import repliesCommentsStore from "../replies/replies-comments-store.js";
|
|
3
|
+
import Logger from "@pkcprotocol/pkc-logger";
|
|
4
|
+
// include replies pages store with feeds for debugging
|
|
5
|
+
const log = Logger("bitsocial-react-hooks:replies:stores");
|
|
6
|
+
const getSortTypeFromPage = (page) => {
|
|
7
|
+
var _a, _b, _c;
|
|
8
|
+
for (const reply of page.comments || []) {
|
|
9
|
+
for (const sortType in (_a = reply === null || reply === void 0 ? void 0 : reply.replies) === null || _a === void 0 ? void 0 : _a.pages) {
|
|
10
|
+
if ((_c = (_b = reply.replies.pages[sortType]) === null || _b === void 0 ? void 0 : _b.comments) === null || _c === void 0 ? void 0 : _c.length) {
|
|
11
|
+
return sortType;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
export const addChildrenRepliesFeedsToAddToStore = (page, comment) => {
|
|
17
|
+
const { feedsOptions, addFeedsToStore } = repliesStore.getState();
|
|
18
|
+
const commentsToAddToStoreOrUpdate = [];
|
|
19
|
+
const feedsToAddToStore = [];
|
|
20
|
+
// assume a page always uses the same sort type recursively
|
|
21
|
+
// could be incorrect, but we don't care about bad pages implementations for now
|
|
22
|
+
const sortType = getSortTypeFromPage(page) || "best";
|
|
23
|
+
const addRepliesFeedsToStoreRecursively = (page, feedOptions) => {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
for (const reply of (page === null || page === void 0 ? void 0 : page.comments) || []) {
|
|
26
|
+
// NOTE: even a comment with no replies needs a feed, to know it has 0 replies and not displace the UI when a new replies appears
|
|
27
|
+
commentsToAddToStoreOrUpdate.push(reply);
|
|
28
|
+
feedsToAddToStore.push(Object.assign(Object.assign({}, feedOptions), { commentCid: reply === null || reply === void 0 ? void 0 : reply.cid, commentDepth: reply === null || reply === void 0 ? void 0 : reply.depth }));
|
|
29
|
+
addRepliesFeedsToStoreRecursively((_b = (_a = reply === null || reply === void 0 ? void 0 : reply.replies) === null || _a === void 0 ? void 0 : _a.pages) === null || _b === void 0 ? void 0 : _b[sortType], feedOptions);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const feedsOptionsToAddToStore = Object.values(feedsOptions).filter((feedOptions) => feedOptions.commentCid === comment.cid && !feedOptions.flat);
|
|
33
|
+
for (const feedOptions of feedsOptionsToAddToStore) {
|
|
34
|
+
addRepliesFeedsToStoreRecursively(page, feedOptions);
|
|
35
|
+
}
|
|
36
|
+
log("repliesPagesStore.addChildrenRepliesFeedsToAddToStore", {
|
|
37
|
+
feedsToAddToStore,
|
|
38
|
+
commentsToAddToStoreOrUpdate,
|
|
39
|
+
});
|
|
40
|
+
addFeedsToStore(feedsToAddToStore);
|
|
41
|
+
repliesCommentsStore.getState().addCommentsToStoreOrUpdateComments(commentsToAddToStoreOrUpdate);
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/stores/replies-pages/utils.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,MAAM,YAAY,CAAC;AAEtC,OAAO,oBAAoB,MAAM,mCAAmC,CAAC;AACrE,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,uDAAuD;AACvD,MAAM,GAAG,GAAG,MAAM,CAAC,sCAAsC,CAAC,CAAC;AAE3D,MAAM,mBAAmB,GAAG,CAAC,IAAiB,EAAE,EAAE;;IAChD,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC;QACxC,KAAK,MAAM,QAAQ,IAAI,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,OAAO,0CAAE,KAAK,EAAE,CAAC;YAC7C,IAAI,MAAA,MAAA,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,0CAAE,QAAQ,0CAAE,MAAM,EAAE,CAAC;gBACpD,OAAO,QAAQ,CAAC;YAClB,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAAC,IAAiB,EAAE,OAAgB,EAAE,EAAE;IACzF,MAAM,EAAE,YAAY,EAAE,eAAe,EAAE,GAAG,YAAY,CAAC,QAAQ,EAAE,CAAC;IAClE,MAAM,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C,CAAC;AACnG,CAAC,CAAC","sourcesContent":["import { RepliesPage, RepliesFeedOptions, RepliesFeedsOptions, Comment } from \"../../types\";\nimport repliesStore from \"../replies\";\nimport { getSortTypeFromComment } from \"../replies/utils\";\nimport repliesCommentsStore from \"../replies/replies-comments-store\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\n// include replies pages store with feeds for debugging\nconst log = Logger(\"bitsocial-react-hooks:replies:stores\");\n\nconst getSortTypeFromPage = (page: RepliesPage) => {\n for (const reply of page.comments || []) {\n for (const sortType in reply?.replies?.pages) {\n if (reply.replies.pages[sortType]?.comments?.length) {\n return sortType;\n }\n }\n }\n};\n\nexport const addChildrenRepliesFeedsToAddToStore = (page: RepliesPage, comment: Comment) => {\n const { feedsOptions, addFeedsToStore } = repliesStore.getState();\n const commentsToAddToStoreOrUpdate: Comment[] = [];\n const feedsToAddToStore: RepliesFeedOptions[] = [];\n\n // assume a page always uses the same sort type recursively\n // could be incorrect, but we don't care about bad pages implementations for now\n const sortType = getSortTypeFromPage(page) || \"best\";\n\n const addRepliesFeedsToStoreRecursively = (\n page: RepliesPage,\n feedOptions: RepliesFeedOptions,\n ) => {\n for (const reply of page?.comments || []) {\n // NOTE: even a comment with no replies needs a feed, to know it has 0 replies and not displace the UI when a new replies appears\n commentsToAddToStoreOrUpdate.push(reply);\n feedsToAddToStore.push({\n ...feedOptions,\n commentCid: reply?.cid,\n commentDepth: reply?.depth,\n });\n addRepliesFeedsToStoreRecursively(reply?.replies?.pages?.[sortType], feedOptions);\n }\n };\n const feedsOptionsToAddToStore = Object.values(feedsOptions).filter(\n (feedOptions) => feedOptions.commentCid === comment.cid && !feedOptions.flat,\n );\n for (const feedOptions of feedsOptionsToAddToStore) {\n addRepliesFeedsToStoreRecursively(page, feedOptions);\n }\n log(\"repliesPagesStore.addChildrenRepliesFeedsToAddToStore\", {\n feedsToAddToStore,\n commentsToAddToStoreOrUpdate,\n });\n addFeedsToStore(feedsToAddToStore);\n repliesCommentsStore.getState().addCommentsToStoreOrUpdateComments(commentsToAddToStoreOrUpdate);\n};\n"]}
|