From dd4d538a4765f90e68ce458a258c01f8d563dce6 Mon Sep 17 00:00:00 2001
From: xueweihan <595666367@qq.com>
Date: Sun, 29 Sep 2024 16:42:16 +0800
Subject: [PATCH] perf: rank in mobile
---
public/locales/en/home.json | 4 +-
public/locales/zh/home.json | 4 +-
src/components/ImageWithPreview.tsx | 6 +-
.../dropdown/AvatarWithDropdown.tsx | 7 +-
src/components/links/TagLink.tsx | 47 +++++---
src/components/links/rankLink.tsx | 104 +++++++++---------
src/components/loading/skeleton.tsx | 22 +++-
src/components/navbar/IndexBar.tsx | 68 ++++--------
src/components/rankTable/RankTable.tsx | 4 +-
src/components/respository/CommentItem.tsx | 4 +-
src/hooks/useFilterHandling.tsx | 12 --
11 files changed, 146 insertions(+), 136 deletions(-)
diff --git a/public/locales/en/home.json b/public/locales/en/home.json
index aa1e697..a44695d 100644
--- a/public/locales/en/home.json
+++ b/public/locales/en/home.json
@@ -8,9 +8,7 @@
"newest": "Newest",
"monthly": "Monthly",
"yearly": "Yearly",
- "featured": "Featured",
- "tag": "Tags",
- "submit": "Submit"
+ "featured": "Featured"
},
"tag_side": {
"title": "Topics",
diff --git a/public/locales/zh/home.json b/public/locales/zh/home.json
index 7636d2b..b41e755 100644
--- a/public/locales/zh/home.json
+++ b/public/locales/zh/home.json
@@ -8,9 +8,7 @@
"monthly": "月度",
"yearly": "年度",
"all": "全部",
- "featured": "精选",
- "tag": "标签",
- "submit": "提交"
+ "featured": "精选"
},
"tag_side": {
"title": "热门标签",
diff --git a/src/components/ImageWithPreview.tsx b/src/components/ImageWithPreview.tsx
index 9cf9cfd..0535bc0 100644
--- a/src/components/ImageWithPreview.tsx
+++ b/src/components/ImageWithPreview.tsx
@@ -42,7 +42,11 @@ const ImageWithPreview = (props: {
},
toolbarRender: () => <>>,
diff --git a/src/components/dropdown/AvatarWithDropdown.tsx b/src/components/dropdown/AvatarWithDropdown.tsx
index f126e77..222576c 100644
--- a/src/components/dropdown/AvatarWithDropdown.tsx
+++ b/src/components/dropdown/AvatarWithDropdown.tsx
@@ -4,6 +4,7 @@ import { useLoginContext } from '@/hooks/useLoginContext';
import LanguageSwitcher from '@/components/buttons/LanguageSwitcher';
import ThemeSwitcher from '@/components/buttons/ThemeSwitcher';
+import { RepoModal } from '@/components/dialog/RepoModal';
import { CustomLink } from '@/components/links/CustomLink';
import { DEFAULT_AVATAR } from '@/utils/constants';
@@ -52,6 +53,11 @@ const AvatarWithDropdown = ({ t, className }: Props) => {
{t('header.profile')}
+
+
+ {t('header.submit')}
+
+
{t('header.notification')}
@@ -62,7 +68,6 @@ const AvatarWithDropdown = ({ t, className }: Props) => {
)}
-
diff --git a/src/components/links/TagLink.tsx b/src/components/links/TagLink.tsx
index 819a39c..cbe6e7d 100644
--- a/src/components/links/TagLink.tsx
+++ b/src/components/links/TagLink.tsx
@@ -1,32 +1,53 @@
import { NoPrefetchLink } from '@/components/links/CustomLink';
+import { constructURL } from '@/utils/util';
+
import { TagType } from '@/types/tag';
interface Props {
+ year?: number;
+ month?: number;
tid?: string;
sort_by: string;
items: TagType[];
+ rank_by: string;
}
-export default function TagLink(props: Props) {
+export default function TagLink({
+ tid,
+ sort_by,
+ items,
+ rank_by,
+ year,
+ month,
+}: Props) {
return (
-
+
- {props.items.map((item: TagType) => {
+ {items.map((item: TagType) => {
return (
-
- {props.tid == item.tid ? (
-
- {item.name}
-
- ) : (
-
- {item.name}
-
- )}
+
+ {item.name}
+
);
diff --git a/src/components/links/rankLink.tsx b/src/components/links/rankLink.tsx
index 4735864..c7ec6e8 100644
--- a/src/components/links/rankLink.tsx
+++ b/src/components/links/rankLink.tsx
@@ -1,3 +1,5 @@
+import { useMemo } from 'react';
+
import { NoPrefetchLink } from '@/components/links/CustomLink';
import { constructURL } from '@/utils/util';
@@ -13,63 +15,65 @@ interface Props {
items: RankItem[];
}
-export default function RankLink(props: Props) {
- const currentDate = new Date();
- const defaultYear = currentDate.getFullYear();
- const defaultMonth = currentDate.getMonth(); // 上个月,如果是1月则为0
+export default function RankLink({
+ year,
+ month,
+ tid,
+ sort_by,
+ rank_by,
+ items,
+}: Props) {
+ const { yearNum, monthlyYearNum, monthNum } = useMemo(() => {
+ const currentDate = new Date();
+ const defaultYear = currentDate.getFullYear();
+ const defaultMonth = currentDate.getMonth(); // 上个月,如果是1月则为0
+
+ return {
+ yearNum: year ?? defaultYear - 1,
+ monthlyYearNum:
+ year ?? (defaultMonth === 0 ? defaultYear - 1 : defaultYear),
+ monthNum: month ?? (defaultMonth === 0 ? 12 : defaultMonth),
+ };
+ }, [year, month]);
+
+ const renderLink = (item: RankItem) => {
+ const isActive = item.month
+ ? item.key === `${monthlyYearNum}.${monthNum}`
+ : item.key === `${yearNum}`;
+
+ const linkProps = {
+ href: constructURL({
+ sort_by,
+ rank_by,
+ tid,
+ year: item.year,
+ ...(item.month && { month: item.month }),
+ }),
+ };
+
+ const className = `
+ mr-1 inline-flex h-6 items-center justify-center rounded-xl px-2
+ ${
+ isActive
+ ? 'bg-gray-100 text-blue-500 dark:bg-gray-700'
+ : 'text-gray-500 hover:bg-gray-100 hover:text-blue-500 dark:text-gray-200 dark:hover:bg-gray-700'
+ }
+ `;
- // 设置默认值并处理边界条件
- const yearNum = props.year ?? defaultYear - 1;
- const monthlyYearNum =
- props.year ?? (defaultMonth === 0 ? defaultYear - 1 : defaultYear);
- const monthNum = props.month ?? (defaultMonth === 0 ? 12 : defaultMonth);
+ return (
+
+ {item.name}
+
+ );
+ };
return (
- {props.items.map((item: RankItem) => {
+ {items.map((item) => {
return (
-
- {item.month ? (
-
- {item.key == `${monthlyYearNum}.${monthNum}` ? (
-
- {item.name}
-
- ) : (
-
- {item.name}
-
- )}
-
- ) : (
-
- {item.key == `${yearNum}` ? (
-
- {item.name}
-
- ) : (
-
- {item.name}
-
- )}
-
- )}
+ {renderLink(item)}
);
})}
diff --git a/src/components/loading/skeleton.tsx b/src/components/loading/skeleton.tsx
index 6039908..04d51ee 100644
--- a/src/components/loading/skeleton.tsx
+++ b/src/components/loading/skeleton.tsx
@@ -33,8 +33,8 @@ export const HomeSkeleton = ({ loop }: Props) => {
export const TagListSkeleton = () => {
return (
-
-
+
+
@@ -43,12 +43,28 @@ export const TagListSkeleton = () => {
);
};
+export const TagLinkListSkeleton = ({ loop = 6 }) => {
+ return (
+
+
+ {[...Array(loop)].map((_, index) => (
+
+ ))}
+
+
+ );
+};
+
export const StatsSkeleton = () => {
return (
开源项目
diff --git a/src/components/navbar/IndexBar.tsx b/src/components/navbar/IndexBar.tsx
index 68b1018..1d4096d 100644
--- a/src/components/navbar/IndexBar.tsx
+++ b/src/components/navbar/IndexBar.tsx
@@ -3,13 +3,14 @@ import { NextPage } from 'next';
import useFilterHandling from '@/hooks/useFilterHandling';
-import { RepoModal } from '@/components/dialog/RepoModal';
import { NoPrefetchLink } from '@/components/links/CustomLink';
import RankLink from '@/components/links/rankLink';
import TagLink from '@/components/links/TagLink';
import { indexRankBy } from '@/utils/constants';
+import { TagLinkListSkeleton } from '../loading/skeleton';
+
type Props = {
t: (key: string) => string;
i18n_lang: string;
@@ -31,16 +32,8 @@ const IndexBar: NextPage
= ({
}) => {
const defaultURL = '/?sort_by=featured';
- const {
- tagLabelStatus,
- tagItems,
- featuredURL,
- allURL,
- handleTagButton,
- monthlyURL,
- yearlyURL,
- rankItems,
- } = useFilterHandling(tid, sortBy, rankBy, i18n_lang, year, month);
+ const { tagItems, featuredURL, allURL, monthlyURL, yearlyURL, rankItems } =
+ useFilterHandling(tid, sortBy, rankBy, i18n_lang, year, month);
const getSortLinkClassName = (sortName: string, isMobile = false) => {
const isActive = sortBy === sortName;
@@ -52,7 +45,6 @@ const IndexBar: NextPage = ({
{
'text-gray-500 dark:text-gray-200': !isActive,
'bg-gray-100 dark:bg-gray-700 text-blue-500': isActive,
- 'lg:hidden': sortName === 'label',
}
);
} else {
@@ -72,7 +64,7 @@ const IndexBar: NextPage = ({
return (
-
+
{indexRankBy.map((rank) => (
= ({
))}
-
-
+
+
- {/* 移动端 */}
-
-
-
-
-
- {t('nav.submit')}
-
-
-
{/* 移动端标签 */}
- {tagLabelStatus && (
-
-
-
- )}
+
+ {tagItems.length > 0 ? (
+
+ ) : (
+
+ )}
+
{/* 排行榜 */}
{rankItems.length > 0 && (
-
+
-
-

+
+
{title}
diff --git a/src/components/respository/CommentItem.tsx b/src/components/respository/CommentItem.tsx
index d160ba2..999c1ac 100644
--- a/src/components/respository/CommentItem.tsx
+++ b/src/components/respository/CommentItem.tsx
@@ -114,11 +114,11 @@ const CommentItem = (props: CommentItemProps) => {
return (
-
+
diff --git a/src/hooks/useFilterHandling.tsx b/src/hooks/useFilterHandling.tsx
index 22f6778..7c075fa 100644
--- a/src/hooks/useFilterHandling.tsx
+++ b/src/hooks/useFilterHandling.tsx
@@ -1,4 +1,3 @@
-import { useRouter } from 'next/router';
import { useCallback, useEffect, useState } from 'react';
import { getTags } from '@/services/home';
@@ -15,7 +14,6 @@ const useFilterHandling = (
year?: number,
month?: number
) => {
- const router = useRouter();
const [tagLabelStatus, setTagLabelStatus] = useState(false);
const [tagItems, setTagItems] = useState([]);
const [featuredURL, setFeaturedURL] = useState('/?sort_by=featured');
@@ -106,15 +104,6 @@ const useFilterHandling = (
}
}, [tagItems, setTagItems]);
- const handleTagButton = () => {
- setTagLabelStatus(!tagLabelStatus);
- if (!tagLabelStatus && sort_by === 'all') {
- router.push('/?sort_by=all');
- } else if (!tagLabelStatus) {
- router.push('/');
- }
- };
-
useEffect(() => {
if (isMobile()) {
handleTags();
@@ -135,7 +124,6 @@ const useFilterHandling = (
tagItems,
featuredURL,
allURL,
- handleTagButton,
monthlyURL,
yearlyURL,
rankItems,