import { Avatar, Button, Card, Divider, Flex, Image, message as AntdMessage, Popconfirm, Radio, Segmented, Spin, Typography } from "antd"; import { useFavPlayerStore } from "../store/useFavPlayerStore"; import { useNavigate } from "react-router"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useLogto, type IdTokenClaims } from "@logto/react"; import { DeleteOutlined, LoginOutlined, SyncOutlined, UploadOutlined } from "@ant-design/icons"; import { useRequest } from "ahooks"; import type { XCXProfile } from "../types"; import useAutoLogin from "../hooks/useAutoLogin"; import { useAuthHeaders } from "../hooks/useAuthHeaders"; enum SortType { DEFAULT = '注册时间', SCORE_UP = '积分升序', SCORE_DOWN = '积分降序', } enum ShowType { LOCAL = '本地收藏', ACCOUNT = '账号收藏', } export function FavePlayersPage() { const [message, contextHolder] = AntdMessage.useMessage(); const { favMap, unFav } = useFavPlayerStore(state => state); const [sortType, setSortType] = useState(SortType.DEFAULT); const [showType, setShowType] = useState(ShowType.LOCAL); const [claims, setClaims] = useState(); const headers = useAuthHeaders(); const { login } = useAutoLogin(); const localList = Object.values(favMap); const favListRequest = useRequest(async (aud: string) => { const res = await fetch(`/api/fav`, { headers }); const data = await res.json(); return data; }, { manual: true, cacheKey: 'favListRequest', cacheTime: 3 * 60 * 1000 }); 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 () => { const claims = await getIdTokenClaims(); setClaims(claims); favListRequest.runAsync(claims?.aud!); }, 300); return () => clearTimeout(id); }, [isAuthenticated, getIdTokenClaims]); const handleSyncFav = useCallback(async () => { if (!isAuthenticated) return; const claims = await getIdTokenClaims()!; const aud = claims?.aud; const jobs = list.map(async u => { await fetch(`/api/fav/${u.uid}`, { method: 'PUT', headers }); }); message.open({ key: 'sync', content: '同步中...', type: 'loading' }); await Promise.all(jobs); await favListRequest.runAsync(aud!); message.open({ key: 'sync', content: '已同步', type: 'success' }); }, [isAuthenticated, list]); const handleClearLocal = useCallback(() => { list.forEach(e => unFav(e.uid)); }, []); return (
{contextHolder} 收藏的球员 setShowType(e.target.value)} options={[ { label: `${ShowType.LOCAL}(${localList.length})`, value: ShowType.LOCAL}, { label: `${ShowType.ACCOUNT}(${favListRequest.data?.length ?? 0})`, value: ShowType.ACCOUNT} ]} /> ) } { (showType === ShowType.LOCAL && !isAuthenticated && list.length > 0) && ( ) } {(showType === ShowType.LOCAL && ( }} > ))} )}
); }