Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Make the empty digital files card a drag-and-drop dropzone

Damian Legawieca87c20412388
diff --git a/packages/dashboard/src/components/spree/products/digital-assets-card.tsx b/packages/dashboard/src/components/spree/products/digital-assets-card.tsx
index eb5f5ab4b6..9f9a7128fb 100644
--- a/packages/dashboard/src/components/spree/products/digital-assets-card.tsx
+++ b/packages/dashboard/src/components/spree/products/digital-assets-card.tsx
@@ -65,21 +65,33 @@ export function DigitalAssetsCard({
// keeps its today behaviour — a single "Add file" button.
const sourceProviders = providers.filter((p) => !p.requires_attachment)
- async function handleFileSelected(event: React.ChangeEvent<HTMLInputElement>) {
- const file = event.target.files?.[0]
- event.target.value = ''
- if (!file || !productId) return
+ async function handleFiles(files: FileList | File[]) {
+ const list = Array.from(files)
+ if (list.length === 0 || !productId) return
setUploading(true)
try {
- const { signedId } = await directUpload.mutateAsync(file)
- await createAsset.mutateAsync({ signed_id: signedId })
+ for (const file of list) {
+ const { signedId } = await directUpload.mutateAsync(file)
+ await createAsset.mutateAsync({ signed_id: signedId })
+ }
setPage(1)
} finally {
setUploading(false)
}
}
+ function handleFileInput(event: React.ChangeEvent<HTMLInputElement>) {
+ const files = event.target.files
+ event.target.value = ''
+ if (files) void handleFiles(files)
+ }
+
+ function handleDrop(event: React.DragEvent) {
+ event.preventDefault()
+ if (event.dataTransfer.files.length > 0) void handleFiles(event.dataTransfer.files)
+ }
+
async function handleAddProvider(providerType: string) {
await createAsset.mutateAsync({ provider_type: providerType })
setPage(1)
@@ -108,61 +120,83 @@ export function DigitalAssetsCard({
<Card>
<CardHeader>
<CardTitle>{t('admin.digital_assets.title')}</CardTitle>
- <CardAction>
- {sourceProviders.length === 0 ? (
- <Button
- type="button"
- variant="outline"
- size="sm"
- disabled={uploading}
- onClick={() => fileInputRef.current?.click()}
- >
- <UploadIcon className="mr-2 size-4" />
- {uploading
- ? t('admin.digital_assets.uploading_short')
- : t('admin.digital_assets.upload')}
- </Button>
- ) : (
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button type="button" variant="outline" size="sm" disabled={uploading}>
- <PlusIcon className="mr-2 size-4" />
- {uploading
- ? t('admin.digital_assets.uploading_short')
- : t('admin.digital_assets.add')}
- <ChevronDownIcon className="ml-2 size-4" />
- </Button>
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end">
- <DropdownMenuItem onClick={() => fileInputRef.current?.click()}>
- <UploadIcon className="mr-2 size-4" />
- {t('admin.digital_assets.source.upload')}
- </DropdownMenuItem>
- {sourceProviders.map((provider) => (
- <DropdownMenuItem
- key={provider.type}
- onClick={() => handleAddProvider(provider.type)}
- >
- {provider.name}
+ {/* The header action only appears once files exist — an empty card
+ invites the first upload through its dropzone, not a button. */}
+ {assets.length > 0 && (
+ <CardAction>
+ {sourceProviders.length === 0 ? (
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ disabled={uploading}
+ onClick={() => fileInputRef.current?.click()}
+ >
+ <UploadIcon className="mr-2 size-4" />
+ {uploading
+ ? t('admin.digital_assets.uploading_short')
+ : t('admin.digital_assets.upload')}
+ </Button>
+ ) : (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button type="button" variant="outline" size="sm" disabled={uploading}>
+ <PlusIcon className="mr-2 size-4" />
+ {uploading
+ ? t('admin.digital_assets.uploading_short')
+ : t('admin.digital_assets.add')}
+ <ChevronDownIcon className="ml-2 size-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuItem onClick={() => fileInputRef.current?.click()}>
+ <UploadIcon className="mr-2 size-4" />
+ {t('admin.digital_assets.source.upload')}
</DropdownMenuItem>
- ))}
- </DropdownMenuContent>
- </DropdownMenu>
- )}
- <input ref={fileInputRef} type="file" className="hidden" onChange={handleFileSelected} />
- </CardAction>
+ {sourceProviders.map((provider) => (
+ <DropdownMenuItem
+ key={provider.type}
+ onClick={() => handleAddProvider(provider.type)}
+ >
+ {provider.name}
+ </DropdownMenuItem>
+ ))}
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )}
+ </CardAction>
+ )}
</CardHeader>
<CardContent className="flex flex-col gap-4">
<p className="text-muted-foreground text-sm">{t('admin.digital_assets.description')}</p>
+ <input
+ ref={fileInputRef}
+ type="file"
+ multiple
+ className="hidden"
+ onChange={handleFileInput}
+ />
+
{isLoading ? (
<Skeleton className="h-24 w-full" />
) : assets.length === 0 ? (
- <div className="flex flex-col items-center gap-2 rounded-md border border-dashed p-6 text-center">
- <FileIcon className="text-muted-foreground size-6" />
- <p className="text-muted-foreground text-sm">{t('admin.digital_assets.empty')}</p>
- </div>
+ <button
+ type="button"
+ onDrop={handleDrop}
+ onDragOver={(e) => e.preventDefault()}
+ onClick={() => fileInputRef.current?.click()}
+ disabled={uploading}
+ className="flex w-full cursor-pointer flex-col items-center justify-center gap-2 rounded-lg border-2 border-dashed border-border p-6 text-center transition-colors hover:border-foreground/30 disabled:pointer-events-none disabled:opacity-60"
+ >
+ <FileIcon className="size-8 text-muted-foreground" />
+ <p className="text-muted-foreground text-sm">
+ {uploading
+ ? t('admin.digital_assets.uploading_short')
+ : t('admin.digital_assets.drop_hint')}
+ </p>
+ </button>
) : (
<div className="overflow-hidden rounded-md border border-border">
<Table className="border-collapse [&_td]:rounded-none [&_td]:border [&_th]:border [&_td]:border-border [&_th]:border-border">
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index e34a80a149..2fec298cf7 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -856,6 +856,7 @@
"delete_title": "حذف هذا الملف؟",
"description": "ملفات يمكن للمشترين تنزيلها بعد الشراء. أرفقها بمنتج رقمي، أو بمنتج مادي لتسليم دليل أو ضمان مع البضاعة.",
"download": "تنزيل",
+ "drop_hint": "اسحب الملفات وأفلتها هنا، أو انقر للتصفح",
"empty": "لا توجد ملفات بعد",
"limit_invalid": "يجب أن تكون الحدود أعدادًا صحيحة من 1 فأكثر، أو فارغة.",
"limits_help": "اترك حدود التنزيل والأيام فارغة لاستخدام إعدادات المتجر.",
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index 9e8e01a320..4fd4ab51a7 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -839,6 +839,7 @@
"delete_title": "Diese Datei löschen?",
"description": "Dateien, die Käufer nach dem Kauf herunterladen können. Fügen Sie sie einem digitalen Produkt hinzu oder einem physischen, um eine Anleitung oder Garantie mitzuliefern.",
"download": "Herunterladen",
+ "drop_hint": "Dateien hierher ziehen und ablegen oder zum Durchsuchen klicken",
"empty": "Noch keine Dateien",
"limit_invalid": "Limits müssen ganze Zahlen ab 1 sein oder leer bleiben.",
"limits_help": "Lassen Sie die Download- und Tageslimits leer, um die Einstellungen des Shops zu verwenden.",
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index 6ba803fe73..549bcec4b2 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -839,6 +839,7 @@
"delete_title": "Delete this file?",
"description": "Files buyers can download after purchase. Attach them to a digital product, or to a physical one to deliver a manual or warranty alongside the goods.",
"download": "Download",
+ "drop_hint": "Drag & drop files here, or click to browse",
"empty": "No files yet",
"limit_invalid": "Limits must be whole numbers of 1 or more, or blank.",
"limits_help": "Leave the download and day limits blank to use the store's settings.",
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index d5bbfc1017..edc25750dd 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -839,6 +839,7 @@
"delete_title": "Supprimer ce fichier ?",
"description": "Fichiers que les acheteurs peuvent télécharger après l'achat. Associez-les à un produit numérique, ou à un produit physique pour livrer un manuel ou une garantie.",
"download": "Télécharger",
+ "drop_hint": "Glissez-déposez des fichiers ici, ou cliquez pour parcourir",
"empty": "Aucun fichier pour le moment",
"limit_invalid": "Les limites doivent être des entiers supérieurs ou égaux à 1, ou vides.",
"limits_help": "Laissez les limites de téléchargements et de jours vides pour utiliser les paramètres de la boutique.",
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index 923fb28271..5d2d73cbd2 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -857,6 +857,7 @@
"delete_title": "Usunąć ten plik?",
"description": "Pliki, które kupujący mogą pobrać po zakupie. Dołącz je do produktu cyfrowego lub do fizycznego, aby dostarczyć instrukcję albo gwarancję.",
"download": "Pobierz",
+ "drop_hint": "Przeciągnij i upuść pliki tutaj lub kliknij, aby wybrać",
"empty": "Brak plików",
"limit_invalid": "Limity muszą być liczbami całkowitymi od 1 lub puste.",
"limits_help": "Pozostaw limity pobrań i dni puste, aby użyć ustawień sklepu.",
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 2d21a9e967..681437e4e7 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -834,6 +834,7 @@
"delete_title": "删除此文件?",
"description": "买家购买后可下载的文件。可附加到数字商品,也可附加到实体商品以随货提供说明书或保修卡。",
"download": "下载",
+ "drop_hint": "将文件拖放到此处,或点击浏览",
"empty": "暂无文件",
"limit_invalid": "限制必须是不小于 1 的整数,或留空。",
"limits_help": "将下载次数和天数限制留空即可使用商店设置。",