summaryrefslogtreecommitdiff
path: root/components/ui/file-actions.tsx
blob: ed2103d3ea452dbc8127a9a1bf024d1eb9671d1c (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
// components/ui/file-actions.tsx
// 재사용 가능한 파일 액션 컴포넌트들

"use client";

import * as React from "react";
import { 
  Download, 
  Eye, 
  Paperclip, 
  Loader2, 
  AlertCircle,
  FileText,
  Image as ImageIcon,
  Archive
} from "lucide-react";

import { Button } from "@/components/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";

import { useMultiFileDownload } from "@/hooks/use-file-download";
import { getFileInfo, quickDownload, quickPreview, smartFileAction } from "@/lib/file-download";
import { cn } from "@/lib/utils";

/**
 * 파일 아이콘 컴포넌트
 */
interface FileIconProps {
  fileName: string;
  className?: string;
}

export const FileIcon: React.FC<FileIconProps> = ({ fileName, className }) => {
  const fileInfo = getFileInfo(fileName);
  
  const iconMap = {
    pdf: FileText,
    document: FileText,
    spreadsheet: FileText,
    image: ImageIcon,
    archive: Archive,
    other: Paperclip,
  };
  
  const IconComponent = iconMap[fileInfo.type];
  
  return (
    <IconComponent className={cn("h-4 w-4", className)} />
  );
};

/**
 * 기본 파일 다운로드 버튼
 */
interface FileDownloadButtonProps {
  filePath: string;
  fileName: string;
  variant?: "default" | "ghost" | "outline";
  size?: "default" | "sm" | "lg" | "icon";
  children?: React.ReactNode;
  className?: string;
  showIcon?: boolean;
  disabled?: boolean;
}

export const FileDownloadButton: React.FC<FileDownloadButtonProps> = ({
  filePath,
  fileName,
  variant = "ghost",
  size = "icon",
  children,
  className,
  showIcon = true,
  disabled,
}) => {
  const { downloadFile, isFileLoading, getFileError } = useMultiFileDownload();
  
  const isLoading = isFileLoading(filePath);
  const error = getFileError(filePath);
  
  const handleClick = () => {
    if (!disabled && !isLoading) {
      quickDownload(filePath, fileName);
    }
  };

  if (isLoading) {
    return (
      <Button variant={variant} size={size} disabled className={className}>
        <Loader2 className="h-4 w-4 animate-spin" />
        {children}
      </Button>
    );
  }

  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger asChild>
          <Button
            variant={variant}
            size={size}
            onClick={handleClick}
            disabled={disabled}
            className={cn(
              error && "text-destructive hover:text-destructive",
              className
            )}
          >
            {error ? (
              <AlertCircle className="h-4 w-4" />
            ) : showIcon ? (
              <Download className="h-4 w-4" />
            ) : null}
            {children}
          </Button>
        </TooltipTrigger>
        <TooltipContent>
          {error ? `오류: ${error} (클릭하여 재시도)` : `${fileName} 다운로드`}
        </TooltipContent>
      </Tooltip>
    </TooltipProvider>
  );
};

/**
 * 미리보기 버튼
 */
interface FilePreviewButtonProps extends Omit<FileDownloadButtonProps, 'children'> {
  fallbackToDownload?: boolean;
}

export const FilePreviewButton: React.FC<FilePreviewButtonProps> = ({
  filePath,
  fileName,
  variant = "ghost",
  size = "icon",
  className,
  fallbackToDownload = true,
  disabled,
}) => {
  const { isFileLoading, getFileError } = useMultiFileDownload();
  const fileInfo = getFileInfo(fileName);
  
  const isLoading = isFileLoading(filePath);
  const error = getFileError(filePath);
  
  const handleClick = () => {
    if (!disabled && !isLoading) {
      if (fileInfo.canPreview) {
        quickPreview(filePath, fileName);
      } else if (fallbackToDownload) {
        quickDownload(filePath, fileName);
      }
    }
  };

  if (!fileInfo.canPreview && !fallbackToDownload) {
    return (
      <Button variant={variant} size={size} disabled className={className}>
        <Eye className="h-4 w-4 opacity-50" />
      </Button>
    );
  }

  if (isLoading) {
    return (
      <Button variant={variant} size={size} disabled className={className}>
        <Loader2 className="h-4 w-4 animate-spin" />
      </Button>
    );
  }

  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger asChild>
          <Button
            variant={variant}
            size={size}
            onClick={handleClick}
            disabled={disabled}
            className={cn(
              error && "text-destructive hover:text-destructive",
              className
            )}
          >
            {error ? (
              <AlertCircle className="h-4 w-4" />
            ) : fileInfo.canPreview ? (
              <Eye className="h-4 w-4" />
            ) : (
              <Download className="h-4 w-4" />
            )}
          </Button>
        </TooltipTrigger>
        <TooltipContent>
          {error 
            ? `오류: ${error} (클릭하여 재시도)` 
            : fileInfo.canPreview 
              ? `${fileName} 미리보기`
              : `${fileName} 다운로드`
          }
        </TooltipContent>
      </Tooltip>
    </TooltipProvider>
  );
};

/**
 * 드롭다운 파일 액션 버튼 (미리보기 + 다운로드)
 */
