import React, { useState, useEffect, useCallback } from "react"; import { View, Text, Image, ScrollView } from "@tarojs/components"; import Taro from "@tarojs/taro"; import styles from "./MyselfPageContent.module.scss"; import { UserInfoCard } from "@/components/UserInfo/index"; import { UserService } from "@/services/userService"; import ListContainer from "@/container/listContainer"; import { TennisMatch } from "@/../types/list/types"; import { NTRPTestEntryCard } from "@/components"; import { EvaluateScene } from "@/store/evaluateStore"; import { useUserInfo } from "@/store/userStore"; import { usePickerOption } from "@/store/pickerOptionsStore"; import { useGlobalState } from "@/store/global"; const MyselfPageContent: React.FC = () => { const pickerOption = usePickerOption(); const { statusNavbarHeightInfo } = useGlobalState() || {}; const { totalHeight = 98 } = statusNavbarHeightInfo || {}; const instance = (Taro as any).getCurrentInstance(); const user_id = instance.router?.params?.userid || ""; // const is_current_user = !user_id; const user_info = useUserInfo(); const [game_records, set_game_records] = useState([]); const [ended_game_records, setEndedGameRecords] = useState([]); const [loading] = useState(false); const [is_following, setIsFollowing] = useState(false); const [active_tab, setActiveTab] = useState<"hosted" | "participated">( "hosted" ); const [collapseProfile, setCollapseProfile] = useState(false); useEffect(() => { pickerOption.getCities(); pickerOption.getProfessions(); }, []); const { useDidShow } = Taro as any; useDidShow(() => { // 确保从编辑页面返回时刷新数据 }); // 分类球局数据(使用 useCallback 包装,避免每次渲染都创建新函数) const classifyGameRecords = useCallback( ( game_records: TennisMatch[] ): { notEndGames: TennisMatch[]; finishedGames: TennisMatch[] } => { const now = new Date().getTime(); return game_records.reduce( (result, cur) => { let { end_time } = cur; end_time = end_time.replace(/\s/, "T"); new Date(end_time).getTime() > now ? result.notEndGames.push(cur) : result.finishedGames.push(cur); return result; }, { notEndGames: [] as TennisMatch[], finishedGames: [] as TennisMatch[], } ); }, [] ); // 使用 useCallback 包装 load_game_data,避免每次渲染都创建新函数 const load_game_data = useCallback(async () => { try { if (!user_info || !("id" in user_info)) { return; } let games_data; if (active_tab === "hosted") { games_data = await UserService.get_hosted_games(user_info.id); } else { games_data = await UserService.get_participated_games(user_info.id); } const sorted_games = games_data.sort((a, b) => { return ( new Date(a.original_start_time.replace(/\s/, "T")).getTime() - new Date(b.original_start_time.replace(/\s/, "T")).getTime() ); }); const { notEndGames, finishedGames } = classifyGameRecords(sorted_games); set_game_records(notEndGames); setEndedGameRecords(finishedGames); } catch (error) { console.error("加载球局数据失败:", error); } }, [active_tab, user_info, classifyGameRecords]); useEffect(() => { if (!loading) { load_game_data(); } }, [loading, load_game_data]); const handle_follow = async () => { try { const new_following_state = await UserService.toggle_follow( user_id, is_following ); setIsFollowing(new_following_state); (Taro as any).showToast({ title: new_following_state ? "关注成功" : "已取消关注", icon: "success", duration: 1500, }); } catch (error) { console.error("关注操作失败:", error); (Taro as any).showToast({ title: "操作失败,请重试", icon: "error", duration: 2000, }); } }; const goPublish = () => { (Taro as any).navigateTo({ url: "/publish_pages/publishBall/index", }); }; const handle_game_orders = () => { (Taro as any).navigateTo({ url: "/order_pages/orderList/index", }); }; const handle_wallet = () => { (Taro as any).navigateTo({ url: "/user_pages/wallet/index", // url: "/user_pages/other/index?userid=18", }); }; const handleOnTab = (tab) => { setActiveTab(tab); }; const handleScroll = (event: any) => { const scrollData = event.detail; setCollapseProfile(scrollData.scrollTop > 1); }; return ( {!collapseProfile ? ( 球局订单 钱包 ) : null} {!collapseProfile ? ( ) : null} setActiveTab("hosted")} > 我主办的 setActiveTab("participated")} > 我参与的 {}} collapse={true} style={{ paddingBottom: 0, overflow: "hidden" }} listLoadErrorHeight="320px" defaultShowNum={3} /> 往期球局 {}} collapse={true} style={{ paddingBottom: "90px", overflow: "hidden" }} listLoadErrorHeight="320px" defaultShowNum={3} /> ); }; export default MyselfPageContent;