Skip to content
Merged
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 @@ -55,9 +55,15 @@ function RMCostsPageContent() {
// Track whether the initial auto-select has fired (declared before listLoading uses it).
const autoSelectedRef = useRef(false)

// Keep a ref to the latest filters.period so the auto-select effect always
// reads the most up-to-date value (avoids stale-closure issues).
const filtersPeriodRef = useRef(filters.period)
filtersPeriodRef.current = filters.period

// Once autoSelectedRef is true the user has either received a default period
// or deliberately picked "All Periods" (period=""), so we no longer block on
// !filters.period.
// !filters.period. Also skip the "waiting" gate when a period is already
// present from the URL (e.g. after a page reload with ?period=202604).
const listLoading = periodsLoading || (!autoSelectedRef.current && availablePeriods.length > 0 && !filters.period) || isLoading

// Auto-select the latest available period ONCE on first load when the user
Expand All @@ -68,7 +74,9 @@ function RMCostsPageContent() {
if (autoSelectedRef.current) return
if (availablePeriods.length === 0) return
autoSelectedRef.current = true
if (!filters.period) {
// Read period from ref to guarantee we see the latest value (the URL may
// already carry a period param after a page reload).
if (!filtersPeriodRef.current) {
setFilters((prev) => ({ ...prev, period: availablePeriods[0], page: 1 }))
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
8 changes: 4 additions & 4 deletions src/lib/hooks/use-url-state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client"

import { useSearchParams, useRouter, usePathname } from "next/navigation"
import { useCallback, useMemo, useRef, useEffect } from "react"
import { useCallback, useEffect, useMemo, useRef } from "react"

/**
* Configuration for URL state serialization
Expand Down Expand Up @@ -121,10 +121,10 @@ export function useUrlState<T extends object>(
}, [searchParams, defaultValues, deserialize])

// Use ref to access current state in callback without adding it to dependencies
// This prevents setState from changing reference on every state change
// This prevents setState from changing reference on every state change.
// Sync the ref during render (not in an effect) so that setState always reads
// the latest state, even when called from other effects in the same cycle.
const stateRef = useRef(state)

// Sync ref in effect to satisfy ESLint rules while keeping the pattern
useEffect(() => {
stateRef.current = state
}, [state])
Expand Down
Loading