2026-02-17 15:03:34 +00:00
|
|
|
/**
|
|
|
|
|
* Onboarding state - prefers backend, falls back to localStorage for mock/offline.
|
2026-03-01 17:53:18 +00:00
|
|
|
* Hardened: retries on 502/503, never blocks completion.
|
2026-02-17 15:03:34 +00:00
|
|
|
*/
|
|
|
|
|
import { rpcClient } from '@/api/rpc-client'
|
|
|
|
|
|
2026-03-01 17:53:18 +00:00
|
|
|
async function callWithRetry<T>(fn: () => Promise<T>, maxRetries = 3): Promise<T | null> {
|
|
|
|
|
for (let i = 0; i < maxRetries; i++) {
|
|
|
|
|
try {
|
|
|
|
|
return await fn()
|
|
|
|
|
} catch (e) {
|
|
|
|
|
const msg = e instanceof Error ? e.message : ''
|
|
|
|
|
const isRetryable = /502|503|timeout|fetch|network/i.test(msg)
|
|
|
|
|
if (!isRetryable || i === maxRetries - 1) return null
|
|
|
|
|
await new Promise((r) => setTimeout(r, 800 * (i + 1)))
|
|
|
|
|
}
|
2026-02-17 15:03:34 +00:00
|
|
|
}
|
2026-03-01 17:53:18 +00:00
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function isOnboardingComplete(): Promise<boolean> {
|
2026-04-22 04:45:33 -04:00
|
|
|
// Prefer the backend — localStorage gets stale across nodes (a
|
|
|
|
|
// browser that onboarded node A would otherwise treat fresh node B
|
|
|
|
|
// as already-onboarded and skip the wizard entirely). Only fall
|
|
|
|
|
// back to localStorage if the backend is unreachable.
|
2026-03-01 17:53:18 +00:00
|
|
|
const result = await callWithRetry(() => rpcClient.isOnboardingComplete(), 2)
|
2026-04-22 05:42:52 -04:00
|
|
|
if (result !== null) {
|
|
|
|
|
// Re-seed the localStorage cache so non-async consumers
|
|
|
|
|
// (OnboardingWrapper's useVideoBackground computed, etc.) see the
|
|
|
|
|
// right answer after the user clears site data on an already-
|
|
|
|
|
// onboarded node.
|
|
|
|
|
if (result) {
|
|
|
|
|
try { localStorage.setItem('neode_onboarding_complete', '1') } catch {}
|
|
|
|
|
} else {
|
|
|
|
|
try { localStorage.removeItem('neode_onboarding_complete') } catch {}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
2026-04-22 04:45:33 -04:00
|
|
|
return localStorage.getItem('neode_onboarding_complete') === '1'
|
2026-02-17 15:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function completeOnboarding(): Promise<void> {
|
2026-03-01 17:53:18 +00:00
|
|
|
await callWithRetry(() => rpcClient.completeOnboarding(), 3)
|
|
|
|
|
localStorage.setItem('neode_onboarding_complete', '1')
|
2026-04-02 18:20:52 +01:00
|
|
|
localStorage.removeItem('neode_onboarding_step')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Save current onboarding step so refresh resumes where user left off */
|
|
|
|
|
export function saveOnboardingStep(step: string): void {
|
|
|
|
|
localStorage.setItem('neode_onboarding_step', step)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Get the last saved onboarding step, or 'intro' if none */
|
|
|
|
|
export function getSavedOnboardingStep(): string {
|
|
|
|
|
return localStorage.getItem('neode_onboarding_step') || 'intro'
|
2026-02-17 15:03:34 +00:00
|
|
|
}
|