UNPKG

@wordpress/editor

Version:
121 lines (119 loc) 4.38 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/editor/src/components/provider/use-entity-contains-snapshot.js var use_entity_contains_snapshot_exports = {}; __export(use_entity_contains_snapshot_exports, { SNAPSHOT_STATUS_SYNC_WAIT_MS: () => SNAPSHOT_STATUS_SYNC_WAIT_MS, default: () => useEntityContainsSnapshot }); module.exports = __toCommonJS(use_entity_contains_snapshot_exports); var import_element = require("@wordpress/element"); var import_data = require("@wordpress/data"); var import_core_data = require("@wordpress/core-data"); var import_store = require("../../store/index.cjs"); var import_lock_unlock = require("../../lock-unlock.cjs"); var { entityContainsSnapshot } = (0, import_lock_unlock.unlock)(import_core_data.privateApis); var SNAPSHOT_STATUS_SYNC_WAIT_MS = 3e3; function useEntityContainsSnapshot({ postType, postId, snapshot }) { const registry = (0, import_data.useRegistry)(); const [snapshotStatus, setSnapshotStatus] = (0, import_element.useState)("pending"); const isPendingRef = (0, import_element.useRef)(false); const [hasWaitExpired, setHasWaitExpired] = (0, import_element.useState)(false); const { syncStatus, isCollaborationEnabledForPost } = (0, import_data.useSelect)( (select) => { if (!snapshot) { return { syncStatus: void 0, isCollaborationEnabledForPost: false }; } const connectionStatus = (0, import_lock_unlock.unlock)( select(import_core_data.store) ).getEntitySyncConnectionStatus("postType", postType, postId); return { syncStatus: connectionStatus?.status, isCollaborationEnabledForPost: (0, import_lock_unlock.unlock)( select(import_store.store) ).isCollaborationEnabledForCurrentPost() }; }, [postType, postId, snapshot] ); (0, import_element.useLayoutEffect)(() => { const canDefer = snapshot && (0, import_lock_unlock.unlock)( registry.select(import_store.store) ).isCollaborationEnabledForCurrentPost(); if (!canDefer) { setSnapshotStatus("missing"); return; } isPendingRef.current = true; const timeoutId = setTimeout(() => { setHasWaitExpired(true); }, SNAPSHOT_STATUS_SYNC_WAIT_MS); return () => clearTimeout(timeoutId); }, []); (0, import_element.useEffect)(() => { if (!isPendingRef.current) { return; } const containsSnapshot = entityContainsSnapshot( "postType", postType, postId, snapshot ); const isCollaborationEnabled = (0, import_lock_unlock.unlock)( registry.select(import_store.store) ).isCollaborationEnabledForCurrentPost(); const connectionStatus = (0, import_lock_unlock.unlock)( registry.select(import_core_data.store) ).getEntitySyncConnectionStatus("postType", postType, postId); let nextSnapshotStatus; if (containsSnapshot) { nextSnapshotStatus = "present"; } else if (!isCollaborationEnabled) { nextSnapshotStatus = "missing"; } else if ("disconnected" === connectionStatus?.status) { nextSnapshotStatus = "missing"; } else if (hasWaitExpired) { nextSnapshotStatus = "missing"; } else { return; } isPendingRef.current = false; setSnapshotStatus(nextSnapshotStatus); }, [ syncStatus, hasWaitExpired, isCollaborationEnabledForPost, postType, postId ]); return snapshotStatus; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SNAPSHOT_STATUS_SYNC_WAIT_MS }); //# sourceMappingURL=use-entity-contains-snapshot.cjs.map