Skip to content

Commit 35ed439

Browse files
committed
Fetch Real Question Details
1 parent 11317bf commit 35ed439

File tree

3 files changed

+12
-85
lines changed

3 files changed

+12
-85
lines changed

app/(root)/questions/[id]/page.tsx

Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,20 @@ import { Preview } from "@/components/editor/Preview";
33
import Metric from "@/components/Metric";
44
import UserAvatar from "@/components/UserAvatar";
55
import ROUTES from "@/constants/routes";
6+
import { getQuestion } from "@/lib/actions/question.action";
67
import { formatNumber, getTimeStamp } from "@/lib/utils";
78
import Link from "next/link";
9+
import { redirect } from "next/navigation";
10+
import { title } from "process";
811
import React from "react";
912

10-
const sampleQuestion = {
11-
id: "q123",
12-
title: "How to improve React app performance?",
13-
content: `### Question
14-
15-
I'm looking for tips and best practices to enhance the performance of a React application. I have a moderately complex app with multiple components, and I've noticed some performance bottlenecks. What should I focus on?
16-
17-
#### What I've Tried:
18-
- Lazy loading components
19-
- Using React.memo on some components
20-
- Managing state with React Context API
21-
22-
#### Issues:
23-
- The app still lags when rendering large lists.
24-
- Switching between pages feels sluggish.
25-
- Sometimes, re-renders happen unexpectedly.
26-
27-
#### Key Areas I Need Help With:
28-
1. Efficiently handling large datasets.
29-
2. Reducing unnecessary re-renders.
30-
3. Optimizing state management.
31-
32-
Here is a snippet of my code that renders a large list. Maybe I'm doing something wrong here:
33-
34-
\`\`\`js
35-
import React, { useState, useMemo } from "react";
36-
37-
const LargeList = ({ items }) => {
38-
const [filter, setFilter] = useState("");
39-
40-
// Filtering items dynamically
41-
const filteredItems = useMemo(() => {
42-
return items.filter((item) => item.includes(filter));
43-
}, [items, filter]);
44-
45-
return (
46-
<div>
47-
<input
48-
type="text"
49-
value={filter}
50-
onChange={(e) => setFilter(e.target.value)}
51-
placeholder="Filter items"
52-
/>
53-
<ul>
54-
{filteredItems.map((item, index) => (
55-
<li key={index}>{item}</li>
56-
))}
57-
</ul>
58-
</div>
59-
);
60-
};
61-
62-
export default LargeList;
63-
\`\`\`
64-
65-
#### Questions:
66-
1. Is using \`useMemo\` the right approach here, or is there a better alternative?
67-
2. Should I implement virtualization for the list? If yes, which library would you recommend?
68-
3. Are there better ways to optimize state changes when dealing with user input and dynamic data?
69-
70-
Looking forward to your suggestions and examples!
71-
72-
**Tags:** React, Performance, State Management
73-
`,
74-
createdAt: "2025-01-15T12:34:56.789Z",
75-
upvotes: 42,
76-
downvotes: 3,
77-
views: 1234,
78-
answers: 5,
79-
tags: [
80-
{ _id: "tag1", name: "React" },
81-
{ _id: "tag2", name: "Node" },
82-
{ _id: "tag3", name: "PostgreSQL" },
83-
],
84-
author: {
85-
_id: "u456",
86-
name: "Jane Doe",
87-
image: "/avatars/jane-doe.png",
88-
},
89-
};
90-
9113
const QuestionDetails = async ({ params }: RouteParams) => {
9214
const { id } = await params;
15+
const { success, data: question } = await getQuestion({ questionId: id });
16+
17+
if (!success || !question) return redirect("/404");
9318

94-
const { author, createdAt, answers, views, tags, content } = sampleQuestion;
19+
const { author, createdAt, answers, views, tags, content, title } = question;
9520

9621
return (
9722
<>
@@ -117,7 +42,7 @@ const QuestionDetails = async ({ params }: RouteParams) => {
11742
</div>
11843

11944
<h2 className="h2-semibold text-dark200_light900 mt-3.5 w-full">
120-
{sampleQuestion.title}
45+
{title}
12146
</h2>
12247
</div>
12348

app/globals.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ body {
275275
}
276276

277277
.markdown {
278-
@apply max-w-full prose dark:prose-p:text-light-700 dark:prose-ol:text-light-700 dark:prose-ul:text-light-500 dark:prose-strong:text-white dark:prose-headings:text-white prose-headings:text-dark-400 prose-h1:text-dark-300 prose-h2:text-dark-300 prose-p:text-dark-500 prose-ul:text-dark-500 prose-ol:text-dark-500;
278+
@apply max-w-full prose prose-p:my-2 prose-ol:my-0 prose-ul:my-0 dark:prose-p:text-light-700 dark:prose-ol:text-light-700 dark:prose-ul:text-light-500 dark:prose-strong:text-white dark:prose-headings:text-white prose-headings:text-dark-400 prose-h1:text-dark-300 prose-h2:text-dark-300 prose-p:text-dark-500 prose-ul:text-dark-500 prose-ol:text-dark-500;
279279
}
280280

281281
.markdown-editor {

lib/actions/question.action.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ export async function getQuestion(
203203
const { questionId } = validationResult.params!;
204204

205205
try {
206-
const question = await Question.findById(questionId).populate("tags");
206+
const question = await Question.findById(questionId)
207+
.populate("tags")
208+
.populate("author", "_id name image");
207209

208210
if (!question) {
209211
throw new Error("Question not found");

0 commit comments

Comments
 (0)