@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 3.53 kB
Source Map (JSON)
{"version":3,"file":"use-is-invalid-link.cjs","names":["useBlockEditingMode","useSelect","coreStore"],"sources":["../../../src/navigation-link/shared/use-is-invalid-link.js"],"sourcesContent":["import { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useBlockEditingMode } from '@wordpress/block-editor';\n\n/**\n * A React hook to determine if a navigation link or submenu's parent link is invalid.\n *\n * @param {string} kind The kind of link (post-type, custom, taxonomy, etc).\n * @param {string} type The type of post (post, page, etc).\n * @param {number} id The post or term id.\n * @param {boolean} enabled Whether to enable the validation check.\n *\n * @return {Array} Array containing [isInvalid, isDraft] booleans.\n */\nexport const useIsInvalidLink = ( kind, type, id, enabled ) => {\n\tconst isPostType =\n\t\tkind === 'post-type' || type === 'post' || type === 'page';\n\tconst hasId = Number.isInteger( id );\n\tconst blockEditingMode = useBlockEditingMode();\n\n\tconst { postStatus, isDeleted } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! isPostType ) {\n\t\t\t\treturn { postStatus: null, isDeleted: false };\n\t\t\t}\n\n\t\t\t// Fetching the posts status is an \"expensive\" operation. Especially for sites with large navigations.\n\t\t\t// When the block is rendered in a template or other disabled contexts we can skip this check in order\n\t\t\t// to avoid all these additional requests that don't really add any value in that mode.\n\t\t\tif ( blockEditingMode === 'disabled' || ! enabled ) {\n\t\t\t\treturn { postStatus: null, isDeleted: false };\n\t\t\t}\n\n\t\t\tconst { getEntityRecord, hasFinishedResolution } =\n\t\t\t\tselect( coreStore );\n\t\t\tconst entityRecord = getEntityRecord( 'postType', type, id );\n\t\t\tconst hasResolved = hasFinishedResolution( 'getEntityRecord', [\n\t\t\t\t'postType',\n\t\t\t\ttype,\n\t\t\t\tid,\n\t\t\t] );\n\n\t\t\t// If resolution has finished and entityRecord is undefined, the entity was deleted.\n\t\t\tconst deleted = hasResolved && entityRecord === undefined;\n\n\t\t\treturn {\n\t\t\t\tpostStatus: entityRecord?.status,\n\t\t\t\tisDeleted: deleted,\n\t\t\t};\n\t\t},\n\t\t[ isPostType, blockEditingMode, enabled, type, id ]\n\t);\n\n\t// Check Navigation Link validity if:\n\t// 1. Link is 'post-type'.\n\t// 2. It has an id.\n\t// 3. It's neither null, nor undefined, as valid items might be either of those while loading.\n\t// If those conditions are met, check if\n\t// 1. The post status is trash (trashed).\n\t// 2. The entity doesn't exist (deleted).\n\t// If either of those is true, invalidate.\n\tconst isInvalid =\n\t\tisPostType &&\n\t\thasId &&\n\t\t( isDeleted || ( postStatus && 'trash' === postStatus ) );\n\tconst isDraft = 'draft' === postStatus;\n\n\treturn [ isInvalid, isDraft ];\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAcA,MAAa,oBAAqB,MAAM,MAAM,IAAI,YAAa;CAC9D,MAAM,aACL,SAAS,eAAe,SAAS,UAAU,SAAS;CACrD,MAAM,QAAQ,OAAO,UAAW,EAAG;CACnC,MAAM,oBAAA,GAAmBA,wBAAAA,oBAAAA,CAAoB;CAE7C,MAAM,EAAE,YAAY,eAAA,GAAcC,gBAAAA,UAAAA,EAC/B,WAAY;EACb,IAAK,CAAE,YACN,OAAO;GAAE,YAAY;GAAM,WAAW;EAAM;EAM7C,IAAK,qBAAqB,cAAc,CAAE,SACzC,OAAO;GAAE,YAAY;GAAM,WAAW;EAAM;EAG7C,MAAM,EAAE,iBAAiB,0BACxB,OAAQC,qBAAAA,KAAU;EACnB,MAAM,eAAe,gBAAiB,YAAY,MAAM,EAAG;EAQ3D,MAAM,UAPc,sBAAuB,mBAAmB;GAC7D;GACA;GACA;EACD,CAG0B,KAAK,iBAAiB,KAAA;EAEhD,OAAO;GACN,YAAY,cAAc;GAC1B,WAAW;EACZ;CACD,GACA;EAAE;EAAY;EAAkB;EAAS;EAAM;CAAG,CACnD;CAgBA,OAAO,CALN,cACA,UACE,aAAe,cAAc,YAAY,aAC5B,YAAY,UAEA;AAC7B"}