/ src / lib / utils / challenge-utils.ts
challenge-utils.ts
 1  import { ChallengeVerification } from '@plebbit/plebbit-react-hooks';
 2  
 3  export const alertChallengeVerificationFailed = (challengeVerification: ChallengeVerification, publication: any) => {
 4    if (challengeVerification?.challengeSuccess === false) {
 5      console.warn(challengeVerification, publication);
 6      alert(`p/${publication?.subplebbitAddress} challenge error: ${[...(challengeVerification?.challengeErrors || []), challengeVerification?.reason].join(' ')}`);
 7    } else {
 8      console.log(challengeVerification, publication);
 9    }
10  };
11  
12  export const getPublicationType = (publication: any) => {
13    if (!publication) {
14      return;
15    }
16    if (typeof publication.vote === 'number') {
17      return 'vote';
18    }
19    if (publication.parentCid) {
20      return 'reply';
21    }
22    if (publication.commentCid) {
23      return 'edit';
24    }
25    return 'post';
26  };
27  
28  export const getVotePreview = (publication: any) => {
29    if (typeof publication?.vote !== 'number') {
30      return '';
31    }
32    let votePreview = '';
33    if (publication.vote === -1) {
34      votePreview += ' -1';
35    } else {
36      votePreview += ` +${publication.vote}`;
37    }
38    return votePreview;
39  };
40  
41  export const getPublicationPreview = (publication: any) => {
42    if (!publication) {
43      return '';
44    }
45    let publicationPreview = '';
46    if (publication.title) {
47      publicationPreview += publication.title;
48    }
49    if (publication.content) {
50      if (publicationPreview) {
51        publicationPreview += ': ';
52      }
53      publicationPreview += publication.content;
54    }
55    if (!publicationPreview && publication.link) {
56      publicationPreview += publication.link;
57    }
58  
59    if (publicationPreview.length > 50) {
60      publicationPreview = publicationPreview.substring(0, 50) + '...';
61    }
62    return publicationPreview;
63  };