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
|
// @/lib/rfq-last/attachment/revision-history-dialog.tsx
"use client";
import * as React from "react";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Skeleton } from "@/components/ui/skeleton";
import { Alert, AlertDescription } from "@/components/ui/alert";
import {
Download,
Eye,
FileText,
Clock,
User,
MessageSquare,
AlertCircle,
CheckCircle,
} from "lucide-react";
import { format, formatDistanceToNow } from "date-fns";
import { ko } from "date-fns/locale";
import { toast } from "sonner";
import { downloadFile } from "@/lib/file-download";
import {
getRevisionHistory,
type AttachmentWithHistory,
type RevisionHistory,
} from "../service";
import { formatFileSize } from "@/lib/utils";
interface RevisionHistoryDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
attachmentId: number;
attachmentName?: string;
}
export function RevisionHistoryDialog({
open,
onOpenChange,
attachmentId,
attachmentName,
}: RevisionHistoryDialogProps) {
const [loading, setLoading] = React.useState(false);
const [historyData, setHistoryData] = React.useState<AttachmentWithHistory | null>(null);
const [error, setError] = React.useState<string | null>(null);
// 다이얼로그가 열릴 때 데이터 로드
React.useEffect(() => {
if (open && attachmentId) {
loadRevisionHistory();
}
}, [open, attachmentId]);
const loadRevisionHistory = async () => {
setLoading(true);
setError(null);
try {
const result = await getRevisionHistory(attachmentId);
if (result.success && result.data) {
setHistoryData(result.data);
} else {
setError(result.error || "리비전 히스토리를 불러올 수 없습니다.");
}
} catch (err) {
console.error("Load revision history error:", err);
setError("리비전 히스토리 조회 중 오류가 발생했습니다.");
} finally {
setLoading(false);
}
};
// 리비전 다운로드
const handleDownloadRevision = async (revision: RevisionHistory) => {
try {
await downloadFile(revision.filePath, revision.originalFileName, {
action: 'download',
showToast: true,
});
} catch (err) {
console.error("Download revision error:", err);
toast.error("파일 다운로드 중 오류가 발생했습니다.");
}
};
// 리비전 미리보기
const handlePreviewRevision = async (revision: RevisionHistory) => {
try {
await downloadFile(revision.filePath, revision.originalFileName, {
action: 'preview',
showToast: true,
});
} catch (err) {
console.error("Preview revision error:", err);
toast.error("파일 미리보기 중 오류가 발생했습니다.");
}
};
// 리비전 번호에 따른 색상 결정
const getRevisionBadgeVariant = (isLatest: boolean) => {
return isLatest ? "default" : "secondary";
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-5xl max-h-[80vh]">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<FileText className="h-5 w-5" />
리비전 히스토리
</DialogTitle>
<DialogDescription>
{historyData?.originalFileName || attachmentName || "파일"}의 모든 버전 히스토리를 확인할 수 있습니다.
</DialogDescription>
</DialogHeader>
<div className="mt-4">
{loading ? (
<div className="space-y-3">
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
</div>
) : error ? (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertDescription>{error}</AlertDescription>
</Alert>
) : historyData ? (
<>
{/* 파일 정보 헤더 */}
<div className="mb-4 p-3 bg-muted rounded-lg">
<div className="grid grid-cols-2 gap-2 text-sm">
<div>
<span className="text-muted-foreground">일련번호:</span>{" "}
<span className="font-medium font-mono">{historyData.serialNo || "-"}</span>
</div>
<div>
<span className="text-muted-foreground">현재 리비전:</span>{" "}
<Badge variant="default" className="ml-1">
Rev. {historyData.currentRevision || "A"}
</Badge>
</div>
{historyData.description && (
<div className="col-span-2">
<span className="text-muted-foreground">설명:</span>{" "}
<span className="font-medium">{historyData.description}</span>
</div>
)}
</div>
</div>
{/* 리비전 테이블 */}
<ScrollArea className="h-[400px] rounded-md border">
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[80px]">리비전</TableHead>
<TableHead>파일명</TableHead>
<TableHead className="w-[80px]">크기</TableHead>
<TableHead className="w-[100px]">업로드자</TableHead>
<TableHead className="w-[150px]">업로드일시</TableHead>
<TableHead>코멘트</TableHead>
<TableHead className="w-[100px] text-center">작업</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{historyData.revisions.length > 0 ? (
historyData.revisions.map((revision) => (
<TableRow key={revision.id}>
<TableCell>
<Badge
variant={getRevisionBadgeVariant(revision.isLatest)}
className="font-mono"
>
Rev. {revision.revisionNo}
{revision.isLatest && (
<CheckCircle className="ml-1 h-3 w-3" />
)}
</Badge>
</TableCell>
<TableCell>
<div className="flex flex-col">
<span className="text-sm font-medium truncate max-w-[200px]" title={revision.originalFileName}>
{revision.originalFileName}
</span>
{revision.fileName !== revision.originalFileName && (
<span className="text-xs text-muted-foreground truncate max-w-[200px]">
({revision.fileName})
</span>
)}
</div>
</TableCell>
<TableCell>
<span className="text-sm text-muted-foreground">
{formatFileSize(revision.fileSize)}
</span>
</TableCell>
<TableCell>
<div className="flex items-center gap-1">
<User className="h-3 w-3 text-muted-foreground" />
<span className="text-sm">
{revision.createdByName || "Unknown"}
</span>
</div>
</TableCell>
<TableCell>
<div className="flex items-center gap-1">
<Clock className="h-3 w-3 text-muted-foreground" />
<span className="text-sm">
{format(new Date(revision.createdAt), "yyyy-MM-dd HH:mm")}
</span>
</div>
<span className="text-xs text-muted-foreground">
{formatDistanceToNow(new Date(revision.createdAt), {
addSuffix: true,
locale: ko,
})}
</span>
</TableCell>
<TableCell>
{revision.revisionComment ? (
<div className="flex items-start gap-1">
<MessageSquare className="h-3 w-3 text-muted-foreground mt-0.5" />
<span className="text-sm text-muted-foreground">
{revision.revisionComment}
</span>
</div>
) : (
<span className="text-sm text-muted-foreground">-</span>
)}
</TableCell>
<TableCell>
<div className="flex items-center justify-center gap-1">
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={() => handleDownloadRevision(revision)}
title="다운로드"
>
<Download className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
className="h-8 w-8"
onClick={() => handlePreviewRevision(revision)}
title="미리보기"
>
<Eye className="h-4 w-4" />
</Button>
</div>
</TableCell>
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={7} className="text-center text-muted-foreground py-8">
리비전 히스토리가 없습니다.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</ScrollArea>
{/* 요약 정보 */}
<div className="mt-4 flex items-center justify-between text-sm text-muted-foreground">
<span>총 {historyData.revisions.length}개의 리비전</span>
<span>
최초 업로드:{" "}
{historyData.revisions.length > 0
? format(
new Date(
historyData.revisions[historyData.revisions.length - 1].createdAt
),
"yyyy년 MM월 dd일"
)
: "-"}
</span>
</div>
</>
) : null}
</div>
</DialogContent>
</Dialog>
);
}
|