Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import { useEffect, useMemo, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useParams, useNavigate } from 'react-router-dom';
import { FormattedMessage } from 'react-intl';
import { Box, Typography, CircularProgress, Alert, Button, Card, CardContent } from '@mui/material';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import { fetchComments } from '@/store/reducers/comments/comments-actions';
import {
selectComments,
selectCommentsLoading,
selectCommentsError,
} from '@/store/reducers/comments/comments-selectors';
import { formatRelativeTime } from '@/helpers/timeHelper';
import { useIntl } from 'react-intl';
import type { HNItem } from '@/types/hackernews';
interface CommentItemProps {
item: Partial<HNItem>;
allComments: Partial<HNItem>[];
depth?: number;
}
function CommentItem({ item, allComments, depth = 0 }: CommentItemProps) {
const intl = useIntl();
const contentRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (contentRef.current) {
const links = contentRef.current.querySelectorAll('a');
links.forEach((link) => {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
});
}
}, [item.text]);
const childComments = useMemo(() => {
if (!item.kids || item.kids.length === 0) {
return [];
}
return item.kids
.map((kidId) => allComments.find((c) => c.id === kidId))
.filter((c): c is Partial<HNItem> => c !== undefined);
}, [item.kids, allComments]);
if (!item.text && !item.deleted) {
return null;
}
return (
<Box marginLeft={depth * 0.8} role="article" aria-label={`Comment by ${item.by || 'user'}`}>
<Card sx={{ marginBottom: 2 }} data-testid="comment-item">
<CardContent>
<Box display="flex" gap={2} marginBottom={1}>
{item.by && (
<Typography variant="body2" fontWeight="bold">
{item.by}
</Typography>
)}
{item.time && (
<Typography variant="caption" color="text.secondary">
{formatRelativeTime(item.time, intl)}
</Typography>
)}
</Box>
{item.deleted ? (
<Typography variant="body2" color="text.secondary" fontStyle="italic">
[deleted]
</Typography>
) : (
item.text && (
<Typography
ref={contentRef}
variant="body2"
component="div"
dangerouslySetInnerHTML={{ __html: item.text }}
/>
)
)}
</CardContent>
</Card>
{childComments.map((childComment) => (
<CommentItem key={childComment.id} item={childComment} allComments={allComments} depth={depth + 1} />
))}
</Box>
);
}
function Comments() {
const dispatch = useDispatch();
const navigate = useNavigate();
const { postId } = useParams<{ postId: string }>();
const comments = useSelector(selectComments);
const loading = useSelector(selectCommentsLoading);
const error = useSelector(selectCommentsError);
useEffect(() => {
if (postId) {
dispatch(fetchComments(parseInt(postId, 10)));
}
}, [dispatch, postId]);
// Csak a top-level kommenteket jelenítjük meg (amelyek parent-je a postId)
const topLevelComments = useMemo(() => {
if (!postId) return [];
const postIdNum = parseInt(postId, 10);
return comments.filter((comment) => comment.parent === postIdNum);
}, [comments, postId]);
return (
<Box maxWidth="1200px" margin="0 auto" padding={3}>
<Button startIcon={<ArrowBackIcon />} onClick={() => navigate(-1)} sx={{ marginBottom: 2 }} aria-label="Go back">
<FormattedMessage id="comments.back" />
</Button>
<Typography variant="h4" component="h1" gutterBottom>
<FormattedMessage id="comments.title" />
</Typography>
{loading && (
<Box display="flex" justifyContent="center" marginY={4} role="status" aria-live="polite">
<CircularProgress aria-label="Loading comments" />
</Box>
)}
{error && (
<Alert severity="error" sx={{ marginBottom: 2 }}>
{error}
</Alert>
)}
{!loading && !error && topLevelComments.length === 0 && (
<Typography variant="body1" color="text.secondary">
<FormattedMessage id="comments.noComments" />
</Typography>
)}
{!loading && topLevelComments.length > 0 && (
<Box role="list" aria-label="Comments list">
{topLevelComments.map((comment) => (
<CommentItem key={comment.id} item={comment} allComments={comments} />
))}
</Box>
)}
</Box>
);
}
export default Comments;
|