Skip to content

Commit 956b463

Browse files
fix: handle deleted questions and answers in votes and answers pages
1 parent 6aaa1b2 commit 956b463

File tree

2 files changed

+105
-50
lines changed

2 files changed

+105
-50
lines changed

src/app/users/[userId]/[userSlug]/answers/page.tsx

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { Particles } from "@/components/magicui/particles";
32
import Pagination from "@/components/Pagination";
43
import { MarkdownPreview } from "@/components/RTE";
@@ -29,14 +28,29 @@ const Page = async (
2928

3029
const answers = await databases.listDocuments(db, answerCollection, queries);
3130

32-
answers.documents = await Promise.all(
31+
answers.documents = (await Promise.all(
3332
answers.documents.map(async ans => {
34-
const question = await databases.getDocument(db, questionCollection, ans.questionId, [
35-
Query.select(["title"]),
36-
]);
37-
return { ...ans, question };
33+
try {
34+
const question = await databases.getDocument(
35+
db,
36+
questionCollection,
37+
ans.questionId,
38+
[Query.select(["title"])]
39+
);
40+
return { ...ans, question };
41+
} catch (error) {
42+
// Handle deleted questions
43+
console.log(error)
44+
return {
45+
...ans,
46+
question: {
47+
$id: ans.questionId,
48+
title: "[Deleted Question]"
49+
}
50+
};
51+
}
3852
})
39-
);
53+
)).filter(Boolean); // Remove any undefined entries
4054

4155
return (
4256
<div className="px-4">
@@ -52,16 +66,25 @@ const Page = async (
5266
</div>
5367
<div className="mb-4 max-w-3xl space-y-6">
5468
{answers.documents.map(ans => (
55-
<div key={ans.$id}>
69+
<div
70+
key={ans.$id}
71+
className="rounded-xl border border-white/40 p-4 duration-200 hover:bg-white/10"
72+
>
5673
<div className="max-h-40 overflow-auto">
5774
<MarkdownPreview source={ans.content} className="rounded-lg p-4" />
5875
</div>
59-
<Link
60-
href={`/questions/${ans.questionId}/${slugify(ans.question.title)}`}
61-
className="mt-3 inline-block shrink-0 rounded bg-orange-500 px-4 py-2 font-bold text-white hover:bg-orange-600"
62-
>
63-
Question
64-
</Link>
76+
{ans.question.title === "[Deleted Question]" ? (
77+
<span className="mt-3 inline-block text-gray-500 italic">
78+
{ans.question.title}
79+
</span>
80+
) : (
81+
<Link
82+
href={`/questions/${ans.questionId}/${slugify(ans.question.title)}`}
83+
className="mt-3 inline-block shrink-0 rounded bg-orange-500 px-4 py-2 font-bold text-white hover:bg-orange-600"
84+
>
85+
View Question
86+
</Link>
87+
)}
6588
</div>
6689
))}
6790
</div>

src/app/users/[userId]/[userSlug]/votes/page.tsx

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import Pagination from "@/components/Pagination";
33
import { answerCollection, db, questionCollection, voteCollection } from "@/models/name";
44
import { databases } from "@/models/server/config";
@@ -30,46 +30,72 @@ const Page = async (
3030

3131
const votes = await databases.listDocuments(db, voteCollection, query);
3232

33-
votes.documents = await Promise.all(
33+
votes.documents = (await Promise.all(
3434
votes.documents.map(async vote => {
35-
const questionOfTypeQuestion =
36-
vote.type === "question"
37-
? await databases.getDocument(db, questionCollection, vote.typeId, [
38-
Query.select(["title"]),
39-
])
40-
: null;
35+
try {
36+
// For votes on questions
37+
if (vote.type === "question") {
38+
const questionOfTypeQuestion = await databases.getDocument(
39+
db,
40+
questionCollection,
41+
vote.typeId,
42+
[Query.select(["title"])]
43+
);
44+
45+
return {
46+
...vote,
47+
question: questionOfTypeQuestion,
48+
};
49+
}
50+
51+
// For votes on answers
52+
try {
53+
const answer = await databases.getDocument(db, answerCollection, vote.typeId);
54+
const questionOfTypeAnswer = await databases.getDocument(
55+
db,
56+
questionCollection,
57+
answer.questionId,
58+
[Query.select(["title"])]
59+
);
4160

42-
if (questionOfTypeQuestion) {
61+
return {
62+
...vote,
63+
question: questionOfTypeAnswer,
64+
};
65+
} catch (error:any) {
66+
// If answer or its question is deleted, mark it
67+
console.log(error)
68+
return {
69+
...vote,
70+
question: {
71+
$id: vote.typeId,
72+
title: "[Deleted Content]",
73+
},
74+
};
75+
}
76+
} catch (error:any) {
77+
console.log(error)
78+
// Return a placeholder for deleted content
4379
return {
4480
...vote,
45-
question: questionOfTypeQuestion,
81+
question: {
82+
$id: vote.typeId,
83+
title: "[Deleted Content]",
84+
},
4685
};
4786
}
48-
49-
const answer = await databases.getDocument(db, answerCollection, vote.typeId);
50-
const questionOfTypeAnswer = await databases.getDocument(
51-
db,
52-
questionCollection,
53-
answer.questionId,
54-
[Query.select(["title"])]
55-
);
56-
57-
return {
58-
...vote,
59-
question: questionOfTypeAnswer,
60-
};
6187
})
62-
);
88+
)).filter(vote => vote !== null); // Remove any null entries
6389

6490
return (
6591
<div className="px-4">
6692
<Particles
67-
className="fixed inset-0 h-full w-full"
68-
quantity={500}
69-
ease={100}
70-
color="#ffffff"
71-
refresh
72-
/>
93+
className="fixed inset-0 h-full w-full"
94+
quantity={500}
95+
ease={100}
96+
color="#ffffff"
97+
refresh
98+
/>
7399
<div className="mb-4 flex justify-between">
74100
<p>{votes.total} votes</p>
75101
<ul className="flex gap-1">
@@ -118,12 +144,18 @@ const Page = async (
118144
<div className="flex">
119145
<p className="mr-4 shrink-0">{vote.voteStatus}</p>
120146
<p>
121-
<Link
122-
href={`/questions/${vote.question.$id}/${slugify(vote.question.title)}`}
123-
className="text-orange-500 hover:text-orange-600"
124-
>
125-
{vote.question.title}
126-
</Link>
147+
{vote.question.title === "[Deleted Content]" ? (
148+
<span className="text-gray-500 italic">
149+
{vote.question.title}
150+
</span>
151+
) : (
152+
<Link
153+
href={`/questions/${vote.question.$id}/${slugify(vote.question.title)}`}
154+
className="text-orange-500 hover:text-orange-600"
155+
>
156+
{vote.question.title}
157+
</Link>
158+
)}
127159
</p>
128160
</div>
129161
<p className="text-right text-sm">

0 commit comments

Comments
 (0)