@bitsocial/bitsocial-react-hooks 0.2.1 → 0.2.2

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 CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.2.2](https://github.com/bitsocialnet/bitsocial-react-hooks/compare/v0.2.1...v0.2.2) (2026-11-07)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **feeds:** drop community pages a community update no longer links to ([779abd5](https://github.com/bitsocialnet/bitsocial-react-hooks/commit/779abd51301ee14be6b4a1d837d469eb3e61e698)), closes [bitsocialnet/seedit#869](https://github.com/bitsocialnet/seedit/issues/869)
7
+
8
+
9
+
1
10
  ## [0.2.1](https://github.com/bitsocialnet/bitsocial-react-hooks/compare/v0.2.0...v0.2.1) (2026-11-06)
2
11
 
3
12
 
@@ -1 +1 @@
1
- {"version":3,"file":"communities-pages-store.d.ts","sourceRoot":"","sources":["../../../src/stores/communities-pages/communities-pages-store.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAE7C,eAAO,MAAM,GAAG,QAA+C,CAAC;AAChE,OAAO,EACL,SAAS,EACT,aAAa,EACb,gBAAgB,EAEhB,OAAO,EACP,QAAQ,EACT,MAAM,aAAa,CAAC;AA6BrB,4HAA4H;AAC5H,eAAO,MAAM,mBAAmB,GAAI,SAAS,OAAO,GAAG,SAAS,KAAG,MACJ,CAAC;AAKhE,KAAK,qBAAqB,GAAG;IAC3B,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,QAAQ,EAAE,QAAQ,CAAC;IACnB,2BAA2B,EAAE,QAAQ,CAAC;IACtC,wBAAwB,EAAE,QAAQ,CAAC;IACnC,+BAA+B,EAAE,QAAQ,CAAC;CAC3C,CAAC;AAEF,QAAA,MAAM,qBAAqB,oFAgP1B,CAAC;AAsEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,GAC5B,WAAW,SAAS,EACpB,UAAU,MAAM,GAAG,SAAS,EAC5B,kBAAkB,gBAAgB,EAClC,UAAU,MAAM,EAChB,YAAY,MAAM,oBA6BnB,CAAC;AAEF,eAAO,MAAM,wBAAwB,GACnC,WAAW,SAAS,EACpB,UAAU,MAAM,GAAG,SAAS,EAC5B,iBAAkB,QAuBnB,CAAC;AAKF,eAAO,MAAM,0BAA0B,qBAYtC,CAAC;AAGF,eAAO,MAAM,qCAAqC,qBAGjD,CAAC;AAEF,eAAe,qBAAqB,CAAC"}
1
+ {"version":3,"file":"communities-pages-store.d.ts","sourceRoot":"","sources":["../../../src/stores/communities-pages/communities-pages-store.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAE7C,eAAO,MAAM,GAAG,QAA+C,CAAC;AAChE,OAAO,EACL,SAAS,EACT,aAAa,EACb,gBAAgB,EAEhB,OAAO,EACP,QAAQ,EACT,MAAM,aAAa,CAAC;AA6BrB,4HAA4H;AAC5H,eAAO,MAAM,mBAAmB,GAAI,SAAS,OAAO,GAAG,SAAS,KAAG,MACJ,CAAC;AAuFhE,KAAK,qBAAqB,GAAG;IAC3B,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,QAAQ,EAAE,QAAQ,CAAC;IACnB,2BAA2B,EAAE,QAAQ,CAAC;IACtC,wBAAwB,EAAE,QAAQ,CAAC;IACnC,+BAA+B,EAAE,QAAQ,CAAC;CAC3C,CAAC;AAEF,QAAA,MAAM,qBAAqB,oFAqP1B,CAAC;AAsEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,GAC5B,WAAW,SAAS,EACpB,UAAU,MAAM,GAAG,SAAS,EAC5B,kBAAkB,gBAAgB,EAClC,UAAU,MAAM,EAChB,YAAY,MAAM,oBA6BnB,CAAC;AAEF,eAAO,MAAM,wBAAwB,GACnC,WAAW,SAAS,EACpB,UAAU,MAAM,GAAG,SAAS,EAC5B,iBAAkB,QAuBnB,CAAC;AAKF,eAAO,MAAM,0BAA0B,qBAatC,CAAC;AAGF,eAAO,MAAM,qCAAqC,qBAGjD,CAAC;AAEF,eAAe,qBAAqB,CAAC"}
@@ -31,10 +31,75 @@ const getCommunityPageStoreKey = (pageCid, pageType, accountId) => {
31
31
  };
32
32
  /** Freshness for comparison: max(updatedAt, timestamp, 0). Used to decide add vs replace per CID. Exported for coverage. */
33
33
  export const getCommentFreshness = (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); };
34
+ let storedCommunityPageKeys = {};
35
+ // The page keys community records link to: the first page cids of every sort, followed through the
36
+ // stored pages' nextCid. A superset of the chain getCommunityPages walks for one sort.
37
+ const getLinkedCommunityPageKeys = (communityRecords, communitiesPages, pageType, accountId) => {
38
+ var _a;
39
+ const linkedPageKeys = new Set();
40
+ for (const communityRecord of communityRecords) {
41
+ const pages = communityRecord === null || communityRecord === void 0 ? void 0 : communityRecord[pageType];
42
+ const firstPageCids = [
43
+ ...Object.values((pages === null || pages === void 0 ? void 0 : pages.pageCids) || {}),
44
+ ...Object.values((pages === null || pages === void 0 ? void 0 : pages.pages) || {}).map((page) => page === null || page === void 0 ? void 0 : page.nextCid),
45
+ ];
46
+ for (let pageCid of firstPageCids) {
47
+ while (typeof pageCid === "string" && pageCid) {
48
+ const pageKey = getCommunityPageStoreKey(pageCid, pageType, accountId);
49
+ if (linkedPageKeys.has(pageKey)) {
50
+ break;
51
+ }
52
+ linkedPageKeys.add(pageKey);
53
+ pageCid = (_a = communitiesPages[pageKey]) === null || _a === void 0 ? void 0 : _a.nextCid;
54
+ }
55
+ }
56
+ }
57
+ return linkedPageKeys;
58
+ };
59
+ // A community update replaces its page cids, and getCommunityPages only walks from the current ones,
60
+ // so the previous page chains are never read again. Without dropping them, a feed left open keeps a
61
+ // copy of its communities' pages for every update it fetched pages after. Only pages of this
62
+ // community are checked, against its stored records and the record the caller fetched with, so a
63
+ // page fetched with a record that was replaced meanwhile doesn't drop the current chain. A later
64
+ // page shared by the old and new chains is dropped until the new chain reaches it again, and is then
65
+ // read back from the pages database.
66
+ const removeUnlinkedCommunityPages = (communitiesPages, community) => {
67
+ var _a;
68
+ const storedPageKeys = storedCommunityPageKeys[community.address];
69
+ if (!storedPageKeys) {
70
+ return;
71
+ }
72
+ const communityRecords = [
73
+ community,
74
+ ...Object.values(((_a = communitiesStore.getState()) === null || _a === void 0 ? void 0 : _a.communities) || {}).filter((communityRecord) => (communityRecord === null || communityRecord === void 0 ? void 0 : communityRecord.address) === community.address),
75
+ ];
76
+ const linkedPageKeys = {};
77
+ const unlinkedPageKeys = [];
78
+ for (const pageKey in storedPageKeys) {
79
+ if (!communitiesPages[pageKey]) {
80
+ // already removed, e.g. by invalidateCommunityPages
81
+ delete storedPageKeys[pageKey];
82
+ continue;
83
+ }
84
+ const { pageType, accountId } = storedPageKeys[pageKey];
85
+ const pageTypeAndAccount = `${pageType}:${accountId}`;
86
+ if (!linkedPageKeys[pageTypeAndAccount]) {
87
+ linkedPageKeys[pageTypeAndAccount] = getLinkedCommunityPageKeys(communityRecords, communitiesPages, pageType, accountId);
88
+ }
89
+ if (!linkedPageKeys[pageTypeAndAccount].has(pageKey)) {
90
+ unlinkedPageKeys.push(pageKey);
91
+ }
92
+ }
93
+ for (const pageKey of unlinkedPageKeys) {
94
+ delete communitiesPages[pageKey];
95
+ delete storedPageKeys[pageKey];
96
+ }
97
+ };
34
98
  // reset all event listeners in between tests
35
99
  const listeners = [];
36
100
  const communitiesPagesStore = createStore((setState, getState) => ({
37
- // TODO: eventually clear old pages and comments from memory
101
+ // pages a community no longer links to are dropped when one of its pages is added
102
+ // TODO: eventually clear old comments from memory
38
103
  communitiesPages: {},
39
104
  comments: {},
40
105
  addNextCommunityPageToStore: (community, sortType, account, modQueue) => __awaiter(void 0, void 0, void 0, function* () {
@@ -116,10 +181,11 @@ const communitiesPagesStore = createStore((setState, getState) => ({
116
181
  }
117
182
  }
118
183
  }
184
+ storedCommunityPageKeys[community.address] = Object.assign(Object.assign({}, storedCommunityPageKeys[community.address]), { [pageStoreKeyToAdd]: { pageType, accountId: account.id } });
119
185
  setState(({ communitiesPages, comments }) => {
120
- const newState = {
121
- communitiesPages: Object.assign(Object.assign({}, communitiesPages), { [pageStoreKeyToAdd]: page }),
122
- };
186
+ const nextCommunitiesPages = Object.assign(Object.assign({}, communitiesPages), { [pageStoreKeyToAdd]: page });
187
+ removeUnlinkedCommunityPages(nextCommunitiesPages, community);
188
+ const newState = { communitiesPages: nextCommunitiesPages };
123
189
  if (hasNewComments) {
124
190
  newState.comments = Object.assign(Object.assign({}, comments), newComments);
125
191
  }
@@ -302,6 +368,7 @@ const originalState = communitiesPagesStore.getState();
302
368
  // async function because some stores have async init
303
369
  export const resetCommunitiesPagesStore = () => __awaiter(void 0, void 0, void 0, function* () {
304
370
  fetchPagePending = {};
371
+ storedCommunityPageKeys = {};
305
372
  for (const accountId in fetchPageCommunities) {
306
373
  delete fetchPageCommunities[accountId];
307
374
  }
@@ -1 +1 @@
1
- {"version":3,"file":"communities-pages-store.js","sourceRoot":"","sources":["../../../src/stores/communities-pages/communities-pages-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,MAAM,iBAAiB,CAAC;AACpC,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,2DAA2D;AAC3D,MAAM,CAAC,MAAM,GAAG,GAAG,MAAM,CAAC,oCAAoC,CAAC,CAAC;AAShE,OAAO,aAAa,MAAM,aAAa,CAAC;AACxC,OAAO,gBAAsC,MAAM,gBAAgB,CAAC;AACpE,OAAO,cAAc,MAAM,2BAA2B,CAAC;AACvD,OAAO,WAAW,MAAM,SAAS,CAAC;AAClC,OAAO,MAAM,MAAM,kBAAkB,CAAC;AACtC,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,gCAAgC,GACjC,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,mBAAmB,EAAE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CAAC;IACpE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;IACrB,mDAAmD;IACnD,qBAAqB,CAAC,OAAO,EAAE,CAAC;IAChC,yBAAyB;IACzB,qBAAqB,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;AAChD,CAAC,CAAA,CAAC;AAEF,4CAA4C;AAC5C,MAAM,CAAC,MAAM,qCAAqC,GAAG,GAAS,EAAE;IAC9D,MAAM,cAAc,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,sCAAsC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;IAC9F,MAAM,0BAA0B,EAAE,CAAC;AACrC,CAAC,CAAA,CAAC;AAEF,eAAe,qBAAqB,CAAC","sourcesContent":["import utils from \"../../lib/utils\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\n// include communities pages store with feeds for debugging\nexport const log = Logger(\"bitsocial-react-hooks:feeds:stores\");\nimport {\n Community,\n CommunityPage,\n CommunitiesPages,\n Account,\n Comment,\n Comments,\n} from \"../../types\";\nimport accountsStore from \"../accounts\";\nimport communitiesStore, { CommunitiesState } from \"../communities\";\nimport localForageLru from \"../../lib/localforage-lru\";\nimport createStore from \"zustand\";\nimport assert from \"../../lib/assert\";\nimport {\n createPkcCommunity,\n getPkcCreateCommunity,\n normalizeCommentCommunityAddress,\n} from \"../../lib/pkc-compat\";\nimport { getPostPageSortType } from \"../../lib/page-sorts\";\n\nconst communitiesPagesDatabase = localForageLru.createInstance({\n name: \"bitsocialReactHooks-communitiesPages\",\n size: 500,\n});\n\nconst getCommunityPageStoreKey = (pageCid: string, pageType: string, accountId?: string) => {\n if (pageType === \"modQueue\") {\n assert(\n accountId && typeof accountId === \"string\",\n `getCommunityPageStoreKey accountId '${accountId}' invalid for modQueue`,\n );\n return `${accountId}:${pageCid}`;\n }\n return pageCid;\n};\n\n/** Freshness for comparison: max(updatedAt, timestamp, 0). Used to decide add vs replace per CID. Exported for coverage. */\nexport const getCommentFreshness = (comment: Comment | undefined): number =>\n Math.max(comment?.updatedAt ?? 0, comment?.timestamp ?? 0, 0);\n\n// reset all event listeners in between tests\nconst listeners: any = [];\n\ntype CommunitiesPagesState = {\n communitiesPages: CommunitiesPages;\n comments: Comments;\n addNextCommunityPageToStore: Function;\n invalidateCommunityPages: Function;\n addCommunityPageCommentsToStore: Function;\n};\n\nconst communitiesPagesStore = createStore<CommunitiesPagesState>(\n (setState: Function, getState: Function) => ({\n // TODO: eventually clear old pages and comments from memory\n communitiesPages: {},\n comments: {},\n\n addNextCommunityPageToStore: async (\n community: Community,\n sortType: string | undefined,\n account: Account,\n modQueue?: string[],\n ) => {\n assert(\n community?.address && typeof community?.address === \"string\",\n `communitiesPagesStore.addNextCommunityPageToStore community '${community}' invalid`,\n );\n assert(\n sortType === undefined || (typeof sortType === \"string\" && sortType.length > 0),\n `communitiesPagesStore.addNextCommunityPageToStore sortType '${sortType}' invalid`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesPagesStore.addNextCommunityPageToStore account '${account}' invalid`,\n );\n assert(\n !modQueue || Array.isArray(modQueue),\n `communitiesPagesStore.addNextCommunityPageToStore modQueue '${modQueue}' invalid`,\n );\n\n let pageType = \"posts\";\n if (modQueue?.[0]) {\n // TODO: allow multiple modQueue at once, fow now only use first in array\n // TODO: fix 'sortType' is not accurate variable name when pageType is 'modQueue'\n sortType = modQueue[0];\n pageType = \"modQueue\";\n }\n\n // check the preloaded posts on community.posts.pages first, then the community.posts.pageCids\n const communityFirstPageCid = getCommunityFirstPageCid(community, sortType, pageType);\n if (!communityFirstPageCid) {\n log(\n `communitiesPagesStore.addNextCommunityPageToStore community '${community?.address}' sortType '${sortType}' no communityFirstPageCid`,\n );\n return;\n }\n\n // all communities pages in store\n const { communitiesPages } = getState();\n // only specific pages of the community+sortType\n const communityPages = getCommunityPages(\n community,\n sortType,\n communitiesPages,\n pageType,\n account.id,\n );\n\n // if no pages exist yet, add the first page\n let pageCidToAdd: string;\n if (!communityPages.length) {\n pageCidToAdd = communityFirstPageCid;\n } else {\n const nextCid = communityPages[communityPages.length - 1]?.nextCid;\n // if last nextCid is undefined, reached end of pages\n if (!nextCid) {\n log.trace(\"communitiesPagesStore.addNextCommunityPageToStore no more pages\", {\n communityAddress: community.address,\n sortType,\n account,\n });\n return;\n }\n\n pageCidToAdd = nextCid;\n }\n\n // page is already added or pending\n const pageStoreKeyToAdd = getCommunityPageStoreKey(pageCidToAdd, pageType, account.id);\n if (communitiesPages[pageStoreKeyToAdd] || fetchPagePending[pageStoreKeyToAdd]) {\n return;\n }\n\n fetchPagePending[pageStoreKeyToAdd] = true;\n let page: CommunityPage;\n try {\n page = await fetchPage(pageCidToAdd, community.address, account, pageType);\n log.trace(\"communitiesPagesStore.addNextCommunityPageToStore community.posts.getPage\", {\n pageCid: pageCidToAdd,\n communityAddress: community.address,\n account,\n });\n } catch (e) {\n throw e;\n } finally {\n fetchPagePending[pageStoreKeyToAdd] = false;\n }\n\n // find new comments in the page\n const flattenedComments = utils.flattenCommentsPages(page);\n const { comments } = getState();\n let hasNewComments = false;\n const newComments: Comments = {};\n if (pageType !== \"modQueue\") {\n for (const comment of flattenedComments) {\n const normalizedComment = normalizeCommentCommunityAddress(comment) as Comment;\n const existing = comments[normalizedComment.cid];\n if (\n normalizedComment.cid &&\n (!existing || getCommentFreshness(normalizedComment) > getCommentFreshness(existing))\n ) {\n // don't clone the comment to save memory, comments remain a pointer to the page object\n newComments[normalizedComment.cid] = normalizedComment;\n hasNewComments = true;\n }\n }\n }\n\n setState(({ communitiesPages, comments }: any) => {\n const newState: any = {\n communitiesPages: { ...communitiesPages, [pageStoreKeyToAdd]: page },\n };\n if (hasNewComments) {\n newState.comments = { ...comments, ...newComments };\n }\n return newState;\n });\n log(\"communitiesPagesStore.addNextCommunityPageToStore\", {\n pageCid: pageCidToAdd,\n communityAddress: community.address,\n sortType,\n page,\n account,\n });\n\n // when publishing a comment, you don't yet know its CID\n // so when a new comment is fetched, check to see if it's your own\n // comment, and if yes, add the CID to your account comments database\n for (const comment of flattenedComments) {\n accountsStore\n .getState()\n .accountsActionsInternal.addCidToAccountComment(\n normalizeCommentCommunityAddress(comment) as Comment,\n )\n .catch((error: unknown) =>\n log.error(\n \"communitiesPagesStore.addNextCommunityPageToStore addCidToAccountComment error\",\n { comment, error },\n ),\n );\n }\n },\n\n invalidateCommunityPages: async (\n community: Community,\n sortType: string | undefined,\n modQueue?: string[],\n accountId?: string,\n ) => {\n assert(\n community?.address && typeof community?.address === \"string\",\n `communitiesPagesStore.invalidateCommunityPages community '${community}' invalid`,\n );\n assert(\n sortType === undefined || (typeof sortType === \"string\" && sortType.length > 0),\n `communitiesPagesStore.invalidateCommunityPages sortType '${sortType}' invalid`,\n );\n assert(\n !modQueue || Array.isArray(modQueue),\n `communitiesPagesStore.invalidateCommunityPages modQueue '${modQueue}' invalid`,\n );\n\n let pageType = \"posts\";\n if (modQueue?.[0]) {\n // TODO: allow multiple modQueue at once, for now only use first in array\n // TODO: fix 'sortType' is not accurate variable name when pageType is 'modQueue'\n sortType = modQueue[0];\n pageType = \"modQueue\";\n }\n\n const firstPageCid = getCommunityFirstPageCid(community, sortType, pageType);\n if (!firstPageCid) {\n return;\n }\n\n const { communitiesPages } = getState();\n const firstPageKey = getCommunityPageStoreKey(firstPageCid, pageType, accountId);\n const pageKeysToInvalidate = new Set<string>([firstPageKey]);\n let nextPageCid = communitiesPages[firstPageKey]?.nextCid;\n while (nextPageCid) {\n const nextPageKey = getCommunityPageStoreKey(nextPageCid, pageType, accountId);\n pageKeysToInvalidate.add(nextPageKey);\n nextPageCid = communitiesPages[nextPageKey]?.nextCid;\n }\n\n await Promise.all(\n [...pageKeysToInvalidate].map((pageKey) => communitiesPagesDatabase.removeItem(pageKey)),\n );\n\n setState(({ communitiesPages }: any) => {\n const nextCommunitiesPages = { ...communitiesPages };\n for (const pageKey of pageKeysToInvalidate) {\n delete nextCommunitiesPages[pageKey];\n }\n return { communitiesPages: nextCommunitiesPages };\n });\n },\n\n // communities contain preloaded pages, those page comments must be added separately\n addCommunityPageCommentsToStore: (community: Community) => {\n if (!community.posts?.pages) {\n return;\n }\n\n // find new comments in the page\n const flattenedComments = utils.flattenCommentsPages(community.posts.pages);\n const { comments } = getState();\n let hasNewComments = false;\n const newComments: Comments = {};\n for (const comment of flattenedComments) {\n const existing = comments[comment.cid];\n if (\n comment.cid &&\n (!existing || getCommentFreshness(comment) > getCommentFreshness(existing))\n ) {\n // don't clone the comment to save memory, comments remain a pointer to the page object\n newComments[comment.cid] = comment;\n hasNewComments = true;\n }\n }\n\n if (!hasNewComments) {\n return;\n }\n\n setState(({ comments }: any) => {\n return { comments: { ...comments, ...newComments } };\n });\n log(\"communitiesPagesStore.addCommunityPageCommentsToStore\", { community, newComments });\n },\n }),\n);\n\n// set clients states on communities store so the frontend can display it, dont persist in db because a reload cancels updating\nconst onCommunityPostsClientsStateChange =\n (communityAddress: string) =>\n (clientState: string, clientType: string, sortType: string, clientUrl: string) => {\n communitiesStore.setState((state: CommunitiesState) => {\n // make sure not undefined, sometimes happens in e2e tests\n if (!state.communities[communityAddress]) {\n return {};\n }\n const client = { state: clientState };\n const community = { ...state.communities[communityAddress] };\n community.posts = { ...community.posts };\n community.posts.clients = { ...community.posts.clients };\n community.posts.clients[clientType] = { ...community.posts.clients[clientType] };\n community.posts.clients[clientType][sortType] = {\n ...community.posts.clients[clientType][sortType],\n };\n community.posts.clients[clientType][sortType][clientUrl] = client;\n return { communities: { ...state.communities, [community.address]: community } };\n });\n };\n\nconst fetchPageCommunities: {\n [accountId: string]: { pkc: any; communities: { [communityAddress: string]: any } };\n} = {}; // cache created community clients per account because creating them can be slow\nlet fetchPagePending: { [key: string]: boolean } = {};\nconst fetchPage = async (\n pageCid: string,\n communityAddress: string,\n account: Account,\n pageType: string,\n) => {\n // community page is cached\n const pageStoreKey = getCommunityPageStoreKey(pageCid, pageType, account.id);\n const cachedCommunityPage = await communitiesPagesDatabase.getItem(pageStoreKey);\n if (cachedCommunityPage) {\n return cachedCommunityPage;\n }\n if (!fetchPageCommunities[account.id] || fetchPageCommunities[account.id].pkc !== account.pkc) {\n fetchPageCommunities[account.id] = { pkc: account.pkc, communities: {} };\n }\n const accountCommunities = fetchPageCommunities[account.id].communities;\n if (!accountCommunities[communityAddress]) {\n accountCommunities[communityAddress] = await createPkcCommunity(account.pkc, {\n address: communityAddress,\n });\n listeners.push(accountCommunities[communityAddress]);\n\n // set clients states on communities store so the frontend can display it\n utils.pageClientsOnStateChange(\n accountCommunities[communityAddress][pageType]?.clients,\n onCommunityPostsClientsStateChange(communityAddress),\n );\n }\n\n const onError = (error: any) =>\n log.error(\n `communitiesPagesStore community '${communityAddress}' failed community.posts.getPage page cid '${pageCid}':`,\n error,\n );\n const fetchedCommunityPage = await utils.retryInfinity(\n () => accountCommunities[communityAddress][pageType].getPage({ cid: pageCid }),\n { onError },\n );\n await communitiesPagesDatabase.setItem(pageStoreKey, utils.clone(fetchedCommunityPage));\n return fetchedCommunityPage;\n};\n\n/**\n * Util function to get all pages in the store for a\n * specific community+sortType using `CommunityPage.nextCid`\n */\nexport const getCommunityPages = (\n community: Community,\n sortType: string | undefined,\n communitiesPages: CommunitiesPages,\n pageType: string,\n accountId?: string,\n) => {\n assert(\n communitiesPages && typeof communitiesPages === \"object\",\n `getCommunityPages communitiesPages '${communitiesPages}' invalid`,\n );\n const pages: CommunityPage[] = [];\n const firstPageCid = getCommunityFirstPageCid(community, sortType, pageType);\n // community has no pages\n // TODO: if a loaded community doesn't have a first page, it's unclear what we should do\n // should we try to use another sort type by default, like 'hot', or should we just ignore it?\n // 'return pages' to ignore it for now\n if (!firstPageCid) {\n return pages;\n }\n const firstPage = communitiesPages[getCommunityPageStoreKey(firstPageCid, pageType, accountId)];\n if (!firstPage) {\n return pages;\n }\n pages.push(firstPage);\n while (true) {\n const nextCid = pages[pages.length - 1]?.nextCid;\n const nextPageKey = nextCid && getCommunityPageStoreKey(nextCid, pageType, accountId);\n const communityPage = nextPageKey && communitiesPages[nextPageKey];\n if (!communityPage) {\n return pages;\n }\n pages.push(communityPage);\n }\n};\n\nexport const getCommunityFirstPageCid = (\n community: Community,\n sortType: string | undefined,\n pageType = \"posts\",\n) => {\n assert(\n community && typeof community === \"object\",\n `getCommunityFirstPageCid community '${community}' invalid`,\n );\n assert(\n sortType === undefined || (typeof sortType === \"string\" && sortType.length > 0),\n `getCommunityFirstPageCid sortType '${sortType}' invalid`,\n );\n // posts served client-side from a complete preloaded page read that page instead of their own\n const pageSortType = pageType === \"posts\" ? getPostPageSortType(community, sortType) : sortType;\n if (!pageSortType) {\n return;\n }\n // community has preloaded posts for sort type\n if (community[pageType]?.pages?.[pageSortType]?.comments) {\n return community[pageType]?.pages?.[pageSortType]?.nextCid;\n }\n return community[pageType]?.pageCids?.[pageSortType];\n\n // TODO: if a loaded community doesn't have a first page, it's unclear what we should do\n // should we try to use another sort type by default, like 'hot', or should we just ignore it?\n};\n\n// reset store in between tests\nconst originalState = communitiesPagesStore.getState();\n// async function because some stores have async init\nexport const resetCommunitiesPagesStore = async () => {\n fetchPagePending = {};\n for (const accountId in fetchPageCommunities) {\n delete fetchPageCommunities[accountId];\n }\n // remove all event listeners\n listeners.forEach((listener: any) => listener.removeAllListeners());\n listeners.length = 0;\n // destroy all component subscriptions to the store\n communitiesPagesStore.destroy();\n // restore original state\n communitiesPagesStore.setState(originalState);\n};\n\n// reset database and store in between tests\nexport const resetCommunitiesPagesDatabaseAndStore = async () => {\n await localForageLru.createInstance({ name: \"bitsocialReactHooks-communitiesPages\" }).clear();\n await resetCommunitiesPagesStore();\n};\n\nexport default communitiesPagesStore;\n"]}
1
+ {"version":3,"file":"communities-pages-store.js","sourceRoot":"","sources":["../../../src/stores/communities-pages/communities-pages-store.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,MAAM,iBAAiB,CAAC;AACpC,OAAO,MAAM,MAAM,yBAAyB,CAAC;AAC7C,2DAA2D;AAC3D,MAAM,CAAC,MAAM,GAAG,GAAG,MAAM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AAE,CAAC;AACrC,CAAC,CAAA,CAAC;AAEF,eAAe,qBAAqB,CAAC","sourcesContent":["import utils from \"../../lib/utils\";\nimport Logger from \"@pkcprotocol/pkc-logger\";\n// include communities pages store with feeds for debugging\nexport const log = Logger(\"bitsocial-react-hooks:feeds:stores\");\nimport {\n Community,\n CommunityPage,\n CommunitiesPages,\n Account,\n Comment,\n Comments,\n} from \"../../types\";\nimport accountsStore from \"../accounts\";\nimport communitiesStore, { CommunitiesState } from \"../communities\";\nimport localForageLru from \"../../lib/localforage-lru\";\nimport createStore from \"zustand\";\nimport assert from \"../../lib/assert\";\nimport {\n createPkcCommunity,\n getPkcCreateCommunity,\n normalizeCommentCommunityAddress,\n} from \"../../lib/pkc-compat\";\nimport { getPostPageSortType } from \"../../lib/page-sorts\";\n\nconst communitiesPagesDatabase = localForageLru.createInstance({\n name: \"bitsocialReactHooks-communitiesPages\",\n size: 500,\n});\n\nconst getCommunityPageStoreKey = (pageCid: string, pageType: string, accountId?: string) => {\n if (pageType === \"modQueue\") {\n assert(\n accountId && typeof accountId === \"string\",\n `getCommunityPageStoreKey accountId '${accountId}' invalid for modQueue`,\n );\n return `${accountId}:${pageCid}`;\n }\n return pageCid;\n};\n\n/** Freshness for comparison: max(updatedAt, timestamp, 0). Used to decide add vs replace per CID. Exported for coverage. */\nexport const getCommentFreshness = (comment: Comment | undefined): number =>\n Math.max(comment?.updatedAt ?? 0, comment?.timestamp ?? 0, 0);\n\n// the page keys stored for each community address, with the page type and account each was\n// fetched for, to find the pages a community no longer links to\ntype StoredCommunityPage = { pageType: string; accountId: string };\nlet storedCommunityPageKeys: {\n [communityAddress: string]: { [pageStoreKey: string]: StoredCommunityPage };\n} = {};\n\n// The page keys community records link to: the first page cids of every sort, followed through the\n// stored pages' nextCid. A superset of the chain getCommunityPages walks for one sort.\nconst getLinkedCommunityPageKeys = (\n communityRecords: Community[],\n communitiesPages: CommunitiesPages,\n pageType: string,\n accountId: string,\n) => {\n const linkedPageKeys = new Set<string>();\n for (const communityRecord of communityRecords) {\n const pages = communityRecord?.[pageType];\n const firstPageCids = [\n ...Object.values(pages?.pageCids || {}),\n ...Object.values(pages?.pages || {}).map((page: any) => page?.nextCid),\n ];\n for (let pageCid of firstPageCids) {\n while (typeof pageCid === \"string\" && pageCid) {\n const pageKey = getCommunityPageStoreKey(pageCid, pageType, accountId);\n if (linkedPageKeys.has(pageKey)) {\n break;\n }\n linkedPageKeys.add(pageKey);\n pageCid = communitiesPages[pageKey]?.nextCid;\n }\n }\n }\n return linkedPageKeys;\n};\n\n// A community update replaces its page cids, and getCommunityPages only walks from the current ones,\n// so the previous page chains are never read again. Without dropping them, a feed left open keeps a\n// copy of its communities' pages for every update it fetched pages after. Only pages of this\n// community are checked, against its stored records and the record the caller fetched with, so a\n// page fetched with a record that was replaced meanwhile doesn't drop the current chain. A later\n// page shared by the old and new chains is dropped until the new chain reaches it again, and is then\n// read back from the pages database.\nconst removeUnlinkedCommunityPages = (communitiesPages: CommunitiesPages, community: Community) => {\n const storedPageKeys = storedCommunityPageKeys[community.address];\n if (!storedPageKeys) {\n return;\n }\n const communityRecords = [\n community,\n ...Object.values(communitiesStore.getState()?.communities || {}).filter(\n (communityRecord: any) => communityRecord?.address === community.address,\n ),\n ];\n const linkedPageKeys: { [pageTypeAndAccount: string]: Set<string> } = {};\n const unlinkedPageKeys: string[] = [];\n for (const pageKey in storedPageKeys) {\n if (!communitiesPages[pageKey]) {\n // already removed, e.g. by invalidateCommunityPages\n delete storedPageKeys[pageKey];\n continue;\n }\n const { pageType, accountId } = storedPageKeys[pageKey];\n const pageTypeAndAccount = `${pageType}:${accountId}`;\n if (!linkedPageKeys[pageTypeAndAccount]) {\n linkedPageKeys[pageTypeAndAccount] = getLinkedCommunityPageKeys(\n communityRecords,\n communitiesPages,\n pageType,\n accountId,\n );\n }\n if (!linkedPageKeys[pageTypeAndAccount].has(pageKey)) {\n unlinkedPageKeys.push(pageKey);\n }\n }\n for (const pageKey of unlinkedPageKeys) {\n delete communitiesPages[pageKey];\n delete storedPageKeys[pageKey];\n }\n};\n\n// reset all event listeners in between tests\nconst listeners: any = [];\n\ntype CommunitiesPagesState = {\n communitiesPages: CommunitiesPages;\n comments: Comments;\n addNextCommunityPageToStore: Function;\n invalidateCommunityPages: Function;\n addCommunityPageCommentsToStore: Function;\n};\n\nconst communitiesPagesStore = createStore<CommunitiesPagesState>(\n (setState: Function, getState: Function) => ({\n // pages a community no longer links to are dropped when one of its pages is added\n // TODO: eventually clear old comments from memory\n communitiesPages: {},\n comments: {},\n\n addNextCommunityPageToStore: async (\n community: Community,\n sortType: string | undefined,\n account: Account,\n modQueue?: string[],\n ) => {\n assert(\n community?.address && typeof community?.address === \"string\",\n `communitiesPagesStore.addNextCommunityPageToStore community '${community}' invalid`,\n );\n assert(\n sortType === undefined || (typeof sortType === \"string\" && sortType.length > 0),\n `communitiesPagesStore.addNextCommunityPageToStore sortType '${sortType}' invalid`,\n );\n assert(\n typeof getPkcCreateCommunity(account?.pkc) === \"function\",\n `communitiesPagesStore.addNextCommunityPageToStore account '${account}' invalid`,\n );\n assert(\n !modQueue || Array.isArray(modQueue),\n `communitiesPagesStore.addNextCommunityPageToStore modQueue '${modQueue}' invalid`,\n );\n\n let pageType = \"posts\";\n if (modQueue?.[0]) {\n // TODO: allow multiple modQueue at once, fow now only use first in array\n // TODO: fix 'sortType' is not accurate variable name when pageType is 'modQueue'\n sortType = modQueue[0];\n pageType = \"modQueue\";\n }\n\n // check the preloaded posts on community.posts.pages first, then the community.posts.pageCids\n const communityFirstPageCid = getCommunityFirstPageCid(community, sortType, pageType);\n if (!communityFirstPageCid) {\n log(\n `communitiesPagesStore.addNextCommunityPageToStore community '${community?.address}' sortType '${sortType}' no communityFirstPageCid`,\n );\n return;\n }\n\n // all communities pages in store\n const { communitiesPages } = getState();\n // only specific pages of the community+sortType\n const communityPages = getCommunityPages(\n community,\n sortType,\n communitiesPages,\n pageType,\n account.id,\n );\n\n // if no pages exist yet, add the first page\n let pageCidToAdd: string;\n if (!communityPages.length) {\n pageCidToAdd = communityFirstPageCid;\n } else {\n const nextCid = communityPages[communityPages.length - 1]?.nextCid;\n // if last nextCid is undefined, reached end of pages\n if (!nextCid) {\n log.trace(\"communitiesPagesStore.addNextCommunityPageToStore no more pages\", {\n communityAddress: community.address,\n sortType,\n account,\n });\n return;\n }\n\n pageCidToAdd = nextCid;\n }\n\n // page is already added or pending\n const pageStoreKeyToAdd = getCommunityPageStoreKey(pageCidToAdd, pageType, account.id);\n if (communitiesPages[pageStoreKeyToAdd] || fetchPagePending[pageStoreKeyToAdd]) {\n return;\n }\n\n fetchPagePending[pageStoreKeyToAdd] = true;\n let page: CommunityPage;\n try {\n page = await fetchPage(pageCidToAdd, community.address, account, pageType);\n log.trace(\"communitiesPagesStore.addNextCommunityPageToStore community.posts.getPage\", {\n pageCid: pageCidToAdd,\n communityAddress: community.address,\n account,\n });\n } catch (e) {\n throw e;\n } finally {\n fetchPagePending[pageStoreKeyToAdd] = false;\n }\n\n // find new comments in the page\n const flattenedComments = utils.flattenCommentsPages(page);\n const { comments } = getState();\n let hasNewComments = false;\n const newComments: Comments = {};\n if (pageType !== \"modQueue\") {\n for (const comment of flattenedComments) {\n const normalizedComment = normalizeCommentCommunityAddress(comment) as Comment;\n const existing = comments[normalizedComment.cid];\n if (\n normalizedComment.cid &&\n (!existing || getCommentFreshness(normalizedComment) > getCommentFreshness(existing))\n ) {\n // don't clone the comment to save memory, comments remain a pointer to the page object\n newComments[normalizedComment.cid] = normalizedComment;\n hasNewComments = true;\n }\n }\n }\n\n storedCommunityPageKeys[community.address] = {\n ...storedCommunityPageKeys[community.address],\n [pageStoreKeyToAdd]: { pageType, accountId: account.id },\n };\n setState(({ communitiesPages, comments }: any) => {\n const nextCommunitiesPages = { ...communitiesPages, [pageStoreKeyToAdd]: page };\n removeUnlinkedCommunityPages(nextCommunitiesPages, community);\n const newState: any = { communitiesPages: nextCommunitiesPages };\n if (hasNewComments) {\n newState.comments = { ...comments, ...newComments };\n }\n return newState;\n });\n log(\"communitiesPagesStore.addNextCommunityPageToStore\", {\n pageCid: pageCidToAdd,\n communityAddress: community.address,\n sortType,\n page,\n account,\n });\n\n // when publishing a comment, you don't yet know its CID\n // so when a new comment is fetched, check to see if it's your own\n // comment, and if yes, add the CID to your account comments database\n for (const comment of flattenedComments) {\n accountsStore\n .getState()\n .accountsActionsInternal.addCidToAccountComment(\n normalizeCommentCommunityAddress(comment) as Comment,\n )\n .catch((error: unknown) =>\n log.error(\n \"communitiesPagesStore.addNextCommunityPageToStore addCidToAccountComment error\",\n { comment, error },\n ),\n );\n }\n },\n\n invalidateCommunityPages: async (\n community: Community,\n sortType: string | undefined,\n modQueue?: string[],\n accountId?: string,\n ) => {\n assert(\n community?.address && typeof community?.address === \"string\",\n `communitiesPagesStore.invalidateCommunityPages community '${community}' invalid`,\n );\n assert(\n sortType === undefined || (typeof sortType === \"string\" && sortType.length > 0),\n `communitiesPagesStore.invalidateCommunityPages sortType '${sortType}' invalid`,\n );\n assert(\n !modQueue || Array.isArray(modQueue),\n `communitiesPagesStore.invalidateCommunityPages modQueue '${modQueue}' invalid`,\n );\n\n let pageType = \"posts\";\n if (modQueue?.[0]) {\n // TODO: allow multiple modQueue at once, for now only use first in array\n // TODO: fix 'sortType' is not accurate variable name when pageType is 'modQueue'\n sortType = modQueue[0];\n pageType = \"modQueue\";\n }\n\n const firstPageCid = getCommunityFirstPageCid(community, sortType, pageType);\n if (!firstPageCid) {\n return;\n }\n\n const { communitiesPages } = getState();\n const firstPageKey = getCommunityPageStoreKey(firstPageCid, pageType, accountId);\n const pageKeysToInvalidate = new Set<string>([firstPageKey]);\n let nextPageCid = communitiesPages[firstPageKey]?.nextCid;\n while (nextPageCid) {\n const nextPageKey = getCommunityPageStoreKey(nextPageCid, pageType, accountId);\n pageKeysToInvalidate.add(nextPageKey);\n nextPageCid = communitiesPages[nextPageKey]?.nextCid;\n }\n\n await Promise.all(\n [...pageKeysToInvalidate].map((pageKey) => communitiesPagesDatabase.removeItem(pageKey)),\n );\n\n setState(({ communitiesPages }: any) => {\n const nextCommunitiesPages = { ...communitiesPages };\n for (const pageKey of pageKeysToInvalidate) {\n delete nextCommunitiesPages[pageKey];\n }\n return { communitiesPages: nextCommunitiesPages };\n });\n },\n\n // communities contain preloaded pages, those page comments must be added separately\n addCommunityPageCommentsToStore: (community: Community) => {\n if (!community.posts?.pages) {\n return;\n }\n\n // find new comments in the page\n const flattenedComments = utils.flattenCommentsPages(community.posts.pages);\n const { comments } = getState();\n let hasNewComments = false;\n const newComments: Comments = {};\n for (const comment of flattenedComments) {\n const existing = comments[comment.cid];\n if (\n comment.cid &&\n (!existing || getCommentFreshness(comment) > getCommentFreshness(existing))\n ) {\n // don't clone the comment to save memory, comments remain a pointer to the page object\n newComments[comment.cid] = comment;\n hasNewComments = true;\n }\n }\n\n if (!hasNewComments) {\n return;\n }\n\n setState(({ comments }: any) => {\n return { comments: { ...comments, ...newComments } };\n });\n log(\"communitiesPagesStore.addCommunityPageCommentsToStore\", { community, newComments });\n },\n }),\n);\n\n// set clients states on communities store so the frontend can display it, dont persist in db because a reload cancels updating\nconst onCommunityPostsClientsStateChange =\n (communityAddress: string) =>\n (clientState: string, clientType: string, sortType: string, clientUrl: string) => {\n communitiesStore.setState((state: CommunitiesState) => {\n // make sure not undefined, sometimes happens in e2e tests\n if (!state.communities[communityAddress]) {\n return {};\n }\n const client = { state: clientState };\n const community = { ...state.communities[communityAddress] };\n community.posts = { ...community.posts };\n community.posts.clients = { ...community.posts.clients };\n community.posts.clients[clientType] = { ...community.posts.clients[clientType] };\n community.posts.clients[clientType][sortType] = {\n ...community.posts.clients[clientType][sortType],\n };\n community.posts.clients[clientType][sortType][clientUrl] = client;\n return { communities: { ...state.communities, [community.address]: community } };\n });\n };\n\nconst fetchPageCommunities: {\n [accountId: string]: { pkc: any; communities: { [communityAddress: string]: any } };\n} = {}; // cache created community clients per account because creating them can be slow\nlet fetchPagePending: { [key: string]: boolean } = {};\nconst fetchPage = async (\n pageCid: string,\n communityAddress: string,\n account: Account,\n pageType: string,\n) => {\n // community page is cached\n const pageStoreKey = getCommunityPageStoreKey(pageCid, pageType, account.id);\n const cachedCommunityPage = await communitiesPagesDatabase.getItem(pageStoreKey);\n if (cachedCommunityPage) {\n return cachedCommunityPage;\n }\n if (!fetchPageCommunities[account.id] || fetchPageCommunities[account.id].pkc !== account.pkc) {\n fetchPageCommunities[account.id] = { pkc: account.pkc, communities: {} };\n }\n const accountCommunities = fetchPageCommunities[account.id].communities;\n if (!accountCommunities[communityAddress]) {\n accountCommunities[communityAddress] = await createPkcCommunity(account.pkc, {\n address: communityAddress,\n });\n listeners.push(accountCommunities[communityAddress]);\n\n // set clients states on communities store so the frontend can display it\n utils.pageClientsOnStateChange(\n accountCommunities[communityAddress][pageType]?.clients,\n onCommunityPostsClientsStateChange(communityAddress),\n );\n }\n\n const onError = (error: any) =>\n log.error(\n `communitiesPagesStore community '${communityAddress}' failed community.posts.getPage page cid '${pageCid}':`,\n error,\n );\n const fetchedCommunityPage = await utils.retryInfinity(\n () => accountCommunities[communityAddress][pageType].getPage({ cid: pageCid }),\n { onError },\n );\n await communitiesPagesDatabase.setItem(pageStoreKey, utils.clone(fetchedCommunityPage));\n return fetchedCommunityPage;\n};\n\n/**\n * Util function to get all pages in the store for a\n * specific community+sortType using `CommunityPage.nextCid`\n */\nexport const getCommunityPages = (\n community: Community,\n sortType: string | undefined,\n communitiesPages: CommunitiesPages,\n pageType: string,\n accountId?: string,\n) => {\n assert(\n communitiesPages && typeof communitiesPages === \"object\",\n `getCommunityPages communitiesPages '${communitiesPages}' invalid`,\n );\n const pages: CommunityPage[] = [];\n const firstPageCid = getCommunityFirstPageCid(community, sortType, pageType);\n // community has no pages\n // TODO: if a loaded community doesn't have a first page, it's unclear what we should do\n // should we try to use another sort type by default, like 'hot', or should we just ignore it?\n // 'return pages' to ignore it for now\n if (!firstPageCid) {\n return pages;\n }\n const firstPage = communitiesPages[getCommunityPageStoreKey(firstPageCid, pageType, accountId)];\n if (!firstPage) {\n return pages;\n }\n pages.push(firstPage);\n while (true) {\n const nextCid = pages[pages.length - 1]?.nextCid;\n const nextPageKey = nextCid && getCommunityPageStoreKey(nextCid, pageType, accountId);\n const communityPage = nextPageKey && communitiesPages[nextPageKey];\n if (!communityPage) {\n return pages;\n }\n pages.push(communityPage);\n }\n};\n\nexport const getCommunityFirstPageCid = (\n community: Community,\n sortType: string | undefined,\n pageType = \"posts\",\n) => {\n assert(\n community && typeof community === \"object\",\n `getCommunityFirstPageCid community '${community}' invalid`,\n );\n assert(\n sortType === undefined || (typeof sortType === \"string\" && sortType.length > 0),\n `getCommunityFirstPageCid sortType '${sortType}' invalid`,\n );\n // posts served client-side from a complete preloaded page read that page instead of their own\n const pageSortType = pageType === \"posts\" ? getPostPageSortType(community, sortType) : sortType;\n if (!pageSortType) {\n return;\n }\n // community has preloaded posts for sort type\n if (community[pageType]?.pages?.[pageSortType]?.comments) {\n return community[pageType]?.pages?.[pageSortType]?.nextCid;\n }\n return community[pageType]?.pageCids?.[pageSortType];\n\n // TODO: if a loaded community doesn't have a first page, it's unclear what we should do\n // should we try to use another sort type by default, like 'hot', or should we just ignore it?\n};\n\n// reset store in between tests\nconst originalState = communitiesPagesStore.getState();\n// async function because some stores have async init\nexport const resetCommunitiesPagesStore = async () => {\n fetchPagePending = {};\n storedCommunityPageKeys = {};\n for (const accountId in fetchPageCommunities) {\n delete fetchPageCommunities[accountId];\n }\n // remove all event listeners\n listeners.forEach((listener: any) => listener.removeAllListeners());\n listeners.length = 0;\n // destroy all component subscriptions to the store\n communitiesPagesStore.destroy();\n // restore original state\n communitiesPagesStore.setState(originalState);\n};\n\n// reset database and store in between tests\nexport const resetCommunitiesPagesDatabaseAndStore = async () => {\n await localForageLru.createInstance({ name: \"bitsocialReactHooks-communitiesPages\" }).clear();\n await resetCommunitiesPagesStore();\n};\n\nexport default communitiesPagesStore;\n"]}
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "publishConfig": {
15
15
  "access": "public"
16
16
  },
17
- "version": "0.2.1",
17
+ "version": "0.2.2",
18
18
  "packageManager": "yarn@4.13.0",
19
19
  "files": [
20
20
  "CHANGELOG.md",