investigating other approaches to removing page hash

TODO: look at https://github.com/gatsbyjs/gatsby/issues/19401
This commit is contained in:
Chuck Dries 2023-03-09 22:58:26 -08:00
parent a3ff9f6cbe
commit 713aa58b77
7 changed files with 76 additions and 27 deletions

Binary file not shown.

View File

@ -41,6 +41,7 @@
"gatsby-source-filesystem": "^5.0.0", "gatsby-source-filesystem": "^5.0.0",
"gatsby-transformer-sharp": "^5.0.0", "gatsby-transformer-sharp": "^5.0.0",
"kebab-case": "^1.0.1", "kebab-case": "^1.0.1",
"lodash.debounce": "^4.0.8",
"node-iptc": "^1.0.5", "node-iptc": "^1.0.5",
"node-vibrant": "3.1.6", "node-vibrant": "3.1.6",
"postcss": "^8.4.19", "postcss": "^8.4.19",
@ -60,6 +61,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/chroma-js": "^2.1.4", "@types/chroma-js": "^2.1.4",
"@types/lodash.debounce": "^4.0.7",
"@types/node": "^18.8.3", "@types/node": "^18.8.3",
"@types/ramda": "^0.28.15", "@types/ramda": "^0.28.15",
"@types/react": "^18.0.21", "@types/react": "^18.0.21",

View File

