@dimah-s3/ui 0.8.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2,9 +2,9 @@
2
2
  import { AlertCircleIcon, PauseIcon, XIcon, LoaderIcon, DownloadIcon, Trash2Icon, CheckCircleIcon, LoaderCircleIcon, CloudUpload, CircleCheckIcon, InfoIcon, TriangleAlertIcon, OctagonXIcon, Loader2Icon } from 'lucide-react';
3
3
  import { useTranslations } from '@fuma-translate/react';
4
4
  import { useFormatDimahError, formatEta, useDownload, useDelete, useMultiUpload, useUpload, formatAcceptLabels } from '@dimah-s3/react';
5
- import { cn } from 'cnfast';
6
5
  import { Button as Button$1 } from '@base-ui/react/button';
7
6
  import { cva } from 'class-variance-authority';
7
+ import { cn } from 'cnfast';
8
8
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
9
9
  import { Tooltip as Tooltip$1 } from '@base-ui/react/tooltip';
10
10
  import { formatFileSize, truncateFileName } from '@dimah-s3/core';
@@ -17,15 +17,6 @@ import { Toast as Toast$1 } from '@base-ui/react/toast';
17
17
  import { AlertDialog as AlertDialog$1 } from '@base-ui/react/alert-dialog';
18
18
 
19
19
  // src/components/dimah-s3/upload/upload-button.tsx
20
-
21
- // src/lib/attachment-layout.ts
22
- var ATTACHMENT_ERROR_DESCRIPTION_CLASS = "overflow-visible whitespace-normal [overflow-wrap:anywhere]";
23
- var ATTACHMENT_TITLE_CLASS = "max-w-[48ch] dark:shimmer-color-dimah-s3-muted-foreground";
24
- function resolveStatusSlot(slot = true, node) {
25
- if (slot === false) return null;
26
- if (typeof slot === "function") return slot(node);
27
- return node;
28
- }
29
20
  var buttonVariants = cva(
30
21
  "group/button inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
31
22
  {
@@ -111,6 +102,15 @@ function TooltipContent({
111
102
  }
112
103
  ) });
113
104
  }
