mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
import { ChallengeVerification } from '@bitsocialhq/bitsocial-react-hooks';
|
|
import directoriesData from '../../data/5chan-directories.json';
|
|
import { getBoardPath } from './route-utils';
|
|
|
|
const resolveBoardIdentifier = (subplebbitAddress: unknown): string => {
|
|
if (typeof subplebbitAddress !== 'string' || !subplebbitAddress) {
|
|
return 'unknown board';
|
|
}
|
|
|
|
const boardPath = getBoardPath(subplebbitAddress, directoriesData.communities);
|
|
return boardPath === subplebbitAddress ? subplebbitAddress : `/${boardPath}/`;
|
|
};
|
|
|
|
export const alertChallengeVerificationFailed = (challengeVerification: ChallengeVerification, publication: any) => {
|
|
if (challengeVerification?.challengeSuccess === false) {
|
|
console.warn('Challenge Verification Failed:', challengeVerification, 'Publication:', publication);
|
|
|
|
let errorMessages: string[] = [];
|
|
if (challengeVerification?.challengeErrors) {
|
|
if (
|
|
typeof challengeVerification.challengeErrors === 'object' &&
|
|
challengeVerification.challengeErrors !== null &&
|
|
!Array.isArray(challengeVerification.challengeErrors)
|
|
) {
|
|
errorMessages = Object.values(challengeVerification.challengeErrors).filter((val): val is string => typeof val === 'string');
|
|
} else if (Array.isArray(challengeVerification.challengeErrors)) {
|
|
errorMessages = [...challengeVerification.challengeErrors];
|
|
} else {
|
|
console.warn('challengeVerification.challengeErrors is not an object or array:', challengeVerification.challengeErrors);
|
|
}
|
|
}
|
|
|
|
if (challengeVerification?.reason) {
|
|
errorMessages.push(challengeVerification.reason);
|
|
}
|
|
|
|
const finalMessage = errorMessages.filter(Boolean).join(' ');
|
|
|
|
alert(`Error from ${resolveBoardIdentifier(publication?.subplebbitAddress)}: ${finalMessage || 'unknown error'}`);
|
|
} else {
|
|
console.log('Challenge verification succeeded:', challengeVerification);
|
|
}
|
|
};
|
|
|
|
export const getPublicationType = (publication: any) => {
|
|
if (!publication) {
|
|
return;
|
|
}
|
|
if (typeof publication.vote === 'number') {
|
|
return 'vote';
|
|
}
|
|
if (publication.parentCid) {
|
|
return 'reply';
|
|
}
|
|
if (publication.commentCid) {
|
|
return 'edit';
|
|
}
|
|
return 'post';
|
|
};
|
|
|
|
export const getVotePreview = (publication: any) => {
|
|
if (typeof publication?.vote !== 'number') {
|
|
return '';
|
|
}
|
|
let votePreview = '';
|
|
if (publication.vote === -1) {
|
|
votePreview += ' -1';
|
|
} else {
|
|
votePreview += ` +${publication.vote}`;
|
|
}
|
|
return votePreview;
|
|
};
|
|
|
|
export const getPublicationPreview = (publication: any) => {
|
|
if (!publication) {
|
|
return '';
|
|
}
|
|
let publicationPreview = '';
|
|
if (publication.title) {
|
|
publicationPreview += publication.title;
|
|
}
|
|
if (publication.content) {
|
|
if (publicationPreview) {
|
|
publicationPreview += ': ';
|
|
}
|
|
publicationPreview += publication.content;
|
|
}
|
|
if (!publicationPreview && publication.link) {
|
|
publicationPreview += publication.link;
|
|
}
|
|
|
|
if (publicationPreview.length > 50) {
|
|
publicationPreview = publicationPreview.substring(0, 50) + '...';
|
|
}
|
|
return publicationPreview;
|
|
};
|