From 4acfc747d7d4a756eba83d26f6118ff26352bc15 Mon Sep 17 00:00:00 2001 From: Konstantin Pinaev Date: Thu, 21 Nov 2024 15:58:54 +0100 Subject: [PATCH] DIGG-465: Refactring scrollposition --- .../searchPageProviderSettings.ts | 6 +- .../content/Search/SearchResults/index.tsx | 60 ++++++++++--------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/components/content/Search/SearchPage/SearchPageEntryScape/searchPageProviderSettings.ts b/components/content/Search/SearchPage/SearchPageEntryScape/searchPageProviderSettings.ts index b068ba6f..e809f90b 100644 --- a/components/content/Search/SearchPage/SearchPageEntryScape/searchPageProviderSettings.ts +++ b/components/content/Search/SearchPage/SearchPageEntryScape/searchPageProviderSettings.ts @@ -214,14 +214,14 @@ export function createSearchProviderSettings(env: EnvSettings, lang: string) { dcatType: "choice", dcatFilterEnabled: true, indexOrder: 0, - group: "group1", + group: "type", }, { resource: "http://purl.org/dc/terms/publisher", dcatProperty: "dcterms:publisher", type: ESType.uri, indexOrder: 1, - group: "group1", + group: "actor", }, ], }, @@ -250,7 +250,7 @@ export function createSearchProviderSettings(env: EnvSettings, lang: string) { type: ESType.uri, dcatProperty: "dcterms:type", indexOrder: 0, - group: "group1", + group: "default", }, ], }, diff --git a/components/content/Search/SearchResults/index.tsx b/components/content/Search/SearchResults/index.tsx index def9fd46..7743340f 100644 --- a/components/content/Search/SearchResults/index.tsx +++ b/components/content/Search/SearchResults/index.tsx @@ -1,7 +1,6 @@ import { useEffect, useState, FC, Dispatch, SetStateAction } from "react"; import { SearchMode } from "@/components/content/Search/SearchFilters"; import { FileFormatBadge } from "@/components/global/FileFormatBadge"; -import { clearLocalStorage } from "@/utilities"; import useTranslation from "next-translate/useTranslation"; import { SearchSortOrder, SearchContextData } from "@/providers/SearchProvider"; import Link from "next/link"; @@ -34,20 +33,23 @@ const searchFocus = () => { } }; -const saveCurrentScrollPos = () => { - if (typeof localStorage != "undefined" && typeof location != "undefined") { - localStorage.setItem( - `ScrollposY_${location.search}`, - JSON.stringify(window.scrollY), - ); - } -}; +const SCROLL_POS_PREFIX = "ScrollPosY_" as const; -const clearCurrentScrollPos = () => { - if (typeof localStorage != "undefined" && typeof location != "undefined") { - localStorage.setItem(`ScrollposY_${location.search}`, "0"); - } -}; +function getScrollKey(search: string): string { + return `${SCROLL_POS_PREFIX}${search}`; +} + +function saveCurrentScrollPos(): void { + if (typeof window === "undefined") return; + const key = getScrollKey(window.location.search); + localStorage.setItem(key, window.scrollY.toString()); +} + +function clearCurrentScrollPos(): void { + if (typeof window === "undefined") return; + const key = getScrollKey(window.location.search); + localStorage.removeItem(key); +} /** * Adds sorting options to the search-results @@ -184,22 +186,24 @@ export const SearchResults: FC = ({ const { t } = useTranslation(); const hvd = "http://data.europa.eu/r5r/applicableLegislation"; const national = "http://purl.org/dc/terms/subject"; - const searchKey = typeof location != "undefined" ? location.search : "server"; - const posY = - typeof localStorage != "undefined" - ? localStorage.getItem(`ScrollposY_${searchKey}`) - : "0"; useEffect(() => { - clearLocalStorage("ScrollposY_", `ScrollposY_${searchKey}`); - }, [searchKey]); + // Restore scroll position only after results are loaded + if (!search.loadingHits && search.result.hits!.length > 0) { + const scrollKey = getScrollKey(window.location.search); + const savedPosition = localStorage.getItem(scrollKey); + + if (savedPosition) { + window.scrollTo(0, parseInt(savedPosition, 10)); + localStorage.removeItem(scrollKey); // Clear after restoring + } + } + }, [search.loadingHits, search.result.hits]); + // Set compact view state useEffect(() => { - const count = search.result.count || -1; - count > 0 && posY && posY != "0" && window.scrollTo(0, parseInt(posY, 10)); - if (search.request.compact && search.request.compact) setCompact(false); - else setCompact(true); - }); + setCompact(!search.request.compact); + }, [search.request.compact]); const changePage = (page: number) => { if (search.result.pages || 0 > 1) { @@ -310,7 +314,9 @@ export const SearchResults: FC = ({ search.allFacets && !search.loadingFacets && hit.metadata["inScheme_resource"] && ( - {hit.metadata["inScheme_resource"]} + + {hit.metadata["inScheme_resource"]} + )} {isCompact && hit.descriptionLang && (