API reference
MarkdownSource

Actions Hooks

Actions Hooks in Bitsocial React Hooks. Usage, options, and examples.

useSubscribe({communityAddress: string}): {subscribed: boolean | undefined, subscribe: Function, unsubscribe: Function}
useBlock({address?: string, cid?: string}): {blocked: boolean | undefined, block: Function, unblock: Function}
useSaveComment({commentCid: string}): {saved: boolean | undefined, saveComment: Function, unsaveComment: Function}
usePublishComment(options: UsePublishCommentOptions): {index: number, abandonPublish: () => Promise<void>, ...UsePublishCommentResult}
usePublishVote(options: UsePublishVoteOptions): {abandonPublish: () => Promise<void>, ...UsePublishVoteResult}
usePublishCommentEdit(options: UsePublishCommentEditOptions): UsePublishCommentEditResult
usePublishCommentModeration(options: UsePublishCommentModerationOptions): UsePublishCommentModerationResult
usePublishCommunityEdit(options: UsePublishCommunityEditOptions): UsePublishCommunityEditResult
useCreateCommunity(options: CreateCommunityOptions): {createdCommunity: Community | undefined, createCommunity: Function}
useExportCommunity(options?: UseExportCommunityOptions): {
  communityExports: {communityAddress: string, exportId: string}[],
  exportCommunity: () => Promise<void>,
  state: string,
  error: Error | undefined,
  errors: Error[]
}

Each publishComment(), publishVote(), publishCommentEdit(), publishCommentModeration() or publishCommunityEdit() call starts a new result: the previous publication’s challenge, challengeVerification, publishChallengeAnswers, state, error and errors are cleared, and state becomes "failed" if the call rejects before anything is published, for example because the community’s challenge settings don’t load. When a failed challenge answer makes the publication publish again automatically, its new challenge clears the failed challengeVerification. If usePublishComment or usePublishVote publishes again before its previous publication finished, only the newest publication updates the result and abandonPublish() abandons the newest one; usePublishComment also passes onChallenge a third argument that abandons the publication the challenge belongs to, so code that spreads onChallenge’s arguments into an array and appends its own value now finds that value at index 3 instead of 2. usePublishCommentEdit, usePublishCommentModeration and usePublishCommunityEdit also update the result only from the newest publication; an earlier one that is still running keeps calling the app’s onChallenge, onChallengeVerification and onError with its own publication, so a late failure of an earlier edit reaches onError but does not change the newer edit’s state, error or errors. abandonPublish() resets state and the challenge fields and stops the publication if it is still in progress, deleting the pending comment or restoring the previous vote in the account that published it. It leaves a comment alone once its publication succeeded or failed, and a vote once it was verified (use deleteComment to remove the comment). While a comment publishes, index follows it when deleting an earlier comment of the account shifts it. exportCommunity() likewise clears the previous communityExports and errors, and only the latest call’s result is shown.

Final and recoverable publish errors. The publish hooks and the store’s accountsActions.publish* actions call onError(error, publication, info), where info is a PublishErrorInfo, {final: boolean}. final is true when the error ended the publish: pkc-js stopped the publication with it (for example the community never answered, every pubsub provider failed, or the RPC server failed the publish), publish() rejected (for example signing or the RPC publish call failed), or a hook’s publish action rejected, for example while loading the community’s challenge settings, in which case publication is undefined (the store actions reject instead of calling onError then). final is false for any other error: one pkc-js recovers from, for example a challenge it could not decrypt or parse, after which the publication keeps publishing and can still get a challenge and succeed, one a stopped publication emits while it is torn down, for example a failed pubsub unsubscribe before the error that ended the publish, or one emitted after the challenge verification arrived, which decided the outcome. At most one error per publication is final. Run terminal side effects, such as counting a failed post, forgetting a kept password, deleting a temporary account or showing a “failed” alert, only for a final error. A recoverable error is still added to the hook’s error and errors, and to a pending account comment’s error and errors, but leaves the comment pending, keeps a pending comment edit, moderation or community edit, and does not change the hook’s state: pkc-js reports publishingState "failed" for it too, and the hook keeps its previous state until the publication ends. A final error sets the hook’s state to "failed", also when publish() rejected before pkc-js reported any publishing state, and a publishing state pkc-js reports after it, such as an RPC server’s "stopped", does not replace it. Each error is reported once, also when pkc-js both emits it and rejects publish() with it. A challenge answer that could not be published is not an onError call: publishChallengeAnswers() rejects, pkc-js keeps the publication waiting so the answers can be published again, and the hook’s state stays on its last value. onError(error) and onError(error, publication) keep working. In TypeScript, the second and third parameters are declared optional so the publish options stay assignable to Options; annotate info as PublishErrorInfo or read info?.final.

