Skip to content

Commit

Permalink
make sure to only go back if path hasn't changed
Browse files Browse the repository at this point in the history
  • Loading branch information
SuspiciousLookingOwl committed Jan 6, 2024
1 parent c86ec5c commit 2b2677f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/libs/common/hooks/hash-state.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ type Params = {
};

export const useHashState = (params: Params) => {
let currentPath = "";
let currentHash = "";
let windowHash = window.location.hash;

const push = () => {
const randomHash = "#" + Math.random().toString(36).substring(7);
history.pushState(null, "", randomHash);
window.dispatchEvent(new HashChangeEvent("hashchange"));
currentPath = window.location.pathname;
currentHash = randomHash;
};

const back = () => {
if (currentHash && windowHash === currentHash) {
history.back();
}
setTimeout(() => {
if (currentHash && windowHash === currentHash && currentPath === window.location.pathname) {
history.back();
}
});
};

onMount(() => {
Expand Down

0 comments on commit 2b2677f

Please sign in to comment.