"use client"; import { useEffect, useState } from "react"; import { Download, LogOut } from "lucide-react"; import { signOut } from "next-auth/react"; import { BackButton } from "@/app/_components"; import { MobileShell } from "@/app/_components/core"; import { ROUTES } from "@/router/routes"; import { useAppNavigator } from "@/router/use-app-navigator"; import { useAuthDispatch, useAuthState } from "@/stores/auth/auth-context"; import { useUserDispatch, useUserState } from "@/stores/user/user-context"; import { pwaUtil } from "@/utils"; import { SidebarWalletCard, UserHeader, type SidebarUserState, } from "./components"; import styles from "./components/sidebar-screen.module.css"; const FALLBACK_USERNAME = "User name"; export function SidebarScreen() { const navigator = useAppNavigator(); const user = useUserState(); const userDispatch = useUserDispatch(); const auth = useAuthState(); const authDispatch = useAuthDispatch(); const [canShowInstallApp, setCanShowInstallApp] = useState(false); useEffect(() => { if (!auth.hasInitialized || auth.isLoading) return; if (auth.loginStatus === "notLoggedIn") return; userDispatch({ type: "UserFetch" }); }, [ auth.hasInitialized, auth.isLoading, auth.loginStatus, userDispatch, ]); useEffect(() => { pwaUtil.prepareInstallPrompt(); const timer = window.setTimeout(() => { setCanShowInstallApp(pwaUtil.isSupported() && !pwaUtil.isInstalled()); }, 0); return () => { window.clearTimeout(timer); }; }, []); const handleInstallAppClick = async () => { pwaUtil.prepareInstallPrompt(); const result = await pwaUtil.install(); if (result === "accepted" || pwaUtil.isInstalled()) { setCanShowInstallApp(false); } }; const handleLogoutClick = () => { userDispatch({ type: "UserClearLocal" }); authDispatch({ type: "AuthLogoutSubmitted" }); void signOut({ redirect: false }); navigator.openChat({ replace: true }); }; // 状态派生:未登录 / 已登录未 VIP / 已登录 VIP const sidebarState: SidebarUserState = auth.loginStatus === "notLoggedIn" ? "guest" : user.currentUser?.isVip === true ? "vip" : "member"; const isInitializingUser = sidebarState !== "guest" && user.currentUser == null; const name = user.currentUser?.username ?? FALLBACK_USERNAME; const avatarUrl = user.avatarUrl ?? user.currentUser?.avatarUrl ?? null; return (