| 1 |
import React, { useState, useEffect } from 'react'; |
| 2 |
import ChatWindow from './components/ChatWindow'; |
| 3 |
import AuthPrompt from './components/AuthPrompt'; |
| 4 |
import FloatingTrigger from './components/FloatingTrigger'; |
| 5 |
import ResizeHandle from './components/ResizeHandle'; |
| 6 |
import { ChatProvider } from './context/ChatContext'; |
| 7 |
import useDisplayModeStore from './store/useDisplayModeStore'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Wraps the chat content in the correct outer shell for the current display mode. |
| 11 |
* Reads mode from the Zustand store — no prop needed. |
| 12 |
*/ |
| 13 |
function ChatShell({ children }) { |
| 14 |
const { isFullPage } = useDisplayModeStore(); |
| 15 |
|
| 16 |
if (isFullPage) { |
| 17 |
return ( |
| 18 |
<div className="zip-ai-fullpage-app flex h-full w-full flex-col overflow-hidden bg-background"> |
| 19 |
{children} |
| 20 |
</div> |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
return ( |
| 25 |
<div className="flex flex-col absolute inset-0 overflow-hidden bg-background border-l border-border"> |
| 26 |
{children} |
| 27 |
</div> |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
function App() { |
| 32 |
const [config, setConfig] = useState(null); |
| 33 |
const [isReady, setIsReady] = useState(false); |
| 34 |
const [isAuthenticated, setIsAuthenticated] = useState(false); |
| 35 |
const { isFullPage } = useDisplayModeStore(); |
| 36 |
|
| 37 |
useEffect(() => { |
| 38 |
// Get configuration from window (set by wp_localize_script) |
| 39 |
const zipAiConfig = window.ZIP_AI_CONFIG || {}; |
| 40 |
setConfig(zipAiConfig); |
| 41 |
|
| 42 |
// Check if user is authenticated (server-side flag from PHP) |
| 43 |
setIsAuthenticated(!!zipAiConfig.isAuthenticated); |
| 44 |
|
| 45 |
setIsReady(true); |
| 46 |
}, []); |
| 47 |
|
| 48 |
// Handle successful authentication (called if bridge resolves without page reload) |
| 49 |
const handleAuthSuccess = (authData) => { |
| 50 |
const updatedConfig = { |
| 51 |
...config, |
| 52 |
isAuthenticated: true, |
| 53 |
user: authData.user || config.user, |
| 54 |
userId: authData.user?.id || config.userId, |
| 55 |
}; |
| 56 |
|
| 57 |
setConfig(updatedConfig); |
| 58 |
setIsAuthenticated(true); |
| 59 |
|
| 60 |
// Update window config for other components |
| 61 |
window.ZIP_AI_CONFIG = updatedConfig; |
| 62 |
}; |
| 63 |
|
| 64 |
// Show loading state until config is ready |
| 65 |
if (!isReady || !config) { |
| 66 |
return ( |
| 67 |
<> |
| 68 |
{!isFullPage && <FloatingTrigger />} |
| 69 |
{!isFullPage && <ResizeHandle />} |
| 70 |
<div className="flex flex-col items-center justify-center absolute inset-0 gap-4 bg-background"> |
| 71 |
<div className="spinner" aria-hidden="true" /> |
| 72 |
<div className="text-muted-foreground text-sm font-medium tracking-[-0.01em]">Starting Zip AI…</div> |
| 73 |
</div> |
| 74 |
</> |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
// Show auth prompt if not authenticated |
| 79 |
if (!isAuthenticated) { |
| 80 |
return ( |
| 81 |
<> |
| 82 |
{!isFullPage && <FloatingTrigger />} |
| 83 |
{!isFullPage && <ResizeHandle />} |
| 84 |
<AuthPrompt onAuthSuccess={handleAuthSuccess} /> |
| 85 |
</> |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
// Show chat interface if authenticated |
| 90 |
return ( |
| 91 |
<> |
| 92 |
{!isFullPage && <FloatingTrigger />} |
| 93 |
{!isFullPage && <ResizeHandle />} |
| 94 |
<ChatProvider config={config}> |
| 95 |
<ChatShell> |
| 96 |
<ChatWindow /> |
| 97 |
</ChatShell> |
| 98 |
</ChatProvider> |
| 99 |
</> |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
export default App; |
| 104 |
|