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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | import { useMemo, useRef, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { FormattedMessage, useIntl } from 'react-intl';
import { Box, Typography, CircularProgress, Alert, Button, Card, CardContent } from '@mui/material';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import { useQuery } from '@tanstack/react-query';
import { formatRelativeTime } from '@/helpers/timeHelper';
import type { HNItem, HNItemId } from '@/types/hackernews';
const API_BASE_URL = 'https://hacker-news.firebaseio.com/v0';
async function fetchItem(id: HNItemId): Promise<HNItem | null> {
const response = await fetch(`${API_BASE_URL}/item/${id}.json`);
if (!response.ok) {
throw new Error(`Failed to fetch item ${id}: ${response.status} ${response.statusText}`);
}
return response.json();
}
async function fetchCommentsRecursively(itemId: HNItemId): Promise<Partial<HNItem>[]> {
const item = await fetchItem(itemId);
if (!item) return [];
const comments: Partial<HNItem>[] = [];
if (item.kids && item.kids.length > 0) {
const childComments = await Promise.all(
item.kids.map(async (kidId) => {
const kidItem = await fetchItem(kidId);
if (!kidItem) return [];
const nestedComments = kidItem.kids ? await fetchCommentsRecursively(kidId) : [];
return [kidItem, ...nestedComments];
})
);
comments.push(...childComments.flat());
}
return comments;
}
async function fetchPostComments(postId: number): Promise<Partial<HNItem>[]> {
const post = await fetchItem(postId);
if (!post || !post.kids || post.kids.length === 0) {
return [];
}
const allComments = await Promise.all(
post.kids.map(async (kidId) => {
const topLevelComment = await fetchItem(kidId);
if (!topLevelComment) return [];
const nestedComments = topLevelComment.kids ? await fetchCommentsRecursively(kidId) : [];
return [topLevelComment, ...nestedComments];
})
);
return allComments.flat();
}
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 CommentsTanStack() {
const navigate = useNavigate();
const { postId } = useParams<{ postId: string }>();
const postIdNum = postId ? parseInt(postId, 10) : 0;
const {
data: comments = [],
isLoading,
error,
} = useQuery({
queryKey: ['comments', postIdNum],
queryFn: () => fetchPostComments(postIdNum),
enabled: !!postId,
staleTime: 300000,
retry: 2,
});
const topLevelComments = useMemo(() => {
if (!postId) return [];
return comments.filter((comment) => comment.parent === postIdNum);
}, [comments, postId, postIdNum]);
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>
{isLoading && (
<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 instanceof Error ? error.message : 'An error occurred'}
</Alert>
)}
{!isLoading && !error && topLevelComments.length === 0 && (
<Typography variant="body1" color="text.secondary">
<FormattedMessage id="comments.noComments" />
</Typography>
)}
{!isLoading && 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 CommentsTanStack;
|