From eb2cba3af1ad5ae09c563b073f75bfa629a6ae5a Mon Sep 17 00:00:00 2001 From: gosangam Date: Tue, 5 May 2026 01:48:37 +0530 Subject: [PATCH] fix: ts fix --- app/letters/filter-year-browser.tsx | 21 ++++++++---------- .../year/[year]/year-month-browser.tsx | 22 +++++++++---------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/app/letters/filter-year-browser.tsx b/app/letters/filter-year-browser.tsx index 7f701a86..28ca8a1d 100644 --- a/app/letters/filter-year-browser.tsx +++ b/app/letters/filter-year-browser.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState, useMemo, useRef, useEffect } from "react"; +import { useState, useMemo, useRef } from "react"; import Link from "next/link"; import { Card, CardContent } from "@/components/ui/card"; import { buttonVariants } from "@/components/ui/button"; @@ -402,21 +402,18 @@ export function FilterYearBrowser({ }) { const yearGroups = useMemo(() => buildYearGroups(letters), [letters]); - const [activeYear, setActiveYear] = useState( + const [activeYearRaw, setActiveYear] = useState( yearGroups.length > 0 ? yearGroups[0].year : 0, ); - const contentRef = useRef(null); + // Derive a safe active year — if the raw value is no longer valid, fall back to the first group + const activeYear = useMemo(() => { + if (yearGroups.length === 0) return activeYearRaw; + if (yearGroups.find((g) => g.year === activeYearRaw)) return activeYearRaw; + return yearGroups[0].year; + }, [yearGroups, activeYearRaw]); - // Keep active year valid - useEffect(() => { - if ( - yearGroups.length > 0 && - !yearGroups.find((g) => g.year === activeYear) - ) { - setActiveYear(yearGroups[0].year); - } - }, [yearGroups, activeYear]); + const contentRef = useRef(null); const handleYearSelect = (year: number) => { setActiveYear(year); diff --git a/app/letters/year/[year]/year-month-browser.tsx b/app/letters/year/[year]/year-month-browser.tsx index cd245e72..26bb3640 100644 --- a/app/letters/year/[year]/year-month-browser.tsx +++ b/app/letters/year/[year]/year-month-browser.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState, useMemo, useRef, useEffect } from "react"; +import { useState, useMemo, useRef } from "react"; import Link from "next/link"; import { Card, CardContent } from "@/components/ui/card"; import { buttonVariants } from "@/components/ui/button"; @@ -321,21 +321,19 @@ export function YearMonthBrowser({ }) { const allGroups = useMemo(() => buildMonthGroups(letters), [letters]); - const [activeMonth, setActiveMonth] = useState( + const [activeMonthRaw, setActiveMonth] = useState( allGroups.length > 0 ? allGroups[0].month : 1, ); - const contentRef = useRef(null); + // Derive a safe active month — if the raw value is no longer valid, fall back to the first group + const activeMonth = useMemo(() => { + if (allGroups.length === 0) return activeMonthRaw; + if (allGroups.find((g) => g.month === activeMonthRaw)) + return activeMonthRaw; + return allGroups[0].month; + }, [allGroups, activeMonthRaw]); - // Keep active month valid - useEffect(() => { - if ( - allGroups.length > 0 && - !allGroups.find((g) => g.month === activeMonth) - ) { - setActiveMonth(allGroups[0].month); - } - }, [allGroups, activeMonth]); + const contentRef = useRef(null); const handleMonthSelect = (month: number) => { setActiveMonth(month);