UNPKG

@wordpress/block-library

Version:
50 lines (49 loc) • 1.7 kB
import { useSelect } from "@wordpress/data"; import { store } from "@wordpress/core-data"; import { useBlockEditingMode } from "@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 = useBlockEditingMode(); const { postStatus, isDeleted } = useSelect((select) => { if (!isPostType) return { postStatus: null, isDeleted: false }; if (blockEditingMode === "disabled" || !enabled) return { postStatus: null, isDeleted: false }; const { getEntityRecord, hasFinishedResolution } = select(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]; }; export { useIsInvalidLink }; //# sourceMappingURL=use-is-invalid-link.mjs.map