fix(sidebar): trigger logout from click
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { MobileShell } from "@/app/_components/core/mobile-shell";
|
||||
@@ -9,8 +9,6 @@ import { ROUTES } from "@/router/routes";
|
||||
import { useAuthDispatch, useAuthState } from "@/stores/auth/auth-context";
|
||||
import { useUserDispatch, useUserState } from "@/stores/user/user-context";
|
||||
|
||||
import type { UserView } from "@/models/user/user";
|
||||
|
||||
import { BackBar } from "./back-bar";
|
||||
import { UserHeader, type SidebarUserState } from "./user-header";
|
||||
import { VipBenefitsCard } from "./vip-benefits-card";
|
||||
@@ -41,16 +39,11 @@ export function SidebarScreen() {
|
||||
userDispatch({ type: "UserInit" });
|
||||
}, [userDispatch]);
|
||||
|
||||
const prevUserRef = useRef<UserView | null>(user.currentUser);
|
||||
useEffect(() => {
|
||||
const wasLoggedIn = prevUserRef.current != null;
|
||||
const isNowLoggedOut = user.currentUser == null;
|
||||
prevUserRef.current = user.currentUser;
|
||||
if (wasLoggedIn && isNowLoggedOut) {
|
||||
authDispatch({ type: "AuthReset" });
|
||||
router.replace(ROUTES.chat);
|
||||
}
|
||||
}, [user.currentUser, authDispatch, router]);
|
||||
const handleLogoutClick = () => {
|
||||
authDispatch({ type: "AuthReset" });
|
||||
userDispatch({ type: "UserLogout" });
|
||||
router.replace(ROUTES.chat);
|
||||
};
|
||||
|
||||
// 状态派生:未登录 / 已登录未 VIP / 已登录 VIP
|
||||
const sidebarState: SidebarUserState =
|
||||
@@ -111,7 +104,7 @@ export function SidebarScreen() {
|
||||
id: "logout",
|
||||
title: "Log out",
|
||||
destructive: true,
|
||||
onClick: () => userDispatch({ type: "UserLogout" }),
|
||||
onClick: handleLogoutClick,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user