@livo-tv/blocks 1.6.0 → 1.7.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.d.cts CHANGED
@@ -976,6 +976,7 @@ type CreateMultipartUploadControllerOptions = {
976
976
  adapter: MultipartUploadAdapter;
977
977
  onProgress?: (progress: UploadProgress) => void;
978
978
  xhrFactory?: () => XMLHttpRequest;
979
+ sleep?: (ms: number) => Promise<void>;
979
980
  };
980
981
  type MultipartUploadController = {
981
982
  start: () => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -976,6 +976,7 @@ type CreateMultipartUploadControllerOptions = {
976
976
  adapter: MultipartUploadAdapter;
977
977
  onProgress?: (progress: UploadProgress) => void;
978
978
  xhrFactory?: () => XMLHttpRequest;
979
+ sleep?: (ms: number) => Promise<void>;
979
980
  };
980
981
  type MultipartUploadController = {
981
982
  start: () => Promise<void>;
package/dist/index.js CHANGED
@@ -4757,79 +4757,85 @@ function FileDropzone({
4757
4757
  inputRef.current?.click();
4758
4758
  }
4759
4759
  }
4760
- return /* @__PURE__ */ jsxs("div", { className: cn("grid gap-2", className), children: [
4761
- /* @__PURE__ */ jsxs(
4762
- "div",
4763
- {
4764
- role: "button",
4765
- tabIndex: disabled ? -1 : 0,
4766
- "aria-disabled": disabled,
4767
- "aria-describedby": message ? `${inputId}-message` : `${inputId}-hint`,
4768
- "aria-controls": inputId,
4769
- "data-dragging": dragging || void 0,
4770
- className: cn(
4771
- "border-input bg-background focus-visible:border-ring focus-visible:ring-ring/50 rounded-lg border border-dashed px-4 py-8 text-center outline-none focus-visible:ring-[3px]",
4772
- dragging && "border-primary bg-primary/5",
4773
- disabled && "pointer-events-none opacity-50"
4774
- ),
4775
- onClick: () => {
4776
- if (!disabled) inputRef.current?.click();
4777
- },
4778
- onKeyDown,
4779
- onDragEnter: (event) => {
4780
- event.preventDefault();
4781
- if (!disabled) setDragging(true);
4782
- },
4783
- onDragOver: (event) => {
4784
- event.preventDefault();
4785
- },
4786
- onDragLeave: (event) => {
4787
- if (event.currentTarget.contains(event.relatedTarget)) {
4788
- return;
4760
+ return /* @__PURE__ */ jsxs(
4761
+ "div",
4762
+ {
4763
+ className: cn("relative flex w-full min-w-0 flex-col gap-2", className),
4764
+ children: [
4765
+ /* @__PURE__ */ jsxs(
4766
+ "div",
4767
+ {
4768
+ role: "button",
4769
+ tabIndex: disabled ? -1 : 0,
4770
+ "aria-disabled": disabled,
4771
+ "aria-describedby": message ? `${inputId}-message` : `${inputId}-hint`,
4772
+ "aria-controls": inputId,
4773
+ "data-dragging": dragging || void 0,
4774
+ className: cn(
4775
+ "border-input bg-background focus-visible:border-ring focus-visible:ring-ring/50 w-full min-w-0 rounded-lg border border-dashed px-4 py-8 text-center outline-none focus-visible:ring-[3px]",
4776
+ dragging && "border-primary bg-primary/5",
4777
+ disabled && "pointer-events-none opacity-50"
4778
+ ),
4779
+ onClick: () => {
4780
+ if (!disabled) inputRef.current?.click();
4781
+ },
4782
+ onKeyDown,
4783
+ onDragEnter: (event) => {
4784
+ event.preventDefault();
4785
+ if (!disabled) setDragging(true);
4786
+ },
4787
+ onDragOver: (event) => {
4788
+ event.preventDefault();
4789
+ },
4790
+ onDragLeave: (event) => {
4791
+ if (event.currentTarget.contains(event.relatedTarget)) {
4792
+ return;
4793
+ }
4794
+ setDragging(false);
4795
+ },
4796
+ onDrop,
4797
+ children: [
4798
+ /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: labels.idle }),
4799
+ /* @__PURE__ */ jsx(
4800
+ "p",
4801
+ {
4802
+ id: `${inputId}-hint`,
4803
+ className: "text-muted-foreground mt-1 text-xs",
4804
+ children: labels.hint
4805
+ }
4806
+ ),
4807
+ /* @__PURE__ */ jsx("span", { className: "text-primary mt-3 inline-flex text-sm font-medium underline-offset-4", children: labels.browse })
4808
+ ]
4789
4809
  }
4790
- setDragging(false);
4791
- },
4792
- onDrop,
4793
- children: [
4794
- /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: labels.idle }),
4795
- /* @__PURE__ */ jsx(
4796
- "p",
4797
- {
4798
- id: `${inputId}-hint`,
4799
- className: "text-muted-foreground mt-1 text-xs",
4800
- children: labels.hint
4810
+ ),
4811
+ /* @__PURE__ */ jsx(
4812
+ "input",
4813
+ {
4814
+ id: inputId,
4815
+ ref: inputRef,
4816
+ type: "file",
4817
+ accept,
4818
+ disabled,
4819
+ "aria-label": labels.browse,
4820
+ className: "sr-only",
4821
+ onChange: (event) => {
4822
+ takeFile(event.target.files?.[0]);
4823
+ event.target.value = "";
4801
4824
  }
4802
- ),
4803
- /* @__PURE__ */ jsx("span", { className: "text-primary mt-3 inline-flex text-sm font-medium underline-offset-4", children: labels.browse })
4804
- ]
4805
- }
4806
- ),
4807
- /* @__PURE__ */ jsx(
4808
- "input",
4809
- {
4810
- id: inputId,
4811
- ref: inputRef,
4812
- type: "file",
4813
- accept,
4814
- disabled,
4815
- "aria-label": labels.browse,
4816
- className: "sr-only",
4817
- onChange: (event) => {
4818
- takeFile(event.target.files?.[0]);
4819
- event.target.value = "";
4820
- }
4821
- }
4822
- ),
4823
- message ? /* @__PURE__ */ jsx(
4824
- "p",
4825
- {
4826
- id: `${inputId}-message`,
4827
- role: "alert",
4828
- className: "text-destructive text-sm",
4829
- children: message
4830
- }
4831
- ) : null
4832
- ] });
4825
+ }
4826
+ ),
4827
+ message ? /* @__PURE__ */ jsx(
4828
+ "p",
4829
+ {
4830
+ id: `${inputId}-message`,
4831
+ role: "alert",
4832
+ className: "text-destructive text-sm",
4833
+ children: message
4834
+ }
4835
+ ) : null
4836
+ ]
4837
+ }
4838
+ );
4833
4839
  }
