forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

use-go-back.ts18 lines, 692 bytesHistoryRawDownload
1import { useCanGoBack, useLocation, useNavigate, useRouter } from "@tanstack/solid-router";
2import { backTarget } from "@/lib/back-target";
3
4/**
5 * Back within the app: history when there is an in-app entry behind us, the
6 * nearest existing parent route otherwise. The browser's own back on a deep
7 * link or a fresh tab would leave the app.
8 */
9export function useGoBack(): () => void {
10 const router = useRouter();
11 const canGoBack = useCanGoBack();
12 const location = useLocation();
13 const navigate = useNavigate();
14 return () => {
15 if (canGoBack()) return router.history.back();
16 void navigate({ to: backTarget(location().pathname, Object.keys(router.routesByPath)) });
17 };
18}