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 => (