aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/onboarding/onboarding-context.tsx
blob: 0b0eb3465f0af5e99ef184194e1fbc21de8fe0cb (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"use client"

import {
	createContext,
	useContext,
	useState,
	useEffect,
	type ReactNode,
	useMemo,
} from "react"
import { useQueryState } from "nuqs"
import { useIsMobile } from "@hooks/use-mobile"

// Define the context interface
interface OnboardingContextType {
	currentStep: OnboardingStep
	setStep: (step: OnboardingStep) => void
	nextStep: () => void
	previousStep: () => void
	totalSteps: number
	currentStepIndex: number
	// Visible-step aware helpers
	visibleSteps: OnboardingStep[]
	currentVisibleStepIndex: number
	currentVisibleStepNumber: number
	getStepNumberFor: (step: OnboardingStep) => number
	introTriggers: {
		first: boolean
		second: boolean
		third: boolean
		fourth: boolean
	}
	orbsRevealed: boolean
	resetIntroTriggers: () => void
}

// Create the context
const OnboardingContext = createContext<OnboardingContextType | undefined>(
	undefined,
)

// Define the base step order
const BASE_STEP_ORDER = [
	"intro",
	"name",
	"bio",
	"mcp",
	"extension",
	"welcome",
] as const

export type OnboardingStep = (typeof BASE_STEP_ORDER)[number]

interface OnboardingProviderProps {
	children: ReactNode
	initialStep?: OnboardingStep
}

export function OnboardingProvider({
	children,
	initialStep = "intro",
}: OnboardingProviderProps) {
	// Helper function to validate if a step is valid
	const isValidStep = (step: string): step is OnboardingStep => {
		return BASE_STEP_ORDER.includes(step as OnboardingStep)
	}

	const [currentStep, setCurrentStep] = useQueryState("step", {
		defaultValue: initialStep,
		parse: (value: string) => {
			// Validate the step from URL - if invalid, use the initial step
			return isValidStep(value) ? value : initialStep
		},
		serialize: (value: OnboardingStep) => value,
	})
	const [orbsRevealed, setOrbsRevealed] = useState(false)
	const [introTriggers, setIntroTriggers] = useState({
		first: false,
		second: false,
		third: false,
		fourth: false,
	})
	const isMobile = useIsMobile()

	// Compute visible steps based on device
	const visibleSteps = useMemo(() => {
		if (isMobile) {
			// On mobile, hide MCP and Extension steps
			return BASE_STEP_ORDER.filter((s) => s !== "mcp" && s !== "extension")
		}
		return [...BASE_STEP_ORDER]
	}, [isMobile])

	// Setup intro trigger timings when on intro step
	useEffect(() => {
		if (currentStep !== "intro") return

		const cleanups = [
			setTimeout(() => {
				setIntroTriggers((prev) => ({ ...prev, first: true }))
			}, 300),
			setTimeout(() => {
				setIntroTriggers((prev) => ({ ...prev, second: true }))
			}, 300),
			setTimeout(() => {
				setIntroTriggers((prev) => ({ ...prev, third: true }))
			}, 300),
			setTimeout(() => {
				setIntroTriggers((prev) => ({ ...prev, fourth: true }))
			}, 400),
		]

		return () => cleanups.forEach(clearTimeout)
	}, [currentStep])

	// Set orbs as revealed once the fourth trigger is activated OR if we're on any non-intro step
	useEffect(() => {
		if (currentStep !== "intro") {
			// If we're not on the intro step, orbs should always be visible
			// (user has either completed intro or navigated directly to another step)
			if (!orbsRevealed) {
				setOrbsRevealed(true)
			}
		} else if (introTriggers.fourth && !orbsRevealed) {
			// On intro step, reveal orbs only after the fourth trigger
			setOrbsRevealed(true)
		}
	}, [introTriggers.fourth, orbsRevealed, currentStep])

	// Ensure current step is always part of visible steps; if not, advance to the next visible step
	useEffect(() => {
		if (!visibleSteps.includes(currentStep)) {
			if (visibleSteps.length === 0) return
			const baseIndex = BASE_STEP_ORDER.indexOf(currentStep)
			// Find the next visible step after the current base index
			const nextAfterBase = visibleSteps.find(
				(step) => BASE_STEP_ORDER.indexOf(step) > baseIndex,
			)
			const targetStep = nextAfterBase ?? visibleSteps[visibleSteps.length - 1]!
			setCurrentStep(targetStep)
		}
	}, [visibleSteps, currentStep])

	function setStep(step: OnboardingStep) {
		setCurrentStep(step)
	}

	function nextStep() {
		const currentIndex = visibleSteps.indexOf(currentStep)
		const nextIndex = currentIndex + 1

		if (nextIndex < visibleSteps.length) {
			setStep(visibleSteps[nextIndex]!)
		}
	}

	function previousStep() {
		const currentIndex = visibleSteps.indexOf(currentStep)
		const previousIndex = currentIndex - 1

		if (previousIndex >= 0) {
			setStep(visibleSteps[previousIndex]!)
		}
	}

	function resetIntroTriggers() {
		setIntroTriggers({
			first: false,
			second: false,
			third: false,
			fourth: false,
		})
	}

	const currentStepIndex = BASE_STEP_ORDER.indexOf(currentStep)

	// Visible-step aware helpers
	const stepsForNumbering = useMemo(
		() => visibleSteps.filter((s) => s !== "intro" && s !== "welcome"),
		[visibleSteps],
	)

	function getStepNumberFor(step: OnboardingStep): number {
		if (step === "intro" || step === "welcome") {
			return 0
		}
		const idx = stepsForNumbering.indexOf(step)
		return idx === -1 ? 0 : idx + 1
	}

	const currentVisibleStepIndex = useMemo(
		() => visibleSteps.indexOf(currentStep),
		[visibleSteps, currentStep],
	)
	const currentVisibleStepNumber = useMemo(
		() => getStepNumberFor(currentStep),
		[currentStep, stepsForNumbering],
	)
	const totalSteps = stepsForNumbering.length

	const contextValue: OnboardingContextType = {
		currentStep,
		setStep,
		nextStep,
		previousStep,
		totalSteps,
		currentStepIndex,
		visibleSteps,
		currentVisibleStepIndex,
		currentVisibleStepNumber,
		getStepNumberFor,
		introTriggers,
		orbsRevealed,
		resetIntroTriggers,
	}

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

export function useOnboarding() {
	const context = useContext(OnboardingContext)

	if (context === undefined) {
		throw new Error("useOnboarding must be used within an OnboardingProvider")
	}

	return context
}