react-native-cloud-storage
Version:
☁️ Save to & read from iCloud and Google Drive using React Native
73 lines (72 loc) • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useCloudFile = void 0;
var _cloudStorage = _interopRequireDefault(require("../cloud-storage"));
var _react = require("react");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* A utility hook for reading and writing to a single file in the cloud.
* @param path The path to the file.
* @param scope The directory scope the path is in. Defaults to the default scope set for the current provider.
* @param cloudStorageInstance An optional instance of RNCloudStorage to use instead of the default instance.
* @returns An object containing the file's contents and functions for downloading, reading, writing, and removing the file.
*/
const useCloudFile = (path, scope, cloudStorageInstance) => {
const [content, setContent] = (0, _react.useState)(null);
const instance = cloudStorageInstance ?? _cloudStorage.default;
const read = (0, _react.useCallback)(async () => {
const exists = await instance.exists(path, scope);
if (!exists) {
setContent(null);
return;
}
instance.readFile(path, scope).then(setContent);
}, [path, scope, instance]);
(0, _react.useEffect)(() => {
read();
}, [read]);
const write = (0, _react.useCallback)(async newContent => {
await instance.writeFile(path, newContent, scope);
read();
}, [path, scope, read, instance]);
const remove = (0, _react.useCallback)(async () => {
await instance.unlink(path, scope);
setContent(null);
}, [path, scope, instance]);
const sync = (0, _react.useCallback)(async () => {
await instance.triggerSync(path, scope);
}, [path, scope, instance]);
return {
/**
* The content of the file.
*/
content,
/**
* Reads the file from the cloud.
*/
read,
/**
* Writes new content to the file.
*/
write,
/**
* Deletes the file.
*/
remove,
/**
* Triggers synchronization for the file. Needed if the file hasn't been synced yet from iCloud.
* Has no effect on Google Drive.
*/
sync,
/**
* Triggers synchronization for the file. Needed if the file hasn't been synced yet from iCloud.
* Has no effect on Google Drive.
* @deprecated Use `sync` instead.
*/
download: sync
};
};
exports.useCloudFile = useCloudFile;
//# sourceMappingURL=use-cloud-file.js.map