"use client"; import { useState, useEffect, useCallback } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import { Loader2 } from "lucide-react"; import { OnboardingProgress } from "./OnboardingProgress"; import { Step0HealthScore } from "./Step0HealthScore"; import { Step1BrandName } from "./Step1BrandName"; import { Step2Competitors } from "./Step2Competitors"; import { Step3Platforms } from "./Step3Platforms"; import { Step4HealthReport } from "./Step4HealthReport"; import { Step5ActionSuggestions } from "./Step5ActionSuggestions"; import { useOnboardingData } from "@/lib/hooks/use-onboarding-data"; import type { BrandHealthReport, OnboardingState } from "@/types/onboarding"; import type { HealthScoreResponse } from "@/lib/api/health-score"; const initialState: OnboardingState = { currentStep: 0, brandName: "", competitors: [], platforms: [], frequency: "weekly", brandId: null, healthReport: null, preCheckResult: null, }; export default function OnboardingPage() { const { status } = useSession(); const router = useRouter(); const [state, setState] = useState(initialState); const { isCompleted, isLoading: isStatusLoading, createBrand: hookCreateBrand, isCreatingBrand, mutationError, } = useOnboardingData(); const error = mutationError?.message ?? null; useEffect(() => { if (isCompleted) { router.push("/dashboard"); } }, [isCompleted, router]); const createBrand = useCallback(async () => { if (!state.brandName) return null; const brandId = await hookCreateBrand({ name: state.brandName, competitors: state.competitors, platforms: state.platforms, frequency: state.frequency, }); return brandId; }, [ hookCreateBrand, state.brandName, state.competitors, state.platforms, state.frequency, ]); const handleStep0Next = ( brandName: string, healthScore: HealthScoreResponse | null, ) => { setState((prev) => ({ ...prev, brandName, preCheckResult: healthScore, currentStep: 1, })); }; const handleStep1Next = (brandName: string) => { setState((prev) => ({ ...prev, brandName, currentStep: 2, })); }; const handleStep1Skip = () => { router.push("/dashboard"); }; const handleStep2Next = (competitors: string[]) => { setState((prev) => ({ ...prev, competitors, currentStep: 3, })); }; const handleStep2Back = () => { setState((prev) => ({ ...prev, currentStep: 1, })); }; const handleStep2Skip = async () => { setState((prev) => ({ ...prev, competitors: [], currentStep: 3, })); }; const handleStep3Next = async ( platforms: string[], frequency: "daily" | "weekly" | "monthly", ) => { const brandId = await createBrand(); if (brandId) { setState((prev) => ({ ...prev, platforms, frequency, brandId, currentStep: 4, })); } }; const handleStep3Back = () => { setState((prev) => ({ ...prev, currentStep: 2, })); }; const handleStep3Skip = async () => { const defaultPlatforms = [ "wenxin", "kimi", "tongyi", "baidu_ai", "yuanbao", "qingyan", "doubao", ]; const brandId = await createBrand(); if (brandId) { setState((prev) => ({ ...prev, platforms: defaultPlatforms, brandId, currentStep: 4, })); } }; const handleStep4Next = (report: BrandHealthReport) => { setState((prev) => ({ ...prev, healthReport: report, currentStep: 5, })); }; const handleStep4Back = () => { setState((prev) => ({ ...prev, currentStep: 3, })); }; const handleStep5Complete = () => { router.push("/dashboard"); }; const handleStep5Back = () => { if (state.brandId) { setState((prev) => ({ ...prev, currentStep: 4, })); } }; const handleStep5Skip = () => { router.push("/dashboard"); }; if (status === "loading" || isStatusLoading) { return (
); } if (status === "unauthenticated") { if (state.currentStep === 0) { // Step0 不需要登录 } else { router.push("/login"); return null; } } return (

GEO 平台

新用户引导

{error && (
{error}
)} {isCreatingBrand && (

正在创建品牌...

)}
{state.currentStep === 0 && ( )} {state.currentStep === 1 && ( )} {state.currentStep === 2 && ( )} {state.currentStep === 3 && ( )} {state.currentStep === 4 && state.brandId && ( )} {state.currentStep === 5 && state.brandId && ( )}
); }