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
|
import * as React from "react";
import { useSession } from "next-auth/react";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Badge } from "@/components/ui/badge";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { format } from "date-fns";
import { Comment } from "@/lib/qna/types";
import {
MessageCircle,
Trash2,
Edit,
Check,
X,
User,
Plus
} from "lucide-react";
interface ImprovedCommentSectionProps {
answerId: string | number;
comments: Comment[];
onAddComment: (content: string) => Promise<void>;
onDeleteComment: (commentId: string | number) => Promise<void>;
onUpdateComment?: (commentId: string | number, content: string) => Promise<void>;
}
export function ImprovedCommentSection({
answerId,
comments,
onAddComment,
onDeleteComment,
onUpdateComment
}: ImprovedCommentSectionProps) {
const { data: session } = useSession();
// 상태 관리
const [content, setContent] = React.useState("");
const [isSubmitting, setIsSubmitting] = React.useState(false);
const [editingId, setEditingId] = React.useState<string | number | null>(null);
const [editContent, setEditContent] = React.useState("");
const [showCommentForm, setShowCommentForm] = React.useState(false);
// 댓글 작성
const handleSubmit = async () => {
if (!content.trim() || !session?.user) return;
setIsSubmitting(true);
try {
await onAddComment(content);
setContent("");
setShowCommentForm(false);
} catch (error) {
console.error("댓글 작성 실패:", error);
} finally {
setIsSubmitting(false);
}
};
// 댓글 수정 시작
const handleEditStart = (comment: Comment) => {
setEditingId(comment.id);
setEditContent(comment.content);
};
// 댓글 수정 취소
const handleEditCancel = () => {
setEditingId(null);
setEditContent("");
};
// 댓글 수정 저장
const handleEditSave = async (commentId: string | number) => {
if (!editContent.trim() || !onUpdateComment) return;
try {
await onUpdateComment(commentId, editContent);
setEditingId(null);
setEditContent("");
} catch (error) {
console.error("댓글 수정 실패:", error);
}
};
return (
<div className="space-y-4">
{/* 헤더 */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<MessageCircle className="h-4 w-4 text-muted-foreground" />
<span className="text-sm font-medium text-muted-foreground">
댓글
</span>
{comments.length > 0 && (
<Badge variant="secondary" className="text-xs h-5">
{comments.length}
</Badge>
)}
</div>
{session?.user && !showCommentForm && (
<Button
variant="ghost"
size="sm"
onClick={() => setShowCommentForm(true)}
className="gap-2 text-xs"
>
<Plus className="h-3 w-3" />
댓글 작성
</Button>
)}
</div>
{/* 댓글 목록 */}
{comments.length > 0 && (
<div className="space-y-3">
{comments.map((comment) => (
<div key={comment.id} className="group">
<div className="flex gap-3">
{/* 아바타 */}
<Avatar className="h-7 w-7 shrink-0">
<AvatarImage src={comment.authorImageUrl} />
<AvatarFallback className="text-xs">
<User className="h-3 w-3" />
</AvatarFallback>
</Avatar>
{/* 댓글 내용 */}
<div className="flex-1 min-w-0">
<div className="bg-muted/50 rounded-lg px-3 py-2">
{/* 작성자 정보 */}
<div className="flex items-center gap-2 mb-1">
<span className="text-sm font-medium">
{comment.authorName || comment.author}
</span>
<span className="text-xs text-muted-foreground">
{format(new Date(comment.createdAt), "MM월 dd일 HH:mm")}
</span>
</div>
{/* 댓글 텍스트 */}
{editingId === comment.id ? (
<div className="space-y-2">
<Textarea
value={editContent}
onChange={(e) => setEditContent(e.target.value)}
className="min-h-[60px] text-sm resize-none"
maxLength={250}
placeholder="댓글을 입력하세요..."
/>
<div className="flex items-center justify-between">
<span className="text-xs text-muted-foreground">
{editContent.length}/250
</span>
<div className="flex gap-1">
<Button
variant="ghost"
size="sm"
onClick={() => handleEditSave(comment.id)}
disabled={!editContent.trim()}
className="h-7 px-2 text-green-600 hover:text-green-700 hover:bg-green-50"
>
<Check className="h-3 w-3" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={handleEditCancel}
className="h-7 px-2 text-muted-foreground hover:bg-muted"
>
<X className="h-3 w-3" />
</Button>
</div>
</div>
</div>
) : (
<p className="text-sm leading-relaxed whitespace-pre-wrap">
{comment.content}
</p>
)}
</div>
{/* 액션 버튼들 */}
{session?.user?.id === comment.author && editingId !== comment.id && (
<div className="flex gap-1 mt-1 opacity-0 group-hover:opacity-100 transition-opacity">
{onUpdateComment && (
<Button
variant="ghost"
size="sm"
onClick={() => handleEditStart(comment)}
className="h-6 px-2 text-xs text-muted-foreground hover:text-blue-600"
>
<Edit className="h-3 w-3 mr-1" />
수정
</Button>
)}
<AlertDialog>
<AlertDialogTrigger asChild>
<Button
variant="ghost"
size="sm"
className="h-6 px-2 text-xs text-muted-foreground hover:text-destructive"
>
<Trash2 className="h-3 w-3 mr-1" />
삭제
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>댓글 삭제</AlertDialogTitle>
<AlertDialogDescription>
정말로 이 댓글을 삭제하시겠습니까? 이 작업은 되돌릴 수 없습니다.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>취소</AlertDialogCancel>
<AlertDialogAction
onClick={() => onDeleteComment(comment.id)}
className="bg-destructive hover:bg-destructive/90"
>
삭제
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)}
</div>
</div>
</div>
))}
</div>
)}
{/* 댓글 작성 폼 */}
{session?.user && showCommentForm && (
<div className="space-y-3 pt-2 border-t">
<div className="flex gap-3">
<Avatar className="h-7 w-7 shrink-0">
<AvatarImage src={session.user.image} />
<AvatarFallback className="text-xs">
<User className="h-3 w-3" />
</AvatarFallback>
</Avatar>
<div className="flex-1 space-y-2">
<Textarea
value={content}
onChange={(e) => {
if (e.target.value.length <= 250) {
setContent(e.target.value);
}
}}
placeholder="댓글을 입력하세요..."
className="min-h-[80px] resize-none"
maxLength={250}
disabled={isSubmitting}
/>
<div className="flex items-center justify-between">
<span className="text-xs text-muted-foreground">
{content.length}/250
</span>
<div className="flex gap-2">
<Button
variant="ghost"
size="sm"
onClick={() => {
setShowCommentForm(false);
setContent("");
}}
disabled={isSubmitting}
>
취소
</Button>
<Button
size="sm"
onClick={handleSubmit}
disabled={isSubmitting || !content.trim()}
>
{isSubmitting ? "저장 중..." : "등록"}
</Button>
</div>
</div>
</div>
</div>
</div>
)}
{/* 빈 상태 */}
{comments.length === 0 && !showCommentForm && (
<div className="text-center py-6 text-muted-foreground">
<MessageCircle className="h-8 w-8 mx-auto mb-2 opacity-50" />
<p className="text-sm">아직 댓글이 없습니다.</p>
{session?.user && (
<Button
variant="ghost"
size="sm"
onClick={() => setShowCommentForm(true)}
className="mt-2 gap-2"
>
<Plus className="h-3 w-3" />
첫 번째 댓글 작성하기
</Button>
)}
</div>
)}
</div>
);
}
|