Blame view

美国版/Food Labeling Management Platform/src/components/bulk/batch-import-dialog.tsx 9.14 KB
6ce07406   杨鑫   提交
1
2
  import React, { useCallback, useId, useRef, useState } from "react";
  import { FileSpreadsheet, Upload, X } from "lucide-react";
63289723   杨鑫   提交
3
4
5
6
7
8
9
10
11
12
  import { Button } from "../ui/button";
  import {
    Dialog,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogTitle,
  } from "../ui/dialog";
  import { Label } from "../ui/label";
6ce07406   杨鑫   提交
13
  import { cn } from "../ui/utils";
63289723   杨鑫   提交
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  import { toast } from "sonner";
  import { ApiError } from "../../lib/apiClient";
  
  export type BatchImportDialogProps = {
    open: boolean;
    onOpenChange: (open: boolean) => void;
    title: string;
    description?: string;
    /** 弹框底部「下载模板」 */
    onDownloadTemplate: () => void | Promise<void>;
    /** 选择文件后点击 Import 上传 */
    onImportFile: (file: File) => Promise<{ successCount: number; failCount: number }>;
    downloadingTemplate?: boolean;
  };
  
6ce07406   杨鑫   提交
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  const ACCEPT = ".xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  
  function formatFileSize(bytes: number): string {
    if (!Number.isFinite(bytes) || bytes < 0) return "";
    if (bytes < 1024) return `${bytes} B`;
    if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
    return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
  }
  
  function pickFile(list: FileList | null): File | null {
    const f = list?.[0];
    if (!f) return null;
    const name = f.name.toLowerCase();
    if (!name.endsWith(".xlsx")) {
      return null;
    }
    return f;
  }
  