const onError = (error, publication, info) => {
  if (!info.final) return console.warn("pkc-js recovered from", error); // still publishing
  alert(`Publishing failed: ${error.message}`);
};
const { publishComment, state, error } = usePublishComment({ ...publishCommentOptions, onError });

Challenges are keyed by challenge index: the position of the challenge in community.challenges, as a decimal string. challenge.challenges is the record pkc-js sends, unchanged, for example {"0": {challenge, type, caseInsensitive}, "3": {...}}. It holds only the challenges still pending, so its keys can be sparse; build the list to show from Object.entries(challenge.challenges) sorted by numeric key, and keep each key as that challenge’s index. publishChallengeAnswers(challengeAnswers), on the hook result and on the publication passed to onChallenge, takes a ChallengeAnswersByIndex record with one answer per key of that challenge message, for example {"0": "4", "3": "blue"}, or the same record wrapped as {challengeAnswers: {...}}. A key the answers leave out fails that challenge with "No answer.", and keys the challenge message did not ask for are ignored. Missing or empty answers (undefined, [], {}) reject with an error instead of being published. An array of answers is deprecated: it is mapped by position onto the ascending keys of the latest challenge message the publication received. Pre-answers passed as challengeRequest: {challengeAnswers} in the publish options are forwarded to pkc-js unchanged, so they use the same record, keyed by the index in community.challenges and holding only the challenges being pre-answered; when there are none, leave out challengeAnswers (and challengeRequest if it carries nothing else, such as challengeCommentCids), because pkc-js rejects an empty record or an array.

Migrating to 0.3.0. pkc-js 0.0.152 changed the challenge messages from arrays to records keyed by challenge index, with no compatibility path: a client on 0.3.0 cannot complete challenges with a community on an older pkc-js, and an older client cannot complete them with a community on pkc-js 0.0.152 or later. To migrate, read challenge.challenges as a record instead of an array (no more challenges.length, challenges.map or positional indexes), answer with publishChallengeAnswers({[challengeIndex]: answer}) with one key per entry (a challenge your interface completes without input, such as an iframe that verifies on its own, answers "" under its own key), and key any challengeRequest.challengeAnswers pre-answers by challenge index, leaving out challengeAnswers when there are none. publishChallengeAnswers() called without answers now rejects instead of publishing an empty list. Existing array answers keep working through the deprecated positional mapping, which TypeScript marks as deprecated.

Before publishComment, publishCommentEdit, publishVote, publishCommentModeration, or publishCommunityEdit signs a publication, hooks load the community challenge configuration and apply compatible wordfilters. The current @bitsocial/wordfilter-challenge contract uses a JSON array of {src, dst} objects in wordfilter/v1/rules and an optional JSON array of dot-notation paths in wordfilter/v1/fieldNames, both published through publicOptions. Every path starts with the publication type and is resolved against the publication being signed, so a path for another type is simply skipped. Without configured paths the defaults apply: comment.content, comment.title, comment.author.displayName, commentEdit.content, commentEdit.reason, commentEdit.author.displayName, and vote.author.displayName. Moderator and owner text is opt-in through configured paths (commentModeration.commentModeration.reason, communityEdit.communityEdit.title, communityEdit.communityEdit.description); structural and signing fields are never rewritten by community configuration. Matching is literal and case-insensitive, not regular-expression or Unicode-evasion matching. The transformed values are the values signed, published, and stored as the pending account publication. If composed rule sets do not stabilize within eight passes, publication stops before signing.