| 157 | ): Solid.Accessor<BlockerResolver> |
| 158 | |
| 159 | export function useBlocker( |
| 160 | opts?: UseBlockerOpts | LegacyBlockerOpts | LegacyBlockerFn, |
| 161 | condition?: boolean | any, |
| 162 | ): Solid.Accessor<BlockerResolver> | void { |
| 163 | const props = Solid.mergeProps( |
| 164 | { |
| 165 | enableBeforeUnload: true, |
| 166 | disabled: false, |
| 167 | withResolver: false, |
| 168 | }, |
| 169 | _resolveBlockerOpts(opts, condition), |
| 170 | ) |
| 171 | |
| 172 | const router = useRouter() |
| 173 | |
| 174 | const [resolver, setResolver] = Solid.createSignal<BlockerResolver>({ |
| 175 | status: 'idle', |
| 176 | current: undefined, |
| 177 | next: undefined, |
| 178 | action: undefined, |
| 179 | proceed: undefined, |
| 180 | reset: undefined, |
| 181 | }) |
| 182 | |
| 183 | Solid.createEffect(() => { |
| 184 | const blockerFnComposed = async (blockerFnArgs: BlockerFnArgs) => { |
| 185 | function getLocation( |
| 186 | location: HistoryLocation, |
| 187 | ): AnyShouldBlockFnLocation { |
| 188 | const parsedLocation = router.parseLocation(location) |
| 189 | const matchedRoutes = router.getMatchedRoutes(parsedLocation.pathname) |
| 190 | if (matchedRoutes.foundRoute === undefined) { |
| 191 | return { |
| 192 | routeId: '__notFound__', |
| 193 | fullPath: parsedLocation.pathname, |
| 194 | pathname: parsedLocation.pathname, |
| 195 | params: matchedRoutes.routeParams, |
| 196 | search: parsedLocation.search, |
| 197 | } |
| 198 | } |
| 199 | return { |
| 200 | routeId: matchedRoutes.foundRoute.id, |
| 201 | fullPath: matchedRoutes.foundRoute.fullPath, |
| 202 | pathname: parsedLocation.pathname, |
| 203 | params: matchedRoutes.routeParams, |
| 204 | search: parsedLocation.search, |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | const current = getLocation(blockerFnArgs.currentLocation) |
| 209 | const next = getLocation(blockerFnArgs.nextLocation) |
| 210 | |
| 211 | if ( |
| 212 | current.routeId === '__notFound__' && |
| 213 | next.routeId !== '__notFound__' |
| 214 | ) { |
| 215 | return false |
| 216 | } |