-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(storage): add `useBytes` * fix(storage): use `require` in `useBytes`
- Loading branch information
1 parent
99c10e0
commit 462089f
Showing
6 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./useBytes"; | ||
export * from "./useDownloadURL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { StorageError, StorageReference } 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) => { | ||
// TODO: change to regular import in react-firehooks v2 | ||
const firebaseStorage = await require("firebase/storage"); | ||
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); | ||
} |