UNPKG

@wordpress/block-library

Version:
55 lines (54 loc) • 1.94 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_data = require("@wordpress/data"); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_block_editor = require("@wordpress/block-editor"); // packages/block-library/src/navigation-link/shared/use-is-invalid-link.js /** * A React hook to determine if a navigation link or submenu's parent link is invalid. * * @param {string} kind The kind of link (post-type, custom, taxonomy, etc). * @param {string} type The type of post (post, page, etc). * @param {number} id The post or term id. * @param {boolean} enabled Whether to enable the validation check. * * @return {Array} Array containing [isInvalid, isDraft] booleans. */ const useIsInvalidLink = (kind, type, id, enabled) => { const isPostType = kind === "post-type" || type === "post" || type === "page"; const hasId = Number.isInteger(id); const blockEditingMode = (0, _wordpress_block_editor.useBlockEditingMode)(); const { postStatus, isDeleted } = (0, _wordpress_data.useSelect)((select) => { if (!isPostType) return { postStatus: null, isDeleted: false }; if (blockEditingMode === "disabled" || !enabled) return { postStatus: null, isDeleted: false }; const { getEntityRecord, hasFinishedResolution } = select(_wordpress_core_data.store); const entityRecord = getEntityRecord("postType", type, id); const deleted = hasFinishedResolution("getEntityRecord", [ "postType", type, id ]) && entityRecord === void 0; return { postStatus: entityRecord?.status, isDeleted: deleted }; }, [ isPostType, blockEditingMode, enabled, type, id ]); return [isPostType && hasId && (isDeleted || postStatus && "trash" === postStatus), "draft" === postStatus]; }; exports.useIsInvalidLink = useIsInvalidLink; //# sourceMappingURL=use-is-invalid-link.cjs.map