Skip to content

Commit

Permalink
DIGG-465: Refactring scrollposition
Browse files Browse the repository at this point in the history
  • Loading branch information
Kopin1 committed Nov 21, 2024
1 parent c04d8bf commit 4acfc74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
},
Expand Down Expand Up @@ -250,7 +250,7 @@ export function createSearchProviderSettings(env: EnvSettings, lang: string) {
type: ESType.uri,
dcatProperty: "dcterms:type",
indexOrder: 0,
group: "group1",
group: "default",
},
],
},
Expand Down
60 changes: 33 additions & 27 deletions components/content/Search/SearchResults/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -184,22 +186,24 @@ export const SearchResults: FC<SearchResultsProps> = ({
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) {
Expand Down Expand Up @@ -310,7 +314,9 @@ export const SearchResults: FC<SearchResultsProps> = ({
search.allFacets &&
!search.loadingFacets &&
hit.metadata["inScheme_resource"] && (
<span>{hit.metadata["inScheme_resource"]}</span>
<span className="inScheme_resource">
{hit.metadata["inScheme_resource"]}
</span>
)}

{isCompact && hit.descriptionLang && (
Expand Down

0 comments on commit 4acfc74

Please sign in to comment.