Skip to content

Commit

Permalink
feat(storage): add useBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Nov 19, 2021
1 parent 3d2dc70 commit 205869c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ This library consists of 4 modules with many hooks:
- [`messaging`](#Messaging)
- [`useMessagingToken`](#useMessagingToken)
- [`storage`](#Storage)
- [`useBytes`](#useBytes)
- [`useDownloadURL`](#useDownloadURL)

All hooks can be imported from `react-firehooks` directly or via `react-firehooks/<module>` to improve tree-shaking and bundle size.
Expand Down Expand Up @@ -356,6 +357,25 @@ Returns:
import { ... } from 'react-firehooks/storage';
```

#### useBytes

Returns the data of a Google Cloud Storage object

```javascript
const [data, loading, error] = useBytes(storageReference);
```

Params:

- `reference`: Reference to a Google Cloud Storage object
- `maxDownloadSizeBytes`: If set, the maximum allowed size in bytes to retrieve.

Returns:

- `value`: Object data; `undefined` if data of the object is currently being downloaded, or an error occurred
- `loading`: `true` while downloading the data of the object; `false` if the data was downloaded successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useDownloadURL

Returns the download URL of a Google Cloud Storage object
Expand Down
1 change: 1 addition & 0 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./useBytes";
export * from "./useDownloadURL";
35 changes: 35 additions & 0 deletions src/storage/useBytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { StorageError, StorageReference } from "firebase/storage";
import * as firebaseStorage from "firebase/storage";
import { useCallback } from "react";
import { ValueHookResult } from "../common";
import { useOnce } from "../internal/useOnce";
import { isStorageRefEqual } from "./internal";

export type UseBytesResult = ValueHookResult<ArrayBuffer, StorageError>;

/**
* Returns the data of a Google Cloud Storage object
*
* Requires firebase v9.5.0 or greater
*
* @param {StorageReference | undefined | null} reference Reference to a Google Cloud Storage object
* @param {?number} maxDownloadSizeBytes If set, the maximum allowed size in bytes to retrieve
* @returns {UseBytesResult} Data, loading state, and error
* * value: Object data; `undefined` if data of the object is currently being downloaded, or an error occurred
* * loading: `true` while downloading the data of the object; `false` if the data was downloaded successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useBytes(reference: StorageReference | undefined | null, maxDownloadSizeBytes?: number): UseBytesResult {
const fetchBytes = useCallback(
async (ref: StorageReference) => {
if ("getBytes" in firebaseStorage) {
return await firebaseStorage.getBytes(ref, maxDownloadSizeBytes);
} else {
throw new Error("`useBytes` requires firebase v9.5.0 or greater");
}
},
[maxDownloadSizeBytes]
);

return useOnce(reference ?? undefined, fetchBytes, isStorageRefEqual);
}

0 comments on commit 205869c

Please sign in to comment.