105
+
106
+ // src/lib/attachment-layout.ts
107
+ var ATTACHMENT_ERROR_DESCRIPTION_CLASS = "overflow-visible whitespace-normal [overflow-wrap:anywhere]";
108
+ var ATTACHMENT_TITLE_CLASS = "max-w-[48ch] dark:shimmer-color-dimah-s3-muted-foreground";
109
+ function resolveStatusSlot(slot = true, node) {
110
+ if (slot === false) return null;
111
+ if (typeof slot === "function") return slot(node);
112
+ return node;
113
+ }
114
114
  var attachmentVariants = cva(
115
115
  "group/attachment relative flex w-fit max-w-full min-w-0 shrink-0 flex-wrap rounded-xl border bg-card text-card-foreground transition-colors focus-within:ring-1 focus-within:ring-ring/50 has-[>a,>button]:hover:bg-muted/50 data-[state=error]:border-destructive/30 data-[state=idle]:border-dashed",
116
116
  {
@@ -261,7 +261,8 @@ function CircleProgress({
261
261
  percent,
262
262
  size = 20,
263
263
  strokeWidth = 2.5,
264
- className
264
+ className,
265
+ label
265
266
  }) {
266
267
  const r = (size - strokeWidth) / 2;
267
268
  const c = 2 * Math.PI * r;
@@ -273,7 +274,12 @@ function CircleProgress({
273
274
  height: size,
274
275
  className: cn("shrink-0 -rotate-90", className),
275
276
  style: { width: size, height: size },
276
- "aria-hidden": true,
277
+ role: label ? "progressbar" : void 0,
278
+ "aria-label": label,
279
+ "aria-valuenow": label ? Math.round(percent) : void 0,
280
+ "aria-valuemin": label ? 0 : void 0,
281
+ "aria-valuemax": label ? 100 : void 0,
282
+ "aria-hidden": label ? void 0 : true,
277
283
  children: [
278
284
  /* @__PURE__ */ jsx(
279
285
  "circle",
@@ -501,7 +507,11 @@ function FileAttachment({
501
507
  percent: state === "uploading" ? percent : 0,
502
508
  size: progress.size,
503
509
  strokeWidth: progress.strokeWidth,
504
- className: progress.className
510
+ className: progress.className,
511
+ label: state === "uploading" ? t("{percent}%", {
512
+ note: "progress",
513
+ variables: { percent: String(Math.round(percent)) }
514
+ }) : void 0
505
515
  }
506
516
  ) }) : state === "error" ? /* @__PURE__ */ jsx("div", { className: "absolute inset-0 flex items-center justify-center bg-dimah-s3-destructive/20 text-dimah-s3-destructive", children: /* @__PURE__ */ jsx(AlertCircleIcon, {}) }) : null;
507
517
  return /* @__PURE__ */ jsxs(
@@ -670,7 +680,7 @@ function UploadStatus({
670
680
  }
671
681
  );
672
682
  }
673
- if (phase === "validating" || phase === "presigning") {
683
+ if (phase === "validating" || phase === "presigning" || phase === "finalizing") {
674
684
  if (fileInfo) {
675
685
  return /* @__PURE__ */ jsx(
676
686
  FileAttachment,
@@ -680,7 +690,7 @@ function UploadStatus({
680
690
  fileSize: fileInfo.size,
681
691
  fileType: fileInfo.type,
682
692
  previewUrl: fileInfo.previewUrl,
683
- description: t("Preparing\u2026", { note: "upload status" }),
693
+ description: phase === "finalizing" ? t("Finishing\u2026", { note: "upload status" }) : t("Preparing\u2026", { note: "upload status" }),
684
694
  ...layout
685
695
  }
686
696
  );
@@ -689,7 +699,7 @@ function UploadStatus({
689
699
  StatusAttachment,
690
700
  {
691
701
  state: "processing",
692
- title: t("Preparing\u2026", { note: "upload status" }),
702
+ title: phase === "finalizing" ? t("Finishing\u2026", { note: "upload status" }) : t("Preparing\u2026", { note: "upload status" }),
693
703
  ...layout
694
704
  }
695
705
  );
@@ -771,6 +781,7 @@ function MultiUploadStatus({
771
781
  totalProgress,
772
782
  error,
773
783
  onCancel,
784
+ onPause,
774
785
  size,
775
786
  orientation,
776
787
  className
@@ -806,6 +817,18 @@ function MultiUploadStatus({
806
817
  ] }),
807
818
  /* @__PURE__ */ jsx(ProgressValue, {})
808
819
  ] }),
820
+ onPause ? /* @__PURE__ */ jsx(
821
+ AttachmentAction,
822
+ {
823
+ className: "shrink-0",
824
+ "aria-label": t("Pause", { note: "upload control" }),
825
+ onClick: (e) => {
826
+ e.stopPropagation();
827
+ onPause();
828
+ },
829
+ children: /* @__PURE__ */ jsx(PauseIcon, {})
830
+ }
831
+ ) : null,
809
832
  onCancel ? /* @__PURE__ */ jsx(
810
833
  AttachmentAction,
811
834
  {
@@ -886,9 +909,9 @@ function FileList({
886
909
  FileAttachment,
887
910
  {
888
911
  state: fileAttachmentState(f.status),
889
- fileName: f.fileName,
890
- fileSize: f.fileSize,
891
- fileType: f.fileType,
912
+ fileName: f.name,
913
+ fileSize: f.size,
914
+ fileType: f.type,
892
915
  previewUrl: f.previewUrl,
893
916
  percent: f.progress.percent,
894
917
  error: f.error,
@@ -899,8 +922,8 @@ function FileList({
899
922
  )) });
900
923
  }
901
924
  function UploadStatusBlock(props) {
902
- const { size, orientation } = props;
903
- const layout = { size, orientation };
925
+ const { size, orientation, className } = props;
926
+ const layout = { size, orientation, className };
904
927
  if (props.mode === "multi" && props.files.length === 1) {
905
928
  const f = props.files[0];
906
929
  return /* @__PURE__ */ jsx(
@@ -909,12 +932,7 @@ function UploadStatusBlock(props) {
909
932
  phase: props.phase,
910
933
  progress: f.progress,
911
934
  error: f.error ?? props.error,
912
- fileInfo: {
913
- name: f.fileName,
914
- size: f.fileSize,
915
- type: f.fileType,
916
- previewUrl: f.previewUrl
917
- },
935
+ fileInfo: f,
918
936
  onCancel: props.onCancel,
919
937
  onPause: props.onPause,
920
938
  ...layout
@@ -930,6 +948,7 @@ function UploadStatusBlock(props) {
930
948
  totalProgress: props.totalProgress,
931
949
  error: props.error,
932
950
  onCancel: props.onCancel,
951
+ onPause: props.onPause,
933
952
  ...layout
934
953
  }
935
954
  );
@@ -1145,7 +1164,7 @@ function useUploadToast(ctrl, enabled) {
1145
1164
  if (!enabled) return;
1146
1165
  const fileList = files ?? [];
1147
1166
  const isMulti = mode === "multi" && fileList.length > 1;
1148
- const file = fileInfo ?? (fileList.length === 1 ? { name: fileList[0].fileName, size: fileList[0].fileSize } : null);
1167
+ const file = fileInfo ?? (fileList.length === 1 ? { name: fileList[0].name, size: fileList[0].size } : null);
1149
1168
  const total = totalProgress?.total ?? 0;
1150
1169
  if (phase === "idle" && toastIdRef.current) {
1151
1170
  toast.close(toastIdRef.current);
@@ -1218,7 +1237,7 @@ function useUploadToast(ctrl, enabled) {
1218
1237
  if (!enabled || phase !== "uploading") return;
1219
1238
  const fileList = files ?? [];
1220
1239
  const isMulti = mode === "multi" && fileList.length > 1;
1221
- const file = fileInfo ?? (fileList.length === 1 ? { name: fileList[0].fileName, size: fileList[0].fileSize } : null);
1240
+ const file = fileInfo ?? (fileList.length === 1 ? { name: fileList[0].name, size: fileList[0].size } : null);
1222
1241
  const total = totalProgress ?? { loaded: 0, total: 0};
1223
1242
  const singleProgress = progress ?? (fileList.length === 1 ? fileList[0].progress : { loaded: 0});
1224
1243
  const payload = isMulti ? {
@@ -1288,33 +1307,106 @@ function useFileRejectToast(fileRejections, enabled) {
1288
1307
  });
1289
1308
  }, [enabled, fileRejections, t]);
1290
1309
  }
1291
- var EMPTY_PROGRESS = { loaded: 0, total: 0, percent: 0 };
1292
- var EMPTY_FILES = [];
1293
- function UploadButtonSingle({
1294
- className,
1310
+ function canPauseUpload(uploadStore) {
1311
+ return uploadStore != null && uploadStore !== false;
1312
+ }
1313
+ function dropzoneHints(options, t) {
1314
+ const acceptLabels = formatAcceptLabels(options.accept);
1315
+ const maxFiles = options.maxFiles;
1316
+ const limitParts = [];
1317
+ if (maxFiles != null && maxFiles > 0) {
1318
+ limitParts.push(
1319
+ maxFiles === 1 ? t("You can upload a file", { note: "dropzone hint" }) : t("You can upload {count} files", {
1320
+ note: "dropzone hint",
1321
+ variables: { count: String(maxFiles) }
1322
+ })
1323
+ );
1324
+ }
1325
+ if (options.maxFileSize != null) {
1326
+ const size = formatFileSize(options.maxFileSize);
1327
+ limitParts.push(
1328
+ maxFiles != null && maxFiles > 1 ? t("Each up to {size}", {
1329
+ note: "dropzone hint",
1330
+ variables: { size }
1331
+ }) : t("Up to {size}", {
1332
+ note: "dropzone hint",
1333
+ variables: { size }
1334
+ })
1335
+ );
1336
+ }
1337
+ const limitsLine = limitParts.length > 0 ? `${limitParts.join(". ")}.` : null;
1338
+ const acceptLine = acceptLabels.length > 0 ? t("Accepted {types}.", {
1339
+ note: "dropzone hint",
1340
+ variables: { types: acceptLabels.join(", ") }
1341
+ }) : null;
1342
+ return { limitsLine, acceptLine };
1343
+ }
1344
+ function DropzoneChrome({
1295
1345
  label,
1296
- children,
1297
- disabled,
1298
- tooltipText,
1346
+ limitsLine,
1347
+ acceptLine,
1348
+ isDisabled,
1349
+ children
1350
+ }) {
1351
+ if (children != null) return /* @__PURE__ */ jsx(Fragment, { children });
1352
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1353
+ /* @__PURE__ */ jsx(
1354
+ CloudUpload,
1355
+ {
1356
+ className: cn("size-6", isDisabled && "opacity-50"),
1357
+ strokeWidth: 1.5
1358
+ }
1359
+ ),
1360
+ /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col gap-1", isDisabled && "opacity-50"), children: [
1361
+ /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: label }),
1362
+ limitsLine && /* @__PURE__ */ jsx("p", { className: "text-xs text-dimah-s3-muted-foreground", children: limitsLine }),
1363
+ acceptLine && /* @__PURE__ */ jsx("p", { className: "text-xs text-dimah-s3-muted-foreground", children: acceptLine })
1364
+ ] })
1365
+ ] });
1366
+ }
1367
+ function DropzoneFrame({
1368
+ getRootProps,
1369
+ getInputProps,
1370
+ isDisabled,
1371
+ isDragReject,
1372
+ isDragAccept,
1373
+ isDragActive,
1374
+ className,
1375
+ hasCustomChrome,
1376
+ ariaLabel,
1377
+ chrome,
1378
+ status
1379
+ }) {
1380
+ return /* @__PURE__ */ jsxs(
1381
+ "div",
1382
+ {
1383
+ ...getRootProps({
1384
+ "aria-label": ariaLabel,
1385
+ className: cn(
1386
+ "rounded-lg border-2 border-dashed transition-colors",
1387
+ hasCustomChrome ? "flex flex-col items-stretch justify-stretch gap-3 p-0" : "flex flex-col items-center justify-center gap-3 p-6 text-center",
1388
+ isDisabled ? "cursor-not-allowed border-dimah-s3-muted-foreground/25" : "cursor-pointer border-dimah-s3-muted-foreground/25 hover:border-dimah-s3-primary/50",
1389
+ !isDisabled && isDragReject && "border-dimah-s3-destructive bg-dimah-s3-destructive/5",
1390
+ !isDisabled && isDragAccept && "border-dimah-s3-primary bg-dimah-s3-primary/5",
1391
+ !isDisabled && isDragActive && !isDragAccept && !isDragReject && "border-dimah-s3-primary/50",
1392
+ className
1393
+ )
1394
+ }),
1395
+ children: [
1396
+ /* @__PURE__ */ jsx("input", { ...getInputProps() }),
1397
+ chrome,
1398
+ status
1399
+ ]
1400
+ }
1401
+ );
1402
+ }
1403
+ function useSingleUploadUi(ctrl, {
1299
1404
  toast: enableToast = true,
1300
1405
  status: statusSlot = true,
1301
- variant = "default",
1302
- size = "default",
1303
- buttonClassName,
1304
1406
  attachmentSize,
1305
1407
  attachmentOrientation,
1306
- ...options
1408
+ canPause
1307
1409
  }) {
1308
- const t = useTranslations();
1309
- const ctrl = useUpload({
1310
- ...options,
1311
- disabled,
1312
- noDrag: true,
1313
- noClick: true,
1314
- noKeyboard: true
1315
- });
1316
- const canPause = options.uploadStore != null && options.uploadStore !== false;
1317
- const isDisabled = disabled || ctrl.isUploading;
1318
1410
  const toastCtrl = {
1319
1411
  mode: "single",
1320
1412
  phase: ctrl.phase,
@@ -1339,26 +1431,56 @@ function UploadButtonSingle({
1339
1431
  orientation: attachmentOrientation
1340
1432
  }
1341
1433
  );
1342
- const status = resolveStatusSlot(statusSlot, statusNode);
1343
- const buttonLabel = label ?? t("Upload file", { note: "button" });
1344
- const buttonContent = children ?? /* @__PURE__ */ jsxs(Fragment, { children: [
1345
- /* @__PURE__ */ jsx(CloudUpload, { "data-icon": "inline-start" }),
1346
- buttonLabel
1347
- ] });
1348
- const button = /* @__PURE__ */ jsx(
1349
- Button,
1434
+ return { statusNode, statusSlot };
1435
+ }
1436
+ function useMultiUploadUi(ctrl, {
1437
+ toast: enableToast = true,
1438
+ status: statusSlot = true,
1439
+ attachmentSize,
1440
+ attachmentOrientation,
1441
+ canPause
1442
+ }) {
1443
+ const toastCtrl = {
1444
+ mode: "multi",
1445
+ phase: ctrl.phase,
1446
+ files: ctrl.files,
1447
+ totalProgress: ctrl.totalProgress,
1448
+ error: ctrl.error,
1449
+ cancel: ctrl.cancel
1450
+ };
1451
+ useUploadToast(toastCtrl, enableToast);
1452
+ useFileRejectToast(ctrl.fileRejections, enableToast);
1453
+ const statusNode = statusSlot === false ? null : /* @__PURE__ */ jsx(
1454
+ UploadStatusBlock,
1350
1455
  {
1351
- variant,
1352
- size,
1353
- className: buttonClassName,
1354
- disabled: isDisabled,
1355
- onClick: ctrl.open,
1356
- children: buttonContent
1456
+ mode: "multi",
1457
+ phase: ctrl.phase,
1458
+ files: ctrl.files,
1459
+ totalProgress: ctrl.totalProgress,
1460
+ error: ctrl.error,
1461
+ onCancel: ctrl.cancel,
1462
+ onPause: canPause ? ctrl.detach : void 0,
1463
+ size: attachmentSize,
1464
+ orientation: attachmentOrientation
1357
1465
  }
1358
1466
  );
1467
+ return { statusNode, statusSlot };
1468
+ }
1469
+ function resolveDefaultStatus(statusSlot, statusNode, wrap) {
1470
+ if (typeof statusSlot === "function") return statusSlot(statusNode);
1471
+ const defaultStatus = statusNode == null ? null : wrap ? wrap(statusNode) : statusNode;
1472
+ return resolveStatusSlot(statusSlot, defaultStatus);
1473
+ }
1474
+ function buttonShell({
1475
+ className,
1476
+ tooltipText,
1477
+ getInputProps,
1478
+ button,
1479
+ status
1480
+ }) {
1359
1481
  return /* @__PURE__ */ jsxs("div", { className: cn("inline-flex flex-col gap-2", className), children: [
1360
1482
  /* @__PURE__ */ jsxs("div", { className: "inline-flex items-center gap-2", children: [
1361
- /* @__PURE__ */ jsx("input", { ...ctrl.getInputProps() }),
1483
+ /* @__PURE__ */ jsx("input", { ...getInputProps() }),
1362
1484
  tooltipText ? /* @__PURE__ */ jsxs(Tooltip, { children: [
1363
1485
  /* @__PURE__ */ jsx(TooltipTrigger, { render: button }),
1364
1486
  /* @__PURE__ */ jsx(TooltipContent, { children: tooltipText })
@@ -1367,6 +1489,61 @@ function UploadButtonSingle({
1367
1489
  status
1368
1490
  ] });
1369
1491
  }
1492
+ function UploadButtonSingle({
1493
+ className,
1494
+ label,
1495
+ children,
1496
+ disabled,
1497
+ tooltipText,
1498
+ toast: enableToast = true,
1499
+ status: statusSlot = true,
1500
+ variant = "default",
1501
+ size = "default",
1502
+ buttonClassName,
1503
+ attachmentSize,
1504
+ attachmentOrientation,
1505
+ ...options
1506
+ }) {
1507
+ const t = useTranslations();
1508
+ const ctrl = useUpload({
1509
+ ...options,
1510
+ disabled,
1511
+ noDrag: true,
1512
+ noClick: true,
1513
+ noKeyboard: true
1514
+ });
1515
+ const isDisabled = disabled || ctrl.isPending;
1516
+ const { statusNode } = useSingleUploadUi(ctrl, {
1517
+ toast: enableToast,
1518
+ status: statusSlot,
1519
+ attachmentSize,
1520
+ attachmentOrientation,
1521
+ canPause: canPauseUpload(options.uploadStore)
1522
+ });
1523
+ const status = resolveStatusSlot(statusSlot, statusNode);
1524
+ const buttonLabel = label ?? t("Upload file", { note: "button" });
1525
+ const buttonContent = children ?? /* @__PURE__ */ jsxs(Fragment, { children: [
1526
+ /* @__PURE__ */ jsx(CloudUpload, { "data-icon": "inline-start" }),
1527
+ buttonLabel
1528
+ ] });
1529
+ return buttonShell({
1530
+ className,
1531
+ tooltipText,
1532
+ getInputProps: ctrl.getInputProps,
1533
+ status,
1534
+ button: /* @__PURE__ */ jsx(
1535
+ Button,
1536
+ {
1537
+ variant,
1538
+ size,
1539
+ className: buttonClassName,
1540
+ disabled: isDisabled,
1541
+ onClick: ctrl.open,
1542
+ children: buttonContent
1543
+ }
1544
+ )
1545
+ });
1546
+ }
1370
1547
  function UploadButtonMulti({
1371
1548
  className,
1372
1549
  label,
@@ -1390,59 +1567,37 @@ function UploadButtonMulti({
1390
1567
  noClick: true,
1391
1568
  noKeyboard: true
1392
1569
  });
1393
- const canPause = options.uploadStore != null && options.uploadStore !== false;
1394
- const isDisabled = disabled || ctrl.isUploading;
1395
- const toastCtrl = {
1396
- mode: "multi",
1397
- phase: ctrl.phase,
1398
- files: ctrl.files,
1399
- totalProgress: ctrl.totalProgress,
1400
- error: ctrl.error,
1401
- cancel: ctrl.cancel
1402
- };
1403
- useUploadToast(toastCtrl, enableToast);
1404
- useFileRejectToast(ctrl.fileRejections, enableToast);
1405
- const statusNode = statusSlot === false ? null : /* @__PURE__ */ jsx(
1406
- UploadStatusBlock,
1407
- {
1408
- mode: "multi",
1409
- phase: ctrl.phase,
1410
- files: ctrl.files ?? EMPTY_FILES,
1411
- totalProgress: ctrl.totalProgress ?? EMPTY_PROGRESS,
1412
- error: ctrl.error,
1413
- onCancel: ctrl.cancel,
1414
- onPause: canPause ? ctrl.detach : void 0,
1415
- size: attachmentSize,
1416
- orientation: attachmentOrientation
1417
- }
1418
- );
1570
+ const isDisabled = disabled || ctrl.isPending;
1571
+ const { statusNode } = useMultiUploadUi(ctrl, {
1572
+ toast: enableToast,
1573
+ status: statusSlot,
1574
+ attachmentSize,
1575
+ attachmentOrientation,
1576
+ canPause: canPauseUpload(options.uploadStore)
1577
+ });
1419
1578
  const status = resolveStatusSlot(statusSlot, statusNode);
1420
1579
  const buttonLabel = label ?? t("Upload files", { note: "button" });
1421
1580
  const buttonContent = children ?? /* @__PURE__ */ jsxs(Fragment, { children: [
1422
1581
  /* @__PURE__ */ jsx(CloudUpload, { "data-icon": "inline-start" }),
1423
1582
  buttonLabel
1424
1583
  ] });
1425
- const button = /* @__PURE__ */ jsx(
1426
- Button,
1427
- {
1428
- variant,
1429
- size,
1430
- className: buttonClassName,
1431
- disabled: isDisabled,
1432
- onClick: ctrl.open,
1433
- children: buttonContent
1434
- }
1435
- );
1436
- return /* @__PURE__ */ jsxs("div", { className: cn("inline-flex flex-col gap-2", className), children: [
1437
- /* @__PURE__ */ jsxs("div", { className: "inline-flex items-center gap-2", children: [
1438
- /* @__PURE__ */ jsx("input", { ...ctrl.getInputProps() }),
1439
- tooltipText ? /* @__PURE__ */ jsxs(Tooltip, { children: [
1440
- /* @__PURE__ */ jsx(TooltipTrigger, { render: button }),
1441
- /* @__PURE__ */ jsx(TooltipContent, { children: tooltipText })
1442
- ] }) : button
1443
- ] }),
1444
- status
1445
- ] });
1584
+ return buttonShell({
1585
+ className,
1586
+ tooltipText,
1587
+ getInputProps: ctrl.getInputProps,
1588
+ status,
1589
+ button: /* @__PURE__ */ jsx(
1590
+ Button,
1591
+ {
1592
+ variant,
1593
+ size,
1594
+ className: buttonClassName,
1595
+ disabled: isDisabled,
1596
+ onClick: ctrl.open,
1597
+ children: buttonContent
1598
+ }
1599
+ )
1600
+ });
1446
1601
  }
1447
1602
  function UploadButton({ multiple, ...props }) {
1448
1603
  const isMulti = multiple === true || (props.maxFiles ?? 1) > 1;
@@ -1456,62 +1611,6 @@ function UploadButton({ multiple, ...props }) {
1456
1611
  }
1457
1612
  return /* @__PURE__ */ jsx(UploadButtonSingle, { ...props });
1458
1613
  }
1459
- var EMPTY_PROGRESS2 = { loaded: 0, total: 0, percent: 0 };
1460
- var EMPTY_FILES2 = [];
1461
- function dropzoneHints(options, t) {
1462
- const acceptLabels = formatAcceptLabels(options.accept);
1463
- const maxFiles = options.maxFiles;
1464
- const limitParts = [];
1465
- if (maxFiles != null && maxFiles > 0) {
1466
- limitParts.push(
1467
- maxFiles === 1 ? t("You can upload a file", { note: "dropzone hint" }) : t("You can upload {count} files", {
1468
- note: "dropzone hint",
1469
- variables: { count: String(maxFiles) }
1470
- })
1471
- );
1472
- }
1473
- if (options.maxFileSize != null) {
1474
- const size = formatFileSize(options.maxFileSize);
1475
- limitParts.push(
1476
- maxFiles != null && maxFiles > 1 ? t("Each up to {size}", {
1477
- note: "dropzone hint",
1478
- variables: { size }
1479
- }) : t("Up to {size}", {
1480
- note: "dropzone hint",
1481
- variables: { size }
1482
- })
1483
- );
1484
- }
1485
- const limitsLine = limitParts.length > 0 ? `${limitParts.join(". ")}.` : null;
1486
- const acceptLine = acceptLabels.length > 0 ? t("Accepted {types}.", {
1487
- note: "dropzone hint",
1488
- variables: { types: acceptLabels.join(", ") }
1489
- }) : null;
1490
- return { limitsLine, acceptLine };
1491
- }
1492
- function DropzoneChrome({
1493
- label,
1494
- limitsLine,
1495
- acceptLine,
1496
- isDisabled,
1497
- children
1498
- }) {
1499
- if (children != null) return /* @__PURE__ */ jsx(Fragment, { children });
1500
- return /* @__PURE__ */ jsxs(Fragment, { children: [
1501
- /* @__PURE__ */ jsx(
1502
- CloudUpload,
1503
- {
1504
- className: cn("size-6", isDisabled && "opacity-50"),
1505
- strokeWidth: 1.5
1506
- }
1507
- ),
1508
- /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col gap-1", isDisabled && "opacity-50"), children: [
1509
- /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: label }),
1510
- limitsLine && /* @__PURE__ */ jsx("p", { className: "text-xs text-dimah-s3-muted-foreground", children: limitsLine }),
1511
- acceptLine && /* @__PURE__ */ jsx("p", { className: "text-xs text-dimah-s3-muted-foreground", children: acceptLine })
1512
- ] })
1513
- ] });
1514
- }
1515
1614
  function UploadDropzoneSingle({
1516
1615
  className,
1517
1616
  label,
@@ -1525,66 +1624,47 @@ function UploadDropzoneSingle({
1525
1624
  }) {
1526
1625
  const t = useTranslations();
1527
1626
  const ctrl = useUpload({ ...options, disabled });
1528
- const canPause = options.uploadStore != null && options.uploadStore !== false;
1529
- const isDisabled = disabled || ctrl.isUploading;
1627
+ const isDisabled = disabled || ctrl.isPending;
1530
1628
  const hasCustomChrome = children != null;
1531
1629
  const dropzoneLabel = label ?? t("Drag and drop files here", { note: "dropzone" });
1532
1630
  const { limitsLine, acceptLine } = dropzoneHints(options, t);
1533
- const toastCtrl = {
1534
- mode: "single",
1535
- phase: ctrl.phase,
1536
- fileInfo: ctrl.fileInfo,
1537
- progress: ctrl.progress,
1538
- error: ctrl.error,
1539
- cancel: ctrl.cancel
1540
- };
1541
- useUploadToast(toastCtrl, enableToast);
1542
- useFileRejectToast(ctrl.fileRejections, enableToast);
1543
- const statusNode = statusSlot === false ? null : /* @__PURE__ */ jsx(
1544
- UploadStatusBlock,
1631
+ const { statusNode } = useSingleUploadUi(ctrl, {
1632
+ toast: enableToast,
1633
+ status: statusSlot,
1634
+ attachmentSize,
1635
+ attachmentOrientation,
1636
+ canPause: canPauseUpload(options.uploadStore)
1637
+ });
1638
+ const status = resolveDefaultStatus(statusSlot, statusNode, (node) => /* @__PURE__ */ jsx(
1639
+ "div",
1545
1640
  {
1546
- mode: "single",
1547
- phase: ctrl.phase,
1548
- progress: ctrl.progress,
1549
- error: ctrl.error,
1550
- fileInfo: ctrl.fileInfo,
1551
- onCancel: ctrl.cancel,
1552
- onPause: canPause ? ctrl.detach : void 0,
1553
- size: attachmentSize,
1554
- orientation: attachmentOrientation
1641
+ className: hasCustomChrome ? "w-full px-3 pb-3 text-start" : "w-full text-start",
1642
+ children: node
1555
1643
  }
1556
- );
1557
- const defaultStatus = statusNode == null ? null : /* @__PURE__ */ jsx("div", { className: cn("w-full text-start", hasCustomChrome && "px-3 pb-3"), children: statusNode });
1558
- const status = typeof statusSlot === "function" ? statusSlot(statusNode) : resolveStatusSlot(statusSlot, defaultStatus);
1559
- return /* @__PURE__ */ jsxs(
1560
- "div",
1644
+ ));
1645
+ return /* @__PURE__ */ jsx(
1646
+ DropzoneFrame,
1561
1647
  {
1562
- ...ctrl.getRootProps({
1563
- "aria-label": dropzoneLabel,
1564
- className: cn(
1565
- "rounded-lg border-2 border-dashed transition-colors",
1566
- hasCustomChrome ? "flex flex-col items-stretch justify-stretch gap-3 p-0" : "flex flex-col items-center justify-center gap-3 p-6 text-center",
1567
- isDisabled ? "cursor-not-allowed border-dimah-s3-muted-foreground/25" : "cursor-pointer border-dimah-s3-muted-foreground/25 hover:border-dimah-s3-primary/50",
1568
- !isDisabled && ctrl.isDragReject && "border-dimah-s3-destructive bg-dimah-s3-destructive/5",
1569
- !isDisabled && ctrl.isDragAccept && "border-dimah-s3-primary bg-dimah-s3-primary/5",
1570
- !isDisabled && ctrl.isDragActive && !ctrl.isDragAccept && !ctrl.isDragReject && "border-dimah-s3-primary/50",
1571
- className
1572
- )
1573
- }),
1574
- children: [
1575
- /* @__PURE__ */ jsx("input", { ...ctrl.getInputProps() }),
1576
- /* @__PURE__ */ jsx(
1577
- DropzoneChrome,
1578
- {
1579
- label: dropzoneLabel,
1580
- limitsLine,
1581
- acceptLine,
1582
- isDisabled,
1583
- children
1584
- }
1585
- ),
1586
- status
1587
- ]
1648
+ getRootProps: ctrl.getRootProps,
1649
+ getInputProps: ctrl.getInputProps,
1650
+ isDisabled,
1651
+ isDragReject: ctrl.isDragReject,
1652
+ isDragAccept: ctrl.isDragAccept,
1653
+ isDragActive: ctrl.isDragActive,
1654
+ className,
1655
+ hasCustomChrome,
1656
+ ariaLabel: dropzoneLabel,
1657
+ chrome: /* @__PURE__ */ jsx(
1658
+ DropzoneChrome,
1659
+ {
1660
+ label: dropzoneLabel,
1661
+ limitsLine,
1662
+ acceptLine,
1663
+ isDisabled,
1664
+ children
1665
+ }
1666
+ ),
1667
+ status
1588
1668
  }
1589
1669
  );
1590
1670
  }
@@ -1601,66 +1681,47 @@ function UploadDropzoneMulti({
1601
1681
  }) {
1602
1682
  const t = useTranslations();
1603
1683
  const ctrl = useMultiUpload({ ...options, disabled });
1604
- const canPause = options.uploadStore != null && options.uploadStore !== false;
1605
- const isDisabled = disabled || ctrl.isUploading;
1684
+ const isDisabled = disabled || ctrl.isPending;
1606
1685
  const hasCustomChrome = children != null;
1607
1686
  const dropzoneLabel = label ?? t("Drag and drop files here", { note: "dropzone" });
1608
1687
  const { limitsLine, acceptLine } = dropzoneHints(options, t);
1609
- const toastCtrl = {
1610
- mode: "multi",
1611
- phase: ctrl.phase,
1612
- files: ctrl.files,
1613
- totalProgress: ctrl.totalProgress,
1614
- error: ctrl.error,
1615
- cancel: ctrl.cancel
1616
- };
1617
- useUploadToast(toastCtrl, enableToast);
1618
- useFileRejectToast(ctrl.fileRejections, enableToast);
1619
- const statusNode = statusSlot === false ? null : /* @__PURE__ */ jsx(
1620
- UploadStatusBlock,
1688
+ const { statusNode } = useMultiUploadUi(ctrl, {
1689
+ toast: enableToast,
1690
+ status: statusSlot,
1691
+ attachmentSize,
1692
+ attachmentOrientation,
1693
+ canPause: canPauseUpload(options.uploadStore)
1694
+ });
1695
+ const status = resolveDefaultStatus(statusSlot, statusNode, (node) => /* @__PURE__ */ jsx(
1696
+ "div",
1621
1697
  {
1622
- mode: "multi",
1623
- phase: ctrl.phase,
1624
- files: ctrl.files ?? EMPTY_FILES2,
1625
- totalProgress: ctrl.totalProgress ?? EMPTY_PROGRESS2,
1626
- error: ctrl.error,
1627
- onCancel: ctrl.cancel,
1628
- onPause: canPause ? ctrl.detach : void 0,
1629
- size: attachmentSize,
1630
- orientation: attachmentOrientation
1698
+ className: hasCustomChrome ? "w-full px-3 pb-3 text-start" : "w-full text-start",
1699
+ children: node
1631
1700
  }
1632
- );
1633
- const defaultStatus = statusNode == null ? null : /* @__PURE__ */ jsx("div", { className: cn("w-full text-start", hasCustomChrome && "px-3 pb-3"), children: statusNode });
1634
- const status = typeof statusSlot === "function" ? statusSlot(statusNode) : resolveStatusSlot(statusSlot, defaultStatus);
1635
- return /* @__PURE__ */ jsxs(
1636
- "div",
1701
+ ));
1702
+ return /* @__PURE__ */ jsx(
1703
+ DropzoneFrame,
1637
1704
  {
1638
- ...ctrl.getRootProps({
1639
- "aria-label": dropzoneLabel,
1640
- className: cn(
1641
- "rounded-lg border-2 border-dashed transition-colors",
1642
- hasCustomChrome ? "flex flex-col items-stretch justify-stretch gap-3 p-0" : "flex flex-col items-center justify-center gap-3 p-6 text-center",
1643
- isDisabled ? "cursor-not-allowed border-dimah-s3-muted-foreground/25" : "cursor-pointer border-dimah-s3-muted-foreground/25 hover:border-dimah-s3-primary/50",
1644
- !isDisabled && ctrl.isDragReject && "border-dimah-s3-destructive bg-dimah-s3-destructive/5",
1645
- !isDisabled && ctrl.isDragAccept && "border-dimah-s3-primary bg-dimah-s3-primary/5",
1646
- !isDisabled && ctrl.isDragActive && !ctrl.isDragAccept && !ctrl.isDragReject && "border-dimah-s3-primary/50",
1647
- className
1648
- )
1649
- }),
1650
- children: [
1651
- /* @__PURE__ */ jsx("input", { ...ctrl.getInputProps() }),
1652
- /* @__PURE__ */ jsx(
1653
- DropzoneChrome,
1654
- {
1655
- label: dropzoneLabel,
1656
- limitsLine,
1657
- acceptLine,
1658
- isDisabled,
1659
- children
1660
- }
1661
- ),
1662
- status
1663
- ]
1705
+ getRootProps: ctrl.getRootProps,
1706
+ getInputProps: ctrl.getInputProps,
1707
+ isDisabled,
1708
+ isDragReject: ctrl.isDragReject,
1709
+ isDragAccept: ctrl.isDragAccept,
1710
+ isDragActive: ctrl.isDragActive,
1711
+ className,
1712
+ hasCustomChrome,
1713
+ ariaLabel: dropzoneLabel,
1714
+ chrome: /* @__PURE__ */ jsx(
1715
+ DropzoneChrome,
1716
+ {
1717
+ label: dropzoneLabel,
1718
+ limitsLine,
1719
+ acceptLine,
1720
+ isDisabled,
1721
+ children
1722
+ }
1723
+ ),
1724
+ status
1664
1725
  }
1665
1726
  );
1666
1727
  }
@@ -1685,12 +1746,24 @@ function useDownloadToast({
1685
1746
  enabled = true,
1686
1747
  objectKey,
1687
1748
  fileName,
1688
- fileSize
1749
+ fileSize,
1750
+ cancel
1689
1751
  }) {
1690
1752
  const t = useTranslations();
1691
1753
  const formatDimahError = useFormatDimahError();
1692
1754
  const displayName = fileName ?? objectKey.split("/").pop() ?? objectKey;
1755
+ const toastIdRef = useRef(null);
1756
+ const cancelRef = useRef(cancel);
1757
+ useLayoutEffect(() => {
1758
+ cancelRef.current = cancel;
1759
+ });
1693
1760
  const errorNode = (error) => /* @__PURE__ */ jsx("span", { dir: "auto", className: "block [overflow-wrap:anywhere]", children: formatDimahError(error) });
1761
+ const closeLoading = () => {
1762
+ if (toastIdRef.current) {
1763
+ toast.close(toastIdRef.current);
1764
+ toastIdRef.current = null;
1765
+ }
1766
+ };
1694
1767
  const onInitiated = () => {
1695
1768
  if (enabled) {
1696
1769
  toast.add({
@@ -1699,9 +1772,39 @@ function useDownloadToast({
1699
1772
  });
1700
1773
  }
1701
1774
  };
1775
+ const onDownloadStart = () => {
1776
+ if (!enabled) return;
1777
+ closeLoading();
1778
+ toastIdRef.current = toast.add({
1779
+ type: "loading",
1780
+ timeout: 0,
1781
+ title: t("Downloading", { note: "toast" }),
1782
+ description: /* @__PURE__ */ jsx("span", { dir: "auto", children: /* @__PURE__ */ jsx("bdi", { children: truncateFileName(displayName) }) }),
1783
+ actionProps: {
1784
+ children: t("Cancel", { note: "toast action" }),
1785
+ onClick: () => cancelRef.current?.()
1786
+ }
1787
+ });
1788
+ };
1789
+ const onProgress = (_key, progress) => {
1790
+ if (!enabled || !toastIdRef.current) return;
1791
+ toast.update(toastIdRef.current, {
1792
+ type: "loading",
1793
+ timeout: 0,
1794
+ title: t("Downloading", { note: "toast" }),
1795
+ description: /* @__PURE__ */ jsxs("span", { dir: "ltr", className: "inline-block whitespace-nowrap tabular-nums", children: [
1796
+ formatFileSize(progress.loaded),
1797
+ progress.total > 0 ? ` / ${formatFileSize(progress.total)}` : null
1798
+ ] }),
1799
+ actionProps: {
1800
+ children: t("Cancel", { note: "toast action" }),
1801
+ onClick: () => cancelRef.current?.()
1802
+ }
1803
+ });
1804
+ };
1702
1805
  const onSuccess = (_key, actualFileName) => {
1703
1806
  if (!enabled) return;
1704
- toast.close(`dl-${objectKey}`);
1807
+ closeLoading();
1705
1808
  toast.add({
1706
1809
  type: "success",
1707
1810
  title: t("Download complete", { note: "toast" }),
@@ -1723,18 +1826,9 @@ function useDownloadToast({
1723
1826
  ] })
1724
1827
  });
1725
1828
  };
1726
- const onError = (_key, error) => {
1727
- if (!enabled) return;
1728
- toast.close(`dl-${objectKey}`);
1729
- toast.add({
1730
- type: "error",
1731
- title: t("Download failed", { note: "toast" }),
1732
- description: errorNode(error)
1733
- });
1734
- };
1735
- const onErrorWithPhase = (_key, error, _phase) => {
1829
+ const onError = (_key, error, _phase) => {
1736
1830
  if (!enabled) return;
1737
- toast.close(`dl-${objectKey}`);
1831
+ closeLoading();
1738
1832
  toast.add({
1739
1833
  type: "error",
1740
1834
  title: t("Download failed", { note: "toast" }),
@@ -1743,30 +1837,42 @@ function useDownloadToast({
1743
1837
  };
1744
1838
  const onCancel = (_key) => {
1745
1839
  if (!enabled) return;
1746
- toast.close(`dl-${objectKey}`);
1840
+ closeLoading();
1747
1841
  toast.add({
1748
1842
  type: "info",
1749
1843
  title: t("Download cancelled", { note: "toast" }),
1750
1844
  description: /* @__PURE__ */ jsx("span", { dir: "auto", children: truncateFileName(displayName) })
1751
1845
  });
1752
1846
  };
1753
- return { onInitiated, onSuccess, onError, onErrorWithPhase, onCancel };
1847
+ return {
1848
+ onInitiated,
1849
+ onDownloadStart,
1850
+ onProgress,
1851
+ onSuccess,
1852
+ onError,
1853
+ onCancel
1854
+ };
1754
1855
  }
1755
1856
  function DownloadButton({
1756
1857
  api,
1757
1858
  objectKey,
1758
1859
  fileName,
1860
+ bucket,
1759
1861
  label,
1760
1862
  children,
1761
1863
  className,
1762
1864
  disabled,
1865
+ tooltipText,
1763
1866
  toast: enableToast = true,
1764
1867
  status: statusSlot = true,
1765
1868
  variant = "outline",
1766
1869
  size = "default",
1767
1870
  buttonClassName,
1768
1871
  attachmentSize,
1769
- attachmentOrientation
1872
+ attachmentOrientation,
1873
+ beforeDownload,
1874
+ onInitiated,
1875
+ onError
1770
1876
  }) {
1771
1877
  const t = useTranslations();
1772
1878
  const formatError = useFormatDimahError();
@@ -1777,11 +1883,15 @@ function DownloadButton({
1777
1883
  });
1778
1884
  const dl = useDownload({
1779
1885
  api,
1780
- onInitiated: () => {
1886
+ bucket,
1887
+ beforeDownload,
1888
+ onInitiated: (key) => {
1781
1889
  toastHandlers.onInitiated();
1890
+ onInitiated?.(key);
1782
1891
  },
1783
1892
  onError: (key, error) => {
1784
1893
  toastHandlers.onError(key, error);
1894
+ onError?.(key, error);
1785
1895
  }
1786
1896
  });
1787
1897
  const isPending = dl.phase === "presigning";
@@ -1789,6 +1899,17 @@ function DownloadButton({
1789
1899
  isPending ? /* @__PURE__ */ jsx(LoaderIcon, { className: "animate-spin", "data-icon": "inline-start" }) : /* @__PURE__ */ jsx(DownloadIcon, { "data-icon": "inline-start" }),
1790
1900
  label ?? t("Download", { note: "button" })
1791
1901
  ] });
1902
+ const button = /* @__PURE__ */ jsx(
1903
+ Button,
1904
+ {
1905
+ variant,
1906
+ size,
1907
+ className: buttonClassName,
1908
+ disabled: disabled || isPending,
1909
+ onClick: () => dl.download(objectKey, fileName),
1910
+ children: buttonContent
1911
+ }
1912
+ );
1792
1913
  const statusNode = dl.phase === "error" ? /* @__PURE__ */ jsx(
1793
1914
  StatusAttachment,
1794
1915
  {
@@ -1800,17 +1921,10 @@ function DownloadButton({
1800
1921
  }
1801
1922
  ) : null;
1802
1923
  return /* @__PURE__ */ jsxs("div", { className: cn("inline-flex flex-col gap-2", className), children: [
1803
- /* @__PURE__ */ jsx(
1804
- Button,
1805
- {
1806
- variant,
1807
- size,
1808
- className: buttonClassName,
1809
- disabled: disabled || isPending,
1810
- onClick: () => dl.download(objectKey, fileName),
1811
- children: buttonContent
1812
- }
1813
- ),
1924
+ tooltipText ? /* @__PURE__ */ jsxs(Tooltip, { children: [
1925
+ /* @__PURE__ */ jsx(TooltipTrigger, { render: button }),
1926
+ /* @__PURE__ */ jsx(TooltipContent, { children: tooltipText })
1927
+ ] }) : button,
1814
1928
  resolveStatusSlot(statusSlot, statusNode)
1815
1929
  ] });
1816
1930
  }
@@ -1843,25 +1957,33 @@ function ProgressDownloadButton({
1843
1957
  }) {
1844
1958
  const t = useTranslations();
1845
1959
  const formatError = useFormatDimahError();
1960
+ const dlRef = useRef(null);
1846
1961
  const toastHandlers = useDownloadToast({
1847
1962
  enabled: enableToast,
1848
1963
  objectKey,
1849
1964
  fileName,
1850
- fileSize
1965
+ fileSize,
1966
+ cancel: () => dlRef.current?.cancel()
1851
1967
  });
1852
1968
  const dl = useDownload({
1853
1969
  mode: "fetch",
1854
1970
  api,
1855
1971
  bucket,
1856
1972
  beforeDownload,
1857
- onDownloadStart,
1858
- onProgress,
1973
+ onDownloadStart: (key) => {
1974
+ toastHandlers.onDownloadStart();
1975
+ onDownloadStart?.(key);
1976
+ },
1977
+ onProgress: (key, progress) => {
1978
+ toastHandlers.onProgress(key, progress);
1979
+ onProgress?.(key, progress);
1980
+ },
1859
1981
  onSuccess: (key, actualFileName) => {
1860
1982
  toastHandlers.onSuccess(key, actualFileName);
1861
1983
  onSuccess?.(key, actualFileName);
1862
1984
  },
1863
1985
  onError: (key, error, phase) => {
1864
- toastHandlers.onErrorWithPhase(key, error, phase);
1986
+ toastHandlers.onError(key, error, phase);
1865
1987
  onError?.(key, error, phase);
1866
1988
  },
1867
1989
  onCancel: (key) => {
@@ -1869,6 +1991,9 @@ function ProgressDownloadButton({
1869
1991
  onCancel?.(key);
1870
1992
  }
1871
1993
  });
1994
+ useLayoutEffect(() => {
1995
+ dlRef.current = dl;
1996
+ });
1872
1997
  const isDownloading = dl.phase === "downloading" || dl.phase === "presigning";
1873
1998
  const handleClick = () => {
1874
1999
  if (isDownloading) {