From b093cd754d8756d720455b508824f418a1a6ec4f Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 22 Sep 2022 17:09:13 +0800 Subject: [PATCH] fix: href of item if basePath is not `/` (close alist-org/alist#1748) --- src/hooks/useRouter.ts | 49 +++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/hooks/useRouter.ts b/src/hooks/useRouter.ts index 83e74d4d7..b5c81e40b 100644 --- a/src/hooks/useRouter.ts +++ b/src/hooks/useRouter.ts @@ -4,22 +4,18 @@ import { useNavigate, useParams, useSearchParams, -} from "@solidjs/router"; -import { createMemo } from "solid-js"; -import { - encodePath, - joinBase, - log, - pathDir, - pathJoin, - trimBase, -} from "~/utils"; +} from "@solidjs/router" +import { createMemo } from "solid-js" +import { encodePath, joinBase, log, pathDir, pathJoin, trimBase } from "~/utils" const useRouter = () => { - const navigate = useNavigate(); - const location = useLocation(); - const [searchParams, setSearchParams] = useSearchParams(); - const params = useParams(); + const navigate = useNavigate() + const location = useLocation() + const [searchParams, setSearchParams] = useSearchParams() + const params = useParams() + const pathname = createMemo(() => { + return trimBase(location.pathname) + }) return { to: ( path: string, @@ -27,30 +23,29 @@ const useRouter = () => { options?: Partial ) => { if (!ignore_root && path.startsWith("/")) { - path = joinBase(path); + path = joinBase(path) } - log("to:", path); - navigate(path, options); + log("to:", path) + navigate(path, options) }, replace: (to: string) => { - navigate(encodePath(pathJoin(pathDir(location.pathname), to), true)); + navigate(encodePath(pathJoin(pathDir(location.pathname), to), true)) }, pushHref: (to: string): string => { - return encodePath(pathJoin(location.pathname, to)); + const href = encodePath(pathJoin(pathname(), to)) + return href }, back: () => { - navigate(-1); + navigate(-1) }, forward: () => { - navigate(1); + navigate(1) }, - pathname: createMemo(() => { - return trimBase(location.pathname); - }), + pathname: pathname, searchParams: searchParams, setSearchParams: setSearchParams, params: params, - }; -}; + } +} -export { useRouter }; +export { useRouter }