4834
4840
  function statusLabel(status, labels) {
4835
4841
  switch (status) {
@@ -4862,73 +4868,90 @@ function FileUploadTray({
4862
4868
  const labels = { ...DEFAULT_FILE_UPLOAD_TRAY_LABELS, ...labelOverrides };
4863
4869
  const visible = jobs.filter((job) => job.status !== "cancelled");
4864
4870
  if (visible.length === 0) return null;
4865
- return /* @__PURE__ */ jsx("section", { "aria-label": labels.title, className: cn("grid gap-3", className), children: /* @__PURE__ */ jsx("ul", { className: "grid gap-3", children: visible.map((job) => {
4866
- const active = job.status === "uploading" || job.status === "completing";
4867
- const canPause = job.status === "uploading";
4868
- const canResume = job.status === "paused";
4869
- const canCancel = job.status === "uploading" || job.status === "paused" || job.status === "error";
4870
- return /* @__PURE__ */ jsxs("li", { className: "grid gap-1.5", children: [
4871
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
4872
- /* @__PURE__ */ jsx("p", { className: "min-w-0 flex-1 truncate text-sm font-medium", children: job.name }),
4873
- /* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: statusLabel(job.status, labels) }),
4874
- canPause ? /* @__PURE__ */ jsx(
4875
- Button,
4876
- {
4877
- type: "button",
4878
- variant: "ghost",
4879
- size: "icon-sm",
4880
- "aria-label": `${labels.pause} ${job.name}`,
4881
- onClick: () => onPause(job.id),
4882
- children: /* @__PURE__ */ jsx(Pause, {})
4883
- }
4884
- ) : null,
4885
- canResume ? /* @__PURE__ */ jsx(
4886
- Button,
4887
- {
4888
- type: "button",
4889
- variant: "ghost",
4890
- size: "icon-sm",
4891
- "aria-label": `${labels.resume} ${job.name}`,
4892
- onClick: () => onResume(job.id),
4893
- children: /* @__PURE__ */ jsx(Play, {})
4894
- }
4895
- ) : null,
4896
- canCancel ? /* @__PURE__ */ jsx(
4897
- Button,
4898
- {
4899
- type: "button",
4900
- variant: "ghost",
4901
- size: "icon-sm",
4902
- "aria-label": `${labels.cancel} ${job.name}`,
4903
- onClick: () => onCancel(job.id),
4904
- children: /* @__PURE__ */ jsx(X, {})
4905
- }
4906
- ) : null
4907
- ] }),
4908
- /* @__PURE__ */ jsx(
4909
- "div",
4910
- {
4911
- role: "progressbar",
4912
- "aria-valuemin": 0,
4913
- "aria-valuemax": 100,
4914
- "aria-valuenow": job.percent,
4915
- "aria-label": job.name,
4916
- className: "bg-muted h-2 overflow-hidden rounded-full",
4917
- children: /* @__PURE__ */ jsx(
4871
+ return /* @__PURE__ */ jsx(
4872
+ "section",
4873
+ {
4874
+ "aria-label": labels.title,
4875
+ className: cn("grid min-w-0 gap-3", className),
4876
+ children: /* @__PURE__ */ jsx("ul", { className: "grid min-w-0 gap-3", children: visible.map((job) => {
4877
+ const active = job.status === "uploading" || job.status === "completing";
4878
+ const canPause = job.status === "uploading";
4879
+ const canResume = job.status === "paused";
4880
+ const canCancel = job.status === "uploading" || job.status === "paused" || job.status === "error";
4881
+ return /* @__PURE__ */ jsxs("li", { className: "grid min-w-0 gap-1.5", children: [
4882
+ /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [
4883
+ /* @__PURE__ */ jsx(
4884
+ "p",
4885
+ {
4886
+ className: "min-w-0 flex-1 truncate text-sm font-medium",
4887
+ title: job.name,
4888
+ children: job.name
4889
+ }
4890
+ ),
4891
+ /* @__PURE__ */ jsx("p", { className: "text-muted-foreground shrink-0 text-xs", children: statusLabel(job.status, labels) }),
4892
+ canPause ? /* @__PURE__ */ jsx(
4893
+ Button,
4894
+ {
4895
+ type: "button",
4896
+ variant: "ghost",
4897
+ size: "icon-sm",
4898
+ className: "shrink-0",
4899
+ "aria-label": `${labels.pause} ${job.name}`,
4900
+ onClick: () => onPause(job.id),
4901
+ children: /* @__PURE__ */ jsx(Pause, {})
4902
+ }
4903
+ ) : null,
4904
+ canResume ? /* @__PURE__ */ jsx(
4905
+ Button,
4906
+ {
4907
+ type: "button",
4908
+ variant: "ghost",
4909
+ size: "icon-sm",
4910
+ className: "shrink-0",
4911
+ "aria-label": `${labels.resume} ${job.name}`,
4912
+ onClick: () => onResume(job.id),
4913
+ children: /* @__PURE__ */ jsx(Play, {})
4914
+ }
4915
+ ) : null,
4916
+ canCancel ? /* @__PURE__ */ jsx(
4917
+ Button,
4918
+ {
4919
+ type: "button",
4920
+ variant: "ghost",
4921
+ size: "icon-sm",
4922
+ className: "shrink-0",
4923
+ "aria-label": `${labels.cancel} ${job.name}`,
4924
+ onClick: () => onCancel(job.id),
4925
+ children: /* @__PURE__ */ jsx(X, {})
4926
+ }
4927
+ ) : null
4928
+ ] }),
4929
+ /* @__PURE__ */ jsx(
4918
4930
  "div",
4919
4931
  {
4920
- className: cn(
4921
- "bg-primary h-full",
4922
- active && "motion-safe:transition-[width]"
4923
- ),
4924
- style: { width: `${job.percent}%` }
4932
+ role: "progressbar",
4933
+ "aria-valuemin": 0,
4934
+ "aria-valuemax": 100,
4935
+ "aria-valuenow": job.percent,
4936
+ "aria-label": job.name,
4937
+ className: "bg-muted h-2 overflow-hidden rounded-full",
4938
+ children: /* @__PURE__ */ jsx(
4939
+ "div",
4940
+ {
4941
+ className: cn(
4942
+ "bg-primary h-full",
4943
+ active && "motion-safe:transition-[width]"
4944
+ ),
4945
+ style: { width: `${job.percent}%` }
4946
+ }
4947
+ )
4925
4948
  }
4926
- )
4927
- }
4928
- ),
4929
- job.error ? /* @__PURE__ */ jsx("p", { role: "alert", className: "text-destructive text-xs", children: job.error }) : null
4930
- ] }, job.id);
4931
- }) }) });
4949
+ ),
4950
+ job.error ? /* @__PURE__ */ jsx("p", { role: "alert", className: "text-destructive text-xs", children: job.error }) : null
4951
+ ] }, job.id);
4952
+ }) })
4953
+ }
4954
+ );
4932
4955
  }
