aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/new/onboarding/welcome/layout.tsx
blob: 07d3c4b39a92a5538b5c6b5990752a31627c4384 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"use client"

import {
	createContext,
	useContext,
	useState,
	useEffect,
	useCallback,
	useRef,
	type ReactNode,
} from "react"
import { useRouter, useSearchParams } from "next/navigation"
import { useOnboardingContext, type MemoryFormData } from "../layout"
import { analytics } from "@/lib/analytics"

export const WELCOME_STEPS = [
	"input",
	"greeting",
	"welcome",
	"username",
	"features",
	"memories",
] as const
export type WelcomeStep = (typeof WELCOME_STEPS)[number]

interface WelcomeContextValue {
	name: string
	setName: (name: string) => void
	isSubmitting: boolean
	setIsSubmitting: (value: boolean) => void
	showWelcomeContent: boolean
	memoryFormData: MemoryFormData
	setMemoryFormData: (data: MemoryFormData) => void
	currentStep: WelcomeStep
	goToStep: (step: WelcomeStep) => void
	goToSetup: (step?: string) => void
}

const WelcomeContext = createContext<WelcomeContextValue | null>(null)

export function useWelcomeContext() {
	const ctx = useContext(WelcomeContext)
	if (!ctx) {
		throw new Error("useWelcomeContext must be used within WelcomeLayout")
	}
	return ctx
}

export default function WelcomeLayout({ children }: { children: ReactNode }) {
	const router = useRouter()
	const searchParams = useSearchParams()
	const { name, setName, memoryFormData, setMemoryFormData } =
		useOnboardingContext()

	const stepParam = searchParams.get("step")
	const currentStep: WelcomeStep = WELCOME_STEPS.includes(
		stepParam as WelcomeStep,
	)
		? (stepParam as WelcomeStep)
		: "input"

	const [isSubmitting, setIsSubmitting] = useState(false)
	const [showWelcomeContent, setShowWelcomeContent] = useState(false)
	const isMountedRef = useRef(true)
	const hasTrackedInitialStep = useRef(false)

	useEffect(() => {
		isMountedRef.current = true
		return () => {
			isMountedRef.current = false
		}
	}, [])

	useEffect(() => {
		if (currentStep === "input") {
			setShowWelcomeContent(false)
			const timer = setTimeout(() => {
				if (isMountedRef.current) {
					setShowWelcomeContent(true)
				}
			}, 1000)
			return () => clearTimeout(timer)
		}
		setShowWelcomeContent(true)
	}, [currentStep])

	useEffect(() => {
		const timers: NodeJS.Timeout[] = []

		if (currentStep === "greeting") {
			timers.push(
				setTimeout(() => {
					if (isMountedRef.current) {
						analytics.onboardingStepViewed({ step: "welcome", trigger: "auto" })
						router.replace("/new/onboarding/welcome?step=welcome")
					}
				}, 2000),
			)
		} else if (currentStep === "welcome") {
			timers.push(
				setTimeout(() => {
					if (isMountedRef.current) {
						analytics.onboardingStepViewed({
							step: "username",
							trigger: "auto",
						})
						router.replace("/new/onboarding/welcome?step=username")
					}
				}, 2000),
			)
		}

		return () => {
			timers.forEach(clearTimeout)
		}
	}, [currentStep, router])

	useEffect(() => {
		if (!hasTrackedInitialStep.current) {
			analytics.onboardingStepViewed({
				step: currentStep,
				trigger: "user",
			})
			hasTrackedInitialStep.current = true
		}
	}, [currentStep])

	const goToStep = useCallback(
		(step: WelcomeStep) => {
			analytics.onboardingStepViewed({ step, trigger: "user" })
			router.push(`/new/onboarding/welcome?step=${step}`)
		},
		[router],
	)

	const goToSetup = useCallback(
		(step = "relatable") => {
			router.push(`/new/onboarding/setup?step=${step}`)
		},
		[router],
	)

	const contextValue: WelcomeContextValue = {
		name,
		setName,
		isSubmitting,
		setIsSubmitting,
		showWelcomeContent,
		memoryFormData,
		setMemoryFormData,
		currentStep,
		goToStep,
		goToSetup,
	}

	return (
		<WelcomeContext.Provider value={contextValue}>
			{children}
		</WelcomeContext.Provider>
	)
}