interface FileActionsDropdownProps {
  filePath: string;
  fileName: string;
  description?: string;
  variant?: "default" | "ghost" | "outline";
  size?: "default" | "sm" | "lg" | "icon";
  className?: string;
  disabled?: boolean;
  triggerIcon?: React.ReactNode;
}

export const FileActionsDropdown: React.FC<FileActionsDropdownProps> = ({
  filePath,
  fileName,
  variant = "ghost",
  size = "icon",
  className,
  disabled,
  triggerIcon,
  description
}) => {
  const { isFileLoading, getFileError } = useMultiFileDownload();
  const fileInfo = getFileInfo(fileName);
  
  const isLoading = isFileLoading(filePath);
  const error = getFileError(filePath);

  const handlePreview = () => quickPreview(filePath, fileName);
  const handleDownload = () => quickDownload(filePath, fileName);

  if (isLoading) {
    return (
      <Button variant={variant} size={size} disabled className={className}>
        <Loader2 className="h-4 w-4 animate-spin" />
      </Button>
    );
  }

  if (error) {
    return (
      <TooltipProvider>
        <Tooltip>
          <TooltipTrigger asChild>
            <Button
              variant={variant}
              size={size}
              onClick={handleDownload}
              className={cn("text-destructive hover:text-destructive", className)}
            >
              <AlertCircle className="h-4 w-4" />
            </Button>
          </TooltipTrigger>
          <TooltipContent>
            <div className="text-sm">
              <div className="font-medium text-destructive">오류 발생</div>
              <div className="text-muted-foreground">{error}</div>
              <div className="mt-1 text-xs">클릭하여 재시도</div>
            </div>
          </TooltipContent>
        </Tooltip>
      </TooltipProvider>
    );
  }

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button 
          variant={variant} 
          size={size} 
          disabled={disabled}
          className={className}
        >
          {triggerIcon || <Paperclip className="h-4 w-4" />}
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent align="end">
        {fileInfo.canPreview && (
          <>
            <DropdownMenuItem onClick={handlePreview}>
              <Eye className="mr-2 h-4 w-4" />
              {fileInfo.icon} 미리보기
            </DropdownMenuItem>
            <DropdownMenuSeparator />
          </>
        )}
        <DropdownMenuItem onClick={handleDownload}>
          <Download className="mr-2 h-4 w-4" />
          {description} 다운로드
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
};

/**
 * 스마트 파일 액션 버튼 (자동 판단)
 */
interface SmartFileActionButtonProps extends Omit<FileDownloadButtonProps, 'children'> {
  showLabel?: boolean;
}

export const SmartFileActionButton: React.FC<SmartFileActionButtonProps> = ({
  filePath,
  fileName,
  variant = "ghost",
  size = "icon",
  className,
  showLabel = false,
  disabled,
}) => {
  const { isFileLoading, getFileError } = useMultiFileDownload();
  const fileInfo = getFileInfo(fileName);
  
  const isLoading = isFileLoading(filePath);
  const error = getFileError(filePath);
  
  const handleClick = () => {
    if (!disabled && !isLoading) {
      smartFileAction(filePath, fileName);
    }
  };

  if (isLoading) {
    return (
      <Button variant={variant} size={size} disabled className={className}>
        <Loader2 className="h-4 w-4 animate-spin" />
        {showLabel && <span className="ml-2">처리 중...</span>}
      </Button>
    );
  }

  const actionText = fileInfo.canPreview ? '미리보기' : '다운로드';
  const IconComponent = fileInfo.canPreview ? Eye : Download;

  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger asChild>
          <Button
            variant={variant}
            size={size}
            onClick={handleClick}
            disabled={disabled}
            className={cn(
              error && "text-destructive hover:text-destructive",
              className
            )}
          >
            {error ? (
              <AlertCircle className="h-4 w-4" />
            ) : (
              <IconComponent className="h-4 w-4" />
            )}
            {showLabel && (
              <span className="ml-2">
                {error ? '재시도' : actionText}
              </span>
            )}
          </Button>
        </TooltipTrigger>
        <TooltipContent>
          {error 
            ? `오류: ${error} (클릭하여 재시도)` 
            : `${fileInfo.icon} ${fileName} ${actionText}`
          }
        </TooltipContent>
      </Tooltip>
    </TooltipProvider>
  );
};

/**
 * 파일명 링크 컴포넌트
 */
interface FileNameLinkProps {
  filePath: string;
  fileName: string;
  className?: string;
  showIcon?: boolean;
  maxLength?: number;
}

export const FileNameLink: React.FC<FileNameLinkProps> = ({
  filePath,
  fileName,
  className,
  showIcon = true,
  maxLength = 200,
}) => {
  const fileInfo = getFileInfo(fileName);
  
  const handleClick = () => {
    smartFileAction(filePath, fileName);
  };

  const displayName = fileName.length > maxLength 
    ? `${fileName.substring(0, maxLength)}...`
    : fileName;

  return (
    <button
      onClick={handleClick}
      className={cn(
        "flex items-center gap-1 text-blue-600 hover:text-blue-800 hover:underline cursor-pointer text-left",
        className
      )}
      title={`${fileInfo.icon} ${fileName} ${fileInfo.canPreview ? '미리보기' : '다운로드'}`}
    >
      {showIcon && (
        <span className="text-xs flex-shrink-0">{fileInfo.icon}</span>
      )}
      <span className="truncate">{displayName}</span>
    </button>
  );
};