From 8083cdf13b7c63788c8093a4f5af67b8d31031dc Mon Sep 17 00:00:00 2001 From: arvinxx Date: Sat, 13 Feb 2021 15:40:16 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20fix:=20=E4=BF=AE=E5=A4=8D=E4=BB=96?= =?UTF-8?q?=E4=BA=BA=E4=B8=BB=E9=A1=B5=E5=87=BA=E7=8E=B0=E8=87=AA=E5=B7=B1?= =?UTF-8?q?=E6=B4=BB=E8=B7=83=E7=8A=B6=E6=80=81=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/contentScripts/activityMap/app/index.tsx | 6 +-- .../activityMap/app/useFetchData.ts | 25 ------------ .../activityMap/app/useHeatmapData.ts | 39 +++++++++++++++++++ src/utils/activityMap.ts | 9 +++-- 4 files changed, 48 insertions(+), 31 deletions(-) delete mode 100644 src/contentScripts/activityMap/app/useFetchData.ts create mode 100644 src/contentScripts/activityMap/app/useHeatmapData.ts diff --git a/src/contentScripts/activityMap/app/index.tsx b/src/contentScripts/activityMap/app/index.tsx index 31ae6ad..a6c4fe8 100644 --- a/src/contentScripts/activityMap/app/index.tsx +++ b/src/contentScripts/activityMap/app/index.tsx @@ -3,15 +3,15 @@ import { Button, Col, Row, Skeleton } from 'antd'; import { ReloadOutlined, InfoCircleOutlined } from '@ant-design/icons'; import Heatmap from './Heatmap'; -import { useFetchData } from './useFetchData'; +import { useHeatmapData } from './useHeatmapData'; import styles from './style.less'; import { yuqueToken } from '@/utils'; import React from 'react'; const App: FC = () => { - const { loading, data, fetchData } = useFetchData(); + const { loading, data, fetchData, isHome } = useHeatmapData(); - return ( + return yuqueToken && !isHome ? null : (
diff --git a/src/contentScripts/activityMap/app/useFetchData.ts b/src/contentScripts/activityMap/app/useFetchData.ts deleted file mode 100644 index 9eb40e2..0000000 --- a/src/contentScripts/activityMap/app/useFetchData.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { useEffect, useState } from 'react'; -import { getActivityData, mapToHeatData } from '@/utils'; -import { useLocalStorageState } from 'ahooks'; - -export const useFetchData = () => { - const [loading, setLoading] = useState(false); - - const [data, setData] = useLocalStorageState('HeatmapRowData', []); - - const fetchData = async () => { - setLoading(true); - const activityData = await getActivityData(); - setLoading(false); - - if (activityData) { - setData(activityData!); - } - }; - useEffect(() => { - if (data?.length === 0) { - fetchData(); - } - }, [data]); - return { data: mapToHeatData(data!), loading, fetchData }; -}; diff --git a/src/contentScripts/activityMap/app/useHeatmapData.ts b/src/contentScripts/activityMap/app/useHeatmapData.ts new file mode 100644 index 0000000..f69a2cd --- /dev/null +++ b/src/contentScripts/activityMap/app/useHeatmapData.ts @@ -0,0 +1,39 @@ +import { useEffect, useState } from 'react'; +import { getActivityData, mapToHeatData, yuqueToken } from '@/utils'; +import { useLocalStorageState } from 'ahooks'; + +export const useHeatmapData = () => { + const [loading, setLoading] = useState(false); + + const [data, setData] = useLocalStorageState('HeatmapRowData', []); + + const [loginPath, setLoginPath] = useLocalStorageState('LOGIN_PATH', ''); + + /** + * 获取数据 + */ + const fetchData = async () => { + setLoading(true); + const result = await getActivityData(); + setLoading(false); + + if (result) { + const { data: activityData, username } = result; + setLoginPath(username); + setData(activityData!); + } + }; + + useEffect(() => { + if ((yuqueToken && !loginPath) || data?.length === 0) { + fetchData(); + } + }, [data]); + + return { + data: mapToHeatData(data!), + loading, + fetchData, + isHome: location.pathname.substr(1) === loginPath, + }; +}; diff --git a/src/utils/activityMap.ts b/src/utils/activityMap.ts index 7aebd12..a4c50a9 100644 --- a/src/utils/activityMap.ts +++ b/src/utils/activityMap.ts @@ -118,9 +118,12 @@ export const getActivityData = async () => { const docs: ActivityDoc[] = flattenDeep(tempDocList); - return docs.filter(({ user_id }) => { - return user_id === id; - }); + return { + username: login, + data: docs.filter(({ user_id }) => { + return user_id === id; + }), + }; }; /**