import { useState, useEffect } from 'react';
import { useDaily } from '@daily-co/daily-react';
import { CVIProvider } from '../components/cvi/components/cvi-provider';
import { HairCheck } from '../components/cvi/components/hair-check';
import { ExtendedConversationBlock } from '../components/extended/ExtendedConversationBlock';
import './app.css';
import './haircheck-fixes.css';
// Wrapper component that prejoins Daily for HairCheck
function HairCheckWithRoom({ roomUrl, onJoin }: { roomUrl: string; onJoin: () => void }) {
const daily = useDaily();
const [isPrejoining, setIsPrejoining] = useState(true);
useEffect(() => {
if (!daily || !roomUrl) return;
const prejoin = async () => {
try {
console.log('[HairCheckWithRoom] Prejoining Daily room for camera access');
await daily.preAuth({ url: roomUrl });
await daily.startCamera();
setIsPrejoining(false);
} catch (err) {
console.error('[HairCheckWithRoom] Failed to prejoin:', err);
setIsPrejoining(false);
}
};
void prejoin();
// Cleanup: leave Daily when unmounting (transitioning to conversation)
return () => {
if (daily) {
console.log('[HairCheckWithRoom] Leaving Daily before conversation');
daily.leave().catch(err => {
console.error('[HairCheckWithRoom] Error leaving:', err);
});
}
};
}, [daily, roomUrl]);
if (isPrejoining) {
return (
Setting up your camera...
);
}
return ;
}
type DemoStage = 'haircheck' | 'conversation';
interface DemoSessionResponse {
daily_room_url: string;
session_id: string;
status: string;
}
interface DemoSession {
dailyUrl: string;
sessionId: string;
}
const MOTIA_API_URL = import.meta.env.VITE_MOTIA_API_URL || 'https://slide-system-production.up.railway.app';
/**
* Main App component orchestrating the demo experience flow.
*
* Flow:
* 1. Fetch Daily.co room URL from Railway Motia API
* 2. Show HairCheck (device testing)
* 3. User clicks "Join Demo" → Transition to Conversation
* 4. User clicks "Leave Demo" → Return to HairCheck
*/
export function App() {
const [stage, setStage] = useState('haircheck');
const [demoSession, setDemoSession] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(true);
// Fetch Daily room URL from Railway Motia API on mount
useEffect(() => {
const createDemoSession = async () => {
try {
console.log('[App] Creating demo session via Railway API:', MOTIA_API_URL);
const response = await fetch(`${MOTIA_API_URL}/demo-sessions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
session_type: 'live_demo',
environment: 'prod',
}),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data: DemoSessionResponse = await response.json();
console.log('[App] Demo session created:', data);
setDemoSession({
dailyUrl: data.daily_room_url,
sessionId: data.session_id,
});
setIsLoading(false);
} catch (err) {
console.error('[App] Error creating demo session:', err);
setError(
err instanceof Error
? err.message
: 'Failed to initialize demo. Please try again.'
);
setIsLoading(false);
}
};
void createDemoSession();
}, []);
const handleJoinDemo = () => {
if (demoSession) {
console.log('[App] Transitioning to conversation stage');
setStage('conversation');
}
};
const handleLeaveDemo = () => {
console.log('[App] Returning to haircheck stage');
setStage('haircheck');
};
const handleRetry = () => {
console.log('[App] Retrying demo initialization');
window.location.reload();
};
// Error state
if (error) {
return (
⚠️
Unable to Initialize Demo
{error}
);
}
// Loading state
if (isLoading || !demoSession) {
return (
);
}
// Main demo flow
return (
{stage === 'haircheck' && (
)}
{stage === 'conversation' && (
)}
);
}
export default App;