use-pwa
Version:
use-pwa is install and update handler for pwa.
171 lines • 8.28 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
function useBoolean(initialValue) {
var _a = (0, react_1.useState)(initialValue), value = _a[0], setValue = _a[1];
var setTrue = (0, react_1.useCallback)(function () { return setValue(true); }, []);
return { value: value, setTrue: setTrue };
}
function usePwa() {
var _this = this;
var beforeinstallprompt = (0, react_1.useRef)();
var _a = useBoolean(false), appinstalled = _a.value, onAppinstalled = _a.setTrue;
var _b = useBoolean(false), canInstallprompt = _b.value, onCanInstallprompt = _b.setTrue;
var _c = useBoolean(false), enabledA2hs = _c.value, onEnabledA2hs = _c.setTrue;
var _d = useBoolean(false), enabledPwa = _d.value, onEnabledPwa = _d.setTrue;
var _e = useBoolean(false), isPwa = _e.value, onIsPwa = _e.setTrue;
var _f = (0, react_1.useState)(), userChoice = _f[0], setUserChoice = _f[1];
var handleAppinstalled = (0, react_1.useCallback)(function () { return onAppinstalled(); }, [onAppinstalled]);
var handleBeforeinstallprompt = (0, react_1.useCallback)(function (event) {
beforeinstallprompt.current = event;
onCanInstallprompt();
}, [onCanInstallprompt]);
var showInstallPrompt = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var userChoice;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!beforeinstallprompt.current) {
return [2 /*return*/];
}
return [4 /*yield*/, beforeinstallprompt.current.prompt()];
case 1:
_a.sent();
return [4 /*yield*/, beforeinstallprompt.current.userChoice];
case 2:
userChoice = _a.sent();
setUserChoice(userChoice);
return [2 /*return*/];
}
});
}); }, []);
var _g = (0, react_1.useState)({
appinstalled: false,
beforeinstallprompt: false,
enabledA2hs: false,
enabledPwa: false,
isPwa: false,
}), completed = _g[0], setCompleted = _g[1];
var isLoading = (0, react_1.useMemo)(function () { return Object.values(completed).includes(false); }, [completed]);
(0, react_1.useEffect)(function () {
window.addEventListener("appinstalled", handleAppinstalled);
setCompleted(function (prevCompleted) { return (__assign(__assign({}, prevCompleted), { appinstalled: true })); });
return function () { return window.removeEventListener("appinstalled", handleAppinstalled); };
}, [handleAppinstalled]);
(0, react_1.useEffect)(function () {
// @ts-ignore
window.addEventListener("beforeinstallprompt", handleBeforeinstallprompt);
setCompleted(function (prevCompleted) { return (__assign(__assign({}, prevCompleted), { beforeinstallprompt: true })); });
return function () {
// @ts-ignore
return window.removeEventListener("beforeinstallprompt", handleBeforeinstallprompt);
};
}, [handleBeforeinstallprompt]);
(0, react_1.useEffect)(function () {
try {
var lownerUserAgent = window.navigator.userAgent.toLowerCase();
if (!lownerUserAgent.includes("iphone") &&
!lownerUserAgent.includes("ipad")) {
return;
}
onEnabledA2hs();
}
finally {
setCompleted(function (prevCompleted) { return (__assign(__assign({}, prevCompleted), { enabledA2hs: true })); });
}
}, [onEnabledA2hs]);
(0, react_1.useEffect)(function () {
try {
if (!("BeforeInstallPromptEvent" in window)) {
return;
}
onEnabledPwa();
}
finally {
setCompleted(function (prevCompleted) { return (__assign(__assign({}, prevCompleted), { enabledPwa: true })); });
}
}, [onEnabledPwa]);
(0, react_1.useEffect)(function () {
try {
// Android Trusted Web App
if (window.document.referrer.includes("android-app://")) {
onIsPwa();
return;
}
// Chrome PWA (supporting fullscreen, standalone, minimal-ui)
if (["fullscreen", "standalone", "minimal-ui"].some(function (displayMode) {
return window.matchMedia("(display-mode: " + displayMode + ")").matches;
})) {
onIsPwa();
return;
}
// NOT iOS PWA Standalone
if (!("standalone" in window.navigator) || !window.navigator.standalone) {
return;
}
onIsPwa();
}
finally {
setCompleted(function (prevCompleted) { return (__assign(__assign({}, prevCompleted), { isPwa: true })); });
}
}, [onIsPwa]);
return {
appinstalled: appinstalled,
canInstallprompt: canInstallprompt,
enabledA2hs: enabledA2hs,
enabledPwa: enabledPwa,
isLoading: isLoading,
isPwa: isPwa,
showInstallPrompt: showInstallPrompt,
userChoice: userChoice,
};
}
exports.default = usePwa;
//# sourceMappingURL=index.js.map