UNPKG

infinity-forge

Version:
125 lines • 6.49 kB
"use strict"; 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useDynamicSection = useDynamicSection; var jsx_runtime_1 = require("react/jsx-runtime"); var router_1 = require("next/router"); var ui_1 = require("../../../../../ui/index.js"); var context_1 = require("../context.js"); var edit_section_1 = require("./edit-section.js"); var react_1 = require("react"); function useDynamicSection(props) { var _a; var router = (0, router_1.useRouter)(); var currentLocale = router.locale || 'pt-BR'; var refSectionID = (0, ui_1.generateRef)(props.refSection, router, props.isGlobal); var atena = (0, context_1.useAtena)(); var sections = (0, react_1.useMemo)(function () { return atena.sections; }, [atena.sections]); var findSection = (sections === null || sections === void 0 ? void 0 : sections.find(function (section) { return section.ref === refSectionID; })) || {}; var sectionFinal = findSection; var initialDataSection = { enabled: false, images: [{ url: '/images/default-image.webp' }], linkUrl: '', videoUrl: '', }; if (!findSection || Object.keys(findSection).length === 0) { return __assign({ Section: Section }, initialDataSection); } var parsedJsonContent = (0, ui_1.parseJSON)(findSection.jsonContent); var jsonContentFinal = parsedJsonContent; var isI18nGlobal = props.refSection === 'i18n' && props.isGlobal; if (((_a = parsedJsonContent === null || parsedJsonContent === void 0 ? void 0 : parsedJsonContent.i18n) === null || _a === void 0 ? void 0 : _a[currentLocale]) && props.refSection !== 'i18n_global') { var i18nLang = parsedJsonContent.i18n[currentLocale]; if ((i18nLang === null || i18nLang === void 0 ? void 0 : i18nLang.jsonContent) && !isI18nGlobal) { jsonContentFinal = mergeDeep(parsedJsonContent, i18nLang === null || i18nLang === void 0 ? void 0 : i18nLang.jsonContent, true); } else { jsonContentFinal = mergeDeep(parsedJsonContent, i18nLang, true); } var _ = i18nLang.jsonContent, i18nLangRootFields = __rest(i18nLang, ["jsonContent"]); sectionFinal = __assign(__assign(__assign({}, findSection), i18nLangRootFields), { jsonContent: jsonContentFinal }); } var linkFull = { text: sectionFinal === null || sectionFinal === void 0 ? void 0 : sectionFinal.linkText, href: sectionFinal === null || sectionFinal === void 0 ? void 0 : sectionFinal.linkUrl, svg: jsonContentFinal === null || jsonContentFinal === void 0 ? void 0 : jsonContentFinal.linkSvg, target: (jsonContentFinal === null || jsonContentFinal === void 0 ? void 0 : jsonContentFinal.linkTarget) || '_self', }; function RenderSection(_a) { var children = _a.children, onSucess = _a.onSucess; var propsEditSectionComponent = { refSection: refSectionID, section: findSection, fields: props.fields, }; if (!(atena === null || atena === void 0 ? void 0 : atena.hasPermission)) { return (sectionFinal === null || sectionFinal === void 0 ? void 0 : sectionFinal.enabled) ? children : (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}); } if ((!sectionFinal.enabled && router.query.showPreview !== 'true') || props.editModeIsOutside) { return (0, jsx_runtime_1.jsx)(edit_section_1.EditSectionComponent, __assign({}, propsEditSectionComponent, { onSucess: onSucess, dynamicSectionProps: props })); } return ((0, jsx_runtime_1.jsx)(edit_section_1.EditSectionComponent, __assign({}, propsEditSectionComponent, { onSucess: onSucess, dynamicSectionProps: props, children: children }))); } function Section(props) { return ((0, jsx_runtime_1.jsx)(ui_1.Error, { name: refSectionID, children: (0, jsx_runtime_1.jsx)(RenderSection, __assign({}, props)) })); } return __assign(__assign(__assign({}, initialDataSection), sectionFinal), { linkFull: linkFull, images: sectionFinal.images && sectionFinal.images.length > 0 ? sectionFinal.images : [{ url: '/images/default-image.webp' }], Section: Section, jsonContent: jsonContentFinal }); } function mergeDeep(base, override, isRootJsonContent) { if (isRootJsonContent === void 0) { isRootJsonContent = false; } if (typeof base !== 'object' || base === null) return override; if (typeof override !== 'object' || override === null) return override; if (Array.isArray(base) && Array.isArray(override)) { return override.map(function (item, i) { return (i < base.length ? mergeDeep(base[i], item) : item); }); } var result = Array.isArray(base) ? [] : {}; var keys = new Set(__spreadArray(__spreadArray([], Object.keys(base), true), Object.keys(override), true)); keys.forEach(function (key) { if (isRootJsonContent && key === 'i18n') { result[key] = base[key]; } else if (key in override) { result[key] = mergeDeep(base[key], override[key], false); } else { result[key] = base[key]; } }); return result; } //# sourceMappingURL=index.js.map