UNPKG

react-js-plugins

Version:

A powerful and efficient React utility library designed to enhance application performance by streamlining and simplifying the management of complex asynchronous operations.

77 lines (76 loc) 3.43 kB
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); }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useEffect, useCallback, useRef, lazy, Suspense } from 'react'; import NProgress from 'nprogress'; import { ErrorBoundary } from 'react-error-boundary'; import { _injectAutoUpdater, chunk } from './chunk75342'; export var promise = function (ms) { return new Promise(function (res) { return setTimeout(res, ms); }); }; function Fallback(_a) { var error = _a.error; var isProd = process.env.NODE_ENV === 'production'; return (_jsx("div", { style: { padding: '16px' }, children: isProd ? (_jsxs("div", { style: { padding: '8px', border: '1px solid #d0cfcf', borderLeft: '2px solid #00abff', backgroundColor: '#e3f2fd', color: '#0d47a1', borderRadius: '6px', }, children: [_jsx("strong", { style: { display: 'block', marginBottom: '4px' }, children: "Page Loading Error" }), "Please check your network connection..."] })) : (_jsxs("div", { style: { padding: '8px', border: '1px solid #d0cfcf', borderTop: '2px solid rgba(179, 11, 11, 0.68)', backgroundColor: '#ffebee', color: '#b71c1c', borderRadius: '6px', }, children: [_jsx("strong", { style: { display: 'block', marginBottom: '4px' }, children: "Error" }), error.message] })) })); } var Error = function (_a) { var children = _a.children; var firstRender = useRef(false); var handleReload = useCallback(function () { if (!firstRender.current) { firstRender.current = true; promise(1500).then(function () { window.location.reload(); }); } }, []); return (_jsx(ErrorBoundary, { FallbackComponent: Fallback, onError: function () { if (process.env.NODE_ENV === 'production') { handleReload(); } }, children: children })); }; var LoadingScreen = function () { useEffect(function () { NProgress.start(); NProgress.configure({ showSpinner: false }); _injectAutoUpdater(); return function () { NProgress.done(); }; }, []); var theme = localStorage === null || localStorage === void 0 ? void 0 : localStorage.getItem('theme'); var backgroundColor = theme === 'dark' ? '#0a0a0a' : '#ffffff'; return (_jsx("div", { style: { backgroundColor: backgroundColor, minHeight: '100vh', } })); }; export var LazyLoader = function (importFunc) { if (!chunk()) { return function () { return (_jsx("div", { className: "p-4 text-red-500", children: "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop." })); }; } var Component = lazy(importFunc); return function (props) { return (_jsx(Error, { children: _jsx(Suspense, { fallback: _jsx(LoadingScreen, {}), children: _jsx(Component, __assign({}, props)) }) })); }; };