summaryrefslogtreecommitdiff
path: root/hooks/use-file-download.ts
blob: 4b25661e8e3cce55bb168708c53e8eff0e146c58 (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
// hooks/use-file-download.ts
// 파일 다운로드 상태 관리 훅

import { useState, useCallback } from 'react';
import { downloadFile, type FileDownloadOptions, type FileDownloadResult } from '@/lib/file-download';

/**
 * 파일 다운로드 상태
 */
interface FileDownloadState {
  isLoading: boolean;
  error: string | null;
  progress: number;
  lastResult: FileDownloadResult | null;
}

/**
 * 단일 파일 다운로드 훅
 */
export const useFileDownload = () => {
  const [state, setState] = useState<FileDownloadState>({
    isLoading: false,
    error: null,
    progress: 0,
    lastResult: null,
  });

  const download = useCallback(async (
    filePath: string,
    fileName: string,
    options: FileDownloadOptions = {}
  ): Promise<FileDownloadResult> => {
    setState(prev => ({
      ...prev,
      isLoading: true,
      error: null,
      progress: 0,
    }));

    const result = await downloadFile(filePath, fileName, {
      ...options,
      onProgress: (progress) => {
        setState(prev => ({ ...prev, progress }));
        if (options.onProgress) options.onProgress(progress);
      },
      onError: (error) => {
        setState(prev => ({ ...prev, error, isLoading: false }));
        if (options.onError) options.onError(error);
      },
      onSuccess: (fileName, fileSize) => {
        setState(prev => ({ 
          ...prev, 
          isLoading: false, 
          error: null,
          progress: 100 
        }));
        if (options.onSuccess) options.onSuccess(fileName, fileSize);
      },
    });

    setState(prev => ({
      ...prev,
      isLoading: false,
      lastResult: result,
    }));

    return result;
  }, []);

  const reset = useCallback(() => {
    setState({
      isLoading: false,
      error: null,
      progress: 0,
      lastResult: null,
    });
  }, []);

  return {
    ...state,
    download,
    reset,
  };
};

/**
 * 다중 파일 다운로드 훅
 */
export const useMultiFileDownload = () => {
  const [downloads, setDownloads] = useState<Map<string, FileDownloadState>>(new Map());

  const getFileState = useCallback((filePath: string): FileDownloadState => {
    return downloads.get(filePath) || {
      isLoading: false,
      error: null,
      progress: 0,
      lastResult: null,
    };
  }, [downloads]);

  const isFileLoading = useCallback((filePath: string): boolean => {
    return downloads.get(filePath)?.isLoading || false;
  }, [downloads]);

  const getFileError = useCallback((filePath: string): string | null => {
    return downloads.get(filePath)?.error || null;
  }, [downloads]);

  const updateFileState = useCallback((filePath: string, updates: Partial<FileDownloadState>) => {
    setDownloads(prev => {
      const newMap = new Map(prev);
      const currentState = newMap.get(filePath) || {
        isLoading: false,
        error: null,
        progress: 0,
        lastResult: null,
      };
      newMap.set(filePath, { ...currentState, ...updates });
      return newMap;
    });
  }, []);

  const downloadFile = useCallback(async (
    filePath: string,
    fileName: string,
    options: FileDownloadOptions = {}
  ): Promise<FileDownloadResult> => {
    updateFileState(filePath, {
      isLoading: true,
      error: null,
      progress: 0,
    });

    const result = await downloadFile(filePath, fileName, {
      ...options,
      showToast: options.showToast ?? true,
      onProgress: (progress) => {
        updateFileState(filePath, { progress });
        if (options.onProgress) options.onProgress(progress);
      },
      onError: (error) => {
        updateFileState(filePath, { error, isLoading: false });
        if (options.onError) options.onError(error);
      },
      onSuccess: (fileName, fileSize) => {
        updateFileState(filePath, { 
          isLoading: false, 
          error: null,
          progress: 100 
        });
        if (options.onSuccess) options.onSuccess(fileName, fileSize);
      },
    });

    updateFileState(filePath, {
      isLoading: false,
      lastResult: result,
    });

    return result;
  }, [updateFileState]);

  const resetFile = useCallback((filePath: string) => {
    setDownloads(prev => {
      const newMap = new Map(prev);
      newMap.delete(filePath);
      return newMap;
    });
  }, []);

  const resetAll = useCallback(() => {
    setDownloads(new Map());
  }, []);

  return {
    downloads,
    getFileState,
    isFileLoading,
    getFileError,
    downloadFile,
    resetFile,
    resetAll,
  };
};

/**
 * 파일 다운로드 기본 설정 훅
 */
export const useFileDownloadConfig = (defaultOptions: Partial<FileDownloadOptions> = {}) => {
  const { download, ...state } = useFileDownload();

  const downloadWithDefaults = useCallback((
    filePath: string,
    fileName: string,
    options: FileDownloadOptions = {}
  ) => {
    return download(filePath, fileName, { ...defaultOptions, ...options });
  }, [download, defaultOptions]);

  return {
    ...state,
    download: downloadWithDefaults,
  };
};