Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix: More thorough use of the link jumping feature of the routing component

haitao(lj)1fc7af13c264
diff --git a/ui/src/components/Header/components/NavItems/index.tsx b/ui/src/components/Header/components/NavItems/index.tsx
index 4b377ad7..4147cbbb 100644
--- a/ui/src/components/Header/components/NavItems/index.tsx
+++ b/ui/src/components/Header/components/NavItems/index.tsx
@@ -1,7 +1,7 @@
import { FC, memo } from 'react';
import { Nav, Dropdown } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Link, NavLink } from 'react-router-dom';
+import { NavLink, useNavigate } from 'react-router-dom';
import type * as Type from '@/common/interface';
import { Avatar, Icon } from '@/components';
@@ -14,6 +14,13 @@ interface Props {
const Index: FC<Props> = ({ redDot, userInfo, logOut }) => {
const { t } = useTranslation();
+ const navigate = useNavigate();
+ const handleLinkClick = (evt) => {
+ evt.preventDefault();
+ const { href } = evt.currentTarget;
+ const { pathname } = new URL(href);
+ navigate(pathname);
+ };
return (
<>
<Nav className="flex-row">
@@ -26,7 +33,7 @@ const Index: FC<Props> = ({ redDot, userInfo, logOut }) => {
</Nav.Link>
<Nav.Link
- as={Link}
+ as={NavLink}
to="/users/notifications/achievement"
className="icon-link d-flex align-items-center justify-content-center p-0 me-3 position-relative">
<Icon name="trophy-fill" className="fs-4" />
@@ -46,17 +53,26 @@ const Index: FC<Props> = ({ redDot, userInfo, logOut }) => {
</Dropdown.Toggle>
<Dropdown.Menu>
- <Dropdown.Item href={`/users/${userInfo.username}`}>
+ <Dropdown.Item
+ href={`/users/${userInfo.username}`}
+ onClick={handleLinkClick}>
{t('header.nav.profile')}
</Dropdown.Item>
- <Dropdown.Item href="/users/settings/profile">
+ <Dropdown.Item
+ href="/users/settings/profile"
+ onClick={handleLinkClick}>
{t('header.nav.setting')}
</Dropdown.Item>
{userInfo?.is_admin ? (
- <Dropdown.Item href="/admin">{t('header.nav.admin')}</Dropdown.Item>
+ <Dropdown.Item href="/admin" onClick={handleLinkClick}>
+ {t('header.nav.admin')}
+ </Dropdown.Item>
) : null}
{redDot?.can_revision ? (
- <Dropdown.Item href="/review" className="position-relative">
+ <Dropdown.Item
+ href="/review"
+ className="position-relative"
+ onClick={handleLinkClick}>
{t('header.nav.review')}
{redDot?.revision > 0 && (
<span className="position-absolute top-50 translate-middle-y end-0 me-3 p-2 bg-danger border border-light rounded-circle">
diff --git a/ui/src/components/Header/index.tsx b/ui/src/components/Header/index.tsx
index 226bcd2c..a66b26ab 100644
--- a/ui/src/components/Header/index.tsx
+++ b/ui/src/components/Header/index.tsx
@@ -49,6 +49,14 @@ const Header: FC = () => {
const handleInput = (val) => {
setSearch(val);
};
+ const handleSearch = (evt) => {
+ evt.preventDefault();
+ if (!searchStr) {
+ return;
+ }
+ const searchUrl = `/search?q=${encodeURIComponent(searchStr)}`;
+ navigate(searchUrl);
+ };
const handleLogout = async () => {
await logout();
@@ -153,7 +161,10 @@ const Header: FC = () => {
<hr className="hr lg-none mt-2" />
<Col lg={4} className="d-flex justify-content-center">
- <Form action="/search" className="w-75 px-0 px-lg-2">
+ <Form
+ action="/search"
+ className="w-75 px-0 px-lg-2"
+ onSubmit={handleSearch}>
<FormControl
placeholder={t('header.search.placeholder')}
className="placeholder-search"
diff --git a/ui/src/components/Tag/index.tsx b/ui/src/components/Tag/index.tsx
index eb0c9d49..67bd2805 100644
--- a/ui/src/components/Tag/index.tsx
+++ b/ui/src/components/Tag/index.tsx
@@ -1,4 +1,5 @@
import React, { memo, FC } from 'react';
+import { Link } from 'react-router-dom';
import classNames from 'classnames';
@@ -21,8 +22,8 @@ const Index: FC<IProps> = ({
href ||= pathFactory.tagLanding(data?.slug_name);
return (
- <a
- href={href}
+ <Link
+ to={href}
className={classNames(
'badge-tag rounded-1',
data.reserved && 'badge-tag-reserved',
@@ -30,7 +31,7 @@ const Index: FC<IProps> = ({
className,
)}>
<span className={textClassName}>{data.slug_name}</span>
- </a>
+ </Link>
);
};