From d25079586b484d4857d444760512f5197bfc2dbd Mon Sep 17 00:00:00 2001 From: Ying Date: Sat, 4 Jul 2026 13:03:44 +0100 Subject: [PATCH] fix: scope mentors page default view to the open cycle's type On first load, default the mentorship-type filter to the open cycle's type (from the API's openCycle field) so an ad-hoc cycle lists only ad-hoc mentors and a long-term cycle only long-term mentors. The backend then applies the full type + month scoping. Applied once via a ref so an explicit "All" selection is preserved. Closes #304 --- src/pages/mentorship/mentors.tsx | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/pages/mentorship/mentors.tsx b/src/pages/mentorship/mentors.tsx index 090b5b9..a01895d 100644 --- a/src/pages/mentorship/mentors.tsx +++ b/src/pages/mentorship/mentors.tsx @@ -16,7 +16,7 @@ import { Collapse, } from '@mui/material'; import { useRouter } from 'next/router'; -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { BreadCrumbsDynamic, MentorProfileCard, Title } from '@components'; import { @@ -63,6 +63,9 @@ const MentorsPage = () => { const [filterSection, setFilterSection] = useState( null, ); + // Ensures the open-cycle type default is applied only once, so an explicit + // "All" selection is never overridden. + const didApplyCycleDefaultRef = useRef(false); // Initialize filter state from URL query params useEffect(() => { @@ -127,6 +130,35 @@ const MentorsPage = () => { setMentorsState([]); setFilterSection(null); } + + // On first load, scope the default view to the currently open cycle's + // mentorship type so an ad-hoc cycle lists only ad-hoc mentors (and a + // long-term cycle only long-term mentors). The backend then applies the + // full type + month scoping. Applied once; an explicit "All" selection + // clears the filter and is preserved. + const openCycle = result?.openCycle ?? result?.data?.openCycle; + if ( + !didApplyCycleDefaultRef.current && + !query.mentorshipTypes && + openCycle?.active && + FILTER_MENTORSHIP_TYPES_OPTIONS.some( + (option) => option.value === openCycle.mentorshipType, + ) + ) { + didApplyCycleDefaultRef.current = true; + setSelectedMentorshipType(openCycle.mentorshipType); + router.replace( + { + pathname: router.pathname, + query: { + ...router.query, + mentorshipTypes: openCycle.mentorshipType, + }, + }, + undefined, + { shallow: true }, + ); + } } catch (err) { // eslint-disable-next-line no-console console.error('Failed to fetch mentors with params', err);