Skip to content

Commit

Permalink
Merge pull request #1297 from thunderstore-io/01-20-fix_issue_searchp…
Browse files Browse the repository at this point in the history
…arams_resetting_on_page_reload

Fix issue: searchParams resetting on page reload
  • Loading branch information
Oksamies authored Jan 20, 2025
2 parents 14140d4 + 877d1be commit 131855f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const OFF = CATEGORY_STATES[0];

interface Props {
searchValue: string;
setSearchValue: (v: "") => void;
setSearchValue: (v: string) => void;
categories: CategorySelection[];
setCategories: (v: CategorySelection[]) => void;
}
Expand All @@ -30,8 +30,10 @@ export const CategoryTagCloud = (props: Props) => {
categories.map((c) => (c.id === id ? { ...c, selection: OFF } : c))
);

const clearAll = () =>
const clearAll = () => {
setCategories(categories.map((c) => ({ ...c, selection: OFF })));
setSearchValue("");
};

return (
<div className={styles.root}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export enum PackageOrderOptions {
}
export type PackageOrderOptionsType = `${PackageOrderOptions}`;

export function isPackageOrderOptions(value: string) {
const enumValues = Object.values(PackageOrderOptions) as string[];
return enumValues.includes(value);
}

const selectOptions = [
{
value: PackageOrderOptions.Updated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { StalenessIndicator } from "@thunderstore/cyberstorm/src/components/StalenessIndicator/StalenessIndicator";
import { PackageCount } from "./PackageCount";
import {
isPackageOrderOptions,
PackageOrder,
PackageOrderOptions,
PackageOrderOptionsType,
Expand Down Expand Up @@ -77,14 +78,47 @@ export function PackageSearch(props: Props) {

const [searchParams, setSearchParams] = useSearchParams();

const initialOrder = searchParams.get("ordering");
const initialSection = searchParams.get("section");
const initialDeprecated = searchParams.get("deprecated");
const initialNsfw = searchParams.get("nsfw");
const initialPage = searchParams.get("page");
const initialIncludedCategories = searchParams.get("includedCategories");
const initialExcludedCategories = searchParams.get("excludedCategories");

const [searchParamsBlob, setSearchParamsBlob] = useState<SearchParamsType>({
order: undefined,
section: allSections.length === 0 ? "" : allSections[0]?.uuid,
deprecated: false,
nsfw: false,
page: 1,
includedCategories: "",
excludedCategories: "",
order:
initialOrder && isPackageOrderOptions(initialOrder)
? (initialOrder as PackageOrderOptionsType)
: undefined,
section:
allSections.length === 0 ? "" : initialSection ?? allSections[0]?.uuid,
deprecated:
initialDeprecated === null
? false
: initialDeprecated === "true"
? true
: initialDeprecated === "false"
? false
: false,
nsfw:
initialNsfw === null
? false
: initialNsfw === "true"
? true
: initialNsfw === "false"
? false
: false,
page:
initialPage &&
!Number.isNaN(Number.parseInt(initialPage)) &&
Number.isSafeInteger(Number.parseInt(initialPage))
? Number.parseInt(initialPage)
: 1,
includedCategories:
initialIncludedCategories !== null ? initialIncludedCategories : "",
excludedCategories:
initialExcludedCategories !== null ? initialExcludedCategories : "",
});

// TODO: Disabled until we can figure out how a proper way to display skeletons
Expand Down

0 comments on commit 131855f

Please sign in to comment.