File size: 1,650 Bytes
cc276cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useEffect, useState } from 'react';
import { useAuth } from '@/contexts/auth-context';
import { Onboarding } from '@/components/onboarding';
import { Recovery } from '@/components/recovery';
import { ChatInterface } from '@/components/chat-interface';
import { LoadingScreen } from './loading-screen';
import { PreparationScreen } from './preparation-screen';

const PREPARATION_FLAG = 'isAppPrepared_v4';


export const AuthWrapper = () => {
    const authContext = useAuth();
    const [isClient, setIsClient] = useState(false);
    const [isPrepared, setIsPrepared] = useState(true); // Default to true to avoid flash

    useEffect(() => {
        setIsClient(true);
        // Check preparation flag from localStorage only on the client
        if (localStorage.getItem(PREPARATION_FLAG) !== 'true') {
            setIsPrepared(false);
        }
    }, []);
    
    const handlePreparationComplete = () => {
        localStorage.setItem(PREPARATION_FLAG, 'true');
        window.location.reload();
    };

    if (!isClient) {
        return <LoadingScreen />;
    }

    if (!isPrepared) {
        return <PreparationScreen onPrepared={handlePreparationComplete} />;
    }
    
    if (!authContext) {
        return <LoadingScreen />;
    }
    
    const { authStatus } = authContext;

    if (authStatus === 'loading') {
        return <LoadingScreen />;
    }

    if (authStatus === 'onboarding') {
        return <Onboarding />;
    }

    if (authStatus === 'recovery') {
        return <Recovery />;
    }

    if (authStatus === 'authenticated') {
        return <ChatInterface />;
    }

    return null; 
};