mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
30 lines
863 B
TypeScript
30 lines
863 B
TypeScript
import * as React from "react"
|
|
import type { NavigateOptions } from "@tanstack/react-router"
|
|
import { useLocation, useNavigate } from "@tanstack/react-router"
|
|
|
|
type Resolve = (value?: unknown) => void
|
|
|
|
export const useAwaitableNavigate = () => {
|
|
const navigate = useNavigate()
|
|
const location = useLocation()
|
|
const resolveFunctionsRef = React.useRef<Resolve[]>([])
|
|
const resolveAll = () => {
|
|
resolveFunctionsRef.current.forEach((resolve) => resolve())
|
|
resolveFunctionsRef.current.splice(0, resolveFunctionsRef.current.length)
|
|
}
|
|
const [, startTransition] = React.useTransition()
|
|
|
|
React.useEffect(() => {
|
|
resolveAll()
|
|
}, [location])
|
|
|
|
return (options: NavigateOptions) => {
|
|
return new Promise((res) => {
|
|
startTransition(() => {
|
|
resolveFunctionsRef.current.push(res)
|
|
res(navigate(options))
|
|
})
|
|
})
|
|
}
|
|
}
|