From d3b187ae4b28f0e7d5af8821154abced740b3c3b Mon Sep 17 00:00:00 2001 From: Jonathan Poltak Samosir Date: Wed, 29 May 2024 13:18:46 +0700 Subject: [PATCH] Block page nav away during ribbon write RPC ops --- .../ribbon/react/containers/ribbon/index.tsx | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/in-page-ui/ribbon/react/containers/ribbon/index.tsx b/src/in-page-ui/ribbon/react/containers/ribbon/index.tsx index 4a25aeba83..adfc4bad9b 100644 --- a/src/in-page-ui/ribbon/react/containers/ribbon/index.tsx +++ b/src/in-page-ui/ribbon/react/containers/ribbon/index.tsx @@ -36,7 +36,7 @@ export default class RibbonContainer extends StatefulUIElement< > { private ribbonRef = React.createRef() - constructor(props) { + constructor(props: RibbonContainerProps) { super( props, new RibbonContainerLogic({ @@ -48,6 +48,15 @@ export default class RibbonContainer extends StatefulUIElement< this.ribbonRef?.current?.focusCreateForm(), }), ) + + props.events.on('bookmarkPage', () => + this.processEvent('toggleBookmark', null), + ) + props.events.on('openSpacePickerInRibbon', () => + this.processEvent('setShowListsPicker', { value: true }), + ) + props.inPageUI.events.on('ribbonAction', this.handleExternalAction) + window.addEventListener('beforeunload', this.handleBeforeUnload) } private get normalizedPageUrl(): string | null { @@ -56,24 +65,13 @@ export default class RibbonContainer extends StatefulUIElement< : null } - async componentDidMount() { - await super.componentDidMount() - this.props.inPageUI.events.on('ribbonAction', this.handleExternalAction) - - this.props.events.on('bookmarkPage', () => - this.processEvent('toggleBookmark', null), - ) - this.props.events.on('openSpacePickerInRibbon', () => - this.processEvent('setShowListsPicker', { value: true }), - ) - } - async componentWillUnmount() { - await super.componentWillUnmount() this.props.inPageUI.events.removeListener( 'ribbonAction', this.handleExternalAction, ) + window.removeEventListener('beforeunload', this.handleBeforeUnload) + await super.componentWillUnmount() } componentDidUpdate(prevProps: RibbonContainerProps) { @@ -90,6 +88,14 @@ export default class RibbonContainer extends StatefulUIElement< } } + // Block user nav away when some write RPC op is occurring + private handleBeforeUnload = (e: BeforeUnloadEvent) => { + let shouldBlockUnload = this.state.bookmark.loadState === 'running' + if (shouldBlockUnload) { + e.preventDefault() + } + } + private whichFeed = () => { if (process.env.NODE_ENV === 'production') { return 'https://memex.social/feed'