Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

fix: fix error on deep link to deleted comment (#838)

Eric Lee4a187b732074
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts b/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts
index d6a008cc..2dbec0d1 100644
--- a/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts
+++ b/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts
@@ -55,6 +55,10 @@ export async function getPreselectedCommentMetadata(challengeId: number, comment
});
const index = challengeComments.findIndex((comment) => comment.id === commentId);
+
+ // this `commentId` couldn't be found (perhaps it was deleted)
+ if (index === -1) return;
+
const selectedComment = challengeComments[index];
const page = Math.ceil((index + 1) / PAGESIZE);
@@ -88,8 +92,11 @@ export async function getPreselectedSolutionCommentMetadata(
if (!solution || !solution.solutionComment) return;
const comments = solution.solutionComment;
-
const index = comments.findIndex((comment) => comment.id === commentId);
+
+ // this `commentId` couldn't be found (perhaps it was deleted)
+ if (index === -1) return;
+
const selectedComment = comments[index];
const page = Math.ceil((index + 1) / PAGESIZE);
diff --git a/apps/web/tests/e2e/authenticated/comment.test.ts b/apps/web/tests/e2e/authenticated/comment.test.ts
index bf0f5478..ce50d6cb 100644
--- a/apps/web/tests/e2e/authenticated/comment.test.ts
+++ b/apps/web/tests/e2e/authenticated/comment.test.ts
@@ -101,7 +101,7 @@ test.describe('create, edit, and delete comments', () => {
});
});
-test.describe('share comment', () => {
+test.describe('sharing comments', () => {
const commentNumber = Math.floor(Math.random() * 1000);
const parentComment = `Here is my comment ${commentNumber}`;
@@ -127,4 +127,33 @@ test.describe('share comment', () => {
await page.goto(`${page.url()}/comments/${comment.id}`);
await expect(page.locator('div[id^=comment]', { hasText: parentComment })).toBeVisible();
});
+
+ test('visiting a link to a deleted comment successfully loads page with remaining comments', async ({
+ page,
+ }) => {
+ const commentA = await prisma.comment.create({
+ data: {
+ rootChallengeId: 6,
+ text: 'to be deleted',
+ userId: USER.id,
+ },
+ });
+
+ await prisma.comment.create({
+ data: {
+ rootChallengeId: 6,
+ text: 'this comment is timeless',
+ userId: USER.id,
+ },
+ });
+
+ // delete comment A
+ await prisma.comment.delete({ where: { id: commentA.id } });
+ // but visit a deep link to it anyway
+ await page.goto(`/challenge/6/comments/${commentA.id}`);
+ // assert other comments, such as comment B, are still visible on page load
+ await expect(
+ page.locator('div[id^=comment]', { hasText: 'this comment is timeless' }),
+ ).toBeVisible();
+ });
});