2024-04-25 22:02:55 +02:00
|
|
|
import { useEffect } from 'react';
|
2025-03-05 12:06:27 +01:00
|
|
|
import { useNavigate, useParams } from 'react-router-dom';
|
2026-04-20 22:11:36 +07:00
|
|
|
import { useAccountComments } from '@bitsocial/bitsocial-react-hooks';
|
2026-01-28 16:51:27 +08:00
|
|
|
import { useDirectories } from '../../hooks/use-directories';
|
2026-03-20 21:25:57 +08:00
|
|
|
import useSafeAccountComment from '../../hooks/use-safe-account-comment';
|
2026-04-17 10:48:27 +07:00
|
|
|
import { getCommentCommunityAddress } from '../../lib/utils/comment-utils';
|
2025-11-07 12:50:58 +01:00
|
|
|
import { getBoardPath } from '../../lib/utils/route-utils';
|
2024-06-11 15:49:26 +02:00
|
|
|
import { Post } from '../post';
|
2024-04-25 22:02:55 +02:00
|
|
|
|
2026-05-21 17:39:35 +07:00
|
|
|
type PendingAccountComment = {
|
|
|
|
|
index?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hasPendingAccountCommentIndex = (accountComments: PendingAccountComment[] | undefined, accountCommentIndex: number) => {
|
|
|
|
|
if (!accountComments || accountComments.length === 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let hasExplicitIndices = false;
|
|
|
|
|
for (const accountComment of accountComments) {
|
|
|
|
|
if (typeof accountComment?.index !== 'number') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasExplicitIndices = true;
|
|
|
|
|
if (accountComment.index === accountCommentIndex) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hasExplicitIndices ? false : accountCommentIndex < accountComments.length;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-25 22:02:55 +02:00
|
|
|
const PendingPost = () => {
|
2025-02-20 22:05:51 +01:00
|
|
|
const { accountComments } = useAccountComments();
|
2024-04-25 22:02:55 +02:00
|
|
|
const { accountCommentIndex } = useParams<{ accountCommentIndex?: string }>();
|
2026-03-20 21:25:57 +08:00
|
|
|
const normalizedAccountCommentIndex = accountCommentIndex === undefined ? undefined : Number(accountCommentIndex);
|
|
|
|
|
const hasNormalizedAccountCommentIndex = normalizedAccountCommentIndex !== undefined && !Number.isNaN(normalizedAccountCommentIndex);
|
|
|
|
|
const post = useSafeAccountComment({ commentIndex: accountCommentIndex });
|
2024-04-25 22:02:55 +02:00
|
|
|
const navigate = useNavigate();
|
2026-01-28 16:51:27 +08:00
|
|
|
const directories = useDirectories();
|
2024-04-25 22:02:55 +02:00
|
|
|
|
|
|
|
|
useEffect(() => window.scrollTo(0, 0), []);
|
|
|
|
|
|
2025-02-20 22:05:51 +01:00
|
|
|
const isValidAccountCommentIndex =
|
|
|
|
|
!accountCommentIndex ||
|
2026-03-20 21:25:57 +08:00
|
|
|
(hasNormalizedAccountCommentIndex &&
|
|
|
|
|
normalizedAccountCommentIndex >= 0 &&
|
|
|
|
|
Number.isInteger(normalizedAccountCommentIndex) &&
|
2026-05-21 17:39:35 +07:00
|
|
|
hasPendingAccountCommentIndex(accountComments, normalizedAccountCommentIndex));
|
2025-02-20 22:05:51 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isValidAccountCommentIndex) {
|
|
|
|
|
navigate('/not-found', { replace: true });
|
|
|
|
|
}
|
|
|
|
|
}, [isValidAccountCommentIndex, navigate]);
|
|
|
|
|
|
2024-04-25 22:02:55 +02:00
|
|
|
useEffect(() => {
|
2026-04-17 10:48:27 +07:00
|
|
|
const postCommunityAddress = getCommentCommunityAddress(post);
|
2026-03-13 13:25:10 +08:00
|
|
|
if (post?.cid && postCommunityAddress) {
|
|
|
|
|
const boardPath = getBoardPath(postCommunityAddress, directories);
|
2025-11-07 12:50:58 +01:00
|
|
|
navigate(`/${boardPath}/thread/${post.cid}`, { replace: true });
|
2024-04-25 22:02:55 +02:00
|
|
|
}
|
2026-01-28 16:51:27 +08:00
|
|
|
}, [post, navigate, directories]);
|
2024-04-25 22:02:55 +02:00
|
|
|
|
2025-03-05 12:06:27 +01:00
|
|
|
return <Post post={post} />;
|
2024-04-25 22:02:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default PendingPost;
|