From a90ec328f620d8ed1430f21dec265235528c0cf3 Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Wed, 8 Mar 2023 08:36:42 -0800 Subject: [PATCH] store sort and filter keys exclusively in url --- src/pages/photogallery.tsx | 69 +++++++++++++++++++++----------------- src/utils.ts | 4 +-- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/pages/photogallery.tsx b/src/pages/photogallery.tsx index c96bba4..783ab7b 100644 --- a/src/pages/photogallery.tsx +++ b/src/pages/photogallery.tsx @@ -1,6 +1,6 @@ import * as React from "react"; import * as R from "ramda"; -import { graphql, Link, PageProps } from "gatsby"; +import { graphql, Link, navigate, PageProps } from "gatsby"; import { Helmet } from "react-helmet"; // import { Picker, Item } from "@adobe/react-spectrum"; @@ -47,10 +47,13 @@ const GalleryPage = ({ data, location }: PageProps { @@ -104,11 +109,16 @@ const GalleryPage = ({ data, location }: PageProps { + window.addEventListener("wheel", removeHash); + }, [removeHash]); + const scrollIntoView = React.useCallback(() => { if (!hash) { return; @@ -120,23 +130,22 @@ const GalleryPage = ({ data, location }: PageProps { - const url = new URL(window.location.toString()); + // const url = new URL(window.location.toString()); - const sortKeyFromUrl = url.searchParams.get("sort"); - if (sortKeyFromUrl) { - _setSortKey(sortKeyFromUrl); - } else { - _setSortKey('rating') - } + // const sortKeyFromUrl = url.searchParams.get("sort"); + // if (sortKeyFromUrl) { + // _setSortKey(sortKeyFromUrl); + // } else { + // _setSortKey('rating') + // } - const filterKeyFromUrl = url.searchParams.get("filter"); - if (filterKeyFromUrl) { - _setKeyword(filterKeyFromUrl); - } + // const filterKeyFromUrl = url.searchParams.get("filter"); + // if (filterKeyFromUrl) { + // // _setKeyword(filterKeyFromUrl); + // } // hacky but it works for now setTimeout(() => { diff --git a/src/utils.ts b/src/utils.ts index bf033c3..8cada82 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -101,7 +101,7 @@ export const getShutterFractionFromExposureTime = (exposureTime: number) => { interface galleryPageUrlProps { keyword: string | null; - sortKey: string; + sortKey: string | null; } export const getGalleryPageUrl = ( @@ -123,7 +123,7 @@ export const getGalleryPageUrl = ( } } if (sortKey !== undefined) { - if (sortKey === "rating") { + if (sortKey === "rating" || sortKey === null) { url.searchParams.delete("sort"); } else { url.searchParams.set("sort", sortKey);