-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathApp.tsx
68 lines (61 loc) · 1.93 KB
/
App.tsx
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
import { useEffect, useState } from 'react'
import { DailyProvider } from '@daily-co/daily-react'
import { WelcomeScreen } from '@/components/WelcomeScreen'
import { HairCheckScreen } from '@/components/HairCheckScreen'
import { CallScreen } from '@/components/CallScreen'
import { createConversation, endConversation } from '@/api'
import { IConversation } from '@/types'
import { useToast } from "@/hooks/use-toast"
function App() {
const { toast } = useToast()
const [screen, setScreen] = useState<'welcome' | 'hairCheck' | 'call'>('welcome')
const [conversation, setConversation] = useState<IConversation | null>(null)
const [loading, setLoading] = useState(false)
useEffect(() => {
return () => {
if (conversation) {
void endConversation(conversation.conversation_id)
}
}
}, [conversation])
const handleStart = async () => {
try {
setLoading(true)
const conversation = await createConversation()
setConversation(conversation)
setScreen('hairCheck')
} catch {
toast({
variant: "destructive",
title: "Uh oh! Something went wrong.",
description: 'Check console for details',
})
} finally {
setLoading(false)
}
}
const handleEnd = async () => {
try {
if (!conversation) return
await endConversation(conversation.conversation_id)
} catch (error) {
console.error(error)
} finally {
setConversation(null)
setScreen('welcome')
}
}
const handleJoin = () => {
setScreen('call')
}
return (
<main>
<DailyProvider>
{screen === 'welcome' && <WelcomeScreen onStart={handleStart} loading={loading} />}
{screen === 'hairCheck' && <HairCheckScreen handleEnd={handleEnd} handleJoin={handleJoin} />}
{screen === 'call' && conversation && <CallScreen conversation={conversation} handleEnd={handleEnd} />}
</DailyProvider>
</main>
)
}
export default App