4933
4956
  function formatActiveBadge(count) {
4934
4957
  return count > 9 ? "9+" : String(count);
@@ -4981,25 +5004,63 @@ function FileUploadMenu({
4981
5004
  ]
4982
5005
  }
4983
5006
  ) }),
4984
- /* @__PURE__ */ jsxs(DropdownMenuContent, { align, className: "w-80 p-0", children: [
4985
- /* @__PURE__ */ jsx("div", { className: "flex items-center justify-between border-b px-4 py-3", children: /* @__PURE__ */ jsx("h4", { className: "font-semibold", children: labels.title }) }),
4986
- /* @__PURE__ */ jsx("div", { className: "max-h-80 overflow-y-auto p-3", children: /* @__PURE__ */ jsx(
4987
- FileUploadTray,
4988
- {
4989
- jobs: visible,
4990
- labels,
4991
- onPause,
4992
- onResume,
4993
- onCancel
4994
- }
4995
- ) })
4996
- ] })
5007
+ /* @__PURE__ */ jsxs(
5008
+ DropdownMenuContent,
5009
+ {
5010
+ align,
5011
+ className: "w-80 min-w-0 max-w-[calc(100vw-2rem)] overflow-x-hidden p-0",
5012
+ children: [
5013
+ /* @__PURE__ */ jsx("div", { className: "flex items-center justify-between border-b px-4 py-3", children: /* @__PURE__ */ jsx("h4", { className: "font-semibold", children: labels.title }) }),
5014
+ /* @__PURE__ */ jsx("div", { className: "max-h-80 min-w-0 overflow-x-hidden overflow-y-auto p-3", children: /* @__PURE__ */ jsx(
5015
+ FileUploadTray,
5016
+ {
5017
+ jobs: visible,
5018
+ labels,
5019
+ onPause,
5020
+ onResume,
5021
+ onCancel
5022
+ }
5023
+ ) })
5024
+ ]
5025
+ }
5026
+ )
4997
5027
  ]
