From d171e496ff2cb143b62ecf7d379f41ad0b6ff6cb Mon Sep 17 00:00:00 2001 From: kyuuseiryuu Date: Mon, 9 Mar 2026 17:16:21 +0900 Subject: [PATCH] =?UTF-8?q?refactor(page):=20=E5=90=88=E5=B9=B6=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E4=B8=8E=E8=BF=9C=E7=A8=8B=E7=90=83=E5=91=98=E6=94=B6?= =?UTF-8?q?=E8=97=8F=E5=88=97=E8=A1=A8=E9=80=BB=E8=BE=91=E5=B9=B6=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将本地列表 (localList) 和请求数据 (favListRequest.data) 的映射逻辑统一至主 list useMemo 中。 - 移除冗余的 showList useMemo,直接基于 showType 动态生成 list。 - 修正 sortType 默认分支显式返回 localList,确保排序逻辑一致性。 - 更新依赖数组并清理未使用的 isAuthenticated 相关逻辑。 - 调整空状态判断条件以匹配新的 list 结构。 --- src/page/FavPlayersPage.tsx | 46 ++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/page/FavPlayersPage.tsx b/src/page/FavPlayersPage.tsx index 7bafead..652cc47 100644 --- a/src/page/FavPlayersPage.tsx +++ b/src/page/FavPlayersPage.tsx @@ -1,5 +1,5 @@ import { Avatar, Button, Card, Divider, Flex, Image, Popconfirm, Radio, Segmented, Spin, Typography } from "antd"; -import { useFavPlayerStore } from "../store/useFavPlayerStore"; +import { useFavPlayerStore, type FavPlayer } from "../store/useFavPlayerStore"; import { useNavigate } from "react-router"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useLogto } from "@logto/react"; @@ -20,22 +20,28 @@ enum ShowType { export function FavePlayersPage() { const { favMap, unFav } = useFavPlayerStore(state => state); const [sortType, setSortType] = useState(SortType.DEFAULT); - const list = useMemo(() => { - const l = Object.values(favMap); - switch (sortType) { - case SortType.DEFAULT: return l; - case SortType.SCORE_UP: return l.sort(({ score: a }, { score: b }) => +a - +b); - case SortType.SCORE_DOWN: return l.sort(({ score: b }, { score: a }) => +a - +b); - default: return l; - } - }, [favMap, sortType]); - const navigate = useNavigate(); - const { isAuthenticated, getIdTokenClaims } = useLogto(); + const [showType, setShowType] = useState(ShowType.LOCAL); + const localList = Object.values(favMap); const favListRequest = useRequest(async (aud: string) => { const res = await fetch(`/api/fav/${aud}`); const data = await res.json(); return data; }, { manual: true }); + const list = useMemo(() => { + const l = showType === ShowType.LOCAL ? localList : favListRequest.data?.map(e => ({ + ...e, + name: e.username, + avatar: e.realpic, + })) || []; + switch (sortType) { + case SortType.DEFAULT: return l; + case SortType.SCORE_UP: return l.sort(({ score: a }, { score: b }) => +a - +b); + case SortType.SCORE_DOWN: return l.sort(({ score: b }, { score: a }) => +a - +b); + default: return localList; + } + }, [favMap, sortType, showType, localList, favListRequest]); + const navigate = useNavigate(); + const { isAuthenticated, getIdTokenClaims } = useLogto(); useEffect(() => { if (!isAuthenticated) return; const id = setTimeout(async () => { @@ -54,24 +60,12 @@ export function FavePlayersPage() { await Promise.all(jobs); await favListRequest.runAsync(aud!); }, [isAuthenticated, list]); - const [showType, setShowType] = useState(ShowType.LOCAL); const handleClearLocal = useCallback(() => { list.forEach(e => unFav(e.uid)); }, []); useEffect(() => { setShowType(isAuthenticated ? ShowType.ACCOUNT : ShowType.LOCAL); }, [isAuthenticated]); - const showList = useMemo(() => { - if (showType === ShowType.LOCAL) { - return list; - } - if (!isAuthenticated) return []; - return favListRequest.data?.map(e => ({ - ...e, - name: e.username, - avatar: e.realpic, - })) ?? []; - }, [showType, isAuthenticated, list, favListRequest.data]); return (
@@ -99,14 +93,14 @@ export function FavePlayersPage() { />
- {!favListRequest.loading && showList.length === 0 ? ( + {!favListRequest.loading && list.length === 0 ? ( <> 暂无收藏的球员 ) : ( <> - {showList.map(e => ( + {list.map(e => (