Skip to content

Commit d5526ae

Browse files
committed
fix: fixed double comments
1 parent 6fe40c8 commit d5526ae

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/app/posts/[id]/page.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,20 @@ export default function PostPage() {
171171

172172
// Filter out optimistic updates and merge with real data
173173
setComments(prevComments => {
174-
const tempComments = prevComments.filter(c => c.id.startsWith('temp-'));
175174
const realComments = commentsData;
176175

177-
// Detect new comments for animation
178-
const prevCommentIds = new Set(prevComments.map(c => c.id));
176+
// Get all real comment contents to match against optimistic ones
177+
const realCommentContents = new Set(realComments.map(c => c.content.trim()));
178+
179+
// Keep only temp comments that don't have a matching real comment yet
180+
const tempComments = prevComments.filter(c =>
181+
c.id.startsWith('temp-') && !realCommentContents.has(c.content.trim())
182+
);
183+
184+
// Detect new comments for animation (only real comments that weren't in previous state)
185+
const prevRealCommentIds = new Set(prevComments.filter(c => !c.id.startsWith('temp-')).map(c => c.id));
179186
const newCommentIds = realComments
180-
.filter(c => !prevCommentIds.has(c.id) && !c.id.startsWith('temp-'))
187+
.filter(c => !prevRealCommentIds.has(c.id))
181188
.map(c => c.id);
182189

183190
if (newCommentIds.length > 0) {

0 commit comments

Comments
 (0)