summaryrefslogtreecommitdiff
path: root/lib/swp/table/swp-table-columns.tsx
blob: 573acf1b60946433853678fe0436ab0e6510dec8 (plain)
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
456
457
458
459
460
461
462
463
464
465
"use client";

import { ColumnDef } from "@tanstack/react-table";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { ChevronDown, ChevronRight, FileIcon, Download, Loader2 } from "lucide-react";
import { formatDistanceToNow } from "date-fns";
import { ko } from "date-fns/locale";
import type { SwpDocumentWithStats } from "../actions";
import { downloadSwpFile } from "../actions";
import { useState } from "react";
import { toast } from "sonner";

export const swpDocumentColumns: ColumnDef<SwpDocumentWithStats>[] = [
  {
    id: "expander",
    header: () => null,
    cell: () => {
      return (
        <Button
          variant="ghost"
          size="sm"
          className="h-8 w-8 p-0"
        >
          <ChevronRight className="h-4 w-4" />
        </Button>
      );
    },
    size: 50,
  },
  {
    accessorKey: "DOC_NO",
    header: "문서번호",
    cell: ({ row }) => (
      <div className="font-mono text-sm">{row.original.DOC_NO}</div>
    ),
    size: 250,
  },
  {
    accessorKey: "DOC_TITLE",
    header: "문서제목",
    cell: ({ row }) => (
      <div className="max-w-md truncate" title={row.original.DOC_TITLE}>
        {row.original.DOC_TITLE}
      </div>
    ),
    size: 300,
  },
  {
    accessorKey: "PROJ_NO",
    header: "프로젝트",
    cell: ({ row }) => (
      <div>
        <div className="font-medium">{row.original.PROJ_NO}</div>
        {row.original.PROJ_NM && (
          <div className="text-xs text-muted-foreground truncate max-w-[150px]">
            {row.original.PROJ_NM}
          </div>
        )}
      </div>
    ),
    size: 150,
  },
  {
    accessorKey: "PKG_NO",
    header: "패키지",
    cell: ({ row }) => row.original.PKG_NO || "-",
    size: 100,
  },
  {
    accessorKey: "VNDR_CD",
    header: "업체",
    cell: ({ row }) => (
      <div>
        {row.original.VNDR_CD && (
          <div className="text-xs text-muted-foreground">{row.original.VNDR_CD}</div>
        )}
        {row.original.CPY_NM && (
          <div className="text-sm truncate max-w-[120px]" title={row.original.CPY_NM}>
            {row.original.CPY_NM}
          </div>
        )}
      </div>
    ),
    size: 120,
  },
  {
    accessorKey: "STAGE",
    header: "스테이지",
    cell: ({ row }) => {
      const stage = row.original.STAGE;
      if (!stage) return "-";
      
      const color = 
        stage === "IFC" ? "bg-green-100 text-green-800" :
        stage === "IFA" ? "bg-blue-100 text-blue-800" :
        "bg-gray-100 text-gray-800";
      
      return (
        <Badge variant="outline" className={color}>
          {stage}
        </Badge>
      );
    },
    size: 80,
  },
  {
    accessorKey: "LTST_REV_NO",
    header: "최신 REV",
    cell: ({ row }) => row.original.LTST_REV_NO || "-",
    size: 80,
  },
  {
    id: "stats",
    header: "REV/파일",
    cell: ({ row }) => (
      <div className="text-center">
        <div className="text-sm font-medium">
          {row.original.revision_count} / {row.original.file_count}
        </div>
      </div>
    ),
    size: 100,
  },
  {
    accessorKey: "sync_status",
    header: "상태",
    cell: ({ row }) => {
      const status = row.original.sync_status;
      const color = 
        status === "synced" ? "bg-green-100 text-green-800" :
        status === "pending" ? "bg-yellow-100 text-yellow-800" :
        "bg-red-100 text-red-800";
      
      return (
        <Badge variant="outline" className={color}>
          {status}
        </Badge>
      );
    },
    size: 80,
  },
  {
    accessorKey: "last_synced_at",
    header: "동기화",
    cell: ({ row }) => (
      <div className="text-xs text-muted-foreground">
        {formatDistanceToNow(new Date(row.original.last_synced_at), {
          addSuffix: true,
          locale: ko,
        })}
      </div>
    ),
    size: 100,
  },
];

