refactor: migrate state imports from contexts to stores directory
This commit is contained in:
@@ -10,18 +10,25 @@
|
||||
* - 居中布局 (MainAxisAlignment.center)
|
||||
* - Facebook 按钮:带渐变(primaryGradient)+ 圆角 24 + 高度 48
|
||||
* - 加载中:按钮内显示 spinner
|
||||
*
|
||||
* Skip 按钮使用 `<Link>` 而非 `<button onClick>`:
|
||||
* - 保留 Next.js SPA 体验 + prefetch
|
||||
* - 避免编程式 `router.push`(消除水合后副作用)
|
||||
* - 右键"在新标签页打开"等浏览器行为自然支持
|
||||
*/
|
||||
import { useAuthState, useAuthDispatch } from "@/contexts/auth/auth-context";
|
||||
import Link from "next/link";
|
||||
import { useAuthState, useAuthDispatch } from "@/stores/auth/auth-context";
|
||||
import { LoadingIndicator } from "@/app/_components/core/loading-indicator";
|
||||
import styles from "./splash-button.module.css";
|
||||
|
||||
export interface SplashButtonProps {
|
||||
onSkip: () => void;
|
||||
/** Skip 链接的目标路由(href) */
|
||||
skipHref: string;
|
||||
onFacebookLogin: () => void;
|
||||
}
|
||||
|
||||
export function SplashButton({
|
||||
onSkip,
|
||||
skipHref,
|
||||
onFacebookLogin,
|
||||
}: SplashButtonProps) {
|
||||
const state = useAuthState();
|
||||
@@ -30,15 +37,16 @@ export function SplashButton({
|
||||
|
||||
return (
|
||||
<div className={styles.wrapper}>
|
||||
{/* Skip 文本按钮 */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSkip}
|
||||
{/* Skip 文本链接(next/link 渲染为 <a>,保留 SPA 体验) */}
|
||||
<Link
|
||||
href={skipHref}
|
||||
className={styles.skip}
|
||||
disabled={isLoading}
|
||||
aria-disabled={isLoading}
|
||||
tabIndex={isLoading ? -1 : 0}
|
||||
style={isLoading ? { pointerEvents: "none", opacity: 0.5 } : undefined}
|
||||
>
|
||||
Skip
|
||||
</button>
|
||||
</Link>
|
||||
|
||||
<div className={styles.separator} aria-hidden="true" />
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
*/
|
||||
import styles from "./splash-content.module.css";
|
||||
|
||||
const APOSTROPHE = "'";
|
||||
const APOSTROPHE = "'";
|
||||
|
||||
export function SplashContent() {
|
||||
return (
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
import { useAuthState } from "@/contexts/auth/auth-context";
|
||||
import { useChatDispatch } from "@/contexts/chat/chat-context";
|
||||
import { useUserDispatch } from "@/contexts/user/user-context";
|
||||
import { useAuthState } from "@/stores/auth/auth-context";
|
||||
import { useChatDispatch } from "@/stores/chat/chat-context";
|
||||
import { useUserDispatch } from "@/stores/user/user-context";
|
||||
import { useAuthGate } from "@/lib/auth/use-auth-gate";
|
||||
import { ROUTES } from "@/lib/routes";
|
||||
import { MobileShell } from "@/app/_components/core/mobile-shell";
|
||||
@@ -17,26 +14,31 @@ import { SplashButton } from "./splash-button";
|
||||
import styles from "./splash-screen.module.css";
|
||||
|
||||
export function SplashScreen() {
|
||||
const router = useRouter();
|
||||
const { isAuthed } = useAuthGate();
|
||||
// useAuthGate 仍消费(仅读取 isAuthed 用于子组件 gating,不触发跳转)
|
||||
useAuthGate();
|
||||
// 以下 state/dispatch 当前未在本组件消费;保留以便未来按状态驱动的跳转
|
||||
// 重新启用时再解注释 useEffect 即可。
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const state = useAuthState();
|
||||
const wasSuccess = useRef(false);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const chatDispatch = useChatDispatch();
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const userDispatch = useUserDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
if (isAuthed) router.replace(ROUTES.chat);
|
||||
}, [isAuthed, router]);
|
||||
// 已登录时跳 /chat(已注释:改由 SplashButton 的 Link + 路由守卫统一处理)
|
||||
// useEffect(() => {
|
||||
// if (isAuthed) router.replace(ROUTES.chat);
|
||||
// }, [isAuthed, router]);
|
||||
|
||||
// 登录成功跳 /chat
|
||||
useEffect(() => {
|
||||
if (state.isSuccess && !wasSuccess.current) {
|
||||
wasSuccess.current = true;
|
||||
chatDispatch({ type: "ChatAuthStatusChanged" });
|
||||
userDispatch({ type: "UserInit" });
|
||||
router.replace(ROUTES.chat);
|
||||
}
|
||||
}, [state.isSuccess, chatDispatch, userDispatch, router]);
|
||||
// 登录成功跳 /chat(已注释:改由 SplashButton 的 Link + 路由守卫统一处理)
|
||||
// useEffect(() => {
|
||||
// if (state.isSuccess && !wasSuccess.current) {
|
||||
// wasSuccess.current = true;
|
||||
// chatDispatch({ type: "ChatAuthStatusChanged" });
|
||||
// userDispatch({ type: "UserInit" });
|
||||
// router.replace(ROUTES.chat);
|
||||
// }
|
||||
// }, [state.isSuccess, chatDispatch, userDispatch, router]);
|
||||
|
||||
return (
|
||||
<MobileShell background="var(--color-sidebar-background)">
|
||||
@@ -50,7 +52,7 @@ export function SplashScreen() {
|
||||
<SplashContent />
|
||||
<div className={styles.buttonArea}>
|
||||
<SplashButton
|
||||
onSkip={() => router.push(ROUTES.chat)}
|
||||
skipHref={ROUTES.chat}
|
||||
onFacebookLogin={() => {
|
||||
/* Facebook 登录触发由 SplashButton 内部 dispatch */
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user