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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
"use client";
import * as React from "react";
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import { format } from "date-fns";
import { Button } from "@/components/ui/button";
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import {
ArrowLeft,
MessageSquare,
Edit,
Trash2,
Plus,
Clock,
User
} from "lucide-react";
import TiptapEditor from "@/components/qna/tiptap-editor";
import { ImprovedCommentSection } from "./improved-comment-section";
import {
createAnswer,
deleteQna,
deleteAnswer,
updateAnswer,
createComment,
deleteComment,
updateComment,
} from "@/lib/qna/service";
import { Question } from "@/lib/qna/types";
interface QnaDetailProps {
question: Question;
}
export default function QnaDetail({ question }: QnaDetailProps) {
const router = useRouter();
const { data: session } = useSession();
// -------------------------------------------------------------------------
// STATE
// -------------------------------------------------------------------------
const [answerContent, setAnswerContent] = React.useState<string>("");
const [loading, setLoading] = React.useState(false);
const [isAnswerDialogOpen, setIsAnswerDialogOpen] = React.useState(false);
const [editingAnswerId, setEditingAnswerId] = React.useState<number | null>(null);
// -------------------------------------------------------------------------
// DERIVED
// -------------------------------------------------------------------------
const isAuthor = session?.user?.id === question.author;
const hasAnswers = (question.answers ?? []).length > 0;
const userAnswer = question.answers?.find((a) => a.author === session?.user?.id) ?? null;
// -------------------------------------------------------------------------
// HANDLERS – NAVIGATION
// -------------------------------------------------------------------------
const handleGoBack = () => {
router.push("/evcp/qna");
};
// -------------------------------------------------------------------------
// HANDLERS – QUESTION
// -------------------------------------------------------------------------
const handleDeleteQuestion = async () => {
try {
await deleteQna(question.id);
router.push("/evcp/qna");
router.refresh();
} catch (err) {
console.error("질문 삭제 실패:", err);
}
};
// -------------------------------------------------------------------------
// HANDLERS – ANSWER
// -------------------------------------------------------------------------
const handleSubmitAnswer = async () => {
if (!answerContent.trim()) return;
setLoading(true);
try {
await createAnswer({ qnaId: question.id, content: answerContent });
setAnswerContent("");
setIsAnswerDialogOpen(false);
router.refresh();
} catch (err) {
console.error("답변 저장 실패:", err);
} finally {
setLoading(false);
}
};
const handleUpdateAnswer = async (answerId: number) => {
if (!answerContent.trim()) return;
setLoading(true);
try {
await updateAnswer(answerId, answerContent);
setAnswerContent("");
setEditingAnswerId(null);
setIsAnswerDialogOpen(false);
router.refresh();
} catch (err) {
console.error("답변 수정 실패:", err);
} finally {
setLoading(false);
}
};
const handleDeleteAnswer = async (id: number) => {
try {
await deleteAnswer(id);
router.refresh();
} catch (err) {
console.error("답변 삭제 실패:", err);
}
};
const startEditingAnswer = (answer: any) => {
setAnswerContent(answer.content);
setEditingAnswerId(answer.id);
setIsAnswerDialogOpen(true);
};
const resetAnswerDialog = () => {
setAnswerContent("");
setEditingAnswerId(null);
setIsAnswerDialogOpen(false);
};
return (
<div className="min-h-screen bg-gray-50/50">
{/* 헤더 */}
<div className="border-b bg-white">
<div className="container mx-auto px-4 py-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-4">
<Button
variant="ghost"
size="sm"
onClick={handleGoBack}
className="gap-2"
>
<ArrowLeft className="h-4 w-4" />
목록으로
</Button>
{/* <div className="h-6 w-px bg-border" /> */}
{/* <Badge variant="outline">
{getCategoryLabel(question.category as any, 'ko')}
</Badge> */}
</div>
{isAuthor && !hasAnswers && (
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
onClick={() => router.push(`/evcp/qna/${question.id}/edit`)}
className="gap-2"
>
<Edit className="h-4 w-4" />
수정
</Button>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="outline" size="sm" className="gap-2 text-destructive hover:text-destructive">
<Trash2 className="h-4 w-4" />
삭제
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>질문 삭제</AlertDialogTitle>
<AlertDialogDescription>
정말로 이 질문을 삭제하시겠습니까? 이 작업은 되돌릴 수 없습니다.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>취소</AlertDialogCancel>
<AlertDialogAction onClick={handleDeleteQuestion} className="bg-destructive hover:bg-destructive/90">
삭제
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)}
</div>
</div>
</div>
{/* 메인 컨텐츠 */}
<div className="container mx-auto px-4 py-6 max-w-4xl">
<div className="space-y-6">
{/* 질문 영역 */}
<Card>
<CardHeader className="pb-4">
<div className="space-y-3">
<CardTitle className="text-2xl leading-tight">
{question.title}
</CardTitle>
<div className="flex items-center gap-4 text-sm text-muted-foreground">
<div className="flex items-center gap-2">
<Avatar className="h-6 w-6">
<AvatarImage src={question.authorImageUrl} />
<AvatarFallback>
<User className="h-3 w-3" />
</AvatarFallback>
</Avatar>
<span className="font-medium text-foreground">
{question.authorName}
</span>
</div>
<div className="flex items-center gap-1">
<Clock className="h-4 w-4" />
<time dateTime={question.createdAt}>
{format(new Date(question.createdAt), "yyyy년 MM월 dd일 HH:mm")}
</time>
</div>
{hasAnswers && (
<div className="flex items-center gap-1">
<MessageSquare className="h-4 w-4" />
<span>답변 {question.answers?.length}개</span>
</div>
)}
</div>
</div>
</CardHeader>
<CardContent>
<div
className="prose prose-sm max-w-none prose-headings:text-foreground prose-p:text-foreground prose-strong:text-foreground prose-em:text-foreground"
dangerouslySetInnerHTML={{ __html: question.content || "내용 없음" }}
/>
</CardContent>
</Card>
{/* 답변 영역 */}
<div className="space-y-4">
<div className="flex items-center justify-between">
<h2 className="text-xl font-semibold flex items-center gap-2">
<MessageSquare className="h-5 w-5" />
답변 {hasAnswers ? `(${question.answers?.length})` : ''}
</h2>
{session?.user && (
<Dialog open={isAnswerDialogOpen} onOpenChange={setIsAnswerDialogOpen}>
<DialogTrigger asChild>
<Button
className="gap-2"
onClick={() => {
if (!userAnswer) {
setAnswerContent("");
setEditingAnswerId(null);
}
}}
>
<Plus className="h-4 w-4" />
{userAnswer ? '답변 수정' : '답변 작성'}
</Button>
</DialogTrigger>
<DialogContent className="max-w-4xl max-h-[90vh]">
<DialogHeader>
<DialogTitle>
{editingAnswerId ? '답변 수정' : '새 답변 작성'}
</DialogTitle>
<DialogDescription>
질문에 대한 답변을 작성해주세요. 다른 사용자들이 이해하기 쉽도록 구체적으로 작성해주세요.
</DialogDescription>
</DialogHeader>
<div className="flex-1 min-h-0">
<TiptapEditor
content={answerContent}
setContent={setAnswerContent}
disabled={loading}
height="400px"
/>
</div>
<DialogFooter className="gap-2 pt-4 border-t">
<Button
variant="outline"
onClick={resetAnswerDialog}
disabled={loading}
>
취소
</Button>
<Button
onClick={() => editingAnswerId ? handleUpdateAnswer(editingAnswerId) : handleSubmitAnswer()}
disabled={loading || !answerContent.trim()}
>
{loading ? '저장 중...' : editingAnswerId ? '수정' : '등록'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)}
</div>
{hasAnswers ? (
<div className="space-y-4">
{question.answers?.map((answer, index) => (
<Card key={answer.id} className="relative">
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<Avatar className="h-8 w-8">
<AvatarImage src={answer.authorImageUrl} />
<AvatarFallback>
<User className="h-4 w-4" />
</AvatarFallback>
</Avatar>
<div>
<div className="font-medium text-sm">
{answer.authorName ?? `사용자 ${answer.author}`}
</div>
<div className="text-xs text-muted-foreground">
{format(new Date(answer.createdAt), "yyyy년 MM월 dd일 HH:mm")}
</div>
</div>
</div>
{session?.user?.id === answer.author && (
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="sm"
onClick={() => startEditingAnswer(answer)}
className="h-8 gap-1 text-muted-foreground hover:text-blue-600"
>
<Edit className="h-3 w-3" />
수정
</Button>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
variant="ghost"
size="sm"
className="h-8 gap-1 text-muted-foreground hover:text-destructive"
>
<Trash2 className="h-3 w-3" />
삭제
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>답변 삭제</AlertDialogTitle>
<AlertDialogDescription>
정말로 이 답변을 삭제하시겠습니까? 이 작업은 되돌릴 수 없습니다.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>취소</AlertDialogCancel>
<AlertDialogAction
onClick={() => handleDeleteAnswer(answer.id)}
className="bg-destructive hover:bg-destructive/90"
>
삭제
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)}
</div>
</CardHeader>
<CardContent className="pt-0">
<div
className="prose prose-sm max-w-none prose-headings:text-foreground prose-p:text-foreground prose-strong:text-foreground prose-em:text-foreground"
dangerouslySetInnerHTML={{ __html: answer.content }}
/>
{/* 답변별 댓글 섹션 */}
<div className="mt-6 pt-4 border-t">
<ImprovedCommentSection
answerId={answer.id}
comments={answer.comments ?? []}
onAddComment={async (content) => {
if (!session?.user?.id) return;
try {
await createComment({ content, answerId: answer.id });
router.refresh();
} catch (err) {
console.error("댓글 작성 실패:", err);
}
}}
onDeleteComment={async (commentId) => {
try {
await deleteComment(commentId);
router.refresh();
} catch (err) {
console.error("댓글 삭제 실패:", err);
}
}}
onUpdateComment={async (commentId, content) => {
try {
await updateComment(commentId, content);
router.refresh();
} catch (err) {
console.error("댓글 수정 실패:", err);
}
}}
/>
</div>
</CardContent>
</Card>
))}
</div>
) : (
<Card className="py-12">
<CardContent className="text-center">
<MessageSquare className="h-12 w-12 text-muted-foreground mx-auto mb-4" />
<p className="text-muted-foreground mb-4">
아직 등록된 답변이 없습니다.
</p>
{session?.user && (
<Button
onClick={() => setIsAnswerDialogOpen(true)}
className="gap-2"
>
<Plus className="h-4 w-4" />
첫 번째 답변 작성하기
</Button>
)}
</CardContent>
</Card>
)}
</div>
</div>
</div>
</div>
);
}
|