@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
72 lines (71 loc) • 2.22 kB
JavaScript
// packages/editor/src/components/provider/use-autosave-notice.js
import { useLayoutEffect } from "@wordpress/element";
import { useDispatch, useRegistry } from "@wordpress/data";
import { __ } from "@wordpress/i18n";
import { store as noticesStore } from "@wordpress/notices";
import { getQueryArg } from "@wordpress/url";
import { store as editorStore } from "../../store/index.mjs";
import { unlock } from "../../lock-unlock.mjs";
import useEntityContainsSnapshot from "./use-entity-contains-snapshot.mjs";
function showAutosaveExistsNotice({
autosave,
createWarningNotice,
registry,
setCurrentRevisionId
}) {
const autosaveId = Number(getQueryArg(autosave.editLink, "revision"));
createWarningNotice(
__(
"There is an autosave of this post that is more recent than the version below."
),
{
id: "autosave-exists",
actions: [
{
label: __("View the autosave"),
...autosaveId ? {
onClick: () => {
const { disableVisualRevisions } = registry.select(editorStore).getEditorSettings();
if (disableVisualRevisions) {
window.location.href = autosave.editLink;
return;
}
setCurrentRevisionId(autosaveId);
}
} : { url: autosave.editLink }
}
]
}
);
}
function useAutosaveNotice({ post, recovery, settings }) {
const registry = useRegistry();
const { createWarningNotice } = useDispatch(noticesStore);
const { setCurrentRevisionId } = unlock(useDispatch(editorStore));
const snapshotStatus = useEntityContainsSnapshot({
postType: post.type,
postId: post.id,
snapshot: recovery ? void 0 : settings.autosave?.crdtSnapshot
});
useLayoutEffect(() => {
if (recovery || !settings.autosave) {
return;
}
if ("present" === snapshotStatus) {
return;
}
if ("pending" === snapshotStatus) {
return;
}
showAutosaveExistsNotice({
autosave: settings.autosave,
createWarningNotice,
registry,
setCurrentRevisionId
});
}, [snapshotStatus]);
}
export {
useAutosaveNotice as default
};
//# sourceMappingURL=use-autosave-notice.mjs.map