@douglasneuroinformatics/libui 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.
@@ -1,13 +1,20 @@
1
1
  import type { Promisable } from 'type-fest';
2
- type DownloadOptions<T extends Blob | string = Blob | string> = T extends Blob ? {
3
- blobType: 'image/png';
4
- } : {
2
+ type DownloadTextOptions = {
5
3
  blobType: 'text/csv' | 'text/plain';
6
4
  };
5
+ type DownloadBlobOptions = {
6
+ blobType: 'application/zip' | 'image/jpeg' | 'image/png' | 'image/webp';
7
+ };
8
+ interface DownloadFunction {
9
+ (filename: string, data: Blob, options: DownloadBlobOptions): Promise<void>;
10
+ (filename: string, data: () => Promisable<Blob>, options: DownloadBlobOptions): Promise<void>;
11
+ (filename: string, data: string, options?: DownloadTextOptions): Promise<void>;
12
+ (filename: string, data: () => Promisable<string>, options?: DownloadTextOptions): Promise<void>;
13
+ }
7
14
  /**
8
15
  * Used to trigger downloads of arbitrary data to the client
9
16
  * @returns A function to invoke the download
10
17
  */
11
- export declare function useDownload(): <T extends string | Blob>(filename: string, fetchData: () => Promisable<T>, options?: DownloadOptions<T>) => Promise<void>;
18
+ export declare function useDownload(): DownloadFunction;
12
19
  export {};
13
20
  //# sourceMappingURL=useDownload.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useDownload.d.ts","sourceRoot":"","sources":["../../src/hooks/useDownload.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAI5C,KAAK,eAAe,CAAC,CAAC,SAAS,IAAI,GAAG,MAAM,GAAG,IAAI,GAAG,MAAM,IAAI,CAAC,SAAS,IAAI,GAC1E;IAAE,QAAQ,EAAE,WAAW,CAAA;CAAE,GACzB;IAAE,QAAQ,EAAE,UAAU,GAAG,YAAY,CAAA;CAAE,CAAC;AAE5C;;;GAGG;AACH,wBAAgB,WAAW,wCA2Bb,MAAM,aACL,MAAM,WAAW,CAAC,CAAC,YACpB,gBAAgB,CAAC,CAAC,mBAiB/B"}
1
+ {"version":3,"file":"useDownload.d.ts","sourceRoot":"","sources":["../../src/hooks/useDownload.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAI5C,KAAK,mBAAmB,GAAG;IACzB,QAAQ,EAAE,UAAU,GAAG,YAAY,CAAC;CACrC,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,QAAQ,EAAE,iBAAiB,GAAG,YAAY,GAAG,WAAW,GAAG,YAAY,CAAC;CACzE,CAAC;AAGF,UAAU,gBAAgB;IACxB,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5E,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9F,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/E,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClG;AAED;;;GAGG;AACH,wBAAgB,WAAW,IAAI,gBAAgB,CA0C9C"}
@@ -22,9 +22,9 @@ export function useDownload() {
22
22
  setState(null);
23
23
  }
24
24
  }, [state]);
25
- return async (filename, fetchData, options) => {
25
+ return async (filename, _data, options) => {
26
26
  try {
27
- const data = await fetchData();
27
+ const data = typeof _data === 'function' ? await _data() : _data;
28
28
  if (typeof data !== 'string' && !options?.blobType) {
29
29
  throw new Error("argument 'blobType' must be defined when download is called with a Blob object");
30
30
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@douglasneuroinformatics/libui",
3
3
  "type": "module",
4
- "version": "2.1.0",
4
+ "version": "2.2.0",
5
5
  "packageManager": "pnpm@8.15.3",
6
6
  "description": "Generic UI components for DNP projects, built using React and TailwindCSS",
7
7
  "author": {
@@ -4,18 +4,30 @@ import type { Promisable } from 'type-fest';
4
4
 
5
5
  import { useNotificationsStore } from './useNotificationsStore.js';
6
6
 
7
- type DownloadOptions<T extends Blob | string = Blob | string> = T extends Blob
8
- ? { blobType: 'image/png' }
9
- : { blobType: 'text/csv' | 'text/plain' };
7
+ type DownloadTextOptions = {
8
+ blobType: 'text/csv' | 'text/plain';
9
+ };
10
+
11
+ type DownloadBlobOptions = {
12
+ blobType: 'application/zip' | 'image/jpeg' | 'image/png' | 'image/webp';
13
+ };
14
+
15
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
16
+ interface DownloadFunction {
17
+ (filename: string, data: Blob, options: DownloadBlobOptions): Promise<void>;
18
+ (filename: string, data: () => Promisable<Blob>, options: DownloadBlobOptions): Promise<void>;
19
+ (filename: string, data: string, options?: DownloadTextOptions): Promise<void>;
20
+ (filename: string, data: () => Promisable<string>, options?: DownloadTextOptions): Promise<void>;
21
+ }
10
22
 
11
23
  /**
12
24
  * Used to trigger downloads of arbitrary data to the client
13
25
  * @returns A function to invoke the download
14
26
  */
15
- export function useDownload() {
27
+ export function useDownload(): DownloadFunction {
16
28
  const notifications = useNotificationsStore();
17
29
  const [state, setState] = useState<{
18
- blobType: DownloadOptions['blobType'];
30
+ blobType: string;
19
31
  data: Blob | string;
20
32
  filename: string;
21
33
  } | null>(null);
@@ -38,13 +50,9 @@ export function useDownload() {
38
50
  }
39
51
  }, [state]);
40
52
 
41
- return async <T extends Blob | string>(
42
- filename: string,
43
- fetchData: () => Promisable<T>,
44
- options?: DownloadOptions<T>
45
- ) => {
53
+ return async (filename, _data, options) => {
46
54
  try {
47
- const data = await fetchData();
55
+ const data = typeof _data === 'function' ? await _data() : _data;
48
56
  if (typeof data !== 'string' && !options?.blobType) {
49
57
  throw new Error("argument 'blobType' must be defined when download is called with a Blob object");
50
58
  }