Skip to content

Commit 3172d3b

Browse files
snomiaoclaude
andcommitted
fix: Address Copilot review comments and fix build errors
- Fix null searchParams handling in useRouterQuery hook - Add useMemo to prevent query object recreation - Fix stale closure issue in updateQuery callback - Fix router.push missing pathname in claim-nodes page - Remove unused router import in admin page - Fix grammar in providers.tsx comment Fixes TypeScript error: 'searchParams' is possibly 'null' Resolves Vercel build failure 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bec08b9 commit 3172d3b

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

app/admin/claim-nodes/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function ClaimNodesPage() {
2929
// Update URL with new page parameter
3030
const params = new URLSearchParams(searchParams?.toString())
3131
params.set('page', page.toString())
32-
router.push(`?${params.toString()}`)
32+
router.push(`/admin/claim-nodes?${params.toString()}`)
3333
}
3434

3535
// Use the page from searchParams for the API call

app/admin/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { Breadcrumb } from 'flowbite-react'
44
import Link from 'next/link'
5-
import { useRouter } from 'next/navigation'
65
import {
76
HiHome,
87
HiOutlineAdjustments,
@@ -14,7 +13,6 @@ import withAdmin from '@/components/common/HOC/authAdmin'
1413
import { useNextTranslation } from '@/src/hooks/i18n'
1514

1615
function AdminDashboard() {
17-
const router = useRouter()
1816
const { t } = useNextTranslation()
1917

2018
return (

app/providers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function Providers({ children }: { children: React.ReactNode }) {
5252

5353
useEffect(() => {
5454
// General localStorage cache invalidation for all endpoints
55-
// this interceptors will user always have latest data after edit.
55+
// This interceptor ensures users always have the latest data after edit.
5656
const responseInterceptor = AXIOS_INSTANCE.interceptors.response.use(
5757
function onSuccess(response: AxiosResponse) {
5858
const req = response.config

src/hooks/useRouterQuery.app.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useRouter, useSearchParams, usePathname } from 'next/navigation'
22
import { filter, omit } from 'rambda'
3-
import { useCallback } from 'react'
3+
import { useCallback, useMemo } from 'react'
44

55
/**
66
* A hook to easily access and update URL query parameters (App Router version)
@@ -29,8 +29,11 @@ export function useRouterQuery<
2929
const pathname = usePathname()
3030
const searchParams = useSearchParams()
3131

32-
// Convert URLSearchParams to object
33-
const query = Object.fromEntries(searchParams.entries()) as T
32+
// Convert URLSearchParams to object (memoized to avoid recreation)
33+
const query = useMemo(
34+
() => Object.fromEntries(searchParams?.entries() ?? []) as T,
35+
[searchParams]
36+
)
3437

3538
/**
3639
* Update query parameters
@@ -44,14 +47,19 @@ export function useRouterQuery<
4447
*/
4548
const updateQuery = useCallback(
4649
(newParams: Partial<T>, replace = false) => {
50+
// Get current query from searchParams to avoid stale closures
51+
const currentQuery = Object.fromEntries(
52+
searchParams?.entries() ?? []
53+
) as T
54+
4755
// Filter out null and undefined values
4856
const filteredParams = filter((e) => e != null, newParams)
4957

5058
// Prepare the final query object
5159
const finalQuery = replace
5260
? filteredParams
5361
: {
54-
...omit(Object.keys(newParams), query),
62+
...omit(Object.keys(newParams), currentQuery),
5563
...filteredParams,
5664
}
5765

@@ -68,9 +76,9 @@ export function useRouterQuery<
6876
? `${pathname}?${params.toString()}`
6977
: pathname
7078

71-
router.push(newUrl)
79+
router.push(newUrl ?? '/')
7280
},
73-
[router, pathname, query]
81+
[router, pathname, searchParams]
7482
)
7583

7684
return [query, updateQuery] as const

0 commit comments

Comments
 (0)