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(ui): fixed the ui issue of skeleton screen flickering (#830)

easye529d33dbf45
diff --git a/ui/src/common/constants.ts b/ui/src/common/constants.ts
index 8868c9a0..ec7f1ba6 100644
--- a/ui/src/common/constants.ts
+++ b/ui/src/common/constants.ts
@@ -30,6 +30,7 @@ export const DRAFT_TIMESIGH_STORAGE_KEY = '|_a_t_s_|';
export const QUESTIONS_ORDER_STORAGE_KEY = '_a_qok_';
export const DEFAULT_THEME = 'system';
export const ADMIN_PRIVILEGE_CUSTOM_LEVEL = 99;
+export const SKELETON_SHOW_TIME = 1000;
export const USER_AGENT_NAMES = {
SegmentFault: 'SegmentFault',
diff --git a/ui/src/components/QuestionList/index.tsx b/ui/src/components/QuestionList/index.tsx
index 4f410990..94a300d8 100644
--- a/ui/src/components/QuestionList/index.tsx
+++ b/ui/src/components/QuestionList/index.tsx
@@ -36,6 +36,7 @@ import {
Icon,
} from '@/components';
import * as Type from '@/common/interface';
+import { useSkeletonControl } from '@/hooks';
export const QUESTION_ORDER_KEYS: Type.QuestionOrderBy[] = [
'active',
@@ -59,11 +60,13 @@ const QuestionList: FC<Props> = ({
}) => {
const { t } = useTranslation('translation', { keyPrefix: 'question' });
const [urlSearchParams] = useSearchParams();
+ const { isSkeletonShow } = useSkeletonControl(isLoading);
const curOrder =
order || urlSearchParams.get('order') || QUESTION_ORDER_KEYS[0];
const curPage = Number(urlSearchParams.get('page')) || 1;
const pageSize = 20;
const count = data?.count || 0;
+
return (
<div>
<div className="mb-3 d-flex flex-wrap justify-content-between">
@@ -80,7 +83,7 @@ const QuestionList: FC<Props> = ({
/>
</div>
<ListGroup className="rounded-0">
- {isLoading ? (
+ {isSkeletonShow ? (
<QuestionListLoader />
) : (
data?.list?.map((li) => {
diff --git a/ui/src/hooks/index.ts b/ui/src/hooks/index.ts
index 54c7fd28..d0615fa3 100644
--- a/ui/src/hooks/index.ts
+++ b/ui/src/hooks/index.ts
@@ -29,6 +29,7 @@ import useLoginRedirect from './useLoginRedirect';
import usePromptWithUnload from './usePrompt';
import useActivationEmailModal from './useActivationEmailModal';
import useCaptchaModal from './useCaptchaModal';
+import useSkeletonControl from './useSkeletonControl';
export {
useTagModal,
@@ -43,4 +44,5 @@ export {
usePromptWithUnload,
useActivationEmailModal,
useCaptchaModal,
+ useSkeletonControl,
};
diff --git a/ui/src/hooks/useSkeletonControl/index.tsx b/ui/src/hooks/useSkeletonControl/index.tsx
new file mode 100644
index 00000000..2642ba9f
--- /dev/null
+++ b/ui/src/hooks/useSkeletonControl/index.tsx
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { useEffect, useRef, useState } from 'react';
+
+import { SKELETON_SHOW_TIME } from '@/common/constants';
+
+/**
+ * @param needShowFirst whether the skeleton should show at first
+ *
+ * Why need 'needShowFirst' param?
+ * Sometimes we need skeleton screens to take up space in the dom from the start
+ *
+ * If you set the 'needShowFirst' param as false, If the interface time is too short,
+ * the skeleton screen will not be displayed, which can reduce the time occupation
+ */
+const useSkeletonControl = (isLoading: boolean) => {
+ const [isSkeletonShow, setIsSkeletonShow] = useState(false);
+ const timer = useRef<NodeJS.Timeout | null>(null);
+ const openSkeleton = () => {
+ if (timer.current) {
+ clearTimeout(timer.current);
+ }
+ timer.current = setTimeout(() => {
+ setIsSkeletonShow(true);
+ }, SKELETON_SHOW_TIME);
+ };
+
+ const closeSkeleton = () => {
+ clearTimeout(timer.current as NodeJS.Timeout);
+ setIsSkeletonShow(false);
+ };
+
+ useEffect(() => {
+ if (isLoading) {
+ openSkeleton();
+ } else {
+ closeSkeleton();
+ }
+ }, [isLoading]);
+
+ return { isSkeletonShow };
+};
+
+export default useSkeletonControl;
diff --git a/ui/src/pages/Questions/Detail/index.tsx b/ui/src/pages/Questions/Detail/index.tsx
index e0908eac..b289ac68 100644
--- a/ui/src/pages/Questions/Detail/index.tsx
+++ b/ui/src/pages/Questions/Detail/index.tsx
@@ -30,7 +30,7 @@ import { useTranslation } from 'react-i18next';
import { Pagination, CustomSidebar } from '@/components';
import { loggedUserInfoStore, toastStore } from '@/stores';
import { scrollToElementTop, scrollToDocTop } from '@/utils';
-import { usePageTags, usePageUsers } from '@/hooks';
+import { usePageTags, usePageUsers, useSkeletonControl } from '@/hooks';
import type {
ListResult,
QuestionDetailRes,
@@ -68,6 +68,7 @@ const Index = () => {
const order = urlSearch.get('order') || '';
const [question, setQuestion] = useState<QuestionDetailRes | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
+ const { isSkeletonShow } = useSkeletonControl(isLoading);
const [answers, setAnswers] = useState<ListResult<AnswerItem>>({
count: -1,
list: [],
@@ -239,7 +240,7 @@ const Index = () => {
<Row className="questionDetailPage pt-4 mb-5">
<Col className="page-main flex-auto">
{question?.operation?.level && <Alert data={question.operation} />}
- {isLoading ? (
+ {isSkeletonShow ? (
<ContentLoader />
) : (
<Question
diff --git a/ui/src/pages/Search/index.tsx b/ui/src/pages/Search/index.tsx
index c7c42d96..8517172d 100644
--- a/ui/src/pages/Search/index.tsx
+++ b/ui/src/pages/Search/index.tsx
@@ -22,7 +22,7 @@ import { useTranslation } from 'react-i18next';
import { useSearchParams } from 'react-router-dom';
import { useEffect, useState } from 'react';
-import { usePageTags, useCaptchaModal } from '@/hooks';
+import { usePageTags, useCaptchaModal, useSkeletonControl } from '@/hooks';
import { Pagination } from '@/components';
import { getSearchResult } from '@/services';
import type { SearchParams, SearchRes } from '@/common/interface';
@@ -43,6 +43,7 @@ const Index = () => {
const q = searchParams.get('q') || '';
const order = searchParams.get('order') || 'active';
const [isLoading, setIsLoading] = useState(false);
+ const { isSkeletonShow } = useSkeletonControl(isLoading);
const [data, setData] = useState<SearchRes>({
count: 0,
list: [],
@@ -102,7 +103,7 @@ const Index = () => {
<Head data={extra} />
<SearchHead sort={order} count={count} />
<ListGroup className="rounded-0 mb-5">
- {isLoading ? (
+ {isSkeletonShow ? (
<ListLoader />
) : (
list?.map((item) => {
diff --git a/ui/src/pages/Tags/index.tsx b/ui/src/pages/Tags/index.tsx
index aa0f8c6e..e8f682f7 100644
--- a/ui/src/pages/Tags/index.tsx
+++ b/ui/src/pages/Tags/index.tsx
@@ -22,7 +22,7 @@ import { Row, Col, Card, Button, Form, Stack } from 'react-bootstrap';
import { useSearchParams, Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
-import { usePageTags } from '@/hooks';
+import { usePageTags, useSkeletonControl } from '@/hooks';
import { Tag, Pagination, QueryGroup, TagsLoader } from '@/components';
import { formatCount, escapeRemove } from '@/utils';
import { tryNormalLogged } from '@/utils/guard';
@@ -52,6 +52,8 @@ const Tags = () => {
...(sort ? { query_cond: sort } : {}),
});
+ const { isSkeletonShow } = useSkeletonControl(isLoading);
+
const handleChange = (e) => {
setSearchTag(e.target.value);
};
@@ -67,9 +69,11 @@ const Tags = () => {
mutate();
});
};
+
usePageTags({
title: t('tags', { keyPrefix: 'page_title' }),
});
+
return (
<Row className="py-4 mb-4">
<Col xxl={12}>
@@ -106,7 +110,7 @@ const Tags = () => {
<Col className="mt-4" xxl={12}>
<Row>
- {isLoading ? (
+ {isSkeletonShow ? (
<TagsLoader />
) : (
tags?.list?.map((tag) => (