@trap_stevo/legendarybuilderproreact-ui
Version:
The legendary UI & utility API that makes your application a legendary application. ~ Created by Steven Compton
120 lines (116 loc) • 5.24 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _typeof from "@babel/runtime/helpers/typeof";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/*
Created by Hassan Steven Compton.
March 7, 2024.
*/
import React, { createContext, useContext, useState, useEffect, useRef } from 'react';
var AuthContext = /*#__PURE__*/createContext();
/**
* HUDSessionProvider provides session cache management within an application, keeping track of session data with the ability to even keep track of data offline.
*
* @component
* @param {React.ReactNode} children - The content wrapped by this component.
* @returns {JSX.Element} The HUDSessionProvider component encapsulating children with session cache context.
*/
export var HUDSessionProvider = function HUDSessionProvider(_ref) {
var children = _ref.children;
var currentSessionCache = function currentSessionCache() {
var savedCache = localStorage.getItem("HUDSessionCache");
return savedCache ? JSON.parse(savedCache) : {};
};
var sessionCacheRef = useRef(currentSessionCache());
var _useState = useState(sessionCacheRef.current),
_useState2 = _slicedToArray(_useState, 2),
sessionCache = _useState2[0],
setSessionCache = _useState2[1];
var addToSessionCache = function addToSessionCache(id, data) {
var multiData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var updatedCache;
if (multiData && _typeof(multiData) === "object" && !Array.isArray(multiData)) {
updatedCache = _objectSpread(_objectSpread({}, sessionCacheRef.current), multiData);
sessionCacheRef.current = updatedCache;
localStorage.setItem("HUDSessionCache", JSON.stringify(updatedCache));
syncSessionCache();
return;
}
updatedCache = _objectSpread(_objectSpread({}, sessionCacheRef.current), {}, _defineProperty({}, id, data));
sessionCacheRef.current = updatedCache;
localStorage.setItem("HUDSessionCache", JSON.stringify(updatedCache));
syncSessionCache();
return;
};
var updateSessionCache = function updateSessionCache() {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
sessionCacheRef.current = data;
localStorage.setItem("HUDSessionCache", JSON.stringify(data));
syncSessionCache();
return;
};
var getFromSessionCache = function getFromSessionCache(id) {
return sessionCacheRef.current[id] !== undefined ? sessionCacheRef.current[id] : null;
};
var getSessionCache = function getSessionCache() {
return sessionCacheRef.current;
};
var clearFromSessionCache = function clearFromSessionCache(ids) {
if (Array.isArray(ids)) {
ids.forEach(function (id) {
delete sessionCacheRef.current[id];
});
} else if (!Array.isArray(ids)) {
delete sessionCacheRef.current[ids];
}
localStorage.setItem("HUDSessionCache", JSON.stringify(sessionCacheRef.current));
syncSessionCache();
return;
};
var clearSessionCache = function clearSessionCache() {
for (var key in sessionCacheRef) {
if (sessionCacheRef.hasOwnProperty(key)) {
delete sessionCacheRef[key];
}
}
localStorage.removeItem("HUDSessionCache");
syncSessionCache();
};
var syncSessionCache = function syncSessionCache() {
setSessionCache(_objectSpread({}, sessionCacheRef.current));
};
useEffect(function () {
var handleStorageUpdate = function handleStorageUpdate() {
var updatedCache = currentSessionCache();
sessionCacheRef.current = updatedCache;
syncSessionCache();
};
window.addEventListener("storage", handleStorageUpdate);
return function () {
window.removeEventListener("storage", handleStorageUpdate);
};
}, []);
useEffect(function () {
setSessionCache(sessionCacheRef.current);
}, []);
return /*#__PURE__*/React.createElement(AuthContext.Provider, {
value: {
sessionCache: sessionCache,
sessionCacheState: sessionCacheRef,
addToSessionCache: addToSessionCache,
getSessionCache: getSessionCache,
getFromSessionCache: getFromSessionCache,
updateSessionCache: updateSessionCache,
clearFromSessionCache: clearFromSessionCache,
clearSessionCache: clearSessionCache
}
}, children);
};
export var useHUDSession = function useHUDSession() {
var context = useContext(AuthContext);
if (!context) {
throw new Error("Must use within an SessionProvider");
}
return context;
};