react-native-cloud-storage
Version:
☁️ Save to & read from iCloud and Google Drive using React Native
34 lines (32 loc) • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useIsCloudAvailable = void 0;
var _react = require("react");
var _cloudStorage = _interopRequireDefault(require("../cloud-storage"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* A hook that tests whether or not the cloud storage is available.
* @param cloudStorageInstance - An optional instance of RNCloudStorage to use instead of the default instance.
* @returns A boolean indicating whether or not the cloud storage is available.
*/
const useIsCloudAvailable = cloudStorageInstance => {
const [isAvailable, setIsAvailable] = (0, _react.useState)(false);
const instance = cloudStorageInstance ?? _cloudStorage.default.getDefaultInstance();
const handleAvailabilityChange = (0, _react.useCallback)(available => {
setIsAvailable(available);
}, []);
(0, _react.useEffect)(() => {
// Set the initial availability state
instance.isCloudAvailable().then(setIsAvailable);
// Listen for changes to the cloud availability using the native event emitter
instance.subscribeToCloudAvailability(handleAvailabilityChange);
return () => {
instance.unsubscribeFromCloudAvailability(handleAvailabilityChange);
};
}, [instance, handleAvailabilityChange]);
return isAvailable;
};
exports.useIsCloudAvailable = useIsCloudAvailable;
//# sourceMappingURL=use-is-cloud-available.js.map