Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { TOURNAMENTS_SORT } from "../constants/query_params";

const TournamentsFilter: React.FC = () => {
const t = useTranslations();
const { closeInfo } = useTournamentsSection();
const { closeInfo, defaultSort } = useTournamentsSection();
const { params, setParam, shallowNavigateToSearchParams } = useSearchParams();
const sortBy =
(params.get(TOURNAMENTS_SORT) as TournamentsSortBy) ??
TournamentsSortBy.Featured;
(params.get(TOURNAMENTS_SORT) as TournamentsSortBy) ?? defaultSort;

const sortOptions: SelectOption<TournamentsSortBy>[] = [
{ value: TournamentsSortBy.Featured, label: t("featured") },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { createContext, useContext, useMemo, useState } from "react";

import { TournamentPreview } from "@/types/projects";
import { TournamentPreview, TournamentsSortBy } from "@/types/projects";

import { selectTournamentsForSection } from "../helpers";
import { useTournamentFilters } from "../hooks/use_tournament_filters";
Expand All @@ -13,6 +13,7 @@ type TournamentsSectionCtxValue = {
items: TournamentPreview[];
count: number;
nowTs?: number;
defaultSort: TournamentsSortBy;
infoOpen: boolean;
toggleInfo: () => void;
closeInfo: () => void;
Expand All @@ -31,12 +32,18 @@ export function TournamentsSectionProvider(props: {
const { tournaments, current, children, nowTs } = props;
const [infoOpen, setInfoOpen] = useState(true);

const defaultSort =
current === "archived"
? TournamentsSortBy.StartDateDesc
: TournamentsSortBy.Featured;

const sectionItems = useMemo(
() => selectTournamentsForSection(tournaments, current),
[tournaments, current]
);

const { filtered } = useTournamentFilters(sectionItems);
const filterOpts = useMemo(() => ({ defaultSort }), [defaultSort]);
const { filtered } = useTournamentFilters(sectionItems, filterOpts);

const value = useMemo<TournamentsSectionCtxValue>(
() => ({
Expand All @@ -45,10 +52,11 @@ export function TournamentsSectionProvider(props: {
count: filtered.length,
infoOpen,
nowTs,
defaultSort,
toggleInfo: () => setInfoOpen((v) => !v),
closeInfo: () => setInfoOpen(false),
}),
[current, filtered, infoOpen, nowTs]
[current, filtered, infoOpen, nowTs, defaultSort]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import { useMemo } from "react";

import useSearchParams from "@/hooks/use_search_params";
import { TournamentPreview } from "@/types/projects";
import { TournamentPreview, TournamentsSortBy } from "@/types/projects";

import { filterTournamentsFromParams } from "../helpers/tournament_filters";

type Options = {
disableClientSort?: boolean;
defaultSort?: TournamentsSortBy;
};

export function useTournamentFilters(
Expand Down