63289723   杨鑫   提交
48
49
50
51
52
53
54
55
56
  export function BatchImportDialog({
    open,
    onOpenChange,
    title,
    description,
    onDownloadTemplate,
    onImportFile,
    downloadingTemplate = false,
  }: BatchImportDialogProps) {
6ce07406   杨鑫   提交
57
    const fileInputId = useId();
63289723   杨鑫   提交
58
59
60
    const inputRef = useRef<HTMLInputElement | null>(null);
    const [file, setFile] = useState<File | null>(null);
    const [busy, setBusy] = useState(false);
6ce07406   杨鑫   提交
61
    const [dragActive, setDragActive] = useState(false);
63289723   杨鑫   提交
62
63
64
  
    const reset = () => {
      setFile(null);
6ce07406   杨鑫   提交
65
      setDragActive(false);
63289723   杨鑫   提交
66
67
68
      if (inputRef.current) inputRef.current.value = "";
    };
  
6ce07406   杨鑫   提交
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    const clearSelectedFile = () => {
      setFile(null);
      if (inputRef.current) inputRef.current.value = "";
    };
  
    const applyFiles = useCallback((list: FileList | null) => {
      const f = pickFile(list);
      if (list?.length && !f) {
        toast.error("Invalid file", { description: "Please choose an .xlsx file." });
        return;
      }
      setFile(f);
    }, []);
  
    /** 延迟触发:避免嵌在 Radix Dialog 内时,同步 click() 导致部分浏览器不触发 change */
    const openPicker = () => {
      window.setTimeout(() => {
        const el = inputRef.current;
        if (!el) return;
        el.value = "";
        el.click();
      }, 0);
    };
  
63289723   杨鑫   提交
93
94
95
96
97
98
99
100
    return (
      <Dialog
        open={open}
        onOpenChange={(v) => {
          if (!v) reset();
          onOpenChange(v);
        }}
      >
6ce07406   杨鑫   提交
101
102
103
104
105
106
107
108
109
110
111
112
113
        <DialogContent
          className={cn("gap-4")}
          style={{
            padding: 20,
            width: "min(50vw, calc(100vw - 2rem))",
            maxWidth: "min(50vw, calc(100vw - 2rem))",
          }}
        >
          <DialogHeader className="space-y-1.5">
            <DialogTitle className="text-base">{title}</DialogTitle>
            {description ? (
              <DialogDescription className="text-xs leading-relaxed">{description}</DialogDescription>
            ) : null}
63289723   杨鑫   提交
114
          </DialogHeader>
6ce07406   杨鑫   提交
115
116
  
          <div className="flex flex-col gap-3">
63289723   杨鑫   提交
117
            <div className="space-y-2">
6ce07406   杨鑫   提交
118
119
120
              <Label htmlFor={fileInputId} className="text-sm">
                Excel file (.xlsx)
              </Label>
63289723   杨鑫   提交
121
              <input
6ce07406   杨鑫   提交
122
                id={fileInputId}
63289723   杨鑫   提交
123
124
                ref={inputRef}
                type="file"
6ce07406   杨鑫   提交
125
126
127
128
129
                accept={ACCEPT}
                className="sr-only"
                tabIndex={-1}
                aria-label="Choose Excel spreadsheet"
                onChange={(e) => applyFiles(e.target.files)}
63289723   杨鑫   提交
130
              />
6ce07406   杨鑫   提交
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
              <button
                type="button"
                className={cn(
                  "flex w-full flex-col items-center justify-center gap-2 rounded-lg border-2 border-dashed px-4 py-5 text-center transition-colors outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2",
                  dragActive
                    ? "border-blue-400 bg-blue-50/80"
                    : "border-gray-300 bg-gray-50/90 hover:border-gray-400 hover:bg-gray-50",
                )}
                onClick={openPicker}
                onKeyDown={(e) => {
                  if (e.key === "Enter" || e.key === " ") {
                    e.preventDefault();
                    openPicker();
                  }
                }}
                onDragEnter={(e) => {
                  e.preventDefault();
                  setDragActive(true);
                }}
                onDragOver={(e) => {
                  e.preventDefault();
                  e.dataTransfer.dropEffect = "copy";
                }}
                onDragLeave={(e) => {
                  e.preventDefault();
                  if (!e.currentTarget.contains(e.relatedTarget as Node)) setDragActive(false);
                }}
                onDrop={(e) => {
                  e.preventDefault();
                  setDragActive(false);
                  applyFiles(e.dataTransfer.files);
                }}
              >
                <Upload className="h-7 w-7 shrink-0 text-gray-400" aria-hidden />
                <div className="text-sm text-gray-700">
                  <span className="font-medium text-blue-700">Browse</span>
                  <span className="text-gray-600"> or drop your file here</span>
                </div>
                <p className="text-xs text-gray-500">
                  {file ? "Click again to replace the file" : "Only .xlsx is accepted"}
                </p>
              </button>
  
              {file ? (
                <div
                  role="status"
                  aria-live="polite"
                  className="flex items-center gap-3 rounded-lg border border-blue-200 bg-blue-50/60 px-3 py-2.5"
                >
                  <FileSpreadsheet className="h-8 w-8 shrink-0 text-blue-600" aria-hidden />
                  <div className="min-w-0 flex-1">
                    <div className="text-xs font-medium uppercase tracking-wide text-blue-800">Selected file</div>
                    <div className="truncate text-sm font-semibold text-gray-900" title={file.name}>
                      {file.name}
                    </div>
                    {formatFileSize(file.size) ? (
                      <div className="text-xs text-gray-600">{formatFileSize(file.size)}</div>
                    ) : null}
                  </div>
                  <Button
                    type="button"
                    variant="outline"
                    size="sm"
                    className="shrink-0 border-gray-300 bg-white px-2 text-gray-700 hover:bg-gray-50"
                    onClick={(e) => {
                      e.stopPropagation();
                      clearSelectedFile();
                    }}
                    aria-label="Remove selected file"
                  >
                    <X className="h-4 w-4" aria-hidden />
                    <span className="ml-1 hidden sm:inline">Remove</span>
                  </Button>
                </div>
              ) : (
                <div className="rounded-lg border border-dashed border-gray-200 bg-gray-50/50 px-3 py-2 text-center text-xs text-gray-500">
                  No file selected yet — use Browse or drag an .xlsx above
                </div>
              )}
  
              <p className="text-xs text-gray-600">
                After a file is selected, click <span className="font-medium text-gray-900">Import</span> to upload
                it to the server.
              </p>
            </div>
  
            <div className="flex justify-stretch sm:justify-center">
              <Button
                type="button"
                variant="outline"
                className="w-full sm:w-auto"
                size="sm"
                disabled={downloadingTemplate}
                onClick={() => void onDownloadTemplate()}
              >
                {downloadingTemplate ? "Downloading…" : "Download template"}
              </Button>
63289723   杨鑫   提交
228
229
            </div>
          </div>
6ce07406   杨鑫   提交
230
  
63289723   杨鑫   提交
231
232
233
234
          <DialogFooter className="gap-2 sm:gap-0">
            <Button
              type="button"
              variant="outline"
6ce07406   杨鑫   提交
235
              size="sm"
63289723   杨鑫   提交
236
237
238
239
240
241
242
243
244
              onClick={() => {
                reset();
                onOpenChange(false);
              }}
            >
              Cancel
            </Button>
            <Button
              type="button"
6ce07406   杨鑫   提交
245
246
              size="sm"
              className="bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50"
63289723   杨鑫   提交
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
              disabled={!file || busy}
              onClick={async () => {
                if (!file) return;
                setBusy(true);
                try {
                  const r = await onImportFile(file);
                  toast.success("Import finished", {
                    description: `Success: ${r.successCount}, failed: ${r.failCount}`,
                  });
                  reset();
                  onOpenChange(false);
                } catch (e) {
                  const msg = e instanceof ApiError ? e.message : e instanceof Error ? e.message : "Import failed.";
                  toast.error("Import failed", { description: msg });
                } finally {
                  setBusy(false);
                }
              }}
            >
              {busy ? "Importing…" : "Import"}
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    );
  }