File size: 2,972 Bytes
a786b70
 
5908204
b49adce
0195b42
 
b49adce
a34c56d
 
 
5908204
 
0195b42
 
 
b49adce
0195b42
 
a34c56d
5908204
0195b42
5908204
a786b70
0195b42
a786b70
 
 
 
b49adce
 
5908204
b49adce
 
 
0195b42
 
 
 
a786b70
 
 
 
 
0195b42
b49adce
 
 
 
0195b42
 
 
 
 
 
 
 
 
a34c56d
0195b42
 
 
 
 
 
 
a786b70
 
 
 
 
 
 
0195b42
 
 
 
a786b70
 
 
 
 
 
 
 
 
 
 
0195b42
 
b49adce
5908204
0195b42
 
 
 
 
 
a786b70
 
 
 
0195b42
 
5908204
0195b42
 
b49adce
0195b42
5908204
 
a786b70
 
5908204
a786b70
 
 
5908204
0195b42
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
import { useCallback, useState } from "react";
import { FileItem, UploadStatus } from "../types";

export function useFileUpload() {
  const [files, setFiles] = useState<FileItem[]>([]);
  const [isUploading, setIsUploading] = useState(false);

  // ✅ App.tsx + FileUploader truyền FileItem[]
  const addFiles = useCallback((items: FileItem[]) => {
    setFiles((prev) => [...prev, ...items]);
  }, []);

  const removeFile = useCallback((id: string) => {
    setFiles((prev) => prev.filter((f) => f.id !== id));
  }, []);

  const updateFilePath = useCallback((id: string, path: string) => {
    setFiles((prev) =>
      prev.map((f) => (f.id === id ? { ...f, path } : f))
    );
  }, []);

  const uploadOne = useCallback((item: FileItem) => {
    return new Promise<void>((resolve, reject) => {
      const form = new FormData();
      form.append("file", item.file);
      form.append("path", item.path);

      const xhr = new XMLHttpRequest();
      xhr.open("POST", "/api/upload");

      xhr.upload.onprogress = (e) => {
        if (!e.lengthComputable) return;
        const percent = Math.round((e.loaded / e.total) * 100);

        setFiles((prev) =>
          prev.map((f) =>
            f.id === item.id
              ? {
                  ...f,
                  progress: percent,
                  status: UploadStatus.UPLOADING,
                }
              : f
          )
        );
      };

      xhr.onload = () => {
        if (xhr.status >= 200 && xhr.status < 300) {
          const res = JSON.parse(xhr.responseText);
          setFiles((prev) =>
            prev.map((f) =>
              f.id === item.id
                ? {
                    ...f,
                    progress: 100,
                    status: UploadStatus.SUCCESS,
                    url: res.url,
                  }
                : f
            )
          );
          resolve();
        } else {
          setFiles((prev) =>
            prev.map((f) =>
              f.id === item.id
                ? { ...f, status: UploadStatus.ERROR }
                : f
            )
          );
          reject(new Error("Upload failed"));
        }
      };

      xhr.onerror = () => {
        setFiles((prev) =>
          prev.map((f) =>
            f.id === item.id
              ? { ...f, status: UploadStatus.ERROR }
              : f
          )
        );
        reject(new Error("Network error"));
      };

      xhr.send(form);
    });
  }, []);

  const startUpload = useCallback(async () => {
    if (isUploading) return;
    setIsUploading(true);

    try {
      for (const f of files) {
        if (
          f.status === UploadStatus.IDLE ||
          f.status === UploadStatus.ERROR
        ) {
          await uploadOne(f);
        }
      }
    } finally {
      setIsUploading(false);
    }
  }, [files, uploadOne, isUploading]);

  return {
    files,
    isUploading,
    addFiles,
    removeFile,
    updateFilePath,
    startUpload,
  };
}