[WEB-1610] chore: re-implement reload confirmation hook. (#4880)

* [WEB-1610] chore: re-implement reload confirmation hook.

* chore: attach anchor event listner to window instead of body.
This commit is contained in:
Prateek Shourya 2024-06-20 12:19:29 +05:30 committed by GitHub
parent e04eb1a63d
commit 718453b332
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 41 deletions

View file

@ -2,14 +2,12 @@
// @ts-nocheck
import { NodeViewWrapper } from "@tiptap/react";
import { cn } from "src/lib/utils";
import { useRouter } from "next/navigation";
import { IMentionHighlight } from "src/types/mention-suggestion";
import { useEffect, useState } from "react";
// eslint-disable-next-line import/no-anonymous-default-export
export const MentionNodeView = (props) => {
// TODO: move it to web app
const router = useRouter();
const [highlightsState, setHighlightsState] = useState<IMentionHighlight[]>();
useEffect(() => {
@ -21,25 +19,20 @@ export const MentionNodeView = (props) => {
hightlights();
}, [props.extension.options]);
const handleClick = () => {
if (!props.extension.options.readonly) {
router.push(props.node.attrs.redirect_uri);
}
};
return (
<NodeViewWrapper className="mention-component inline w-fit">
<span
<a
href={props.node.attrs.redirect_uri}
target="_blank"
className={cn("mention rounded bg-custom-primary-100/20 px-1 py-0.5 font-medium text-custom-primary-100", {
"bg-yellow-500/20 text-yellow-500": highlightsState
? highlightsState.includes(props.node.attrs.entity_identifier)
: false,
"cursor-pointer": !props.extension.options.readonly,
})}
onClick={handleClick}
>
@{props.node.attrs.label}
</span>
</a>
</NodeViewWrapper>
);
};

View file

@ -1,41 +1,55 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { useState } from "react";
import { useCallback, useEffect, useState } from "react";
//TODO: remove temp flag isActive later and use showAlert as the source of truth
const useReloadConfirmations = (isActive = true) => {
const [showAlert, setShowAlert] = useState(false);
// const router = useAppRouter();
// const handleBeforeUnload = useCallback(
// (event: BeforeUnloadEvent) => {
// if (!isActive || !showAlert) return;
// event.preventDefault();
// event.returnValue = "";
// },
// [isActive, showAlert]
// );
const handleBeforeUnload = useCallback(
(event: BeforeUnloadEvent) => {
if (!isActive || !showAlert) return;
event.preventDefault();
event.returnValue = "";
},
[isActive, showAlert]
);
// const handleRouteChangeStart = useCallback(
// (url: string, { shallow }: { shallow: boolean }) => {
// if (!isActive || !showAlert) return;
// const leave = confirm("Are you sure you want to leave? Changes you made may not be saved.");
// if (!leave) {
// router.events.emit("routeChangeError", new Error("Route change cancelled by user"), url, shallow);
// throw "routeChange aborted.";
// }
// },
// [isActive, router.events, showAlert]
// );
const handleAnchorClick = useCallback(
(event: MouseEvent) => {
if (!isActive || !showAlert) return;
// Skip if event target is not available or defaultPrevented
if (!event.target || event.defaultPrevented) return;
// Skip control/command/option/alt+click
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
// check if the event target is an anchor or a child of an anchor tag
const eventTarget = event.target as HTMLElement;
if (!eventTarget.closest("a")) return; // This is intentionally not type safe
// check if anchor target is _blank
const anchorElement = eventTarget.closest("a") as HTMLAnchorElement;
const isAnchorTargetBlank = anchorElement.getAttribute("target") === "_blank";
if (isAnchorTargetBlank) return;
// show confirm dialog
const leave = confirm("Are you sure you want to leave? Changes you made may not be saved.");
if (!leave) {
event.preventDefault();
event.stopPropagation();
}
},
[isActive, showAlert]
);
// useEffect(() => {
// window.addEventListener("beforeunload", handleBeforeUnload);
// router.events.on("routeChangeStart", handleRouteChangeStart);
useEffect(() => {
// handle browser refresh
window.addEventListener("beforeunload", handleBeforeUnload, true);
// handle anchor tag click
window.addEventListener("click", handleAnchorClick, true);
// TODO: handle back / forward button click
// return () => {
// window.removeEventListener("beforeunload", handleBeforeUnload);
// router.events.off("routeChangeStart", handleRouteChangeStart);
// };
// }, [handleBeforeUnload, handleRouteChangeStart, router.events]);
return () => {
// cleanup
window.removeEventListener("beforeunload", handleBeforeUnload, true);
window.removeEventListener("click", handleAnchorClick, true);
};
}, [handleAnchorClick, handleBeforeUnload]);
return { setShowAlert };
};