Skip to content

Commit

Permalink
better hash-change-aware code
Browse files Browse the repository at this point in the history
  • Loading branch information
RheingoldRiver committed Oct 6, 2023
1 parent b491ce8 commit c8f8a86
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions src/components/GameStateProvider/GameStateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export default function GameStateProvider({ children }: { children: ReactNode })
const [actionHistory, setActionHistory] = useState<Action[]>([]);
const navigate = useNavigate();

const prevHash = useRef(window.location.hash);

const updateUrl = useRef(
debounce((config: Partial<UrlConfig>) => {
const finalConfig = {
Expand All @@ -74,11 +72,8 @@ export default function GameStateProvider({ children }: { children: ReactNode })
...config,
};
const newHash = serializeUrl(finalConfig);
console.log("New Hash:", newHash);
console.log("Previous Hash:", prevHash.current);
if (newHash === prevHash.current) return;
if (`#/${newHash}` === window.location.hash) return;
navigate("/" + newHash);
prevHash.current = newHash; // Update the previous hash
}, 250)
);

Expand All @@ -88,7 +83,6 @@ export default function GameStateProvider({ children }: { children: ReactNode })

useEffect(() => {
window.addEventListener("hashchange", function () {
if (window.location.hash === prevHash.current) return;
window.location.reload();
});
});
Expand Down

0 comments on commit c8f8a86

Please sign in to comment.