@ -2,6 +2,7 @@ import * as React from "react";
import * as R from "ramda"; import * as R from "ramda";
import { graphql, Link, navigate, PageProps } from "gatsby"; import { graphql, Link, navigate, PageProps } from "gatsby";
import { Helmet } from "react-helmet"; import { Helmet } from "react-helmet";
import debounce from "lodash.debounce";
// import { Picker, Item } from "@adobe/react-spectrum"; // import { Picker, Item } from "@adobe/react-spectrum";
import MasonryGallery from "../components/MasonryGallery"; import MasonryGallery from "../components/MasonryGallery";
@ -86,31 +87,26 @@ const GalleryPage = ({
[filterKeyword, hash, showDebug] [filterKeyword, hash, showDebug]
); );
const removeHash = React.useCallback(() => { const removeHash = React.useCallback(
if (!hash.length) { () => {
return; if (!hash.length) {
} return;
// const url = new URL( }
// typeof window !== "undefined" navigate(
// ? window.location.href.toString() getGalleryPageUrl({ sortKey, keyword: filterKeyword, showDebug }, ""),
// : "https://chuckdries.com/photogallery/" { replace: true, state: { suppressAutoscroll: true } }
// ); );
// window.removeEventListener("scroll", removeHash);
},
[hash, sortKey, filterKeyword, showDebug]
);
// url.hash = ""; // React.useEffect(() => {
// window.history.replaceState(null, "", url.href.toString()); // window.addEventListener("scroll", removeHash);
navigate(getGalleryPageUrl({ sortKey, keyword: filterKeyword, showDebug}, ""), { replace: true }) // return () => {
window.removeEventListener("wheel", removeHash); // window.removeEventListener("scroll", removeHash);
window.removeEventListener("touchmove", removeHash); // };
}, [hash, sortKey, filterKeyword, showDebug]); // }, [removeHash]);
React.useEffect(() => {
window.addEventListener("wheel", removeHash);
window.addEventListener("touchmove", removeHash);
return () => {
window.removeEventListener("wheel", removeHash);
window.removeEventListener("touchmove", removeHash);
}
}, [removeHash]);
React.useEffect(() => { React.useEffect(() => {
// hacky but it works for now // hacky but it works for now
@ -124,12 +120,18 @@ const GalleryPage = ({
console.log("⚠failed to find hash"); console.log("⚠failed to find hash");
return; return;
} }
console.log("scrolling into view manually"); console.log("scrolling into view manually", el.offsetTop);
el.scrollIntoView({ el.scrollIntoView({
block: hash.startsWith("all") ? "start" : "center", block: hash.startsWith("all") ? "start" : "center",
}); });
setTimeout(() => {
removeHash();
}, 500)
}); });
}, [hash]); }, [
hash,
removeHash
]);
const images: GalleryImage[] = React.useMemo(() => { const images: GalleryImage[] = React.useMemo(() => {
const sort = const sort =

View File

@ -92,6 +92,7 @@
body { body {
@apply bg-gray-100; @apply bg-gray-100;
overflow: auto;
/* @apply bg-black; */ /* @apply bg-black; */
/* @apply text-white; */ /* @apply text-white; */
} }

View File

@ -1,4 +1,4 @@
import React from "react"; import React, { useCallback, useRef } from "react";
import { pathOr } from "ramda"; import { pathOr } from "ramda";
// import kebabCase from 'lodash/kebabCase'; // import kebabCase from 'lodash/kebabCase';
@ -150,3 +150,29 @@ export function compareDates<T>(
const diff = -1 * (date1.getTime() - date2.getTime()); const diff = -1 * (date1.getTime() - date2.getTime());
return diff; return diff;
} }
/**
* Returns a memoized function that will only call the passed function when it hasn't been called for the wait period
* @param func The function to be called
* @param wait Wait period after function hasn't been called for
* @returns A memoized function that is debounced
*/
export const useDebouncedCallback = (func: Function, wait: number) => {
// Use a ref to store the timeout between renders
// and prevent changes to it from causing re-renders
const timeout = useRef<ReturnType<typeof setTimeout>>();
return useCallback(
(...args: any) => {
const later = () => {
clearTimeout(timeout.current!);
func(...args);
};
clearTimeout(timeout.current ?? undefined);
timeout.current = setTimeout(later, wait);
},
[func, wait]
);
};

View File

@ -4560,6 +4560,22 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/lodash.debounce@npm:^4.0.7":
version: 4.0.7
resolution: "@types/lodash.debounce@npm:4.0.7"
dependencies:
"@types/lodash": "*"
checksum: e873b2d77f89010876baba3437ef826b17221b98948e00b5590828334a481dea1c8f9d28543210e564adc53199584f42c3cb171f8b6c3614fefc0b4e0888679c
languageName: node
linkType: hard
"@types/lodash@npm:*":
version: 4.14.191
resolution: "@types/lodash@npm:4.14.191"
checksum: ba0d5434e10690869f32d5ea49095250157cae502f10d57de0a723fd72229ce6c6a4979576f0f13e0aa9fbe3ce2457bfb9fa7d4ec3d6daba56730a51906d1491
languageName: node
linkType: hard
"@types/lodash@npm:^4.14.53": "@types/lodash@npm:^4.14.53":
version: 4.14.170 version: 4.14.170
resolution: "@types/lodash@npm:4.14.170" resolution: "@types/lodash@npm:4.14.170"
@ -6464,6 +6480,7 @@ __metadata:
"@react-spectrum/provider": ^3.6.0 "@react-spectrum/provider": ^3.6.0
"@spectrum-icons/workflow": ^4.0.4 "@spectrum-icons/workflow": ^4.0.4
"@types/chroma-js": ^2.1.4 "@types/chroma-js": ^2.1.4
"@types/lodash.debounce": ^4.0.7
"@types/node": ^18.8.3 "@types/node": ^18.8.3
"@types/ramda": ^0.28.15 "@types/ramda": ^0.28.15
"@types/react": ^18.0.21 "@types/react": ^18.0.21
@ -6495,6 +6512,7 @@ __metadata:
gatsby-source-filesystem: ^5.0.0 gatsby-source-filesystem: ^5.0.0
gatsby-transformer-sharp: ^5.0.0 gatsby-transformer-sharp: ^5.0.0
kebab-case: ^1.0.1 kebab-case: ^1.0.1
lodash.debounce: ^4.0.8
node-iptc: ^1.0.5 node-iptc: ^1.0.5
node-vibrant: 3.1.6 node-vibrant: 3.1.6
postcss: ^8.4.19 postcss: ^8.4.19