Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | 1x 59x 1x 141x 56x 56x 56x 56x 38x 37x 37x 36x 35x 35x 10x 8x 8x 8x 8x 1x 7x 7x 3x 2x 1x 1x 4x 3x 3x 3x 1x 1x 5x 4x 4x 4x 7x 6x 6x 4x 4x 2x 1x 1x 4x 4x 1x 1x 1x 3x 1x 1x 7x 7x 7x 7x 1x 4x 3x 3x 2x 1x 1x 4x 2x 1x 2x 2x 1x 1x 3x 2x 1x 3x 2x 2x 2x 1x 3x 3x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x | /**
* WalkMe Core Engine
*
* Pure-function state machine for managing guided tour state.
* All functions are side-effect free and testable without React.
*/
import type {
Tour,
TourStep,
TourState,
WalkmeState,
WalkmeAction,
} from '../types/walkme.types'
// ---------------------------------------------------------------------------
// Initial State
// ---------------------------------------------------------------------------
/** Creates a fresh initial WalkmeState */
export function createInitialState(): WalkmeState {
return {
tours: {},
activeTour: null,
completedTours: [],
skippedTours: [],
tourHistory: {},
initialized: false,
debug: false,
}
}
// ---------------------------------------------------------------------------
// Reducer
// ---------------------------------------------------------------------------
/** Main reducer for the WalkMe state machine */
export function walkmeReducer(state: WalkmeState, action: WalkmeAction): WalkmeState {
switch (action.type) {
case 'INITIALIZE': {
const tours: Record<string, Tour> = {}
for (const tour of action.tours) {
tours[tour.id] = tour
}
return { ...state, tours, initialized: true }
}
case 'START_TOUR': {
if (state.activeTour) return state
const tour = state.tours[action.tourId]
if (!tour) return state
if (tour.steps.length === 0) return state
const tourState: TourState = {
tourId: action.tourId,
status: 'active',
currentStepIndex: 0,
startedAt: new Date().toISOString(),
}
return {
...state,
activeTour: tourState,
tourHistory: { ...state.tourHistory, [action.tourId]: tourState },
}
}
case 'NEXT_STEP': {
if (!state.activeTour || state.activeTour.status !== 'active') return state
const tour = state.tours[state.activeTour.tourId]
Iif (!tour) return state
const nextIndex = state.activeTour.currentStepIndex + 1
// If we've passed the last step, complete the tour
if (nextIndex >= tour.steps.length) {
return completeTourState(state)
}
const updatedTour: TourState = {
...state.activeTour,
currentStepIndex: nextIndex,
}
return {
...state,
activeTour: updatedTour,
tourHistory: { ...state.tourHistory, [updatedTour.tourId]: updatedTour },
}
}
case 'PREV_STEP': {
if (!state.activeTour || state.activeTour.status !== 'active') return state
if (state.activeTour.currentStepIndex <= 0) return state
const updatedTour: TourState = {
...state.activeTour,
currentStepIndex: state.activeTour.currentStepIndex - 1,
}
return {
...state,
activeTour: updatedTour,
tourHistory: { ...state.tourHistory, [updatedTour.tourId]: updatedTour },
}
}
case 'NAVIGATE_TO_STEP': {
if (!state.activeTour || state.activeTour.status !== 'active') return state
const tour = state.tours[state.activeTour.tourId]
Iif (!tour) return state
if (action.stepIndex < 0 || action.stepIndex >= tour.steps.length) return state
const updatedTour: TourState = {
...state.activeTour,
currentStepIndex: action.stepIndex,
}
return {
...state,
activeTour: updatedTour,
tourHistory: { ...state.tourHistory, [updatedTour.tourId]: updatedTour },
}
}
case 'SKIP_TOUR': {
if (!state.activeTour) return state
const tourId = state.activeTour.tourId
const skippedState: TourState = {
...state.activeTour,
status: 'skipped',
skippedAt: new Date().toISOString(),
}
return {
...state,
activeTour: null,
skippedTours: state.skippedTours.includes(tourId)
? state.skippedTours
: [...state.skippedTours, tourId],
tourHistory: { ...state.tourHistory, [tourId]: skippedState },
}
}
case 'COMPLETE_TOUR': {
if (!state.activeTour) return state
return completeTourState(state)
}
case 'PAUSE_TOUR': {
if (!state.activeTour || state.activeTour.status !== 'active') return state
const paused: TourState = {
...state.activeTour,
status: 'paused',
}
return {
...state,
activeTour: paused,
tourHistory: { ...state.tourHistory, [paused.tourId]: paused },
}
}
case 'RESUME_TOUR': {
if (!state.activeTour || state.activeTour.status !== 'paused') return state
const resumed: TourState = {
...state.activeTour,
status: 'active',
}
return {
...state,
activeTour: resumed,
tourHistory: { ...state.tourHistory, [resumed.tourId]: resumed },
}
}
case 'RESET_TOUR': {
const { [action.tourId]: _, ...remainingHistory } = state.tourHistory
return {
...state,
activeTour:
state.activeTour?.tourId === action.tourId ? null : state.activeTour,
completedTours: state.completedTours.filter((id) => id !== action.tourId),
skippedTours: state.skippedTours.filter((id) => id !== action.tourId),
tourHistory: remainingHistory,
}
}
case 'RESET_ALL': {
return {
...state,
activeTour: null,
completedTours: [],
skippedTours: [],
tourHistory: {},
}
}
case 'SET_DEBUG': {
return { ...state, debug: action.enabled }
}
case 'RESTORE_STATE': {
return {
...state,
completedTours: action.completedTours,
skippedTours: action.skippedTours,
tourHistory: action.tourHistory,
activeTour: action.activeTour,
}
}
default:
return state
}
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function completeTourState(state: WalkmeState): WalkmeState {
Iif (!state.activeTour) return state
const tourId = state.activeTour.tourId
const completedState: TourState = {
...state.activeTour,
status: 'completed',
completedAt: new Date().toISOString(),
}
return {
...state,
activeTour: null,
completedTours: state.completedTours.includes(tourId)
? state.completedTours
: [...state.completedTours, tourId],
tourHistory: { ...state.tourHistory, [tourId]: completedState },
}
}
/** Check if a tour can be started */
export function canStartTour(state: WalkmeState, tourId: string): boolean {
if (state.activeTour) return false
const tour = state.tours[tourId]
if (!tour) return false
if (tour.steps.length === 0) return false
return true
}
/** Get the full Tour object for the currently active tour */
export function getActiveTour(state: WalkmeState): Tour | null {
if (!state.activeTour) return null
return state.tours[state.activeTour.tourId] ?? null
}
/** Get the current TourStep for the active tour */
export function getActiveStep(state: WalkmeState): TourStep | null {
const tour = getActiveTour(state)
if (!tour || !state.activeTour) return null
return tour.steps[state.activeTour.currentStepIndex] ?? null
}
/** Check if the current step is the first step */
export function isFirstStep(state: WalkmeState): boolean {
if (!state.activeTour) return false
return state.activeTour.currentStepIndex === 0
}
/** Check if the current step is the last step */
export function isLastStep(state: WalkmeState): boolean {
if (!state.activeTour) return false
const tour = state.tours[state.activeTour.tourId]
Iif (!tour) return false
return state.activeTour.currentStepIndex === tour.steps.length - 1
}
/** Get progress for a specific tour */
export function getTourProgress(
state: WalkmeState,
tourId: string,
): { current: number; total: number; percentage: number } {
const tour = state.tours[tourId]
if (!tour) return { current: 0, total: 0, percentage: 0 }
const total = tour.steps.length
const isActive = state.activeTour?.tourId === tourId
const current = isActive ? state.activeTour!.currentStepIndex + 1 : 0
const percentage = total > 0 ? Math.round((current / total) * 100) : 0
return { current, total, percentage }
}
/** Get global progress across all tours */
export function getGlobalProgress(
state: WalkmeState,
): { completed: number; total: number; percentage: number } {
const total = Object.keys(state.tours).length
const completed = state.completedTours.length
const percentage = total > 0 ? Math.round((completed / total) * 100) : 0
return { completed, total, percentage }
}
|