4998
5028
  }
4999
5029
  );
5000
5030
  }
5001
5031
 
5002
5032
  // src/components/upload/createMultipartUploadController.ts
5033
+ var PART_ATTEMPTS = 3;
5034
+ var PART_RETRY_BACKOFF_MS = [500, 1500];
5035
+ function defaultSleep(ms) {
5036
+ return new Promise((resolve) => {
5037
+ setTimeout(resolve, ms);
5038
+ });
5039
+ }
5040
+ function isAbortError(error) {
5041
+ return error instanceof DOMException && error.name === "AbortError";
5042
+ }
5043
+ function isMissingEtagError(error) {
5044
+ return error instanceof Error && error.message.includes("missing ETag");
5045
+ }
5046
+ function isRetryableStatus(status) {
5047
+ return status === 0 || status === 403 || status === 408 || status === 429 || status >= 500;
5048
+ }
5049
+ function putPartFailure(partNumber, status) {
5050
+ return new Error(`Upload failed (part ${partNumber}, status ${status})`);
5051
+ }
5052
+ function parsePutStatus(error) {
5053
+ if (!(error instanceof Error)) return null;
5054
+ const match = error.message.match(/status (\d+)\)$/);
5055
+ if (!match?.[1]) return null;
5056
+ return Number(match[1]);
5057
+ }
5058
+ function isRetryablePutError(error) {
5059
+ if (isAbortError(error) || isMissingEtagError(error)) return false;
5060
+ const status = parsePutStatus(error);
5061
+ if (status === null) return false;
5062
+ return isRetryableStatus(status);
5063
+ }
5003
5064
  function readEtag(xhr) {
5004
5065
  const raw = xhr.getResponseHeader("ETag") ?? xhr.getResponseHeader("etag") ?? "";
5005
5066
  const etag = raw.trim();
@@ -5023,10 +5084,10 @@ function putPart(input) {
5023
5084
  }
5024
5085
  return;
5025
5086
  }
