File size: 5,285 Bytes
9df6ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from "react";

// ─── Directory drop helper ──────────────────────────────────────────────────
async function* getFilesFromDataTransferItems(items: DataTransferItemList, prefix = ""): AsyncGenerator<File & { __folderPath?: string }> {
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    if (item.kind === "file") {
      const entry = item.webkitGetAsEntry?.();
      if (entry?.isFile) {
        const file = item.getAsFile();
        if (file) {
          if (prefix) {
            (file as any).__folderPath = prefix;
          }
          yield file as File & { __folderPath?: string };
        }
      } else if (entry?.isDirectory) {
        yield* readDirectoryRecursive(entry as any, entry.name);
      }
    }
  }
}

async function* readDirectoryRecursive(dir: any, prefix = ""): AsyncGenerator<File & { __folderPath?: string }> {
  const reader = dir.createReader();
  const entries = await new Promise<any[]>((resolve, reject) => {
    reader.readEntries(resolve, reject);
  });

  for (const entry of entries) {
    const newPrefix = prefix ? `${prefix}/${entry.name}` : entry.name;
    if (entry.isFile) {
      const file = await new Promise<File>((resolve, reject) => {
        (entry as any).file(resolve, reject);
      });
      (file as any).__folderPath = prefix;
      yield file as File & { __folderPath?: string };
    } else if (entry.isDirectory) {
      yield* readDirectoryRecursive(entry, newPrefix);
    }
  }
}

// ─── UploadArea ─────────────────────────────────────────────────────────────
export function UploadArea({

  onFilesSelected,

  busy,

  dragOver,

  onDragOver,

  onDragLeave,

  uploadInputRef,

  uploadFolderRef,

}: {

  onFilesSelected: (files: FileList | (File & { __folderPath?: string })[]) => void;

  busy: boolean;

  dragOver: boolean;

  onDragOver: (e: React.DragEvent) => void;

  onDragLeave: (e: React.DragEvent) => void;

  uploadInputRef: React.RefObject<HTMLInputElement | null>;

  uploadFolderRef: React.RefObject<HTMLInputElement | null>;

}) {
  const handleDrop = async (e: React.DragEvent) => {
    e.preventDefault();
    onDragLeave(e);
    
    if (e.dataTransfer.items?.length) {
      try {
        const allFiles: (File & { __folderPath?: string })[] = [];
        for await (const file of getFilesFromDataTransferItems(e.dataTransfer.items)) {
          allFiles.push(file);
        }
        if (allFiles.length > 0) {
          onFilesSelected(allFiles);
        } else if (e.dataTransfer.files) {
          onFilesSelected(e.dataTransfer.files);
        }
      } catch (err) {
        console.error("Error reading drag-and-drop items:", err);
        if (e.dataTransfer.files) {
          onFilesSelected(e.dataTransfer.files);
        }
      }
    } else if (e.dataTransfer.files) {
      onFilesSelected(e.dataTransfer.files);
    }
  };

  return (
    <div

      onDragOver={onDragOver}

      onDragLeave={onDragLeave}

      onDrop={handleDrop}

      className={`rounded-lg border-2 border-dashed p-4 text-center transition-colors ${

        dragOver

          ? "border-violet-400 bg-violet-50"

          : "border-slate-300 bg-slate-50 hover:border-slate-400"

      } ${busy ? "opacity-60 cursor-not-allowed" : "cursor-pointer"}`}

    >

      <div className="text-sm font-semibold text-slate-600 mb-2">

        {busy ? "Uploading..." : "Drop files or folders here"}

      </div>

      <div className="flex gap-2 justify-center flex-wrap">

        <button

          type="button"

          onClick={() => uploadInputRef.current?.click()}

          disabled={busy}

          className="rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-semibold text-slate-600 hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"

        >

          Select Files

        </button>

        <button

          type="button"

          onClick={() => uploadFolderRef.current?.click()}

          disabled={busy}

          className="rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-semibold text-slate-600 hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"

        >

          Select Folder

        </button>

      </div>

      <input

        ref={uploadInputRef}

        type="file"

        multiple

        onChange={(e) => e.target.files && onFilesSelected(e.target.files)}

        className="hidden"

        disabled={busy}

      />

      <input

        {...({

          ref: uploadFolderRef,

          type: "file",

          webkitdirectory: "true",

          mozdirectory: "true",

          multiple: true,

          onChange: (e) => e.target.files && onFilesSelected(e.target.files),

          className: "hidden",

          disabled: busy,

        } as React.InputHTMLAttributes<HTMLInputElement> & {

          webkitdirectory?: string;

          mozdirectory?: string;

        })}

      />

    </div>

  );

}