@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
|
+
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AAyB,CAAC,CAAC,KAAK,CAAC;IAC1F,OAAO,kBAAkB,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;AACpD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,eAAwB,EAAE,EAAE;IACxD,OAAO,OAAO,CACZ,GAAG,EAAE,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,0BAA0B,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,EACjF,CAAC,eAAe,CAAC,CAClB,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { useEffect, useState, useMemo } from \"react\";\nimport { Comment, CommentsFilter } from \"../../types\";\nimport { useComments } from \"../comments\";\nimport utils from \"../../lib/utils\";\nimport PeerId from \"peer-id\";\nimport { fromString as uint8ArrayFromString } from \"uint8arrays/from-string\";\nimport { toString as uint8ArrayToString } from \"uint8arrays/to-string\";\nimport { create as createMultihash } from \"multiformats/hashes/digest\";\nimport assert from \"assert\";\n\nexport const useAuthorCommentsName = (\n accountId?: string,\n authorAddress?: string,\n filter?: CommentsFilter | undefined,\n) => {\n return useMemo(\n () => accountId + \"-\" + authorAddress + \"-\" + filter?.key,\n [accountId, authorAddress, filter?.key],\n );\n};\n\nconst protobufPublicKeyPrefix = new Uint8Array([8, 1, 18, 32]);\nconst multihashIdentityCode = 0;\nconst getPkcAddressFromPublicKey = (publicKeyBase64: string) => {\n const publicKeyBuffer = uint8ArrayFromString(publicKeyBase64, \"base64\");\n const publicKeyBufferWithPrefix = new Uint8Array(\n protobufPublicKeyPrefix.length + publicKeyBuffer.length,\n );\n publicKeyBufferWithPrefix.set(protobufPublicKeyPrefix, 0);\n publicKeyBufferWithPrefix.set(publicKeyBuffer, protobufPublicKeyPrefix.length);\n const multihash = createMultihash(multihashIdentityCode, publicKeyBufferWithPrefix).bytes;\n return uint8ArrayToString(multihash, \"base58btc\");\n};\n\nexport const usePkcAddress = (publicKeyBase64?: string) => {\n return useMemo(\n () => (publicKeyBase64 ? getPkcAddressFromPublicKey(publicKeyBase64) : undefined),\n [publicKeyBase64],\n );\n};\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Comment, UseCommentsOptions, UseCommentsResult, UseCommentOptions, UseCommentResult, UseValidateCommentOptions, UseValidateCommentResult } from "../types.js";
|
|
2
|
+
export declare function getCommentFreshness(comment: Comment | undefined): number;
|
|
3
|
+
export declare function preferFresher(current: Comment | undefined, candidate: Comment | undefined): Comment | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* @param commentCid - The IPFS CID of the comment to get
|
|
6
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
7
|
+
* the active account.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useComment(options?: UseCommentOptions): UseCommentResult;
|
|
10
|
+
/**
|
|
11
|
+
* @param commentCids - The IPFS CIDs of the comments to get
|
|
12
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
13
|
+
* the active account.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useComments(options?: UseCommentsOptions): UseCommentsResult;
|
|
16
|
+
export declare function useValidateComment(options?: UseValidateCommentOptions): UseValidateCommentResult;
|
|
17
|
+
//# sourceMappingURL=comments.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comments.d.ts","sourceRoot":"","sources":["../../src/hooks/comments.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,OAAO,EACP,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,yBAAyB,EACzB,wBAAwB,EACzB,MAAM,UAAU,CAAC;AAYlB,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,CAGxE;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,OAAO,GAAG,SAAS,EAC5B,SAAS,EAAE,OAAO,GAAG,SAAS,GAC7B,OAAO,GAAG,SAAS,CAIrB;AAqCD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,gBAAgB,CAsKxE;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,iBAAiB,CA0K3E;AAED,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,yBAAyB,GAAG,wBAAwB,CAmDhG"}
|
|
@@ -0,0 +1,351 @@
|
|
|
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 { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
11
|
+
import { useAccount } from "./accounts/index.js";
|
|
12
|
+
import validator from "../lib/validator.js";
|
|
13
|
+
import Logger from "@pkcprotocol/pkc-logger";
|
|
14
|
+
const log = Logger("bitsocial-react-hooks:comments:hooks");
|
|
15
|
+
import assert from "assert";
|
|
16
|
+
import useCommentsStore from "../stores/comments/index.js";
|
|
17
|
+
import useAccountsStore from "../stores/accounts/index.js";
|
|
18
|
+
import { commentIsValid } from "../lib/utils/index.js";
|
|
19
|
+
import { addCommentModeration, addCommentModerationToComments, } from "../lib/utils/comment-moderation.js";
|
|
20
|
+
import useCommunitiesPagesStore from "../stores/communities-pages/index.js";
|
|
21
|
+
import useRepliesPagesStore from "../stores/replies-pages/index.js";
|
|
22
|
+
import shallow from "zustand/shallow";
|
|
23
|
+
export function getCommentFreshness(comment) {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
if (!comment)
|
|
26
|
+
return 0;
|
|
27
|
+
return Math.max((_a = comment.updatedAt) !== null && _a !== void 0 ? _a : 0, (_b = comment.timestamp) !== null && _b !== void 0 ? _b : 0, 0);
|
|
28
|
+
}
|
|
29
|
+
export function preferFresher(current, candidate) {
|
|
30
|
+
if (!candidate)
|
|
31
|
+
return current;
|
|
32
|
+
if (!current)
|
|
33
|
+
return candidate;
|
|
34
|
+
return getCommentFreshness(candidate) > getCommentFreshness(current) ? candidate : current;
|
|
35
|
+
}
|
|
36
|
+
const getCommentStateAndReplyCount = (comment) => {
|
|
37
|
+
let state = (comment === null || comment === void 0 ? void 0 : comment.updatingState) || "initializing";
|
|
38
|
+
// force 'fetching-ipns' even if could be something else, so the frontend can use
|
|
39
|
+
// the correct loading skeleton
|
|
40
|
+
if (comment === null || comment === void 0 ? void 0 : comment.timestamp) {
|
|
41
|
+
state = "fetching-update-ipns";
|
|
42
|
+
}
|
|
43
|
+
// force succeeded even if the comment is fecthing a new update
|
|
44
|
+
if (comment === null || comment === void 0 ? void 0 : comment.updatedAt) {
|
|
45
|
+
state = "succeeded";
|
|
46
|
+
}
|
|
47
|
+
// force succeeded if the comment is newer than 5 minutes, no need to display loading skeleton if comment was just created
|
|
48
|
+
let replyCount = comment === null || comment === void 0 ? void 0 : comment.replyCount;
|
|
49
|
+
if ((comment === null || comment === void 0 ? void 0 : comment.replyCount) === undefined &&
|
|
50
|
+
(comment === null || comment === void 0 ? void 0 : comment.timestamp) &&
|
|
51
|
+
(comment === null || comment === void 0 ? void 0 : comment.timestamp) > Date.now() / 1000 - 5 * 60) {
|
|
52
|
+
state = "succeeded";
|
|
53
|
+
// set replyCount because some frontend are likely to check if replyCount === undefined to show a loading skeleton
|
|
54
|
+
replyCount = 0;
|
|
55
|
+
}
|
|
56
|
+
return { state, replyCount };
|
|
57
|
+
};
|
|
58
|
+
const getCommentsState = (comments) => comments.every((comment) => getCommentStateAndReplyCount(comment).state === "succeeded")
|
|
59
|
+
? "succeeded"
|
|
60
|
+
: "fetching-ipfs";
|
|
61
|
+
let commentAutoUpdateSubscriptionCount = 0;
|
|
62
|
+
let commentsAutoUpdateSubscriptionCount = 0;
|
|
63
|
+
/**
|
|
64
|
+
* @param commentCid - The IPFS CID of the comment to get
|
|
65
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
66
|
+
* the active account.
|
|
67
|
+
*/
|
|
68
|
+
export function useComment(options) {
|
|
69
|
+
assert(!options || typeof options === "object", `useComment options argument '${options}' not an object`);
|
|
70
|
+
const { commentCid, accountName, onlyIfCached, autoUpdate = true } = options !== null && options !== void 0 ? options : {};
|
|
71
|
+
const account = useAccount({ accountName });
|
|
72
|
+
const commentFromStore = useCommentsStore((state) => state.comments[commentCid || ""]);
|
|
73
|
+
const addCommentToStore = useCommentsStore((state) => state.addCommentToStore);
|
|
74
|
+
const startCommentAutoUpdate = useCommentsStore((state) => state.startCommentAutoUpdate);
|
|
75
|
+
const stopCommentAutoUpdate = useCommentsStore((state) => state.stopCommentAutoUpdate);
|
|
76
|
+
const refreshCommentInStore = useCommentsStore((state) => state.refreshComment);
|
|
77
|
+
const communitiesPagesComment = useCommunitiesPagesStore((state) => state.comments[commentCid || ""]);
|
|
78
|
+
const repliesPagesComment = useRepliesPagesStore((state) => state.comments[commentCid || ""]);
|
|
79
|
+
const errors = useCommentsStore((state) => state.errors[commentCid || ""]);
|
|
80
|
+
// get account comment of the cid if any
|
|
81
|
+
const accountCommentInfo = useAccountsStore((state) => state.commentCidsToAccountsComments[commentCid || ""]);
|
|
82
|
+
const accountComment = useAccountsStore((state) => {
|
|
83
|
+
var _a;
|
|
84
|
+
return (_a = state.accountsComments[(accountCommentInfo === null || accountCommentInfo === void 0 ? void 0 : accountCommentInfo.accountId) || ""]) === null || _a === void 0 ? void 0 : _a[Number(accountCommentInfo === null || accountCommentInfo === void 0 ? void 0 : accountCommentInfo.accountCommentIndex)];
|
|
85
|
+
});
|
|
86
|
+
const autoUpdateSubscriptionId = useRef(`useComment-${++commentAutoUpdateSubscriptionCount}`);
|
|
87
|
+
const currentCommentCidRef = useRef(commentCid);
|
|
88
|
+
currentCommentCidRef.current = commentCid;
|
|
89
|
+
const [frozenComment, setFrozenComment] = useState();
|
|
90
|
+
const [freezeSettledCid, setFreezeSettledCid] = useState();
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (!commentCid || !account) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
validator.validateUseCommentArguments(commentCid, account);
|
|
96
|
+
if (!commentFromStore && !onlyIfCached) {
|
|
97
|
+
// if comment isn't already in store, add it
|
|
98
|
+
addCommentToStore(commentCid, account).catch((error) => log.error("useComment addCommentToStore error", { commentCid, error }));
|
|
99
|
+
}
|
|
100
|
+
}, [commentCid, account === null || account === void 0 ? void 0 : account.id, onlyIfCached]);
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (!commentCid || !account || onlyIfCached || !autoUpdate) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
startCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current, account).catch((error) => log.error("useComment startCommentAutoUpdate error", { commentCid, error }));
|
|
106
|
+
return () => {
|
|
107
|
+
stopCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current).catch((error) => log.error("useComment stopCommentAutoUpdate error", { commentCid, error }));
|
|
108
|
+
};
|
|
109
|
+
}, [commentCid, account === null || account === void 0 ? void 0 : account.id, onlyIfCached, autoUpdate]);
|
|
110
|
+
let selectedComment = commentFromStore;
|
|
111
|
+
if (commentCid && communitiesPagesComment) {
|
|
112
|
+
selectedComment = preferFresher(selectedComment, communitiesPagesComment);
|
|
113
|
+
}
|
|
114
|
+
if (commentCid && repliesPagesComment) {
|
|
115
|
+
selectedComment = preferFresher(selectedComment, repliesPagesComment);
|
|
116
|
+
}
|
|
117
|
+
// if comment is still not defined, but account comment is, use account comment
|
|
118
|
+
// check `comment.timestamp` instead of `comment` in case comment exists but in a loading state
|
|
119
|
+
const commentFromStoreNotLoaded = !(selectedComment === null || selectedComment === void 0 ? void 0 : selectedComment.timestamp);
|
|
120
|
+
if (commentCid && commentFromStoreNotLoaded && accountComment) {
|
|
121
|
+
selectedComment = accountComment;
|
|
122
|
+
}
|
|
123
|
+
const selectedCommentState = getCommentStateAndReplyCount(selectedComment).state;
|
|
124
|
+
const freezeSettledForCurrentCid = freezeSettledCid === commentCid;
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (autoUpdate) {
|
|
127
|
+
setFrozenComment(undefined);
|
|
128
|
+
setFreezeSettledCid(undefined);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
setFrozenComment(undefined);
|
|
132
|
+
setFreezeSettledCid(undefined);
|
|
133
|
+
}, [commentCid, autoUpdate]);
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
if (autoUpdate) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (!commentCid) {
|
|
139
|
+
setFrozenComment(undefined);
|
|
140
|
+
setFreezeSettledCid(undefined);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (freezeSettledForCurrentCid || !selectedComment) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
setFrozenComment(selectedComment);
|
|
147
|
+
if (selectedCommentState === "succeeded") {
|
|
148
|
+
setFreezeSettledCid(commentCid);
|
|
149
|
+
}
|
|
150
|
+
}, [autoUpdate, commentCid, selectedComment, selectedCommentState, freezeSettledForCurrentCid]);
|
|
151
|
+
const frozenCommentForCurrentCid = (frozenComment === null || frozenComment === void 0 ? void 0 : frozenComment.cid) === commentCid ? frozenComment : undefined;
|
|
152
|
+
let comment = autoUpdate
|
|
153
|
+
? selectedComment
|
|
154
|
+
: freezeSettledForCurrentCid
|
|
155
|
+
? frozenCommentForCurrentCid
|
|
156
|
+
: frozenCommentForCurrentCid || selectedComment;
|
|
157
|
+
comment = addCommentModeration(comment);
|
|
158
|
+
const { state, replyCount } = getCommentStateAndReplyCount(comment);
|
|
159
|
+
if (account && commentCid) {
|
|
160
|
+
log("useComment", {
|
|
161
|
+
commentCid,
|
|
162
|
+
comment,
|
|
163
|
+
replyCount,
|
|
164
|
+
state,
|
|
165
|
+
commentFromStore,
|
|
166
|
+
communitiesPagesComment,
|
|
167
|
+
repliesPagesComment,
|
|
168
|
+
accountComment,
|
|
169
|
+
commentsStore: useCommentsStore.getState().comments,
|
|
170
|
+
account,
|
|
171
|
+
onlyIfCached,
|
|
172
|
+
autoUpdate,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const refresh = useCallback(() => __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
if (!commentCid || !account) {
|
|
177
|
+
throw Error("useComment cannot refresh comment not initialized yet");
|
|
178
|
+
}
|
|
179
|
+
const refreshCommentCid = commentCid;
|
|
180
|
+
const refreshedComment = yield refreshCommentInStore(refreshCommentCid, account);
|
|
181
|
+
if (!autoUpdate && refreshedComment && currentCommentCidRef.current === refreshCommentCid) {
|
|
182
|
+
setFrozenComment(refreshedComment);
|
|
183
|
+
setFreezeSettledCid(refreshCommentCid);
|
|
184
|
+
}
|
|
185
|
+
}), [account, autoUpdate, commentCid, refreshCommentInStore]);
|
|
186
|
+
return useMemo(() => (Object.assign(Object.assign({}, comment), { replyCount,
|
|
187
|
+
state,
|
|
188
|
+
refresh, error: errors === null || errors === void 0 ? void 0 : errors[errors.length - 1], errors: errors || [] })), [comment, commentCid, errors, refresh, state, replyCount]);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @param commentCids - The IPFS CIDs of the comments to get
|
|
192
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
193
|
+
* the active account.
|
|
194
|
+
*/
|
|
195
|
+
export function useComments(options) {
|
|
196
|
+
assert(!options || typeof options === "object", `useComments options argument '${options}' not an object`);
|
|
197
|
+
const { commentCids = [], accountName, onlyIfCached, autoUpdate = true } = options !== null && options !== void 0 ? options : {};
|
|
198
|
+
const account = useAccount({ accountName });
|
|
199
|
+
const commentsStoreComments = useCommentsStore((state) => commentCids.map((commentCid) => state.comments[commentCid || ""]), shallow);
|
|
200
|
+
const communitiesPagesComments = useCommunitiesPagesStore((state) => commentCids.map((commentCid) => state.comments[commentCid || ""]), shallow);
|
|
201
|
+
const addCommentToStore = useCommentsStore((state) => state.addCommentToStore);
|
|
202
|
+
const startCommentAutoUpdate = useCommentsStore((state) => state.startCommentAutoUpdate);
|
|
203
|
+
const stopCommentAutoUpdate = useCommentsStore((state) => state.stopCommentAutoUpdate);
|
|
204
|
+
const refreshCommentInStore = useCommentsStore((state) => state.refreshComment);
|
|
205
|
+
const autoUpdateSubscriptionId = useRef(`useComments-${++commentsAutoUpdateSubscriptionCount}`);
|
|
206
|
+
const commentCidsKey = JSON.stringify(commentCids);
|
|
207
|
+
const commentsKey = `${(account === null || account === void 0 ? void 0 : account.id) || ""}:${commentCidsKey}`;
|
|
208
|
+
const currentCommentsKeyRef = useRef(commentsKey);
|
|
209
|
+
currentCommentsKeyRef.current = commentsKey;
|
|
210
|
+
const [frozenComments, setFrozenComments] = useState([]);
|
|
211
|
+
const [frozenCommentsKey, setFrozenCommentsKey] = useState();
|
|
212
|
+
const [freezeSettledKey, setFreezeSettledKey] = useState();
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
if (!commentCids || !account) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
validator.validateUseCommentsArguments(commentCids, account);
|
|
218
|
+
if (onlyIfCached) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const uniqueCommentCids = new Set(commentCids);
|
|
222
|
+
for (const commentCid of uniqueCommentCids) {
|
|
223
|
+
addCommentToStore(commentCid, account).catch((error) => log.error("useComments addCommentToStore error", { commentCid, error }));
|
|
224
|
+
}
|
|
225
|
+
}, [commentCidsKey, account === null || account === void 0 ? void 0 : account.id, onlyIfCached]);
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
if (!commentCids || !account || onlyIfCached || !autoUpdate) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const uniqueCommentCids = [...new Set(commentCids)];
|
|
231
|
+
for (const commentCid of uniqueCommentCids) {
|
|
232
|
+
startCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current, account).catch((error) => log.error("useComments startCommentAutoUpdate error", { commentCid, error }));
|
|
233
|
+
}
|
|
234
|
+
return () => {
|
|
235
|
+
for (const commentCid of uniqueCommentCids) {
|
|
236
|
+
stopCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current).catch((error) => log.error("useComments stopCommentAutoUpdate error", { commentCid, error }));
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}, [commentCidsKey, account === null || account === void 0 ? void 0 : account.id, onlyIfCached, autoUpdate]);
|
|
240
|
+
if (account && (commentCids === null || commentCids === void 0 ? void 0 : commentCids.length)) {
|
|
241
|
+
log("useComments", {
|
|
242
|
+
commentCids,
|
|
243
|
+
commentsStoreComments,
|
|
244
|
+
commentsStore: useCommentsStore.getState().comments,
|
|
245
|
+
account,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
// if comment from community pages exists and is fresher (or current missing), use it instead
|
|
249
|
+
const liveComments = useMemo(() => {
|
|
250
|
+
const result = [...commentsStoreComments];
|
|
251
|
+
for (const i in result) {
|
|
252
|
+
const candidate = communitiesPagesComments[i];
|
|
253
|
+
if (candidate)
|
|
254
|
+
result[i] = preferFresher(result[i], candidate);
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
}, [commentsStoreComments, communitiesPagesComments]);
|
|
258
|
+
const liveCommentsSettled = liveComments.every((comment) => getCommentStateAndReplyCount(comment).state === "succeeded");
|
|
259
|
+
const freezeSettledForCurrentKey = freezeSettledKey === commentsKey;
|
|
260
|
+
useEffect(() => {
|
|
261
|
+
if (autoUpdate) {
|
|
262
|
+
setFrozenComments([]);
|
|
263
|
+
setFrozenCommentsKey(undefined);
|
|
264
|
+
setFreezeSettledKey(undefined);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
setFrozenComments([]);
|
|
268
|
+
setFrozenCommentsKey(undefined);
|
|
269
|
+
setFreezeSettledKey(undefined);
|
|
270
|
+
}, [commentsKey, autoUpdate]);
|
|
271
|
+
useEffect(() => {
|
|
272
|
+
if (autoUpdate || freezeSettledForCurrentKey) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
setFrozenComments(liveComments);
|
|
276
|
+
setFrozenCommentsKey(commentsKey);
|
|
277
|
+
if (liveCommentsSettled) {
|
|
278
|
+
setFreezeSettledKey(commentsKey);
|
|
279
|
+
}
|
|
280
|
+
}, [autoUpdate, commentsKey, freezeSettledForCurrentKey, liveComments, liveCommentsSettled]);
|
|
281
|
+
const frozenCommentsForCurrentSelection = frozenCommentsKey === commentsKey ? frozenComments : undefined;
|
|
282
|
+
const comments = autoUpdate ? liveComments : frozenCommentsForCurrentSelection || liveComments;
|
|
283
|
+
const normalizedComments = useMemo(() => addCommentModerationToComments(comments), [comments]);
|
|
284
|
+
// succeed if no comments are undefined
|
|
285
|
+
const state = getCommentsState(normalizedComments);
|
|
286
|
+
const refresh = useCallback(() => __awaiter(this, void 0, void 0, function* () {
|
|
287
|
+
if (!account) {
|
|
288
|
+
throw Error("useComments cannot refresh comments not initialized yet");
|
|
289
|
+
}
|
|
290
|
+
const uniqueCommentCids = [...new Set(commentCids)];
|
|
291
|
+
const refreshedComments = yield Promise.all(uniqueCommentCids.map((commentCid) => refreshCommentInStore(commentCid, account)));
|
|
292
|
+
if (!autoUpdate && currentCommentsKeyRef.current === commentsKey) {
|
|
293
|
+
const latestCommunitiesPagesComments = useCommunitiesPagesStore.getState().comments;
|
|
294
|
+
const refreshedCommentsByCid = uniqueCommentCids.reduce((refreshedCommentsMap, commentCid, index) => {
|
|
295
|
+
refreshedCommentsMap[commentCid] = refreshedComments[index];
|
|
296
|
+
return refreshedCommentsMap;
|
|
297
|
+
}, {});
|
|
298
|
+
setFrozenComments(commentCids.map((commentCid) => preferFresher(refreshedCommentsByCid[commentCid || ""], latestCommunitiesPagesComments[commentCid || ""])));
|
|
299
|
+
setFrozenCommentsKey(commentsKey);
|
|
300
|
+
setFreezeSettledKey(commentsKey);
|
|
301
|
+
}
|
|
302
|
+
}), [account, autoUpdate, commentCids, commentsKey, refreshCommentInStore]);
|
|
303
|
+
return useMemo(() => ({
|
|
304
|
+
comments: normalizedComments,
|
|
305
|
+
state,
|
|
306
|
+
refresh,
|
|
307
|
+
error: undefined,
|
|
308
|
+
errors: [],
|
|
309
|
+
}), [normalizedComments, commentsKey, refresh, state]);
|
|
310
|
+
}
|
|
311
|
+
export function useValidateComment(options) {
|
|
312
|
+
assert(!options || typeof options === "object", `useValidateComment options argument '${options}' not an object`);
|
|
313
|
+
let { comment, validateReplies, accountName } = options !== null && options !== void 0 ? options : {};
|
|
314
|
+
validateReplies = validateReplies !== null && validateReplies !== void 0 ? validateReplies : true;
|
|
315
|
+
const [validated, setValidated] = useState();
|
|
316
|
+
const [errors] = useState([]);
|
|
317
|
+
const account = useAccount({ accountName });
|
|
318
|
+
useEffect(() => {
|
|
319
|
+
if (!comment || !(account === null || account === void 0 ? void 0 : account.pkc)) {
|
|
320
|
+
setValidated(undefined);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
// don't automatically block community because what community it comes from
|
|
324
|
+
// a malicious community could try to block other communities, etc
|
|
325
|
+
const blockCommunity = false;
|
|
326
|
+
commentIsValid(comment, { validateReplies, blockCommunity }, account.pkc).then((validated) => setValidated(validated));
|
|
327
|
+
}, [comment, validateReplies, account === null || account === void 0 ? void 0 : account.pkc]);
|
|
328
|
+
let state = "initializing";
|
|
329
|
+
if (validated === true) {
|
|
330
|
+
state = "succeeded";
|
|
331
|
+
}
|
|
332
|
+
if (validated === false) {
|
|
333
|
+
state = "failed";
|
|
334
|
+
}
|
|
335
|
+
// start valid at true always because most of the time the value will be true and we dont want to cause a rerender
|
|
336
|
+
let valid = true;
|
|
337
|
+
if (validated == false) {
|
|
338
|
+
valid = false;
|
|
339
|
+
}
|
|
340
|
+
// if comment isn't defined, it would be confusing for valid to be true
|
|
341
|
+
if (!comment) {
|
|
342
|
+
valid = false;
|
|
343
|
+
}
|
|
344
|
+
return useMemo(() => ({
|
|
345
|
+
valid,
|
|
346
|
+
state,
|
|
347
|
+
error: errors[errors.length - 1],
|
|
348
|
+
errors,
|
|
349
|
+
}), [valid, state]);
|
|
350
|
+
}
|
|
351
|
+
//# sourceMappingURL=comments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comments.js","sourceRoot":"","sources":["../../src/hooks/comments.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,SAAS,MAAM,kBAAkB,CAAC;AACzC,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,MAAM,GAAG,GAAG,MAAM,CAAC,sCAAsC,CAAC,CAAC;AAC3D,OAAO,MAAM,MAAM,QAAQ,CAAC;AAU5B,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EACL,oBAAoB,EACpB,8BAA8B,GAC/B,MAAM,iCAAiC,CAAC;AACzC,OAAO,wBAAwB,MAAM,6BAA6B,CAAC;AACnE,OAAO,oBAAoB,MAAM,yBAAyB,CAAC;AAC3D,OAAO,OAAO,MAAM,iBAAiB,CAAC;AAEtC,MAAM,UAAU,mBAAmB,CAAC,OAA4B;;IAC9D,IAAI,CAAC,OAAO;QAAE,OAAO,CAAC,CAAC;IACvB,OAAO,IAAI,CA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AI,KAAK,GAAG,IAAI,CAAC;IACjB,IAAI,SAAS,IAAI,KAAK,EAAE,CAAC;QACvB,KAAK,GAAG,KAAK,CAAC;IAChB,CAAC;IACD,uEAAuE;IACvE,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,KAAK,GAAG,KAAK,CAAC;IAChB,CAAC;IAED,OAAO,OAAO,CACZ,GAAG,EAAE,CAAC,CAAC;QACL,KAAK;QACL,KAAK;QACL,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;QAChC,MAAM;KACP,CAAC,EACF,CAAC,KAAK,EAAE,KAAK,CAAC,CACf,CAAC;AACJ,CAAC","sourcesContent":["import { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport { useAccount } from \"./accounts\";\nimport validator from \"../lib/validator\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:comments:hooks\");\nimport assert from \"assert\";\nimport {\n Comment,\n UseCommentsOptions,\n UseCommentsResult,\n UseCommentOptions,\n UseCommentResult,\n UseValidateCommentOptions,\n UseValidateCommentResult,\n} from \"../types\";\nimport useCommentsStore from \"../stores/comments\";\nimport useAccountsStore from \"../stores/accounts\";\nimport { commentIsValid } from \"../lib/utils\";\nimport {\n addCommentModeration,\n addCommentModerationToComments,\n} from \"../lib/utils/comment-moderation\";\nimport useCommunitiesPagesStore from \"../stores/communities-pages\";\nimport useRepliesPagesStore from \"../stores/replies-pages\";\nimport shallow from \"zustand/shallow\";\n\nexport function getCommentFreshness(comment: Comment | undefined): number {\n if (!comment) return 0;\n return Math.max(comment.updatedAt ?? 0, comment.timestamp ?? 0, 0);\n}\n\nexport function preferFresher(\n current: Comment | undefined,\n candidate: Comment | undefined,\n): Comment | undefined {\n if (!candidate) return current;\n if (!current) return candidate;\n return getCommentFreshness(candidate) > getCommentFreshness(current) ? candidate : current;\n}\n\nconst getCommentStateAndReplyCount = (comment: Comment | undefined) => {\n let state = comment?.updatingState || \"initializing\";\n // force 'fetching-ipns' even if could be something else, so the frontend can use\n // the correct loading skeleton\n if (comment?.timestamp) {\n state = \"fetching-update-ipns\";\n }\n // force succeeded even if the comment is fecthing a new update\n if (comment?.updatedAt) {\n state = \"succeeded\";\n }\n\n // force succeeded if the comment is newer than 5 minutes, no need to display loading skeleton if comment was just created\n let replyCount = comment?.replyCount;\n if (\n comment?.replyCount === undefined &&\n comment?.timestamp &&\n comment?.timestamp > Date.now() / 1000 - 5 * 60\n ) {\n state = \"succeeded\";\n // set replyCount because some frontend are likely to check if replyCount === undefined to show a loading skeleton\n replyCount = 0;\n }\n\n return { state, replyCount };\n};\n\nconst getCommentsState = (comments: (Comment | undefined)[]) =>\n comments.every((comment) => getCommentStateAndReplyCount(comment).state === \"succeeded\")\n ? \"succeeded\"\n : \"fetching-ipfs\";\n\nlet commentAutoUpdateSubscriptionCount = 0;\nlet commentsAutoUpdateSubscriptionCount = 0;\n\n/**\n * @param commentCid - The IPFS CID of the comment to get\n * @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use\n * the active account.\n */\nexport function useComment(options?: UseCommentOptions): UseCommentResult {\n assert(\n !options || typeof options === \"object\",\n `useComment options argument '${options}' not an object`,\n );\n const { commentCid, accountName, onlyIfCached, autoUpdate = true } = options ?? {};\n const account = useAccount({ accountName });\n const commentFromStore = useCommentsStore((state: any) => state.comments[commentCid || \"\"]);\n const addCommentToStore = useCommentsStore((state: any) => state.addCommentToStore);\n const startCommentAutoUpdate = useCommentsStore((state: any) => state.startCommentAutoUpdate);\n const stopCommentAutoUpdate = useCommentsStore((state: any) => state.stopCommentAutoUpdate);\n const refreshCommentInStore = useCommentsStore((state: any) => state.refreshComment);\n const communitiesPagesComment = useCommunitiesPagesStore(\n (state: any) => state.comments[commentCid || \"\"],\n );\n const repliesPagesComment = useRepliesPagesStore(\n (state: any) => state.comments[commentCid || \"\"],\n );\n const errors = useCommentsStore((state: any) => state.errors[commentCid || \"\"]);\n\n // get account comment of the cid if any\n const accountCommentInfo = useAccountsStore(\n (state: any) => state.commentCidsToAccountsComments[commentCid || \"\"],\n );\n const accountComment = useAccountsStore(\n (state: any) =>\n state.accountsComments[accountCommentInfo?.accountId || \"\"]?.[\n Number(accountCommentInfo?.accountCommentIndex)\n ],\n );\n const autoUpdateSubscriptionId = useRef(`useComment-${++commentAutoUpdateSubscriptionCount}`);\n const currentCommentCidRef = useRef<string | undefined>(commentCid);\n currentCommentCidRef.current = commentCid;\n const [frozenComment, setFrozenComment] = useState<Comment | undefined>();\n const [freezeSettledCid, setFreezeSettledCid] = useState<string>();\n\n useEffect(() => {\n if (!commentCid || !account) {\n return;\n }\n validator.validateUseCommentArguments(commentCid, account);\n if (!commentFromStore && !onlyIfCached) {\n // if comment isn't already in store, add it\n addCommentToStore(commentCid, account).catch((error: unknown) =>\n log.error(\"useComment addCommentToStore error\", { commentCid, error }),\n );\n }\n }, [commentCid, account?.id, onlyIfCached]);\n\n useEffect(() => {\n if (!commentCid || !account || onlyIfCached || !autoUpdate) {\n return;\n }\n\n startCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current, account).catch(\n (error: unknown) =>\n log.error(\"useComment startCommentAutoUpdate error\", { commentCid, error }),\n );\n\n return () => {\n stopCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current).catch((error: unknown) =>\n log.error(\"useComment stopCommentAutoUpdate error\", { commentCid, error }),\n );\n };\n }, [commentCid, account?.id, onlyIfCached, autoUpdate]);\n\n let selectedComment = commentFromStore;\n\n if (commentCid && communitiesPagesComment) {\n selectedComment = preferFresher(selectedComment, communitiesPagesComment);\n }\n if (commentCid && repliesPagesComment) {\n selectedComment = preferFresher(selectedComment, repliesPagesComment);\n }\n\n // if comment is still not defined, but account comment is, use account comment\n // check `comment.timestamp` instead of `comment` in case comment exists but in a loading state\n const commentFromStoreNotLoaded = !selectedComment?.timestamp;\n if (commentCid && commentFromStoreNotLoaded && accountComment) {\n selectedComment = accountComment;\n }\n\n const selectedCommentState = getCommentStateAndReplyCount(selectedComment).state;\n const freezeSettledForCurrentCid = freezeSettledCid === commentCid;\n\n useEffect(() => {\n if (autoUpdate) {\n setFrozenComment(undefined);\n setFreezeSettledCid(undefined);\n return;\n }\n\n setFrozenComment(undefined);\n setFreezeSettledCid(undefined);\n }, [commentCid, autoUpdate]);\n\n useEffect(() => {\n if (autoUpdate) {\n return;\n }\n if (!commentCid) {\n setFrozenComment(undefined);\n setFreezeSettledCid(undefined);\n return;\n }\n if (freezeSettledForCurrentCid || !selectedComment) {\n return;\n }\n\n setFrozenComment(selectedComment);\n if (selectedCommentState === \"succeeded\") {\n setFreezeSettledCid(commentCid);\n }\n }, [autoUpdate, commentCid, selectedComment, selectedCommentState, freezeSettledForCurrentCid]);\n\n const frozenCommentForCurrentCid = frozenComment?.cid === commentCid ? frozenComment : undefined;\n let comment = autoUpdate\n ? selectedComment\n : freezeSettledForCurrentCid\n ? frozenCommentForCurrentCid\n : frozenCommentForCurrentCid || selectedComment;\n comment = addCommentModeration(comment);\n\n const { state, replyCount } = getCommentStateAndReplyCount(comment);\n\n if (account && commentCid) {\n log(\"useComment\", {\n commentCid,\n comment,\n replyCount,\n state,\n commentFromStore,\n communitiesPagesComment,\n repliesPagesComment,\n accountComment,\n commentsStore: useCommentsStore.getState().comments,\n account,\n onlyIfCached,\n autoUpdate,\n });\n }\n\n const refresh = useCallback(async () => {\n if (!commentCid || !account) {\n throw Error(\"useComment cannot refresh comment not initialized yet\");\n }\n\n const refreshCommentCid = commentCid;\n const refreshedComment = await refreshCommentInStore(refreshCommentCid, account);\n if (!autoUpdate && refreshedComment && currentCommentCidRef.current === refreshCommentCid) {\n setFrozenComment(refreshedComment);\n setFreezeSettledCid(refreshCommentCid);\n }\n }, [account, autoUpdate, commentCid, refreshCommentInStore]);\n\n return useMemo(\n () => ({\n ...comment,\n replyCount,\n state,\n refresh,\n error: errors?.[errors.length - 1],\n errors: errors || [],\n }),\n [comment, commentCid, errors, refresh, state, replyCount],\n );\n}\n\n/**\n * @param commentCids - The IPFS CIDs of the comments to get\n * @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use\n * the active account.\n */\nexport function useComments(options?: UseCommentsOptions): UseCommentsResult {\n assert(\n !options || typeof options === \"object\",\n `useComments options argument '${options}' not an object`,\n );\n const { commentCids = [], accountName, onlyIfCached, autoUpdate = true } = options ?? {};\n const account = useAccount({ accountName });\n const commentsStoreComments: (Comment | undefined)[] = useCommentsStore(\n (state: any) => commentCids.map((commentCid) => state.comments[commentCid || \"\"]),\n shallow,\n );\n const communitiesPagesComments: (Comment | undefined)[] = useCommunitiesPagesStore(\n (state: any) => commentCids.map((commentCid) => state.comments[commentCid || \"\"]),\n shallow,\n );\n\n const addCommentToStore = useCommentsStore((state: any) => state.addCommentToStore);\n const startCommentAutoUpdate = useCommentsStore((state: any) => state.startCommentAutoUpdate);\n const stopCommentAutoUpdate = useCommentsStore((state: any) => state.stopCommentAutoUpdate);\n const refreshCommentInStore = useCommentsStore((state: any) => state.refreshComment);\n const autoUpdateSubscriptionId = useRef(`useComments-${++commentsAutoUpdateSubscriptionCount}`);\n const commentCidsKey = JSON.stringify(commentCids);\n const commentsKey = `${account?.id || \"\"}:${commentCidsKey}`;\n const currentCommentsKeyRef = useRef(commentsKey);\n currentCommentsKeyRef.current = commentsKey;\n const [frozenComments, setFrozenComments] = useState<(Comment | undefined)[]>([]);\n const [frozenCommentsKey, setFrozenCommentsKey] = useState<string>();\n const [freezeSettledKey, setFreezeSettledKey] = useState<string>();\n\n useEffect(() => {\n if (!commentCids || !account) {\n return;\n }\n validator.validateUseCommentsArguments(commentCids, account);\n if (onlyIfCached) {\n return;\n }\n const uniqueCommentCids = new Set(commentCids);\n for (const commentCid of uniqueCommentCids) {\n addCommentToStore(commentCid, account).catch((error: unknown) =>\n log.error(\"useComments addCommentToStore error\", { commentCid, error }),\n );\n }\n }, [commentCidsKey, account?.id, onlyIfCached]);\n\n useEffect(() => {\n if (!commentCids || !account || onlyIfCached || !autoUpdate) {\n return;\n }\n\n const uniqueCommentCids = [...new Set(commentCids)];\n for (const commentCid of uniqueCommentCids) {\n startCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current, account).catch(\n (error: unknown) =>\n log.error(\"useComments startCommentAutoUpdate error\", { commentCid, error }),\n );\n }\n\n return () => {\n for (const commentCid of uniqueCommentCids) {\n stopCommentAutoUpdate(commentCid, autoUpdateSubscriptionId.current).catch(\n (error: unknown) =>\n log.error(\"useComments stopCommentAutoUpdate error\", { commentCid, error }),\n );\n }\n };\n }, [commentCidsKey, account?.id, onlyIfCached, autoUpdate]);\n\n if (account && commentCids?.length) {\n log(\"useComments\", {\n commentCids,\n commentsStoreComments,\n commentsStore: useCommentsStore.getState().comments,\n account,\n });\n }\n\n // if comment from community pages exists and is fresher (or current missing), use it instead\n const liveComments = useMemo(() => {\n const result = [...commentsStoreComments];\n for (const i in result) {\n const candidate = communitiesPagesComments[i];\n if (candidate) result[i] = preferFresher(result[i], candidate);\n }\n return result;\n }, [commentsStoreComments, communitiesPagesComments]);\n\n const liveCommentsSettled = liveComments.every(\n (comment) => getCommentStateAndReplyCount(comment).state === \"succeeded\",\n );\n const freezeSettledForCurrentKey = freezeSettledKey === commentsKey;\n\n useEffect(() => {\n if (autoUpdate) {\n setFrozenComments([]);\n setFrozenCommentsKey(undefined);\n setFreezeSettledKey(undefined);\n return;\n }\n\n setFrozenComments([]);\n setFrozenCommentsKey(undefined);\n setFreezeSettledKey(undefined);\n }, [commentsKey, autoUpdate]);\n\n useEffect(() => {\n if (autoUpdate || freezeSettledForCurrentKey) {\n return;\n }\n\n setFrozenComments(liveComments);\n setFrozenCommentsKey(commentsKey);\n if (liveCommentsSettled) {\n setFreezeSettledKey(commentsKey);\n }\n }, [autoUpdate, commentsKey, freezeSettledForCurrentKey, liveComments, liveCommentsSettled]);\n\n const frozenCommentsForCurrentSelection =\n frozenCommentsKey === commentsKey ? frozenComments : undefined;\n const comments = autoUpdate ? liveComments : frozenCommentsForCurrentSelection || liveComments;\n const normalizedComments = useMemo(() => addCommentModerationToComments(comments), [comments]);\n\n // succeed if no comments are undefined\n const state = getCommentsState(normalizedComments);\n\n const refresh = useCallback(async () => {\n if (!account) {\n throw Error(\"useComments cannot refresh comments not initialized yet\");\n }\n const uniqueCommentCids = [...new Set(commentCids)];\n const refreshedComments = await Promise.all(\n uniqueCommentCids.map((commentCid) => refreshCommentInStore(commentCid, account)),\n );\n\n if (!autoUpdate && currentCommentsKeyRef.current === commentsKey) {\n const latestCommunitiesPagesComments = useCommunitiesPagesStore.getState().comments;\n const refreshedCommentsByCid = uniqueCommentCids.reduce(\n (\n refreshedCommentsMap: { [commentCid: string]: Comment | undefined },\n commentCid,\n index,\n ) => {\n refreshedCommentsMap[commentCid] = refreshedComments[index];\n return refreshedCommentsMap;\n },\n {},\n );\n setFrozenComments(\n commentCids.map((commentCid) =>\n preferFresher(\n refreshedCommentsByCid[commentCid || \"\"],\n latestCommunitiesPagesComments[commentCid || \"\"],\n ),\n ),\n );\n setFrozenCommentsKey(commentsKey);\n setFreezeSettledKey(commentsKey);\n }\n }, [account, autoUpdate, commentCids, commentsKey, refreshCommentInStore]);\n\n return useMemo(\n () => ({\n comments: normalizedComments,\n state,\n refresh,\n error: undefined,\n errors: [],\n }),\n [normalizedComments, commentsKey, refresh, state],\n );\n}\n\nexport function useValidateComment(options?: UseValidateCommentOptions): UseValidateCommentResult {\n assert(\n !options || typeof options === \"object\",\n `useValidateComment options argument '${options}' not an object`,\n );\n let { comment, validateReplies, accountName } = options ?? {};\n validateReplies = validateReplies ?? true;\n const [validated, setValidated] = useState<boolean | undefined>();\n const [errors] = useState([]);\n const account = useAccount({ accountName });\n\n useEffect(() => {\n if (!comment || !account?.pkc) {\n setValidated(undefined);\n return;\n }\n // don't automatically block community because what community it comes from\n // a malicious community could try to block other communities, etc\n const blockCommunity = false;\n commentIsValid(comment, { validateReplies, blockCommunity }, account.pkc).then((validated) =>\n setValidated(validated),\n );\n }, [comment, validateReplies, account?.pkc]);\n\n let state = \"initializing\";\n if (validated === true) {\n state = \"succeeded\";\n }\n if (validated === false) {\n state = \"failed\";\n }\n\n // start valid at true always because most of the time the value will be true and we dont want to cause a rerender\n let valid = true;\n if (validated == false) {\n valid = false;\n }\n // if comment isn't defined, it would be confusing for valid to be true\n if (!comment) {\n valid = false;\n }\n\n return useMemo(\n () => ({\n valid,\n state,\n error: errors[errors.length - 1],\n errors,\n }),\n [valid, state],\n );\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ChainProviders, UseResolvedCommunityAddressOptions, UseResolvedCommunityAddressResult, UseCommunityOptions, UseCommunityResult, UseCommunitiesOptions, UseCommunitiesResult, UseCommunityStatsOptions, UseCommunityStatsResult } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* @param community - The community identifier, e.g. {name: 'memes.eth'} or {publicKey: '12D3KooW...'}
|
|
4
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
5
|
+
* the active account.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useCommunity(options?: UseCommunityOptions): UseCommunityResult;
|
|
8
|
+
/**
|
|
9
|
+
* @param community - The community identifier, e.g. {name: 'memes.eth'} or {publicKey: '12D3KooW...'}
|
|
10
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
11
|
+
* the active account.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useCommunityStats(options?: UseCommunityStatsOptions): UseCommunityStatsResult;
|
|
14
|
+
/**
|
|
15
|
+
* @param communities - The communities to fetch, e.g. [{name: 'memes.eth'}, {publicKey: '12D3KooW...'}]
|
|
16
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
17
|
+
* the active account.
|
|
18
|
+
*/
|
|
19
|
+
export declare function useCommunities(options?: UseCommunitiesOptions): UseCommunitiesResult;
|
|
20
|
+
/**
|
|
21
|
+
* Returns all the owner communities created by pkc-js by calling pkc.listCommunities()
|
|
22
|
+
*/
|
|
23
|
+
export declare function useListCommunities(accountName?: string): string[];
|
|
24
|
+
/**
|
|
25
|
+
* @param communityAddress - The community address to resolve to a public key, e.g. 'news.eth' resolves to '12D3KooW...'.
|
|
26
|
+
* @param acountName - The nickname of the account, e.g. 'Account 1'. If no accountName is provided, use
|
|
27
|
+
* the active account.
|
|
28
|
+
*/
|
|
29
|
+
export declare function useResolvedCommunityAddress(options?: UseResolvedCommunityAddressOptions): UseResolvedCommunityAddressResult;
|
|
30
|
+
export declare const resolveCommunityAddress: (communityAddress: string, chainProviders: ChainProviders) => Promise<any>;
|
|
31
|
+
//# sourceMappingURL=communities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"communities.d.ts","sourceRoot":"","sources":["../../src/hooks/communities.ts"],"names":[],"mappings":"AAMA,OAAO,EAGL,cAAc,EACd,kCAAkC,EAClC,iCAAiC,EACjC,mBAAmB,EACnB,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,wBAAwB,EACxB,uBAAuB,EACxB,MAAM,UAAU,CAAC;AAyClB;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,kBAAkB,CAgH9E;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,CAAC,EAAE,wBAAwB,GAAG,uBAAuB,CAyF7F;AAeD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,oBAAoB,CA2EpF;AAGD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,CAAC,EAAE,MAAM,YAqBtD;AAED;;;;GAIG;AAEH,wBAAgB,2BAA2B,CACzC,OAAO,CAAC,EAAE,kCAAkC,GAC3C,iCAAiC,CAgGnC;AAGD,eAAO,MAAM,uBAAuB,GAClC,kBAAkB,MAAM,EACxB,gBAAgB,cAAc,iBAe/B,CAAC"}
|