From 231c4eb86771a44b24248ca403fcbb8c44fff74b Mon Sep 17 00:00:00 2001
From: joonhoekim <26rote@gmail.com>
Date: Fri, 24 Oct 2025 19:44:04 +0900
Subject: (김준회) SWP 파일 업로드 처리, 다운로드는 임시 처리(네트워크경로에서
 다운로드받도록)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/swp/table/swp-table-columns.tsx | 83 +++++++++++++++++++++++++++++++------
 1 file changed, 71 insertions(+), 12 deletions(-)

(limited to 'lib/swp/table/swp-table-columns.tsx')

diff --git a/lib/swp/table/swp-table-columns.tsx b/lib/swp/table/swp-table-columns.tsx
index b18e2b27..573acf1b 100644
--- a/lib/swp/table/swp-table-columns.tsx
+++ b/lib/swp/table/swp-table-columns.tsx
@@ -3,10 +3,13 @@
 import { ColumnDef } from "@tanstack/react-table";
 import { Badge } from "@/components/ui/badge";
 import { Button } from "@/components/ui/button";
-import { ChevronDown, ChevronRight, FileIcon, Download } from "lucide-react";
+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>[] = [
   {
@@ -388,19 +391,75 @@ export const swpFileColumns: ColumnDef<FileRow>[] = [
     id: "actions",
     header: "작업",
     cell: ({ row }) => (
-      <Button
-        variant="outline"
-        size="sm"
-        onClick={() => {
-          // TODO: 파일 다운로드 로직 구현
-          console.log("Download file:", row.original.FILE_NM);
-        }}
-      >
-        <Download className="h-4 w-4 mr-1" />
-        다운로드
-      </Button>
+      <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>
+  );
+}
+
-- 
cgit v1.2.3

