@bitsocial/bitsocial-react-hooks 0.1.30 → 0.1.32
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/CHANGELOG.md +20 -1
- package/README.md +24 -7
- package/dist/hooks/comments.d.ts.map +1 -1
- package/dist/hooks/comments.js +5 -1
- package/dist/hooks/comments.js.map +1 -1
- package/dist/hooks/communities.d.ts.map +1 -1
- package/dist/hooks/communities.js +2 -1
- package/dist/hooks/communities.js.map +1 -1
- package/dist/hooks/replies.d.ts.map +1 -1
- package/dist/hooks/replies.js +6 -3
- package/dist/hooks/replies.js.map +1 -1
- package/dist/lib/utils/optimistic-vote-counts.d.ts +11 -0
- package/dist/lib/utils/optimistic-vote-counts.d.ts.map +1 -0
- package/dist/lib/utils/optimistic-vote-counts.js +70 -0
- package/dist/lib/utils/optimistic-vote-counts.js.map +1 -0
- package/dist/stores/accounts/accounts-actions.d.ts.map +1 -1
- package/dist/stores/accounts/accounts-actions.js +9 -2
- package/dist/stores/accounts/accounts-actions.js.map +1 -1
- package/dist/stores/accounts/utils.d.ts +2 -0
- package/dist/stores/accounts/utils.d.ts.map +1 -1
- package/dist/stores/accounts/utils.js +15 -0
- package/dist/stores/accounts/utils.js.map +1 -1
- package/dist/stores/communities/communities-store.d.ts +9 -1
- package/dist/stores/communities/communities-store.d.ts.map +1 -1
- package/dist/stores/communities/communities-store.js +36 -3
- package/dist/stores/communities/communities-store.js.map +1 -1
- package/dist/stores/replies/replies-store.d.ts.map +1 -1
- package/dist/stores/replies/replies-store.js +12 -8
- package/dist/stores/replies/replies-store.js.map +1 -1
- package/dist/stores/replies/utils.d.ts.map +1 -1
- package/dist/stores/replies/utils.js +6 -13
- package/dist/stores/replies/utils.js.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +22 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replies.js","sourceRo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AC;IACF,MAAM,wBAAwB,GAAG,OAAO,CACtC,GAAG,EAAE,CAAC,8BAA8B,CAAC,cAAc,CAAC,EACpD,CAAC,cAAc,CAAC,CACjB,CAAC;IAEF,OAAO,OAAO,CACZ,GAAG,EAAE,CAAC,CAAC;QACL,OAAO,EAAE,iBAAiB;QAC1B,eAAe,EAAE,yBAAyB;QAC1C,cAAc,EAAE,wBAAwB;QACxC,OAAO;QACP,QAAQ;QACR,KAAK;QACL,KAAK;QACL,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;QAChC,MAAM;KACP,CAAC,EACF;QACE,iBAAiB;QACjB,yBAAyB;QACzB,wBAAwB;QACxB,eAAe;QACf,OAAO;QACP,MAAM;KACP,CACF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,GAAQ,EAAE,CAAC;AAC3B,MAAM,aAAa,GAAG,GAAS,EAAE,kDAAE,CAAC,CAAA,CAAC","sourcesContent":["import { useEffect, useState, useMemo } from \"react\";\nimport { useAccount } from \"./accounts\";\nimport validator from \"../lib/validator\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:replies:hooks\");\nimport assert from \"assert\";\nimport { UseRepliesOptions, UseRepliesResult } from \"../types\";\nimport { addCommentModerationToComments } from \"../lib/utils/comment-moderation\";\nimport useRepliesStore, {\n RepliesState,\n feedOptionsToFeedName,\n getRepliesFirstPageSkipValidation,\n} from \"../stores/replies\";\n\n/** Pure helper to append an error to the errors array; used for deterministic coverage of reset/loadMore catch paths. */\nexport function appendErrorToErrors(prevErrors: Error[], e: Error): Error[] {\n return [...prevErrors, e];\n}\n\nexport function useReplies(options?: UseRepliesOptions): UseRepliesResult {\n assert(\n !options || typeof options === \"object\",\n `useReplies options argument '${options}' not an object`,\n );\n const opts = options ?? {};\n let {\n comment,\n sortType,\n accountName,\n onlyIfCached,\n flat,\n flatDepth,\n accountComments,\n repliesPerPage,\n filter,\n validateOptimistically,\n streamPage,\n } = opts;\n sortType = sortType || \"best\";\n flatDepth = typeof flatDepth === \"number\" ? flatDepth : 0;\n validateOptimistically = validateOptimistically !== false;\n const invalidFlatDepth =\n flat && typeof comment?.depth === \"number\" && flatDepth !== comment.depth;\n validator.validateUseRepliesArguments(\n comment,\n sortType,\n accountName,\n onlyIfCached,\n flat,\n accountComments,\n repliesPerPage,\n filter,\n );\n\n const [errors, setErrors] = useState<Error[]>([]);\n\n // add replies to store\n const account = useAccount({ accountName });\n const feedOptions = {\n commentCid: comment?.cid,\n commentDepth: comment?.depth,\n postCid: comment?.postCid,\n sortType,\n accountId: account?.id,\n onlyIfCached,\n repliesPerPage,\n flat,\n accountComments,\n filter,\n streamPage,\n };\n const repliesFeedName = feedOptionsToFeedName(feedOptions);\n const addFeedToStoreOrUpdateComment = useRepliesStore(\n (state: RepliesState) => state.addFeedToStoreOrUpdateComment,\n );\n useEffect(() => {\n if (!comment?.cid || !account || invalidFlatDepth) {\n return;\n }\n addFeedToStoreOrUpdateComment(comment, feedOptions).catch((error: unknown) =>\n log.error(\"useReplies addFeedToStoreOrUpdateComment error\", {\n repliesFeedName,\n comment,\n feedOptions,\n error,\n }),\n );\n }, [repliesFeedName, comment]);\n\n let replies = useRepliesStore((state: RepliesState) => state.loadedFeeds[repliesFeedName || \"\"]);\n let bufferedReplies = useRepliesStore(\n (state: RepliesState) => state.bufferedFeeds[repliesFeedName || \"\"],\n );\n let updatedReplies = useRepliesStore(\n (state: RepliesState) => state.updatedFeeds[repliesFeedName || \"\"],\n );\n let hasMore = useRepliesStore(\n (state: RepliesState) => state.feedsHaveMore[repliesFeedName || \"\"],\n );\n hasMore = comment\n ? repliesFeedName && typeof hasMore === \"boolean\"\n ? hasMore\n : !onlyIfCached\n : false;\n\n const incrementFeedPageNumber = useRepliesStore(\n (state: RepliesState) => state.incrementFeedPageNumber,\n );\n let loadMore = async () => {\n try {\n if (!comment?.cid || !account) {\n throw Error(\"useReplies cannot load more replies not initalized yet\");\n }\n incrementFeedPageNumber(repliesFeedName);\n } catch (e: any) {\n await new Promise((r) => setTimeout(r, 50));\n setErrors(appendErrorToErrors(errors, e));\n }\n };\n\n const resetFeed = useRepliesStore((state: RepliesState) => state.resetFeed);\n let reset = async () => {\n try {\n if (!comment?.cid || !account) {\n throw Error(\"useReplies cannot reset replies not initalized yet\");\n }\n resetFeed(repliesFeedName);\n } catch (e: any) {\n await new Promise((r) => setTimeout(r, 50));\n setErrors(appendErrorToErrors(errors, e));\n }\n };\n\n // optimistically avoid the initial validation delay by using skipped validation until validated feed is loaded\n const skipValidation = useMemo(() => {\n if (validateOptimistically && !replies && comment?.cid && account?.id) {\n return getRepliesFirstPageSkipValidation(comment, feedOptions);\n }\n }, [validateOptimistically, replies, comment?.cid, account?.id, comment, repliesFeedName]);\n if (validateOptimistically && !replies && skipValidation?.replies?.length) {\n replies = skipValidation.replies;\n hasMore = skipValidation.hasMore;\n }\n\n // don't display nested replies when flat\n // to start flat replies at a depth other than 0, e.g. a twitter reply thread, change flatDepth\n if (invalidFlatDepth) {\n replies = emptyArray;\n bufferedReplies = emptyArray;\n updatedReplies = emptyArray;\n hasMore = false;\n loadMore = emptyFunction;\n reset = emptyFunction;\n }\n\n if (account && comment?.cid) {\n log(\"useReplies\", {\n repliesLength: replies?.length || 0,\n hasMore,\n comment,\n sortType,\n onlyIfCached,\n flat,\n flatDepth,\n repliesStoreOptions: useRepliesStore.getState().feedsOptions,\n repliesStore: useRepliesStore.getState(),\n invalidFlatDepth,\n });\n }\n\n const state = !hasMore ? \"succeeded\" : \"fetching\";\n const normalizedReplies = useMemo(() => addCommentModerationToComments(replies), [replies]);\n const normalizedBufferedReplies = useMemo(\n () => addCommentModerationToComments(bufferedReplies),\n [bufferedReplies],\n );\n const normalizedUpdatedReplies = useMemo(\n () => addCommentModerationToComments(updatedReplies),\n [updatedReplies],\n );\n\n return useMemo(\n () => ({\n replies: normalizedReplies,\n bufferedReplies: normalizedBufferedReplies,\n updatedReplies: normalizedUpdatedReplies,\n hasMore,\n loadMore,\n reset,\n state,\n error: errors[errors.length - 1],\n errors,\n }),\n [\n normalizedReplies,\n normalizedBufferedReplies,\n normalizedUpdatedReplies,\n repliesFeedName,\n hasMore,\n errors,\n ],\n );\n}\n\nconst emptyArray: any = [];\nconst emptyFunction = async () => {};\n"]}
|
|
1
|
+
{"version":3,"file":"replies.js","sourceRoot":"","sources":["../../src/hooks/replies.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AACrD,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,qCAAqC,CAAC,CAAC;AAC1D,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,8BAA8B,EAAE,MAAM,iCAAiC,CAAC;AACjF,OAAO,eAAe,EAAE,EAEtB,qBAAqB,EACrB,iCAAiC,GAClC,MAAM,mBAAmB,CAAC;AAC3B,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AAExF,yHAAyH;AACzH,MAAM,UAAU,mBAAmB,CAAC,UAAmB,EAAE,CAAQ;IAC/D,OAAO,CAAC,GAAG,UAAU,EAAE,CAAC,CAAC,CAAC;AAC5B,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,OAA2B;;IACpD,MAAM,CACJ,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EACvC,gCAAgC,OAAO,iBAAiB,CACzD,CAAC;IACF,MAAM,IAAI,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;IAC3B,IAAI,EACF,OAAO,EACP,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,IAAI,EACJ,SAAS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AS,EAAE,kDAAE,CAAC,CAAA,CAAC","sourcesContent":["import { useEffect, useState, useMemo } from \"react\";\nimport { useAccount } from \"./accounts\";\nimport validator from \"../lib/validator\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\nconst log = Logger(\"bitsocial-react-hooks:replies:hooks\");\nimport assert from \"assert\";\nimport { UseRepliesOptions, UseRepliesResult } from \"../types\";\nimport { addCommentModerationToComments } from \"../lib/utils/comment-moderation\";\nimport useRepliesStore, {\n RepliesState,\n feedOptionsToFeedName,\n getRepliesFirstPageSkipValidation,\n} from \"../stores/replies\";\nimport useAccountsStore from \"../stores/accounts\";\nimport { addOptimisticVoteCountsToComments } from \"../lib/utils/optimistic-vote-counts\";\n\n/** Pure helper to append an error to the errors array; used for deterministic coverage of reset/loadMore catch paths. */\nexport function appendErrorToErrors(prevErrors: Error[], e: Error): Error[] {\n return [...prevErrors, e];\n}\n\nexport function useReplies(options?: UseRepliesOptions): UseRepliesResult {\n assert(\n !options || typeof options === \"object\",\n `useReplies options argument '${options}' not an object`,\n );\n const opts = options ?? {};\n let {\n comment,\n sortType,\n accountName,\n onlyIfCached,\n flat,\n flatDepth,\n accountComments,\n repliesPerPage,\n filter,\n validateOptimistically,\n streamPage,\n } = opts;\n sortType = sortType || \"best\";\n flatDepth = typeof flatDepth === \"number\" ? flatDepth : 0;\n validateOptimistically = validateOptimistically !== false;\n const invalidFlatDepth =\n flat && typeof comment?.depth === \"number\" && flatDepth !== comment.depth;\n validator.validateUseRepliesArguments(\n comment,\n sortType,\n accountName,\n onlyIfCached,\n flat,\n accountComments,\n repliesPerPage,\n filter,\n );\n\n const [errors, setErrors] = useState<Error[]>([]);\n\n // add replies to store\n const account = useAccount({ accountName });\n const accountVotes = useAccountsStore((state: any) =>\n account?.id ? state.accountsVotes[account.id] : undefined,\n );\n const feedOptions = {\n commentCid: comment?.cid,\n commentDepth: comment?.depth,\n postCid: comment?.postCid,\n sortType,\n accountId: account?.id,\n onlyIfCached,\n repliesPerPage,\n flat,\n accountComments,\n filter,\n streamPage,\n };\n const repliesFeedName = feedOptionsToFeedName(feedOptions);\n const addFeedToStoreOrUpdateComment = useRepliesStore(\n (state: RepliesState) => state.addFeedToStoreOrUpdateComment,\n );\n useEffect(() => {\n if (!comment?.cid || !account || invalidFlatDepth) {\n return;\n }\n addFeedToStoreOrUpdateComment(comment, feedOptions).catch((error: unknown) =>\n log.error(\"useReplies addFeedToStoreOrUpdateComment error\", {\n repliesFeedName,\n comment,\n feedOptions,\n error,\n }),\n );\n }, [repliesFeedName, comment]);\n\n let replies = useRepliesStore((state: RepliesState) => state.loadedFeeds[repliesFeedName || \"\"]);\n let bufferedReplies = useRepliesStore(\n (state: RepliesState) => state.bufferedFeeds[repliesFeedName || \"\"],\n );\n let updatedReplies = useRepliesStore(\n (state: RepliesState) => state.updatedFeeds[repliesFeedName || \"\"],\n );\n let hasMore = useRepliesStore(\n (state: RepliesState) => state.feedsHaveMore[repliesFeedName || \"\"],\n );\n hasMore = comment\n ? repliesFeedName && typeof hasMore === \"boolean\"\n ? hasMore\n : !onlyIfCached\n : false;\n\n const incrementFeedPageNumber = useRepliesStore(\n (state: RepliesState) => state.incrementFeedPageNumber,\n );\n let loadMore = async () => {\n try {\n if (!comment?.cid || !account) {\n throw Error(\"useReplies cannot load more replies not initalized yet\");\n }\n incrementFeedPageNumber(repliesFeedName);\n } catch (e: any) {\n await new Promise((r) => setTimeout(r, 50));\n setErrors(appendErrorToErrors(errors, e));\n }\n };\n\n const resetFeed = useRepliesStore((state: RepliesState) => state.resetFeed);\n let reset = async () => {\n try {\n if (!comment?.cid || !account) {\n throw Error(\"useReplies cannot reset replies not initalized yet\");\n }\n resetFeed(repliesFeedName);\n } catch (e: any) {\n await new Promise((r) => setTimeout(r, 50));\n setErrors(appendErrorToErrors(errors, e));\n }\n };\n\n // optimistically avoid the initial validation delay by using skipped validation until validated feed is loaded\n const skipValidation = useMemo(() => {\n if (validateOptimistically && !replies && comment?.cid && account?.id) {\n return getRepliesFirstPageSkipValidation(comment, feedOptions);\n }\n }, [validateOptimistically, replies, comment?.cid, account?.id, comment, repliesFeedName]);\n if (validateOptimistically && !replies && skipValidation?.replies?.length) {\n replies = skipValidation.replies;\n hasMore = skipValidation.hasMore;\n }\n\n // don't display nested replies when flat\n // to start flat replies at a depth other than 0, e.g. a twitter reply thread, change flatDepth\n if (invalidFlatDepth) {\n replies = emptyArray;\n bufferedReplies = emptyArray;\n updatedReplies = emptyArray;\n hasMore = false;\n loadMore = emptyFunction;\n reset = emptyFunction;\n }\n\n if (account && comment?.cid) {\n log(\"useReplies\", {\n repliesLength: replies?.length || 0,\n hasMore,\n comment,\n sortType,\n onlyIfCached,\n flat,\n flatDepth,\n repliesStoreOptions: useRepliesStore.getState().feedsOptions,\n repliesStore: useRepliesStore.getState(),\n invalidFlatDepth,\n });\n }\n\n const state = !hasMore ? \"succeeded\" : \"fetching\";\n const normalizedReplies = useMemo(\n () => addOptimisticVoteCountsToComments(addCommentModerationToComments(replies), accountVotes),\n [replies, accountVotes],\n );\n const normalizedBufferedReplies = useMemo(\n () =>\n addOptimisticVoteCountsToComments(\n addCommentModerationToComments(bufferedReplies),\n accountVotes,\n ),\n [bufferedReplies, accountVotes],\n );\n const normalizedUpdatedReplies = useMemo(\n () =>\n addOptimisticVoteCountsToComments(\n addCommentModerationToComments(updatedReplies),\n accountVotes,\n ),\n [updatedReplies, accountVotes],\n );\n\n return useMemo(\n () => ({\n replies: normalizedReplies,\n bufferedReplies: normalizedBufferedReplies,\n updatedReplies: normalizedUpdatedReplies,\n hasMore,\n loadMore,\n reset,\n state,\n error: errors[errors.length - 1],\n errors,\n }),\n [\n normalizedReplies,\n normalizedBufferedReplies,\n normalizedUpdatedReplies,\n repliesFeedName,\n hasMore,\n errors,\n ],\n );\n}\n\nconst emptyArray: any = [];\nconst emptyFunction = async () => {};\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AccountVote, Comment } from "../../types.js";
|
|
2
|
+
export declare const optimisticVoteBaseKey = "_optimisticVoteBase";
|
|
3
|
+
export declare const optimisticVoteObservedAtKey = "_optimisticVoteObservedAt";
|
|
4
|
+
export declare const optimisticVoteTransitionsKey = "_optimisticVoteTransitions";
|
|
5
|
+
export declare const getCommentVoteCountsVersion: (comment: Comment | undefined) => number;
|
|
6
|
+
export declare const addOptimisticVoteMetadata: (accountVote: AccountVote, previousAccountVote: AccountVote | undefined, comment: Comment | undefined) => AccountVote;
|
|
7
|
+
export declare const addOptimisticVoteCounts: (comment: Comment | undefined, accountVote: AccountVote | undefined) => Comment | undefined;
|
|
8
|
+
export declare const addOptimisticVoteCountsToComments: <T extends Comment | undefined>(comments: T[] | undefined, accountVotes: {
|
|
9
|
+
[commentCid: string]: AccountVote;
|
|
10
|
+
} | undefined) => T[];
|
|
11
|
+
//# sourceMappingURL=optimistic-vote-counts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optimistic-vote-counts.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/optimistic-vote-counts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEnD,eAAO,MAAM,qBAAqB,wBAAwB,CAAC;AAC3D,eAAO,MAAM,2BAA2B,8BAA8B,CAAC;AACvE,eAAO,MAAM,4BAA4B,+BAA+B,CAAC;AASzE,eAAO,MAAM,2BAA2B,GAAI,SAAS,OAAO,GAAG,SAAS,KAAG,MACZ,CAAC;AAEhE,eAAO,MAAM,yBAAyB,GACpC,aAAa,WAAW,EACxB,qBAAqB,WAAW,GAAG,SAAS,EAC5C,SAAS,OAAO,GAAG,SAAS,KAC3B,WAoDF,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAClC,SAAS,OAAO,GAAG,SAAS,EAC5B,aAAa,WAAW,GAAG,SAAS,KACnC,OAAO,GAAG,SAkCZ,CAAC;AAEF,eAAO,MAAM,iCAAiC,GAAI,CAAC,SAAS,OAAO,GAAG,SAAS,EAC7E,UAAU,CAAC,EAAE,GAAG,SAAS,EACzB,cAAc;IAAE,CAAC,UAAU,EAAE,MAAM,GAAG,WAAW,CAAA;CAAE,GAAG,SAAS,KAC9D,CAAC,EAGM,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export const optimisticVoteBaseKey = "_optimisticVoteBase";
|
|
2
|
+
export const optimisticVoteObservedAtKey = "_optimisticVoteObservedAt";
|
|
3
|
+
export const optimisticVoteTransitionsKey = "_optimisticVoteTransitions";
|
|
4
|
+
const normalizeVote = (vote) => (vote === 1 || vote === -1 ? vote : 0);
|
|
5
|
+
export const getCommentVoteCountsVersion = (comment) => { var _a, _b; return Math.max((_a = comment === null || comment === void 0 ? void 0 : comment.updatedAt) !== null && _a !== void 0 ? _a : 0, (_b = comment === null || comment === void 0 ? void 0 : comment.timestamp) !== null && _b !== void 0 ? _b : 0, 0); };
|
|
6
|
+
export const addOptimisticVoteMetadata = (accountVote, previousAccountVote, comment) => {
|
|
7
|
+
const currentVersion = getCommentVoteCountsVersion(comment);
|
|
8
|
+
// Keep the full local transition chain so cached comment copies at different
|
|
9
|
+
// versions can each determine which account vote their counts already include.
|
|
10
|
+
const previousTransitions = Array.isArray(previousAccountVote === null || previousAccountVote === void 0 ? void 0 : previousAccountVote[optimisticVoteTransitionsKey])
|
|
11
|
+
? previousAccountVote[optimisticVoteTransitionsKey]
|
|
12
|
+
: [];
|
|
13
|
+
const hasPreviousTransitionHistory = previousTransitions.length > 0 &&
|
|
14
|
+
typeof (previousAccountVote === null || previousAccountVote === void 0 ? void 0 : previousAccountVote[optimisticVoteObservedAtKey]) === "number";
|
|
15
|
+
const recoveredPreviousTransitions = !hasPreviousTransitionHistory &&
|
|
16
|
+
previousAccountVote &&
|
|
17
|
+
typeof previousAccountVote.timestamp === "number"
|
|
18
|
+
? [
|
|
19
|
+
{
|
|
20
|
+
vote: normalizeVote(previousAccountVote.vote),
|
|
21
|
+
timestamp: previousAccountVote.timestamp,
|
|
22
|
+
},
|
|
23
|
+
]
|
|
24
|
+
: [];
|
|
25
|
+
const transitions = hasPreviousTransitionHistory
|
|
26
|
+
? previousTransitions
|
|
27
|
+
: recoveredPreviousTransitions;
|
|
28
|
+
const observedAt = hasPreviousTransitionHistory
|
|
29
|
+
? previousAccountVote[optimisticVoteObservedAtKey]
|
|
30
|
+
: currentVersion || Math.max((accountVote.timestamp || 1) - 1, 0);
|
|
31
|
+
const baseVote = hasPreviousTransitionHistory
|
|
32
|
+
? normalizeVote(previousAccountVote === null || previousAccountVote === void 0 ? void 0 : previousAccountVote[optimisticVoteBaseKey])
|
|
33
|
+
: recoveredPreviousTransitions.length > 0
|
|
34
|
+
? 0
|
|
35
|
+
: normalizeVote(previousAccountVote === null || previousAccountVote === void 0 ? void 0 : previousAccountVote.vote);
|
|
36
|
+
const lastTransition = transitions[transitions.length - 1];
|
|
37
|
+
const transition = {
|
|
38
|
+
vote: normalizeVote(accountVote.vote),
|
|
39
|
+
// A displayed comment can only include this vote after both the vote was
|
|
40
|
+
// published and every comment copy used to establish the initial baseline.
|
|
41
|
+
timestamp: Math.max(accountVote.timestamp || 0, observedAt + 1, lastTransition ? lastTransition.timestamp + 1 : 0),
|
|
42
|
+
};
|
|
43
|
+
return Object.assign(Object.assign({}, accountVote), { [optimisticVoteBaseKey]: baseVote, [optimisticVoteObservedAtKey]: observedAt, [optimisticVoteTransitionsKey]: [...transitions, transition] });
|
|
44
|
+
};
|
|
45
|
+
export const addOptimisticVoteCounts = (comment, accountVote) => {
|
|
46
|
+
if (!comment ||
|
|
47
|
+
!accountVote ||
|
|
48
|
+
typeof accountVote[optimisticVoteObservedAtKey] !== "number" ||
|
|
49
|
+
!Array.isArray(accountVote[optimisticVoteTransitionsKey]) ||
|
|
50
|
+
typeof comment.upvoteCount !== "number" ||
|
|
51
|
+
typeof comment.downvoteCount !== "number") {
|
|
52
|
+
return comment;
|
|
53
|
+
}
|
|
54
|
+
const commentVersion = getCommentVoteCountsVersion(comment);
|
|
55
|
+
let baseVote = normalizeVote(accountVote[optimisticVoteBaseKey]);
|
|
56
|
+
// Each transition timestamp already includes the initial baseline, so every
|
|
57
|
+
// displayed copy can reconcile directly against its own canonical version.
|
|
58
|
+
for (const transition of accountVote[optimisticVoteTransitionsKey]) {
|
|
59
|
+
if (typeof (transition === null || transition === void 0 ? void 0 : transition.timestamp) === "number" && transition.timestamp <= commentVersion) {
|
|
60
|
+
baseVote = normalizeVote(transition.vote);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const vote = normalizeVote(accountVote.vote);
|
|
64
|
+
if (baseVote === vote) {
|
|
65
|
+
return comment;
|
|
66
|
+
}
|
|
67
|
+
return Object.assign(Object.assign({}, comment), { upvoteCount: Math.max(0, comment.upvoteCount + Number(vote === 1) - Number(baseVote === 1)), downvoteCount: Math.max(0, comment.downvoteCount + Number(vote === -1) - Number(baseVote === -1)) });
|
|
68
|
+
};
|
|
69
|
+
export const addOptimisticVoteCountsToComments = (comments, accountVotes) => (comments || []).map((comment) => addOptimisticVoteCounts(comment, (comment === null || comment === void 0 ? void 0 : comment.cid) ? accountVotes === null || accountVotes === void 0 ? void 0 : accountVotes[comment.cid] : undefined));
|
|
70
|
+
//# sourceMappingURL=optimistic-vote-counts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optimistic-vote-counts.js","sourceRoot":"","sources":["../../../src/lib/utils/optimistic-vote-counts.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,qBAAqB,GAAG,qBAAqB,CAAC;AAC3D,MAAM,CAAC,MAAM,2BAA2B,GAAG,2BAA2B,CAAC;AACvE,MAAM,CAAC,MAAM,4BAA4B,GAAG,4BAA4B,CAAC;AAEzE,MAAM,aAAa,GAAG,CAAC,IAAa,EAAc,EAAE,CAAC,CAAC,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC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AQ,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAC/B,uBAAuB,CAAC,OAAO,EAAE,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,EAAC,CAAC,CAAC,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAClF,CAAC","sourcesContent":["import { AccountVote, Comment } from \"../../types\";\n\nexport const optimisticVoteBaseKey = \"_optimisticVoteBase\";\nexport const optimisticVoteObservedAtKey = \"_optimisticVoteObservedAt\";\nexport const optimisticVoteTransitionsKey = \"_optimisticVoteTransitions\";\n\nconst normalizeVote = (vote: unknown): -1 | 0 | 1 => (vote === 1 || vote === -1 ? vote : 0);\n\ntype OptimisticVoteTransition = {\n vote: -1 | 0 | 1;\n timestamp: number;\n};\n\nexport const getCommentVoteCountsVersion = (comment: Comment | undefined): number =>\n Math.max(comment?.updatedAt ?? 0, comment?.timestamp ?? 0, 0);\n\nexport const addOptimisticVoteMetadata = (\n accountVote: AccountVote,\n previousAccountVote: AccountVote | undefined,\n comment: Comment | undefined,\n): AccountVote => {\n const currentVersion = getCommentVoteCountsVersion(comment);\n // Keep the full local transition chain so cached comment copies at different\n // versions can each determine which account vote their counts already include.\n const previousTransitions: OptimisticVoteTransition[] = Array.isArray(\n previousAccountVote?.[optimisticVoteTransitionsKey],\n )\n ? previousAccountVote[optimisticVoteTransitionsKey]\n : [];\n const hasPreviousTransitionHistory =\n previousTransitions.length > 0 &&\n typeof previousAccountVote?.[optimisticVoteObservedAtKey] === \"number\";\n const recoveredPreviousTransitions: OptimisticVoteTransition[] =\n !hasPreviousTransitionHistory &&\n previousAccountVote &&\n typeof previousAccountVote.timestamp === \"number\"\n ? [\n {\n vote: normalizeVote(previousAccountVote.vote),\n timestamp: previousAccountVote.timestamp,\n },\n ]\n : [];\n const transitions = hasPreviousTransitionHistory\n ? previousTransitions\n : recoveredPreviousTransitions;\n const observedAt = hasPreviousTransitionHistory\n ? previousAccountVote[optimisticVoteObservedAtKey]\n : currentVersion || Math.max((accountVote.timestamp || 1) - 1, 0);\n const baseVote = hasPreviousTransitionHistory\n ? normalizeVote(previousAccountVote?.[optimisticVoteBaseKey])\n : recoveredPreviousTransitions.length > 0\n ? 0\n : normalizeVote(previousAccountVote?.vote);\n const lastTransition = transitions[transitions.length - 1];\n const transition: OptimisticVoteTransition = {\n vote: normalizeVote(accountVote.vote),\n // A displayed comment can only include this vote after both the vote was\n // published and every comment copy used to establish the initial baseline.\n timestamp: Math.max(\n accountVote.timestamp || 0,\n observedAt + 1,\n lastTransition ? lastTransition.timestamp + 1 : 0,\n ),\n };\n\n return {\n ...accountVote,\n [optimisticVoteBaseKey]: baseVote,\n [optimisticVoteObservedAtKey]: observedAt,\n [optimisticVoteTransitionsKey]: [...transitions, transition],\n };\n};\n\nexport const addOptimisticVoteCounts = (\n comment: Comment | undefined,\n accountVote: AccountVote | undefined,\n): Comment | undefined => {\n if (\n !comment ||\n !accountVote ||\n typeof accountVote[optimisticVoteObservedAtKey] !== \"number\" ||\n !Array.isArray(accountVote[optimisticVoteTransitionsKey]) ||\n typeof comment.upvoteCount !== \"number\" ||\n typeof comment.downvoteCount !== \"number\"\n ) {\n return comment;\n }\n\n const commentVersion = getCommentVoteCountsVersion(comment);\n let baseVote = normalizeVote(accountVote[optimisticVoteBaseKey]);\n // Each transition timestamp already includes the initial baseline, so every\n // displayed copy can reconcile directly against its own canonical version.\n for (const transition of accountVote[optimisticVoteTransitionsKey]) {\n if (typeof transition?.timestamp === \"number\" && transition.timestamp <= commentVersion) {\n baseVote = normalizeVote(transition.vote);\n }\n }\n const vote = normalizeVote(accountVote.vote);\n if (baseVote === vote) {\n return comment;\n }\n\n return {\n ...comment,\n upvoteCount: Math.max(0, comment.upvoteCount + Number(vote === 1) - Number(baseVote === 1)),\n downvoteCount: Math.max(\n 0,\n comment.downvoteCount + Number(vote === -1) - Number(baseVote === -1),\n ),\n };\n};\n\nexport const addOptimisticVoteCountsToComments = <T extends Comment | undefined>(\n comments: T[] | undefined,\n accountVotes: { [commentCid: string]: AccountVote } | undefined,\n): T[] =>\n (comments || []).map((comment) =>\n addOptimisticVoteCounts(comment, comment?.cid ? accountVotes?.[comment.cid] : undefined),\n ) as T[];\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accounts-actions.d.ts","sourceRoot":"","sources":["../../../src/stores/accounts/accounts-actions.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,OAAO,EAEP,qBAAqB,EAGrB,kBAAkB,EAClB,yBAAyB,EACzB,+BAA+B,EAC/B,2BAA2B,EAC3B,sBAAsB,EACtB,sBAAsB,EACtB,eAAe,EAGhB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"accounts-actions.d.ts","sourceRoot":"","sources":["../../../src/stores/accounts/accounts-actions.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,OAAO,EAEP,qBAAqB,EAGrB,kBAAkB,EAClB,yBAAyB,EACzB,+BAA+B,EAC/B,2BAA2B,EAC3B,sBAAsB,EACtB,sBAAsB,EACtB,eAAe,EAGhB,MAAM,aAAa,CAAC;AAmKrB,sGAAsG;AACtG,eAAO,MAAM,yBAAyB,GACpC,kBAAkB,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,EACvC,WAAW,MAAM,EACjB,OAAO,MAAM,EACb,SAAS,CAAC,eAAe,EAAE,GAAG,EAAE,EAAE,cAAc,EAAE,GAAG,KAAK,IAAI;;;;;;CAO/D,CAAC;AA2BF,eAAO,MAAM,0BAA0B,GAAI,mBAAmB,GAAG,EAAE,yBAAyB,GAAG,YAGtC,CAAC;AAE1D,eAAO,MAAM,yBAAyB,GAAI,mBAAmB,GAAG,QAK/D,CAAC;AAuCF,eAAO,MAAM,kCAAkC,GAC7C,wBAAwB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CA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30
|
const log = Logger("bitsocial-react-hooks:accounts:stores");
|
|
31
31
|
import * as accountsActionsInternal from "./accounts-actions-internal.js";
|
|
32
32
|
import { backfillPublicationCommunityAddress, createPkcCommunity, createPkcCommunityEdit, getPkcCommunityAddresses, normalizeCommunityEditOptionsForPkc, normalizePublicationOptionsForStore, normalizePublicationOptionsForPkc, withPublishChallengeAnswersCompat, } from "../../lib/pkc-compat.js";
|
|
33
|
-
import { getAccountCommentsIndex, getAccountCommunities, getCommentCidsToAccountsComments, COMMENT_MODERATION_AUTHOR_SUMMARY_KEY, getAccountEditPropertySummary, fetchCommentLinkDimensions, getAccountCommentDepth, addShortAddressesToAccountComment, sanitizeAccountCommentForState, sanitizeStoredAccountComment, } from "./utils.js";
|
|
33
|
+
import { getAccountCommentsIndex, getAccountCommunities, getCommentCidsToAccountsComments, COMMENT_MODERATION_AUTHOR_SUMMARY_KEY, getAccountEditPropertySummary, fetchCommentLinkDimensions, getAccountCommentDepth, addShortAddressesToAccountComment, sanitizeAccountCommentForState, sanitizeStoredAccountComment, getFreshestLoadedComment, } from "./utils.js";
|
|
34
34
|
import isEqual from "lodash.isequal";
|
|
35
35
|
import { v4 as uuid } from "uuid";
|
|
36
36
|
import utils from "../../lib/utils/index.js";
|
|
37
|
+
import { addOptimisticVoteMetadata } from "../../lib/utils/optimistic-vote-counts.js";
|
|
37
38
|
// Active publish-session tracking for pending comments (Task 3)
|
|
38
39
|
const activePublishSessions = new Map();
|
|
39
40
|
const abandonedPublishSessionIds = new Set();
|
|
@@ -1032,6 +1033,7 @@ export const deleteComment = (commentCidOrAccountCommentIndex, accountName) => _
|
|
|
1032
1033
|
log("accountsActions.deleteComment", { accountId: account.id, accountCommentIndex });
|
|
1033
1034
|
});
|
|
1034
1035
|
export const publishVote = (publishVoteOptions, accountName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1036
|
+
var _a, _b;
|
|
1035
1037
|
const { accounts, accountNamesToAccountIds, activeAccountId } = accountsStore.getState();
|
|
1036
1038
|
assert(accounts && accountNamesToAccountIds && activeAccountId, `can't use accountsStore.accountActions before initialized`);
|
|
1037
1039
|
let account = accounts[activeAccountId];
|
|
@@ -1049,7 +1051,12 @@ export const publishVote = (publishVoteOptions, accountName) => __awaiter(void 0
|
|
|
1049
1051
|
delete createVoteOptions.onChallengeVerification;
|
|
1050
1052
|
delete createVoteOptions.onError;
|
|
1051
1053
|
delete createVoteOptions.onPublishingStateChange;
|
|
1052
|
-
const
|
|
1054
|
+
const accountsState = accountsStore.getState();
|
|
1055
|
+
const accountCommentInfo = accountsState.commentCidsToAccountsComments[createVoteOptions.commentCid];
|
|
1056
|
+
const accountComment = accountCommentInfo
|
|
1057
|
+
? (_a = accountsState.accountsComments[accountCommentInfo.accountId]) === null || _a === void 0 ? void 0 : _a[accountCommentInfo.accountCommentIndex]
|
|
1058
|
+
: undefined;
|
|
1059
|
+
const storedCreateVoteOptions = addOptimisticVoteMetadata(normalizePublicationOptionsForStore(createVoteOptions), (_b = accountsState.accountsVotes[account.id]) === null || _b === void 0 ? void 0 : _b[createVoteOptions.commentCid], getFreshestLoadedComment(createVoteOptions.commentCid, accountComment));
|
|
1053
1060
|
let vote = backfillPublicationCommunityAddress(yield account.pkc.createVote(createVoteOptions), createVoteOptions);
|
|
1054
1061
|
let lastChallenge;
|
|
1055
1062
|
const publishAndRetryFailedChallengeVerification = () => __awaiter(void 0, void 0, void 0, function* () {
|