Files
opencode-manager/frontend/src/hooks/useUrlParams.ts
T
Chris Scott 0d14e754e1 refactor: extract shared useUrlParams hook and use push navigation for dialogs (#266)
* refactor: extract shared useUrlParams hook and use push navigation for dialogs

* Remove schedule badges and fix dialog safe area padding

* fix: deduplicate server health toasts with stable IDs

* fix: restore Enabled/Paused status badges on schedule jobs

* refactor: consolidate url state hooks and add shared test utilities
2026-06-07 13:02:56 -04:00

34 lines
1.0 KiB
TypeScript

import { useCallback, useEffect, useMemo, useRef } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
type UrlHistoryMode = 'push' | 'replace'
interface UseUrlParamsReturn {
search: string
searchParams: URLSearchParams
updateParams: (updater: (params: URLSearchParams) => void, mode?: UrlHistoryMode) => void
}
export function useUrlParams(): UseUrlParamsReturn {
const navigate = useNavigate()
const location = useLocation()
const searchRef = useRef(location.search)
useEffect(() => {
searchRef.current = location.search
}, [location.search])
const searchParams = useMemo(() => new URLSearchParams(location.search), [location.search])
const updateParams = useCallback(
(updater: (params: URLSearchParams) => void, mode: UrlHistoryMode = 'replace') => {
const params = new URLSearchParams(searchRef.current)
updater(params)
navigate({ search: params.toString() }, { replace: mode === 'replace' })
},
[navigate],
)
return { search: location.search, searchParams, updateParams }
}