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: use ScrollRestoration component control page position

shuaieba1d4d8d61d
diff --git a/ui/src/components/Pagination/index.tsx b/ui/src/components/Pagination/index.tsx
index ab5dc403..69d113eb 100644
--- a/ui/src/components/Pagination/index.tsx
+++ b/ui/src/components/Pagination/index.tsx
@@ -22,7 +22,7 @@ import { Pagination } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { useSearchParams, useNavigate, useLocation } from 'react-router-dom';
-import { scrollToDocTop, floppyNavigation } from '@/utils';
+import { floppyNavigation } from '@/utils';
interface Props {
currentPage: number;
@@ -71,7 +71,6 @@ const PageItem = ({ page, currentPage, path }: PageItemProps) => {
e.preventDefault();
e.stopPropagation();
navigate(path);
- scrollToDocTop();
}
}}>
{page}
@@ -115,7 +114,6 @@ const Index: FC<Props> = ({
if (floppyNavigation.shouldProcessLinkClick(e)) {
e.preventDefault();
navigate(handleParams(currentPage - 1));
- scrollToDocTop();
}
}}>
{t('prev')}
@@ -212,7 +210,6 @@ const Index: FC<Props> = ({
if (floppyNavigation.shouldProcessLinkClick(e)) {
e.preventDefault();
navigate(handleParams(currentPage + 1));
- scrollToDocTop();
}
}}>
{t('next')}
diff --git a/ui/src/pages/Layout/index.tsx b/ui/src/pages/Layout/index.tsx
index cf7a2f1b..2029b0fa 100644
--- a/ui/src/pages/Layout/index.tsx
+++ b/ui/src/pages/Layout/index.tsx
@@ -18,7 +18,7 @@
*/
import { FC, memo, useEffect } from 'react';
-import { Outlet, useLocation } from 'react-router-dom';
+import { Outlet, useLocation, ScrollRestoration } from 'react-router-dom';
import { HelmetProvider } from 'react-helmet-async';
import { SWRConfig } from 'swr';
@@ -86,6 +86,7 @@ const Layout: FC = () => {
<Footer />
<Customize />
<LoginToContinueModal visible={showLoginToContinueModal} />
+ <ScrollRestoration />
</SWRConfig>
</HelmetProvider>
);
diff --git a/ui/src/pages/Questions/Detail/index.tsx b/ui/src/pages/Questions/Detail/index.tsx
index 2130ddf7..0ab02514 100644
--- a/ui/src/pages/Questions/Detail/index.tsx
+++ b/ui/src/pages/Questions/Detail/index.tsx
@@ -214,7 +214,6 @@ const Index = () => {
if (!qid) {
return;
}
- scrollToDocTop();
getDetail();
requestAnswers();
}, [qid]);
diff --git a/ui/src/pages/Questions/EditAnswer/index.tsx b/ui/src/pages/Questions/EditAnswer/index.tsx
index 9ca6f413..831aa5a2 100644
--- a/ui/src/pages/Questions/EditAnswer/index.tsx
+++ b/ui/src/pages/Questions/EditAnswer/index.tsx
@@ -17,7 +17,7 @@
* under the License.
*/
-import React, { useState, useRef, useEffect, useLayoutEffect } from 'react';
+import React, { useState, useRef, useEffect } from 'react';
import { Row, Col, Form, Button, Card } from 'react-bootstrap';
import { useParams, useNavigate, Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
@@ -25,7 +25,7 @@ import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
import classNames from 'classnames';
-import { handleFormError, scrollToDocTop } from '@/utils';
+import { handleFormError } from '@/utils';
import { usePageTags, usePromptWithUnload } from '@/hooks';
import { useCaptchaPlugin, useRenderHtmlPlugin } from '@/utils/pluginKit';
import { pathFactory } from '@/router/pathFactory';
@@ -47,9 +47,6 @@ interface FormDataItem {
const Index = () => {
const { aid = '', qid = '' } = useParams();
const [focusType, setForceType] = useState('');
- useLayoutEffect(() => {
- scrollToDocTop();
- }, []);
const { t } = useTranslation('translation', { keyPrefix: 'edit_answer' });
const navigate = useNavigate();
diff --git a/ui/src/pages/Tags/Detail/index.tsx b/ui/src/pages/Tags/Detail/index.tsx
index a07236da..42b89d5d 100644
--- a/ui/src/pages/Tags/Detail/index.tsx
+++ b/ui/src/pages/Tags/Detail/index.tsx
@@ -38,7 +38,7 @@ import {
} from '@/services';
import QuestionList, { QUESTION_ORDER_KEYS } from '@/components/QuestionList';
import HotQuestions from '@/components/HotQuestions';
-import { escapeRemove, guard, scrollToDocTop } from '@/utils';
+import { escapeRemove, guard } from '@/utils';
import { pathFactory } from '@/router/pathFactory';
const Index: FC = () => {
@@ -75,12 +75,6 @@ const Index: FC = () => {
});
};
- useEffect(() => {
- if (!listLoading) {
- scrollToDocTop();
- }
- }, [listLoading]);
-
useEffect(() => {
if (tagResp) {
const info = { ...tagResp };