You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by sh...@apache.org on 2024/06/13 06:53:14 UTC

(incubator-answer) 01/01: fix: add basepath logic to route jump

This is an automated email from the ASF dual-hosted git repository.

shuai pushed a commit to branch fix/edit_profile
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit db4a0efbef281fc0a814b13fd6d6def77c709555
Author: shuai <li...@sifou.com>
AuthorDate: Thu Jun 13 14:53:02 2024 +0800

    fix: add basepath logic to route jump
---
 ui/src/utils/floppyNavigation.ts | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/ui/src/utils/floppyNavigation.ts b/ui/src/utils/floppyNavigation.ts
index 69ba4e88..eb06139f 100644
--- a/ui/src/utils/floppyNavigation.ts
+++ b/ui/src/utils/floppyNavigation.ts
@@ -26,16 +26,25 @@ import { getLoginUrl } from '@/utils/userCenter';
 
 const equalToCurrentHref = (target: string, base?: string) => {
   base ||= window.location.origin;
-  const targetUrl = new URL(target, base);
+  const targetUrl = new URL(
+    target.startsWith(REACT_BASE_PATH) ? target : `${REACT_BASE_PATH}${target}`,
+    base,
+  );
   return targetUrl.toString() === window.location.href;
 };
 const matchToCurrentHref = (target: string) => {
   target = (target || '').trim();
+  const hasBasePath = target.startsWith(REACT_BASE_PATH);
   // Empty string or `/` can match any path
   if (!target || target === '/') {
     return true;
   }
   const { pathname, search, hash } = window.location;
+  let pathWithOutBase = pathname;
+  if (!hasBasePath) {
+    pathWithOutBase = pathWithOutBase.replace(REACT_BASE_PATH, '');
+  }
+
   const tPart = target.split('?');
 
   /**
@@ -59,7 +68,7 @@ const matchToCurrentHref = (target: string) => {
    */
   let pathMatch = true;
   const tPath = tPart[0].split('/').filter((_) => !!_);
-  const lPath = pathname.split('/').filter((_) => !!_);
+  const lPath = pathWithOutBase.split('/').filter((_) => !!_);
 
   tPath.forEach((p, i) => {
     const lp = lPath[i];
@@ -73,9 +82,10 @@ const matchToCurrentHref = (target: string) => {
 
 const storageLoginRedirect = () => {
   const { pathname } = window.location;
-  if (pathname !== RouteAlias.login && pathname !== RouteAlias.signUp) {
+  const filterPath = pathname.replace(REACT_BASE_PATH, '');
+  if (filterPath !== RouteAlias.login && filterPath !== RouteAlias.signUp) {
     const loc = window.location;
-    const redirectUrl = loc.href.replace(loc.origin, '');
+    const redirectUrl = loc.href.replace(`${loc.origin}${REACT_BASE_PATH}`, '');
     Storage.set(REDIRECT_PATH_STORAGE_KEY, redirectUrl);
   }
 };
@@ -151,12 +161,12 @@ const navigate = (to: string | number, config: NavigateConfig = {}) => {
       }
       window.location.replace(to);
     } else if (typeof handler === 'function') {
-      if (to === '/test') {
+      if (to === REACT_BASE_PATH) {
         to = '/';
       }
 
-      if (to !== '/test' && to.startsWith('/test')) {
-        to = to.replace('/test', '');
+      if (to !== REACT_BASE_PATH && to.startsWith(REACT_BASE_PATH)) {
+        to = to.replace(REACT_BASE_PATH, '');
       }
       handler(to, config.options);
     }