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, useUserActions } from "@/store/userStore"; import { usePickerOption } from "@/store/pickerOptionsStore"; import { useGlobalState } from "@/store/global"; interface MyselfPageContentProps { isActive?: boolean; } const MyselfPageContent: React.FC = ({ isActive = true }) => { const pickerOption = usePickerOption(); const { statusNavbarHeightInfo } = useGlobalState() || {}; const { totalHeight = 98 } = statusNavbarHeightInfo || {}; const { fetchUserInfo } = useUserActions(); 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 [hasLoaded, setHasLoaded] = useState(false); // 记录是否已经加载过数据 const [collapseProfile, setCollapseProfile] = useState(false); useEffect(() => { pickerOption.getCities(); pickerOption.getProfessions(); }, []); // 当页面激活时,确保用户信息已加载 useEffect(() => { if (isActive) { // 如果用户信息不存在或没有 id,则加载用户信息 if (!user_info || !("id" in user_info) || !user_info.id) { fetchUserInfo(); } } }, [isActive, user_info, fetchUserInfo]); 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 (isActive && !hasLoaded && !loading && user_info && "id" in user_info) { load_game_data(); setHasLoaded(true); } }, [isActive, hasLoaded, loading, load_game_data, user_info]); // 当 active_tab 切换时,如果页面已激活,重新加载数据 useEffect(() => { if (isActive && hasLoaded && !loading && user_info && "id" in user_info) { load_game_data(); } }, [active_tab, isActive, hasLoaded, loading, load_game_data, user_info]); 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: ended_game_records.length ? 0 : "90px", overflow: "hidden", }} listLoadErrorWrapperHeight="fit-content" listLoadErrorWidth="320px" listLoadErrorHeight="152px" listLoadErrorScale="1.2" defaultShowNum={3} /> {ended_game_records.length ? ( <> 往期球局 {}} collapse={true} style={{ paddingBottom: "90px", overflow: "hidden" }} listLoadErrorWrapperHeight="fit-content" listLoadErrorWidth="320px" listLoadErrorHeight="152px" listLoadErrorScale="1.2" defaultShowNum={3} /> ) : null} ); }; export default MyselfPageContent;