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 | 1x 1x 1x 16x 16x 16x 16x 15x 15x 1x 13x 1x 18x 2x 16x 16x 1x 14x 25x 25x 25x 25x 14x 14x 13x 13x 13x 14x 5x 5x 5x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 3x 2x 2x 2x 2x 2x 1x 1x 1x 3x 3x 3x 3x | /**
* WalkMe Storage Module
*
* localStorage persistence adapter for tour state.
* Handles save/load, schema versioning, and graceful SSR fallbacks.
*/
import type { TourState, StorageSchema } from '../types/walkme.types'
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const STORAGE_KEY = 'walkme-state'
const STORAGE_VERSION = 1
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface StorageAdapter {
load(): StorageSchema | null
save(state: StorageSchema): void
reset(): void
resetTour(tourId: string): void
getCompletedTours(): string[]
getSkippedTours(): string[]
getVisitCount(): number
incrementVisitCount(): void
getFirstVisitDate(): string | null
setFirstVisitDate(date: string): void
getActiveTour(): TourState | null
setActiveTour(tour: TourState | null): void
}
// ---------------------------------------------------------------------------
// Utilities
// ---------------------------------------------------------------------------
/** Check if localStorage is available */
export function isStorageAvailable(): boolean {
Iif (typeof window === 'undefined') return false
try {
const testKey = '__walkme_test__'
window.localStorage.setItem(testKey, '1')
window.localStorage.removeItem(testKey)
return true
} catch {
return false
}
}
/** Create a default empty storage schema */
function createDefaultSchema(): StorageSchema {
return {
version: STORAGE_VERSION,
completedTours: [],
skippedTours: [],
activeTour: null,
tourHistory: {},
visitCount: 0,
firstVisitDate: new Date().toISOString(),
}
}
/** Migrate storage data from older versions */
export function migrateStorage(data: unknown): StorageSchema {
if (!data || typeof data !== 'object') {
return createDefaultSchema()
}
const record = data as Record<string, unknown>
// Version 1 (current) - no migration needed, just validate shape
return {
version: STORAGE_VERSION,
completedTours: Array.isArray(record.completedTours)
? (record.completedTours as string[])
: [],
skippedTours: Array.isArray(record.skippedTours)
? (record.skippedTours as string[])
: [],
activeTour: record.activeTour as TourState | null ?? null,
tourHistory:
(record.tourHistory as Record<string, TourState>) ?? {},
visitCount:
typeof record.visitCount === 'number' ? record.visitCount : 0,
firstVisitDate:
typeof record.firstVisitDate === 'string'
? record.firstVisitDate
: new Date().toISOString(),
}
}
// ---------------------------------------------------------------------------
// Factory
// ---------------------------------------------------------------------------
/** Create a storage adapter backed by localStorage */
export function createStorageAdapter(): StorageAdapter {
const available = isStorageAvailable()
function read(): StorageSchema {
Iif (!available) return createDefaultSchema()
try {
const raw = window.localStorage.getItem(STORAGE_KEY)
if (!raw) return createDefaultSchema()
const parsed = JSON.parse(raw)
return migrateStorage(parsed)
} catch {
return createDefaultSchema()
}
}
function write(schema: StorageSchema): void {
Iif (!available) return
try {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(schema))
} catch {
// Storage full or unavailable - silently ignore
}
}
return {
load(): StorageSchema | null {
Iif (!available) return null
return read()
},
save(state: StorageSchema): void {
write(state)
},
reset(): void {
Iif (!available) return
try {
window.localStorage.removeItem(STORAGE_KEY)
} catch {
// Ignore
}
},
resetTour(tourId: string): void {
const state = read()
state.completedTours = state.completedTours.filter((id) => id !== tourId)
state.skippedTours = state.skippedTours.filter((id) => id !== tourId)
const { [tourId]: _, ...remainingHistory } = state.tourHistory
state.tourHistory = remainingHistory
if (state.activeTour?.tourId === tourId) {
state.activeTour = null
}
write(state)
},
getCompletedTours(): string[] {
return read().completedTours
},
getSkippedTours(): string[] {
return read().skippedTours
},
getVisitCount(): number {
return read().visitCount
},
incrementVisitCount(): void {
const state = read()
state.visitCount += 1
write(state)
},
getFirstVisitDate(): string | null {
const state = read()
return state.firstVisitDate || null
},
setFirstVisitDate(date: string): void {
const state = read()
state.firstVisitDate = date
write(state)
},
getActiveTour(): TourState | null {
return read().activeTour
},
setActiveTour(tour: TourState | null): void {
const state = read()
state.activeTour = tour
write(state)
},
}
}
|