// ============================================================================
// 리비전 컬럼 (서브 테이블용)
// ============================================================================

export interface RevisionRow {
  id: number;
  DOC_NO: string;
  REV_NO: string;
  STAGE: string;
  ACTV_NO: string | null;
  OFDC_NO: string | null;
  sync_status: "synced" | "pending" | "error";
  last_synced_at: Date;
  file_count: number;
}

export const swpRevisionColumns: ColumnDef<RevisionRow>[] = [
  {
    id: "expander",
    header: () => null,
    cell: ({ row }) => {
      return row.getCanExpand() ? (
        <Button
          variant="ghost"
          size="sm"
          className="h-8 w-8 p-0 ml-8"
        >
          {row.getIsExpanded() ? (
            <ChevronDown className="h-4 w-4" />
          ) : (
            <ChevronRight className="h-4 w-4" />
          )}
        </Button>
      ) : null;
    },
    size: 100,
  },
  {
    accessorKey: "REV_NO",
    header: "리비전",
    cell: ({ row }) => (
      <Badge variant="secondary" className="font-mono">
        REV {row.original.REV_NO}
      </Badge>
    ),
    size: 100,
  },
  {
    accessorKey: "STAGE",
    header: "스테이지",
    cell: ({ row }) => {
      const stage = row.original.STAGE;
      const color = 
        stage === "IFC" ? "bg-green-100 text-green-800" :
        stage === "IFA" ? "bg-blue-100 text-blue-800" :
        "bg-gray-100 text-gray-800";
      
      return (
        <Badge variant="outline" className={color}>
          {stage}
        </Badge>
      );
    },
    size: 100,
  },
  {
    accessorKey: "OFDC_NO",
    header: "OFDC 번호",
    cell: ({ row }) => (
      <div className="font-mono text-sm">{row.original.OFDC_NO || "-"}</div>
    ),
    size: 200,
  },
  {
    accessorKey: "ACTV_NO",
    header: "Activity",
    cell: ({ row }) => (
      <div className="font-mono text-xs text-muted-foreground">
        {row.original.ACTV_NO || "-"}
      </div>
    ),
    size: 250,
  },
  {
    id: "file_count",
    header: "파일 수",
    cell: ({ row }) => (
      <div className="flex items-center gap-2">
        <FileIcon className="h-4 w-4 text-muted-foreground" />
        <span className="font-medium">{row.original.file_count}</span>
      </div>
    ),
    size: 100,
  },
  {
    accessorKey: "sync_status",
    header: "상태",
    cell: ({ row }) => {
      const status = row.original.sync_status;
      const color = 
        status === "synced" ? "bg-green-100 text-green-800" :
        status === "pending" ? "bg-yellow-100 text-yellow-800" :
        "bg-red-100 text-red-800";
      
      return (
        <Badge variant="outline" className={color}>
          {status}
        </Badge>
      );
    },
    size: 80,
  },
  {
    accessorKey: "last_synced_at",
    header: "동기화",
    cell: ({ row }) => (
      <div className="text-xs text-muted-foreground">
        {formatDistanceToNow(new Date(row.original.last_synced_at), {
          addSuffix: true,
          locale: ko,
        })}
      </div>
    ),
    size: 100,
  },
];

// ============================================================================
// 파일 컬럼 (서브 서브 테이블용)
// ============================================================================

export interface FileRow {
  id: number;
  FILE_NM: string;
  FILE_SEQ: string;
  FILE_SZ: string | null;
  FLD_PATH: string | null;
  STAT: string | null;
  STAT_NM: string | null;
  sync_status: "synced" | "pending" | "error";
  created_at: Date;
}