5026
- reject(new Error(`Upload failed (${input.xhr.status})`));
5087
+ reject(putPartFailure(input.partNumber, input.xhr.status));
5027
5088
  };
5028
5089
  input.xhr.onerror = () => {
5029
- reject(new Error("Upload failed"));
5090
+ reject(putPartFailure(input.partNumber, 0));
5030
5091
  };
5031
5092
  input.xhr.onabort = () => {
5032
5093
  reject(new DOMException("The operation was aborted.", "AbortError"));
@@ -5036,8 +5097,9 @@ function putPart(input) {
5036
5097
  }
5037
5098
  function createMultipartUploadController(options) {
5038
5099
  const ranges = partRanges(options.file.size, options.partSize);
5039
- const completed = [];
5100
+ const sleep = options.sleep ?? defaultSleep;
5040
5101
  const state = { status: "idle" };
5102
+ const completed = [];
5041
5103
  let currentXhr = null;
5042
5104
  let run = null;
5043
5105
  let resumeIndex = 0;
@@ -5057,6 +5119,40 @@ function createMultipartUploadController(options) {
5057
5119
  progressFromBytes(completedBytes + currentPartLoaded, options.file.size)
5058
5120
  );
5059
5121
  }
5122
+ async function putPartWithRetry(partNumber, body) {
5123
+ let lastError;
5124
+ for (let attempt = 1; attempt <= PART_ATTEMPTS; attempt += 1) {
5125
+ if (state.status !== "uploading") {
5126
+ throw lastError ?? new DOMException("The operation was aborted.", "AbortError");
5127
+ }
5128
+ const url = await options.adapter.requestPartUrl(partNumber);
5129
+ if (state.status !== "uploading") {
5130
+ throw new DOMException("The operation was aborted.", "AbortError");
5131
+ }
5132
+ const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
5133
+ currentXhr = xhr;
5134
+ try {
5135
+ return await putPart({
5136
+ url,
5137
+ body,
5138
+ xhr,
5139
+ partNumber,
5140
+ onProgress: (loaded) => emitProgress(loaded)
5141
+ });
5142
+ } catch (error) {
5143
+ lastError = error;
5144
+ if (currentXhr === xhr) currentXhr = null;
5145
+ if (isStopped() || !isRetryablePutError(error) || attempt === PART_ATTEMPTS) {
5146
+ throw error;
5147
+ }
5148
+ const delay = PART_RETRY_BACKOFF_MS[attempt - 1] ?? PART_RETRY_BACKOFF_MS[PART_RETRY_BACKOFF_MS.length - 1];
5149
+ await sleep(delay);
5150
+ } finally {
5151
+ if (currentXhr === xhr) currentXhr = null;
5152
+ }
5153
+ }
5154
+ throw lastError;
5155
+ }
5060
5156
  async function uploadFrom(startIndex) {
5061
5157
  state.status = "uploading";
5062
5158
  for (let index = startIndex; index < ranges.length; index += 1) {
@@ -5064,18 +5160,10 @@ function createMultipartUploadController(options) {
5064
5160
  resumeIndex = index;
5065
5161
  const range = ranges[index];
5066
5162
  if (!range) continue;
5067
- const url = await options.adapter.requestPartUrl(range.partNumber);
5068
- if (state.status !== "uploading") return;
5069
- const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
5070
- currentXhr = xhr;
5071
5163
  const body = options.file.slice(range.start, range.end);
5072
5164
  try {
5073
- const etag = await putPart({
5074
- url,
5075
- body,
5076
- xhr,
5077
- onProgress: (loaded) => emitProgress(loaded)
5078
- });
5165
+ const etag = await putPartWithRetry(range.partNumber, body);
5166
+ if (state.status !== "uploading") return;
5079
5167
  completed.push({ partNumber: range.partNumber, etag });
5080
5168
  emitProgress(0);
5081
5169
  } catch (error) {
@@ -5083,8 +5171,6 @@ function createMultipartUploadController(options) {
5083
5171
  if (isStopped()) return;
5084
5172
  state.status = "error";
5085
5173
  throw error;
5086
- } finally {
5087
- if (currentXhr === xhr) currentXhr = null;
5088
5174
  }
5089
5175
  }
5090
5176
  if (state.status !== "uploading") return;