mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
Move to TanStack Start from Next.js (#184)
This commit is contained in:
29
web/app/hooks/use-awaitable-navigate.ts
Normal file
29
web/app/hooks/use-awaitable-navigate.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user