uplofile 2.1.0 → 2.2.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.ts CHANGED
@@ -6,6 +6,7 @@ declare const Dropzone: ({ asChild, ...rest }: {
6
6
  asChild?: boolean;
7
7
  } & HTMLAttributes<HTMLElement>) => react_jsx_runtime.JSX.Element;
8
8
 
9
+ type MaybePromise<T> = T | Promise<T>;
9
10
  type UploadStatus = "idle" | "uploading" | "done" | "error" | "canceled" | "removing";
10
11
  type UploadFileItem<TMeta = any> = {
11
12
  uid: string;
@@ -19,9 +20,11 @@ type UploadFileItem<TMeta = any> = {
19
20
  error?: string;
20
21
  meta?: TMeta;
21
22
  };
22
- type UploadResult = {
23
+ type UploadResult<TMeta = any> = {
23
24
  url: string;
24
25
  id?: string;
26
+ meta?: TMeta;
27
+ previewUrl?: string;
25
28
  };
26
29
  type UplofileRootRef<TMeta = any> = {
27
30
  setItems: (items: UploadFileItem<TMeta>[] | ((prev: UploadFileItem<TMeta>[]) => UploadFileItem<TMeta>[])) => void;
@@ -40,7 +43,7 @@ type BeforeUploadResult<TMeta = any> = boolean | Array<{
40
43
  }>;
41
44
  type RootProps<TMeta = any> = PropsWithChildren<{
42
45
  multiple?: boolean;
43
- initial?: Array<Pick<UploadFileItem<TMeta>, "uid" | "id" | "name" | "url" | "meta">>;
46
+ initial?: MaybePromise<Array<Pick<UploadFileItem<TMeta>, "uid" | "id" | "name" | "url" | "meta">>>;
44
47
  /**
45
48
  * optimistic (default): remove from UI immediately, call onRemove in the background; if it fails, restore the item and show error.
46
49
  * strict: call onRemove first; only remove from UI if it succeeds.
@@ -52,7 +55,7 @@ type RootProps<TMeta = any> = PropsWithChildren<{
52
55
  accept?: string;
53
56
  beforeUpload?: (items: UploadFileItem<TMeta>[]) => BeforeUploadResult<TMeta> | Promise<BeforeUploadResult<TMeta>>;
54
57
  onChange?: (items: UploadFileItem<TMeta>[]) => Promise<void> | void;
55
- upload: (file: File, signal: AbortSignal, setProgress?: (pct: number) => void) => Promise<UploadResult>;
58
+ upload: (file: File, signal: AbortSignal, setProgress?: (pct: number) => void) => Promise<UploadResult<TMeta>>;
56
59
  onRemove?: (item: UploadFileItem<TMeta>, signal: AbortSignal) => Promise<void | any>;
57
60
  }>;
58
61
  type ItemActions = {
@@ -120,7 +123,7 @@ declare const Remove: ({ uid, asChild, ...rest }: ButtonProps) => react_jsx_runt
120
123
 
121
124
  declare const Root: React.ForwardRefExoticComponent<{
122
125
  multiple?: boolean;
123
- initial?: Pick<UploadFileItem<unknown>, "id" | "name" | "uid" | "url" | "meta">[] | undefined;
126
+ initial?: MaybePromise<Pick<UploadFileItem<unknown>, "id" | "name" | "uid" | "url" | "meta">[]> | undefined;
124
127
  removeMode?: "optimistic" | "strict";
125
128
  name?: string;
126
129
  maxCount?: number;
@@ -128,7 +131,7 @@ declare const Root: React.ForwardRefExoticComponent<{
128
131
  accept?: string;
129
132
  beforeUpload?: ((items: UploadFileItem<unknown>[]) => BeforeUploadResult<unknown> | Promise<BeforeUploadResult<unknown>>) | undefined;
130
133
  onChange?: ((items: UploadFileItem<unknown>[]) => Promise<void> | void) | undefined;
131
- upload: (file: File, signal: AbortSignal, setProgress?: (pct: number) => void) => Promise<UploadResult>;
134
+ upload: (file: File, signal: AbortSignal, setProgress?: (pct: number) => void) => Promise<UploadResult<unknown>>;
132
135
  onRemove?: ((item: UploadFileItem<unknown>, signal: AbortSignal) => Promise<void | any>) | undefined;
133
136
  } & {
134
137
  children?: React.ReactNode | undefined;
@@ -147,4 +150,4 @@ declare const isVideoFile: (item: UploadFileItem<any>, extraExtensions?: string[
147
150
  declare const isImageFile: (item: UploadFileItem<any>, extraExtensions?: string[]) => boolean;
148
151
 
149
152
  export { Cancel, Dropzone, HiddenInput, Preview, Remove, Retry, Root, Trigger, getExtension, isImageFile, isVideoFile, useUplofile };
150
- export type { ImageUploaderContextValue, ItemActions, RootProps, UploadFileItem, UploadResult, UploadStatus, UplofileRootRef };
153
+ export type { BeforeUploadResult, ImageUploaderContextValue, ItemActions, RootProps, UploadFileItem, UploadResult, UploadStatus, UplofileRootRef };
package/dist/index.mjs CHANGED
@@ -124,21 +124,24 @@ const Root = /*#__PURE__*/ forwardRef(({ multiple = true, initial = [], onChange
124
124
  // Hydrate initial items from the server and keep them marked as done
125
125
  useEffect(()=>{
126
126
  if (hasHydratedInitialRef.current) return;
127
- const arr = initial ?? [];
128
- if (!Array.isArray(arr) || arr.length === 0) return;
129
- const mapped = arr.map((it)=>{
130
- return {
131
- uid: it.uid || it.id,
132
- id: it.id,
133
- name: it.name,
134
- url: it.url,
135
- status: "done",
136
- meta: it.meta
137
- };
138
- });
139
- // Only hydrate if the user hasn't already added/modified items locally
140
- setItems((prev)=>prev.length === 0 ? mapped : prev);
141
- hasHydratedInitialRef.current = true;
127
+ const hydrate = async ()=>{
128
+ const arr = await initial;
129
+ if (!Array.isArray(arr) || arr.length === 0) return;
130
+ const mapped = arr.map((it)=>{
131
+ return {
132
+ uid: it.uid || it.id,
133
+ id: it.id,
134
+ name: it.name,
135
+ url: it.url,
136
+ status: "done",
137
+ meta: it.meta
138
+ };
139
+ });
140
+ // Only hydrate if the user hasn't already added/modified items locally
141
+ setItems((prev)=>prev.length === 0 ? mapped : prev);
142
+ hasHydratedInitialRef.current = true;
143
+ };
144
+ void hydrate();
142
145
  }, [
143
146
  initial
144
147
  ]);
@@ -183,13 +186,14 @@ const Root = /*#__PURE__*/ forwardRef(({ multiple = true, initial = [], onChange
183
186
  } catch {
184
187
  /*fail silently*/ }
185
188
  }
186
- const serverPreview = result?.preview || result.url;
189
+ const serverPreview = result.previewUrl ?? result.preview ?? result.url;
187
190
  return {
188
191
  ...it,
189
192
  status: "done",
190
193
  url: result.url,
191
194
  id: result.id ?? it.id,
192
195
  previewUrl: serverPreview,
196
+ meta: result.meta ?? it.meta,
193
197
  progress: 100
194
198
  };
195
199
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uplofile",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Composable file‑upload components for React.",
5
5
  "license": "MIT",
6
6
  "author": "Chris Josh <KristofaJosh>",