mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(publishing): abandon challenge close and clean failed local posts
This commit is contained in:
@@ -18,6 +18,7 @@ const useParentAddress = (parentCid?: string) => {
|
||||
interface ChallengeProps {
|
||||
challenge: ChallengeType;
|
||||
closeModal: () => void;
|
||||
abandonModal: () => void;
|
||||
}
|
||||
|
||||
const TextChallenge = ({ challenge }: { challenge: string }) => <div className={styles.challengeMedia}>{challenge}</div>;
|
||||
@@ -29,12 +30,12 @@ interface IframeChallengeProps {
|
||||
shortSubplebbitAddress?: string;
|
||||
subplebbitAddress?: string;
|
||||
readableUrl: string;
|
||||
closeModal: () => void;
|
||||
onCancel: () => void;
|
||||
onDone: () => void;
|
||||
publicationDetails: React.ReactNode;
|
||||
}
|
||||
|
||||
const IframeChallenge = ({ challenge, shortSubplebbitAddress, subplebbitAddress, readableUrl, closeModal, onDone, publicationDetails }: IframeChallengeProps) => {
|
||||
const IframeChallenge = ({ challenge, shortSubplebbitAddress, subplebbitAddress, readableUrl, onCancel, onDone, publicationDetails }: IframeChallengeProps) => {
|
||||
const account = useAccount();
|
||||
const [theme] = useTheme();
|
||||
const [showIframeConfirmation, setShowIframeConfirmation] = useState(true);
|
||||
@@ -71,9 +72,9 @@ const IframeChallenge = ({ challenge, shortSubplebbitAddress, subplebbitAddress,
|
||||
} catch (error) {
|
||||
console.error('Invalid iframe challenge URL', { error });
|
||||
alert('Error: Invalid URL for authentication challenge');
|
||||
closeModal();
|
||||
onCancel();
|
||||
}
|
||||
}, [account, challenge, closeModal, theme]);
|
||||
}, [account, challenge, onCancel, theme]);
|
||||
|
||||
const sendThemeToIframe = useCallback(() => {
|
||||
if (!iframeRef.current || !iframeOrigin) return;
|
||||
@@ -106,7 +107,7 @@ const IframeChallenge = ({ challenge, shortSubplebbitAddress, subplebbitAddress,
|
||||
<div className={`${styles.challengeFooter} ${styles.iframeFooter}`}>
|
||||
<span className={styles.buttons}>
|
||||
<button onClick={handleLoadIframe}>Open</button>
|
||||
<button onClick={closeModal}>Cancel</button>
|
||||
<button onClick={onCancel}>Cancel</button>
|
||||
</span>
|
||||
</div>
|
||||
</>
|
||||
@@ -134,7 +135,7 @@ const IframeChallenge = ({ challenge, shortSubplebbitAddress, subplebbitAddress,
|
||||
);
|
||||
};
|
||||
|
||||
const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
|
||||
const Challenge = ({ challenge, closeModal, abandonModal }: ChallengeProps) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const challenges = challenge?.[0]?.challenges;
|
||||
@@ -207,7 +208,7 @@ const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
|
||||
closeModal();
|
||||
};
|
||||
|
||||
const onIframeClose = useCallback(() => {
|
||||
const onIframeDone = useCallback(() => {
|
||||
if (!publication) return;
|
||||
publication.publishChallengeAnswers(['']);
|
||||
closeModal();
|
||||
@@ -226,16 +227,12 @@ const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
|
||||
useEffect(() => {
|
||||
const onEscapeKey = (e: KeyboardEvent) => {
|
||||
if (e.key === 'Escape') {
|
||||
if (isIframeChallenge) {
|
||||
onIframeClose();
|
||||
} else {
|
||||
closeModal();
|
||||
}
|
||||
abandonModal();
|
||||
}
|
||||
};
|
||||
document.addEventListener('keydown', onEscapeKey);
|
||||
return () => document.removeEventListener('keydown', onEscapeKey);
|
||||
}, [closeModal, isIframeChallenge, onIframeClose]);
|
||||
}, [abandonModal]);
|
||||
|
||||
const getChallengeUrl = useCallback(() => {
|
||||
try {
|
||||
@@ -314,7 +311,7 @@ const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
|
||||
>
|
||||
<div className={`challengeHandle ${styles.title}`} {...(!isMobile ? bind() : {})}>
|
||||
Challenge for {publicationType}
|
||||
<button className={styles.closeIcon} onClick={closeModal} title='close' />
|
||||
<button className={styles.closeIcon} onClick={abandonModal} title='close' />
|
||||
</div>
|
||||
<div className={styles.publication}>
|
||||
{isIframeChallenge ? (
|
||||
@@ -324,8 +321,8 @@ const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
|
||||
shortSubplebbitAddress={shortSubplebbitAddress}
|
||||
subplebbitAddress={subplebbitAddress}
|
||||
readableUrl={readableUrl}
|
||||
closeModal={closeModal}
|
||||
onDone={onIframeClose}
|
||||
onCancel={abandonModal}
|
||||
onDone={onIframeDone}
|
||||
publicationDetails={publicationDetails}
|
||||
/>
|
||||
) : (
|
||||
@@ -357,7 +354,7 @@ const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
|
||||
{t('submit')}
|
||||
</button>
|
||||
)}
|
||||
<button onClick={closeModal}>Cancel</button>
|
||||
<button onClick={abandonModal}>Cancel</button>
|
||||
{challenges?.length > 1 && (
|
||||
<button disabled={!challenges?.[currentChallengeIndex - 1]} onClick={() => setCurrentChallengeIndex((prev) => prev - 1)}>
|
||||
{t('previous')}
|
||||
@@ -374,14 +371,17 @@ const Challenge = ({ challenge, closeModal }: ChallengeProps) => {
|
||||
};
|
||||
|
||||
const ChallengeModal = () => {
|
||||
const { challenges, removeChallenge } = useChallengesStore();
|
||||
const { challenges, removeChallenge, abandonCurrentChallenge } = useChallengesStore();
|
||||
const isOpen = !!challenges.length;
|
||||
const closeModal = () => removeChallenge();
|
||||
const abandonModal = () => {
|
||||
void abandonCurrentChallenge();
|
||||
};
|
||||
const current = challenges[0];
|
||||
const challenge = current?.challenge;
|
||||
const challengeId = current?.id ?? 0;
|
||||
|
||||
return isOpen && challenge ? <Challenge key={challengeId} challenge={challenge} closeModal={closeModal} /> : null;
|
||||
return isOpen && challenge ? <Challenge key={challengeId} challenge={challenge} closeModal={closeModal} abandonModal={abandonModal} /> : null;
|
||||
};
|
||||
|
||||
export default ChallengeModal;
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useEffect, useRef, useState, useCallback } from 'react';
|
||||
import { Trans, useTranslation } from 'react-i18next';
|
||||
import { Link, useLocation, useNavigationType, useParams } from 'react-router-dom';
|
||||
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
||||
import { Comment, useEditedComment, useReplies, useAccount, useAccountComment } from '@bitsocialhq/bitsocial-react-hooks';
|
||||
import { Comment, deleteComment, useEditedComment, useReplies, useAccount, useAccountComment } from '@bitsocialhq/bitsocial-react-hooks';
|
||||
import getShortAddress from '../../lib/get-short-address';
|
||||
import styles from '../../views/post/post.module.css';
|
||||
import { CommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail, getMediaDimensions } from '../../lib/utils/media-utils';
|
||||
@@ -98,6 +98,7 @@ const PostInfo = ({
|
||||
const displayName = author?.displayName?.trim();
|
||||
const authorRole = roles?.[address]?.role?.replace('moderator', 'mod');
|
||||
const hasFailedState = state === 'failed';
|
||||
const canDeleteFailedPost = hasFailedState && typeof post?.index === 'number';
|
||||
const isReply = parentCid;
|
||||
const { showOmittedReplies } = useShowOmittedReplies();
|
||||
const directories = useDirectories();
|
||||
@@ -161,6 +162,7 @@ const PostInfo = ({
|
||||
});
|
||||
|
||||
const [initiatedPendingAction, setInitiatedPendingAction] = useState<'approve' | 'reject' | null>(null);
|
||||
const [isDeletingFailedPost, setIsDeletingFailedPost] = useState(false);
|
||||
|
||||
const handlePendingApprove = useCallback(async () => {
|
||||
const confirm = window.confirm(t('double_confirm'));
|
||||
@@ -244,6 +246,23 @@ const PostInfo = ({
|
||||
: openReplyModal && openReplyModal(cid, post?.number, postCid, threadNumber, subplebbitAddress);
|
||||
};
|
||||
|
||||
const onDeleteFailedPost = () => {
|
||||
if (isDeletingFailedPost || !canDeleteFailedPost) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsDeletingFailedPost(true);
|
||||
deleteComment(post?.cid || post.index)
|
||||
.then(() => {
|
||||
setIsDeletingFailedPost(false);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Failed to delete failed post:', error);
|
||||
alert(`Failed to delete post: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
setIsDeletingFailedPost(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={styles.postInfo}>
|
||||
{isHidden ? parentCid && <span className={styles.hiddenReplyEditMenuSpacer} /> : <EditMenu post={post} />}
|
||||
@@ -452,6 +471,15 @@ const PostInfo = ({
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
{canDeleteFailedPost && (
|
||||
<span className={styles.failedPublishNotice}>
|
||||
this post failed to publish, it's not visible to other users [{' '}
|
||||
<button type='button' className={styles.failedDeletePostButton} disabled={isDeletingFailedPost} onClick={onDeleteFailedPost}>
|
||||
Delete Post
|
||||
</button>{' '}
|
||||
]
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
{!(removed || deleted) && !isModQueue && <PostMenuDesktop postMenu={postMenuProps} />}
|
||||
{cid && parentCid && <ReplyBacklinks post={post} quotedByMap={quotedByMap} directRepliesByParentCid={directRepliesByParentCid} />}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useEffect, useRef, useState, useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Link, useLocation, useNavigationType, useParams } from 'react-router-dom';
|
||||
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
||||
import { Comment, useEditedComment, useReplies, useAccount, usePublishCommentModeration, useAccountComment } from '@bitsocialhq/bitsocial-react-hooks';
|
||||
import { Comment, deleteComment, useEditedComment, useReplies, useAccount, usePublishCommentModeration, useAccountComment } from '@bitsocialhq/bitsocial-react-hooks';
|
||||
import getShortAddress from '../../lib/get-short-address';
|
||||
import styles from '../../views/post/post.module.css';
|
||||
import { shouldShowSnow } from '../../lib/snow';
|
||||
@@ -188,6 +188,7 @@ const PostInfoAndMedia = ({ post, postReplyCount = 0, roles, threadNumber }: Pos
|
||||
const isOverThreshold = isAwaitingApproval && timeWaiting > alertThresholdSeconds;
|
||||
|
||||
const hasFailedState = state === 'failed';
|
||||
const canDeleteFailedPost = hasFailedState && typeof post?.index === 'number';
|
||||
const postMenuProps = selectPostMenuProps(post);
|
||||
|
||||
const pseudonymityMode = useSubplebbitField(subplebbitAddress, (sub) => sub?.features?.pseudonymityMode);
|
||||
@@ -223,6 +224,24 @@ const PostInfoAndMedia = ({ post, postReplyCount = 0, roles, threadNumber }: Pos
|
||||
: openReplyModal && openReplyModal(cid, post?.number, postCid, threadNumber, subplebbitAddress);
|
||||
};
|
||||
|
||||
const [isDeletingFailedPost, setIsDeletingFailedPost] = useState(false);
|
||||
const onDeleteFailedPost = () => {
|
||||
if (isDeletingFailedPost || !canDeleteFailedPost) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsDeletingFailedPost(true);
|
||||
deleteComment(post?.cid || post.index)
|
||||
.then(() => {
|
||||
setIsDeletingFailedPost(false);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Failed to delete failed post:', error);
|
||||
alert(`Failed to delete post: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
setIsDeletingFailedPost(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className={styles.postInfo}>
|
||||
@@ -393,6 +412,15 @@ const PostInfoAndMedia = ({ post, postReplyCount = 0, roles, threadNumber }: Pos
|
||||
</div>
|
||||
)}
|
||||
</span>
|
||||
{canDeleteFailedPost && (
|
||||
<span className={styles.failedPublishNotice}>
|
||||
this post failed to publish, it's not visible to other users [{' '}
|
||||
<button type='button' className={styles.failedDeletePostButton} disabled={isDeletingFailedPost} onClick={onDeleteFailedPost}>
|
||||
Delete Post
|
||||
</button>{' '}
|
||||
]
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
{(hasThumbnail || link) && !(deleted || removed) && <PostMediaContent key={cid} post={post} link={link} />}
|
||||
|
||||
Reference in New Issue
Block a user