fix filter buttons restoring hash after it's removed

This commit is contained in:
Chuck Dries 2022-07-10 18:08:31 -07:00
parent f021378813
commit cd51223c68
No known key found for this signature in database
GPG Key ID: A00B7AEAE1DC5BE6

View File

@ -16,22 +16,12 @@ const SORT_KEYS = {
date: [], date: [],
}; };
function removeHash() {
const url = new URL(
typeof window !== "undefined"
? window.location.href.toString()
: "https://chuckdries.com/photogallery/"
);
url.hash = "";
window.history.replaceState(null, "", url.href.toString());
window.removeEventListener('wheel', removeHash);
}
const GalleryPage = ({ data }) => { const GalleryPage = ({ data }) => {
const hash = const hash =
typeof window !== "undefined" ? window.location.hash.replace("#", "") : ""; typeof window !== "undefined" ? window.location.hash.replace("#", "") : "";
const [hashCleared, setHashCleared] = React.useState(false);
// ^ used just to force a re-render with the cleared hash value (I know, it's a smell for sure)
const [filterKeyword, _setKeyword] = React.useState(null); const [filterKeyword, _setKeyword] = React.useState(null);
const [sortKey, _setSortKey] = React.useState("rating"); const [sortKey, _setSortKey] = React.useState("rating");
const showDebug = const showDebug =
@ -76,6 +66,20 @@ const GalleryPage = ({ data }) => {
[_setSortKey, filterKeyword, hash] [_setSortKey, filterKeyword, hash]
); );
const removeHash = React.useCallback(() => {
console.log('rh')
const url = new URL(
typeof window !== "undefined"
? window.location.href.toString()
: "https://chuckdries.com/photogallery/"
);
url.hash = "";
window.history.replaceState(null, "", url.href.toString());
window.removeEventListener("wheel", removeHash);
setHashCleared(true);
}, []);
const scrollIntoView = React.useCallback(() => { const scrollIntoView = React.useCallback(() => {
if (!hash) { if (!hash) {
return; return;
@ -88,7 +92,7 @@ const GalleryPage = ({ data }) => {
block: "center", block: "center",
}); });
window.addEventListener("wheel", removeHash); window.addEventListener("wheel", removeHash);
}, [hash]); }, [hash, removeHash]);
React.useEffect(() => { React.useEffect(() => {
const url = new URL(window.location.toString()); const url = new URL(window.location.toString());