UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

91 lines (90 loc) 3.99 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useSnapshot; require("core-js/modules/web.dom-collections.iterator.js"); var _react = require("react"); var _componentHelper = require("../../../shared/component-helper"); var _jsonPointer = _interopRequireDefault(require("../utils/json-pointer")); var _useDataContext = _interopRequireDefault(require("./useDataContext")); var _useData = _interopRequireDefault(require("../Form/data-context/useData")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function useSnapshot(id) { const internalSnapshotsRef = (0, _react.useRef)(); if (!internalSnapshotsRef.current) { internalSnapshotsRef.current = new Map(); } const { getContext } = (0, _useDataContext.default)(id); const { set: setData, update: updateData } = (0, _useData.default)(id); const { internalDataRef, snapshotsRef } = getContext() || {}; const internalData = internalDataRef === null || internalDataRef === void 0 ? void 0 : internalDataRef.current; const createSnapshot = (0, _react.useCallback)(function () { let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : (0, _componentHelper.makeUniqueId)(); let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; let content = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; if (!content) { var _snapshotsRef$current, _snapshotsRef$current2; const snapshotWithPaths = snapshotsRef === null || snapshotsRef === void 0 ? void 0 : (_snapshotsRef$current = snapshotsRef.current) === null || _snapshotsRef$current === void 0 ? void 0 : (_snapshotsRef$current2 = _snapshotsRef$current.get) === null || _snapshotsRef$current2 === void 0 ? void 0 : _snapshotsRef$current2.call(_snapshotsRef$current, name); if (snapshotWithPaths) { const collectedData = new Map(); snapshotWithPaths.forEach((isMounted, path) => { if (isMounted && _jsonPointer.default.has(internalData, path)) { collectedData.set(path, _jsonPointer.default.get(internalData, path)); } }); content = collectedData; } else { content = internalData; } } internalSnapshotsRef.current.set(combineIdWithName(id, name), content); return id; }, [internalData, snapshotsRef]); const getSnapshot = (0, _react.useCallback)(function (id) { let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; return internalSnapshotsRef.current.get(combineIdWithName(id, name)); }, []); const deleteSnapshot = (0, _react.useCallback)(function (id) { let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; internalSnapshotsRef.current.delete(combineIdWithName(id, name)); }, []); const applySnapshot = (0, _react.useCallback)(function (id) { let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; const snapshot = getSnapshot(id, name); if (snapshot instanceof Map) { snapshot.forEach((value, path) => { updateData(path, value); }); } else if (snapshot) { setData(snapshot); } }, [getSnapshot, setData, updateData]); const revertSnapshot = (0, _react.useCallback)(function (id) { let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; applySnapshot(id, name); deleteSnapshot(id, name); }, [applySnapshot, deleteSnapshot]); return (0, _react.useMemo)(() => { return { createSnapshot, revertSnapshot, applySnapshot, internalSnapshotsRef }; }, [applySnapshot, createSnapshot, revertSnapshot]); } function combineIdWithName(id) { let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; return name ? `${id}-${name}` : id; } //# sourceMappingURL=useSnapshot.js.map