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.

refactor(route): tidy "location"、"history" to useLocation、useNavigate

haitao(lj)1beb884d2689
diff --git a/ui/src/components/Share/index.tsx b/ui/src/components/Share/index.tsx
index 5491234d..9aa92200 100644
--- a/ui/src/components/Share/index.tsx
+++ b/ui/src/components/Share/index.tsx
@@ -50,7 +50,7 @@ const Index: FC<IProps> = ({ type, qid, aid, title }) => {
});
};
useEffect(() => {
- if (window.navigator?.canShare?.({ text: '111' })) {
+ if (window.navigator?.canShare?.({ text: 'can_share' })) {
setSystemShareState(true);
}
}, []);
diff --git a/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx b/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx
index 6e330740..5590d4c9 100644
--- a/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx
+++ b/ui/src/pages/Questions/Detail/components/AnswerHead/index.tsx
@@ -1,7 +1,7 @@
import { memo, FC } from 'react';
import { ButtonGroup } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
-import { Link } from 'react-router-dom';
+import { Link, useLocation } from 'react-router-dom';
interface Props {
count: number;
@@ -11,6 +11,7 @@ const Index: FC<Props> = ({ count = 0, order = 'default' }) => {
const { t } = useTranslation('translation', {
keyPrefix: 'question_detail.answers',
});
+ const location = useLocation();
return (
<div
className="d-flex align-items-center justify-content-between mt-5 mb-3"
@@ -20,14 +21,14 @@ const Index: FC<Props> = ({ count = 0, order = 'default' }) => {
</h5>
<ButtonGroup size="sm">
<Link
- to={`${window.location.pathname}?order=default`}
+ to={`${location.pathname}?order=default`}
className={`btn btn-outline-secondary ${
order !== 'updated' ? 'active' : ''
}`}>
{t('score')}
</Link>
<Link
- to={`${window.location.pathname}?order=updated`}
+ to={`${location.pathname}?order=updated`}
className={`btn btn-outline-secondary ${
order === 'updated' ? 'active' : ''
}`}>
diff --git a/ui/src/pages/Questions/Detail/index.tsx b/ui/src/pages/Questions/Detail/index.tsx
index 393faa2e..76be9367 100644
--- a/ui/src/pages/Questions/Detail/index.tsx
+++ b/ui/src/pages/Questions/Detail/index.tsx
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import { Container, Row, Col } from 'react-bootstrap';
-import { useParams, useSearchParams } from 'react-router-dom';
+import { useParams, useSearchParams, useNavigate } from 'react-router-dom';
import { questionDetail, getAnswers } from '@answer/api';
import { Pagination, PageTitle } from '@answer/components';
@@ -25,6 +25,7 @@ import {
import './index.scss';
const Index = () => {
+ const navigate = useNavigate();
const { qid = '', aid = '' } = useParams();
const [urlSearch] = useSearchParams();
@@ -75,7 +76,7 @@ const Index = () => {
const initPage = (type: string) => {
if (type === 'delete_question') {
setTimeout(() => {
- window.history.back();
+ navigate(-1);
}, 1000);
return;
}
diff --git a/ui/src/pages/Questions/EditAnswer/index.tsx b/ui/src/pages/Questions/EditAnswer/index.tsx
index 2e9ca74b..0edd21ca 100644
--- a/ui/src/pages/Questions/EditAnswer/index.tsx
+++ b/ui/src/pages/Questions/EditAnswer/index.tsx
@@ -111,7 +111,7 @@ const Ask = () => {
id: aid,
};
modifyAnswer(params).then(() => {
- window.location.href = `/questions/${qid}/${aid}`;
+ navigate(`/questions/${qid}/${aid}`);
});
};
diff --git a/ui/src/pages/Search/components/SearchHead/index.tsx b/ui/src/pages/Search/components/SearchHead/index.tsx
index 69b64e64..ffb20ae9 100644
--- a/ui/src/pages/Search/components/SearchHead/index.tsx
+++ b/ui/src/pages/Search/components/SearchHead/index.tsx
@@ -1,6 +1,6 @@
import { FC, memo } from 'react';
import { ListGroupItem, ButtonGroup, Button } from 'react-bootstrap';
-import { useSearchParams, useNavigate } from 'react-router-dom';
+import { useSearchParams, useNavigate, useLocation } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
const sortBtns = [
@@ -21,6 +21,7 @@ interface Props {
}
const Index: FC<Props> = ({ sort, count = 0 }) => {
const [searchParams] = useSearchParams();
+ const location = useLocation();
const navigate = useNavigate();
const { t } = useTranslation('translation', {
@@ -28,7 +29,7 @@ const Index: FC<Props> = ({ sort, count = 0 }) => {
});
const handleParams = (order): string => {
- const basePath = window.location.pathname;
+ const basePath = location.pathname;
searchParams.delete('page');
searchParams.set('order', order);
const searchStr = searchParams.toString();
diff --git a/ui/src/pages/Users/ActiveEmail/index.tsx b/ui/src/pages/Users/ActiveEmail/index.tsx
index a2e5a1f0..dee3fcb7 100644
--- a/ui/src/pages/Users/ActiveEmail/index.tsx
+++ b/ui/src/pages/Users/ActiveEmail/index.tsx
@@ -17,7 +17,7 @@ const Index: FC = () => {
activateAccount(encodeURIComponent(code)).then((res) => {
updateUser(res);
setTimeout(() => {
- window.location.href = '/users/account-activation/success';
+ window.location.replace('/users/account-activation/success');
}, 0);
});
}
diff --git a/ui/src/pages/Users/Login/index.tsx b/ui/src/pages/Users/Login/index.tsx
index fe535c10..fd9423fe 100644
--- a/ui/src/pages/Users/Login/index.tsx
+++ b/ui/src/pages/Users/Login/index.tsx
@@ -108,13 +108,9 @@ const Index: React.FC = () => {
setRefresh((pre) => pre + 1);
}
if (res.mail_status === 1) {
- const path = Storage.get('ANSWER_PATH');
+ const path = Storage.get('ANSWER_PATH') || '/';
Storage.remove('ANSWER_PATH');
- if (path) {
- window.location.href = path;
- } else {
- window.location.href = '/';
- }
+ window.location.replace(path);
}
setModalState(false);
diff --git a/ui/src/pages/Users/Personal/components/ListHead/index.tsx b/ui/src/pages/Users/Personal/components/ListHead/index.tsx
index bede73a5..614f66c1 100644
--- a/ui/src/pages/Users/Personal/components/ListHead/index.tsx
+++ b/ui/src/pages/Users/Personal/components/ListHead/index.tsx
@@ -1,6 +1,6 @@
import { FC, memo } from 'react';
import { ButtonGroup, Button } from 'react-bootstrap';
-import { useSearchParams, useNavigate } from 'react-router-dom';
+import { useSearchParams, useNavigate, useLocation } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
const sortBtns = [
@@ -26,10 +26,11 @@ const Index: FC<Props> = ({
}) => {
const [searchParams] = useSearchParams();
const navigate = useNavigate();
+ const location = useLocation();
const { t } = useTranslation('translation', { keyPrefix: 'personal' });
const handleParams = (order): string => {
- const basePath = window.location.pathname;
+ const basePath = location.pathname;
searchParams.delete('page');
searchParams.set('order', order);
const searchStr = searchParams.toString();
diff --git a/ui/src/pages/Users/Suspended/index.tsx b/ui/src/pages/Users/Suspended/index.tsx
index a8dbb23d..293603d0 100644
--- a/ui/src/pages/Users/Suspended/index.tsx
+++ b/ui/src/pages/Users/Suspended/index.tsx
@@ -9,7 +9,7 @@ const Suspended = () => {
const userInfo = userInfoStore((state) => state.user);
if (userInfo.status !== 'forbidden') {
- window.location.href = '/';
+ window.location.replace('/');
return null;
}
diff --git a/ui/src/utils/index.ts b/ui/src/utils/index.ts
index 11b54916..e8ce9da3 100644
--- a/ui/src/utils/index.ts
+++ b/ui/src/utils/index.ts
@@ -34,7 +34,7 @@ function isLogin(needToLogin?: boolean): boolean {
// login and active
if (user.username && user.mail_status === 1) {
if (LOGIN_NEED_BACK.includes(path)) {
- window.location.href = '/';
+ window.location.replace('/');
}
return true;
}
diff --git a/ui/src/utils/request.ts b/ui/src/utils/request.ts
index fa6a9024..d532d707 100644
--- a/ui/src/utils/request.ts
+++ b/ui/src/utils/request.ts
@@ -6,12 +6,6 @@ import { userInfoStore, toastStore } from '@answer/stores';
import Storage from './storage';
-// type Result<T> = {
-// code: number;
-// msg: string;
-// data: T;
-// };
-
const API = {
development: '',
production: '',