export const swpFileColumns: ColumnDef<FileRow>[] = [
  {
    id: "spacer",
    header: () => null,
    cell: () => <div className="w-16" />,
    size: 150,
  },
  {
    accessorKey: "FILE_SEQ",
    header: "순서",
    cell: ({ row }) => (
      <Badge variant="outline" className="font-mono">
        #{row.original.FILE_SEQ}
      </Badge>
    ),
    size: 80,
  },
  {
    accessorKey: "FILE_NM",
    header: "파일명",
    cell: ({ row }) => (
      <div className="flex items-center gap-2">
        <FileIcon className="h-4 w-4 text-blue-500" />
        <span className="font-mono text-sm">{row.original.FILE_NM}</span>
      </div>
    ),
    size: 400,
  },
  {
    accessorKey: "FILE_SZ",
    header: "크기",
    cell: ({ row }) => {
      const size = row.original.FILE_SZ;
      if (!size) return "-";
      
      const bytes = parseInt(size, 10);
      if (isNaN(bytes)) return size;
      
      const kb = bytes / 1024;
      const mb = kb / 1024;
      
      return mb >= 1
        ? `${mb.toFixed(2)} MB`
        : `${kb.toFixed(2)} KB`;
    },
    size: 100,
  },
  {
    accessorKey: "STAT_NM",
    header: "상태",
    cell: ({ row }) => {
      const status = row.original.STAT_NM;
      if (!status) return "-";
      
      const color = status === "Complete" 
        ? "bg-green-100 text-green-800" 
        : "bg-gray-100 text-gray-800";
      
      return (
        <Badge variant="outline" className={color}>
          {status}
        </Badge>
      );
    },
    size: 100,
  },
  {
    accessorKey: "FLD_PATH",
    header: "경로",
    cell: ({ row }) => (
      <div className="font-mono text-xs text-muted-foreground truncate max-w-[200px]" title={row.original.FLD_PATH || ""}>
        {row.original.FLD_PATH || "-"}
      </div>
    ),
    size: 200,
  },
  {
    accessorKey: "created_at",
    header: "생성일",
    cell: ({ row }) => (
      <div className="text-xs text-muted-foreground">
        {formatDistanceToNow(new Date(row.original.created_at), {
          addSuffix: true,
          locale: ko,
        })}
      </div>
    ),
    size: 100,
  },
  {
    id: "actions",
    header: "작업",
    cell: ({ row }) => (
      <DownloadButton fileId={row.original.id} fileName={row.original.FILE_NM} />
    ),
    size: 120,
  },
];

// ============================================================================
// 다운로드 버튼 컴포넌트: 임시 구성. Download.aspx 동작 안해서 일단 네트워크드라이브 사용하도록 처리
// ============================================================================

interface DownloadButtonProps {
  fileId: number;
  fileName: string;
}

function DownloadButton({ fileId, fileName }: DownloadButtonProps) {
  const [isDownloading, setIsDownloading] = useState(false);

  const handleDownload = async () => {
    try {
      setIsDownloading(true);
      
      // 서버 액션 호출
      const result = await downloadSwpFile(fileId);

      if (!result.success || !result.data) {
        toast.error(result.error || "파일 다운로드 실패");
        return;
      }

      // Blob 생성 및 다운로드
      const blob = new Blob([result.data as unknown as BlobPart], { type: result.mimeType });
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement("a");
      link.href = url;
      link.download = result.fileName || fileName;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(url);

      toast.success(`파일 다운로드 완료: ${result.fileName}`);
    } catch (error) {
      console.error("다운로드 오류:", error);
      toast.error("파일 다운로드 중 오류가 발생했습니다.");
    } finally {
      setIsDownloading(false);
    }
  };

  return (
    <Button
      variant="outline"
      size="sm"
      onClick={handleDownload}
      disabled={isDownloading}
    >
      {isDownloading ? (
        <>
          <Loader2 className="h-4 w-4 mr-1 animate-spin" />
          다운로드 중...
        </>
      ) : (
        <>
          <Download className="h-4 w-4 mr-1" />
          다운로드
        </>
      )}
    </Button>
  );
}