0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-21 07:12:54 +00:00

refctor logic

This commit is contained in:
Víctor Losada Hernández
2024-09-07 19:12:59 +02:00
parent 9e1a532105
commit 3360b4e829

View File

@@ -79,17 +79,15 @@ const BrewRenderer = (props)=>{
const locationHash = window.location.hash;
const iframe = document.getElementById('BrewRenderer');
// Regular expression to match page IDs like '#p1'
const pageIdRegex = /^#p\d+$/;
iframe.addEventListener('load', () => {
const handleIframeLoad = () => {
setTimeout(() => {
if (pageIdRegex.test(locationHash)) {
// Extract page number from the ID
const pageNumber = parseInt(locationHash.slice(2));
scrollToPage(pageNumber - 1);
} else {
// Treat it as an element ID
const elementId = locationHash.slice(1); // Remove the leading '#'
const elementId = locationHash.slice(1);
if (elementId) {
getPageContainingElement(elementId)
.then((pageNumber) => {
@@ -103,10 +101,16 @@ const BrewRenderer = (props)=>{
}
}
}, 100);
});
};
if (locationHash) {
iframe.addEventListener('load', handleIframeLoad);
}
// Cleanup function for removing the resize event listener
return () => {
if (locationHash) {
iframe.removeEventListener('load', handleIframeLoad);
}
window.removeEventListener('resize', updateSize);
};
}, []);