change prevent navigation to actually drop listener

This commit is contained in:
Nate Kelley 2025-04-24 11:41:31 -06:00
parent 93f3f880f9
commit 693b652d38
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
4 changed files with 29 additions and 47 deletions

View File

@ -17,6 +17,7 @@ stop:
pkill ollama
fast:
export RUST_LOG=debug
export CARGO_INCREMENTAL=1
cd .. && docker compose up -d redis && cd api && \
export RUST_LOG=debug && \
export CARGO_INCREMENTAL=1 && \
nice cargo watch -C server -x run

8
web/package-lock.json generated
View File

@ -68,7 +68,7 @@
"next-themes": "^0.4.6",
"papaparse": "^5.5.2",
"pluralize": "^8.0.0",
"posthog-js": "^1.236.5",
"posthog-js": "^1.236.6",
"prettier": "^3.5.3",
"prettier-plugin-tailwindcss": "^0.6.11",
"react": "^18",
@ -17986,9 +17986,9 @@
"license": "MIT"
},
"node_modules/posthog-js": {
"version": "1.236.5",
"resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.236.5.tgz",
"integrity": "sha512-2FrWVZwcLyeZAtdDckJaCfsk9m6DMdr/nVPVSqzF7yvm9pDsdbkvB3A16iqRj5L3EcqV2xWOcv8xWmKgdnNnqA==",
"version": "1.236.6",
"resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.236.6.tgz",
"integrity": "sha512-IX4fkn3HCK+ObdHr/AuWd+Ks7bgMpRpOQB93b5rDJAWkG4if4xFVUn5pgEjyCNeOO2GM1ECnp08q9tYNYEfwbA==",
"license": "MIT",
"dependencies": {
"core-js": "^3.38.1",

View File

@ -77,7 +77,7 @@
"next-themes": "^0.4.6",
"papaparse": "^5.5.2",
"pluralize": "^8.0.0",
"posthog-js": "^1.236.5",
"posthog-js": "^1.236.6",
"prettier": "^3.5.3",
"prettier-plugin-tailwindcss": "^0.6.11",
"react": "^18",

View File

@ -38,23 +38,24 @@ export const PreventNavigation: React.FC<PreventNavigationProps> = React.memo(
*/
const handleClick = useMemoizedFn((event: MouseEvent) => {
let originalTarget = event.target as HTMLElement;
let target = event.target as HTMLElement;
let href: string | null = null;
let originalEvent = event;
// Traverse up the DOM tree looking for an anchor tag with href
while (target && !href) {
if (target instanceof HTMLAnchorElement && target.href) {
href = target.href;
break;
}
target = target.parentElement as HTMLElement;
}
// Check if we're navigating to the same URL - if so, allow the navigation
if (href && new URL(href).pathname === window.location.pathname) {
return; // Allow navigation to the same URL
}
const newEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
detail: originalEvent.detail,
screenX: originalEvent.screenX,
screenY: originalEvent.screenY,
clientX: originalEvent.clientX,
clientY: originalEvent.clientY,
ctrlKey: originalEvent.ctrlKey,
altKey: originalEvent.altKey,
shiftKey: originalEvent.shiftKey,
metaKey: originalEvent.metaKey,
button: originalEvent.button,
buttons: originalEvent.buttons
});
if (isDirty) {
event.preventDefault();
@ -75,24 +76,6 @@ export const PreventNavigation: React.FC<PreventNavigationProps> = React.memo(
originalTarget.onclick = null;
}
// Create a new click event
const newEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
detail: originalEvent.detail,
screenX: originalEvent.screenX,
screenY: originalEvent.screenY,
clientX: originalEvent.clientX,
clientY: originalEvent.clientY,
ctrlKey: originalEvent.ctrlKey,
altKey: originalEvent.altKey,
shiftKey: originalEvent.shiftKey,
metaKey: originalEvent.metaKey,
button: originalEvent.button,
buttons: originalEvent.buttons
});
// Dispatch the event directly on the original target
originalTarget.dispatchEvent(newEvent);
@ -100,16 +83,14 @@ export const PreventNavigation: React.FC<PreventNavigationProps> = React.memo(
if (clickHandlers) {
originalTarget.onclick = clickHandlers;
}
// Re-attach our listeners after a short delay
setTimeout(() => {
document.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', handleClick);
});
}, 50);
};
setLeavingPage(true);
} else {
document.querySelectorAll('a').forEach((link) => {
link.removeEventListener('click', handleClick);
});
originalTarget.dispatchEvent(newEvent);
}
});